]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/trace/ring_buffer.c
Merge branch 'tip/tracing/ring-buffer-3' of git://git.kernel.org/pub/scm/linux/kernel...
[net-next-2.6.git] / kernel / trace / ring_buffer.c
CommitLineData
7a8e76a3
SR
1/*
2 * Generic ring buffer
3 *
4 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
5 */
6#include <linux/ring_buffer.h>
14131f2f 7#include <linux/trace_clock.h>
78d904b4 8#include <linux/ftrace_irq.h>
7a8e76a3
SR
9#include <linux/spinlock.h>
10#include <linux/debugfs.h>
11#include <linux/uaccess.h>
a81bd80a 12#include <linux/hardirq.h>
1744a21d 13#include <linux/kmemcheck.h>
7a8e76a3
SR
14#include <linux/module.h>
15#include <linux/percpu.h>
16#include <linux/mutex.h>
7a8e76a3
SR
17#include <linux/init.h>
18#include <linux/hash.h>
19#include <linux/list.h>
554f786e 20#include <linux/cpu.h>
7a8e76a3
SR
21#include <linux/fs.h>
22
182e9f5f
SR
23#include "trace.h"
24
d1b182a8
SR
25/*
26 * The ring buffer header is special. We must manually up keep it.
27 */
28int ring_buffer_print_entry_header(struct trace_seq *s)
29{
30 int ret;
31
334d4169
LJ
32 ret = trace_seq_printf(s, "# compressed entry header\n");
33 ret = trace_seq_printf(s, "\ttype_len : 5 bits\n");
d1b182a8
SR
34 ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
35 ret = trace_seq_printf(s, "\tarray : 32 bits\n");
36 ret = trace_seq_printf(s, "\n");
37 ret = trace_seq_printf(s, "\tpadding : type == %d\n",
38 RINGBUF_TYPE_PADDING);
39 ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
40 RINGBUF_TYPE_TIME_EXTEND);
334d4169
LJ
41 ret = trace_seq_printf(s, "\tdata max type_len == %d\n",
42 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
d1b182a8
SR
43
44 return ret;
45}
46
5cc98548
SR
47/*
48 * The ring buffer is made up of a list of pages. A separate list of pages is
49 * allocated for each CPU. A writer may only write to a buffer that is
50 * associated with the CPU it is currently executing on. A reader may read
51 * from any per cpu buffer.
52 *
53 * The reader is special. For each per cpu buffer, the reader has its own
54 * reader page. When a reader has read the entire reader page, this reader
55 * page is swapped with another page in the ring buffer.
56 *
57 * Now, as long as the writer is off the reader page, the reader can do what
58 * ever it wants with that page. The writer will never write to that page
59 * again (as long as it is out of the ring buffer).
60 *
61 * Here's some silly ASCII art.
62 *
63 * +------+
64 * |reader| RING BUFFER
65 * |page |
66 * +------+ +---+ +---+ +---+
67 * | |-->| |-->| |
68 * +---+ +---+ +---+
69 * ^ |
70 * | |
71 * +---------------+
72 *
73 *
74 * +------+
75 * |reader| RING BUFFER
76 * |page |------------------v
77 * +------+ +---+ +---+ +---+
78 * | |-->| |-->| |
79 * +---+ +---+ +---+
80 * ^ |
81 * | |
82 * +---------------+
83 *
84 *
85 * +------+
86 * |reader| RING BUFFER
87 * |page |------------------v
88 * +------+ +---+ +---+ +---+
89 * ^ | |-->| |-->| |
90 * | +---+ +---+ +---+
91 * | |
92 * | |
93 * +------------------------------+
94 *
95 *
96 * +------+
97 * |buffer| RING BUFFER
98 * |page |------------------v
99 * +------+ +---+ +---+ +---+
100 * ^ | | | |-->| |
101 * | New +---+ +---+ +---+
102 * | Reader------^ |
103 * | page |
104 * +------------------------------+
105 *
106 *
107 * After we make this swap, the reader can hand this page off to the splice
108 * code and be done with it. It can even allocate a new page if it needs to
109 * and swap that into the ring buffer.
110 *
111 * We will be using cmpxchg soon to make all this lockless.
112 *
113 */
114
033601a3
SR
115/*
116 * A fast way to enable or disable all ring buffers is to
117 * call tracing_on or tracing_off. Turning off the ring buffers
118 * prevents all ring buffers from being recorded to.
119 * Turning this switch on, makes it OK to write to the
120 * ring buffer, if the ring buffer is enabled itself.
121 *
122 * There's three layers that must be on in order to write
123 * to the ring buffer.
124 *
125 * 1) This global flag must be set.
126 * 2) The ring buffer must be enabled for recording.
127 * 3) The per cpu buffer must be enabled for recording.
128 *
129 * In case of an anomaly, this global flag has a bit set that
130 * will permantly disable all ring buffers.
131 */
132
133/*
134 * Global flag to disable all recording to ring buffers
135 * This has two bits: ON, DISABLED
136 *
137 * ON DISABLED
138 * ---- ----------
139 * 0 0 : ring buffers are off
140 * 1 0 : ring buffers are on
141 * X 1 : ring buffers are permanently disabled
142 */
143
144enum {
145 RB_BUFFERS_ON_BIT = 0,
146 RB_BUFFERS_DISABLED_BIT = 1,
147};
148
149enum {
150 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
151 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
152};
153
5e39841c 154static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
a3583244 155
474d32b6
SR
156#define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
157
a3583244
SR
158/**
159 * tracing_on - enable all tracing buffers
160 *
161 * This function enables all tracing buffers that may have been
162 * disabled with tracing_off.
163 */
164void tracing_on(void)
165{
033601a3 166 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
a3583244 167}
c4f50183 168EXPORT_SYMBOL_GPL(tracing_on);
a3583244
SR
169
170/**
171 * tracing_off - turn off all tracing buffers
172 *
173 * This function stops all tracing buffers from recording data.
174 * It does not disable any overhead the tracers themselves may
175 * be causing. This function simply causes all recording to
176 * the ring buffers to fail.
177 */
178void tracing_off(void)
179{
033601a3
SR
180 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
181}
c4f50183 182EXPORT_SYMBOL_GPL(tracing_off);
033601a3
SR
183
184/**
185 * tracing_off_permanent - permanently disable ring buffers
186 *
187 * This function, once called, will disable all ring buffers
c3706f00 188 * permanently.
033601a3
SR
189 */
190void tracing_off_permanent(void)
191{
192 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
a3583244
SR
193}
194
988ae9d6
SR
195/**
196 * tracing_is_on - show state of ring buffers enabled
197 */
198int tracing_is_on(void)
199{
200 return ring_buffer_flags == RB_BUFFERS_ON;
201}
202EXPORT_SYMBOL_GPL(tracing_is_on);
203
d06bbd66
IM
204#include "trace.h"
205
e3d6bf0a 206#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
67d34724 207#define RB_ALIGNMENT 4U
334d4169 208#define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
c7b09308 209#define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
334d4169
LJ
210
211/* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
212#define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
7a8e76a3
SR
213
214enum {
215 RB_LEN_TIME_EXTEND = 8,
216 RB_LEN_TIME_STAMP = 16,
217};
218
2d622719
TZ
219static inline int rb_null_event(struct ring_buffer_event *event)
220{
334d4169
LJ
221 return event->type_len == RINGBUF_TYPE_PADDING
222 && event->time_delta == 0;
2d622719
TZ
223}
224
225static inline int rb_discarded_event(struct ring_buffer_event *event)
226{
334d4169 227 return event->type_len == RINGBUF_TYPE_PADDING && event->time_delta;
2d622719
TZ
228}
229
230static void rb_event_set_padding(struct ring_buffer_event *event)
231{
334d4169 232 event->type_len = RINGBUF_TYPE_PADDING;
2d622719
TZ
233 event->time_delta = 0;
234}
235
34a148bf 236static unsigned
2d622719 237rb_event_data_length(struct ring_buffer_event *event)
7a8e76a3
SR
238{
239 unsigned length;
240
334d4169
LJ
241 if (event->type_len)
242 length = event->type_len * RB_ALIGNMENT;
2d622719
TZ
243 else
244 length = event->array[0];
245 return length + RB_EVNT_HDR_SIZE;
246}
247
248/* inline for ring buffer fast paths */
249static unsigned
250rb_event_length(struct ring_buffer_event *event)
251{
334d4169 252 switch (event->type_len) {
7a8e76a3 253 case RINGBUF_TYPE_PADDING:
2d622719
TZ
254 if (rb_null_event(event))
255 /* undefined */
256 return -1;
334d4169 257 return event->array[0] + RB_EVNT_HDR_SIZE;
7a8e76a3
SR
258
259 case RINGBUF_TYPE_TIME_EXTEND:
260 return RB_LEN_TIME_EXTEND;
261
262 case RINGBUF_TYPE_TIME_STAMP:
263 return RB_LEN_TIME_STAMP;
264
265 case RINGBUF_TYPE_DATA:
2d622719 266 return rb_event_data_length(event);
7a8e76a3
SR
267 default:
268 BUG();
269 }
270 /* not hit */
271 return 0;
272}
273
274/**
275 * ring_buffer_event_length - return the length of the event
276 * @event: the event to get the length of
277 */
278unsigned ring_buffer_event_length(struct ring_buffer_event *event)
279{
465634ad 280 unsigned length = rb_event_length(event);
334d4169 281 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
465634ad
RR
282 return length;
283 length -= RB_EVNT_HDR_SIZE;
284 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
285 length -= sizeof(event->array[0]);
286 return length;
7a8e76a3 287}
c4f50183 288EXPORT_SYMBOL_GPL(ring_buffer_event_length);
7a8e76a3
SR
289
290/* inline for ring buffer fast paths */
34a148bf 291static void *
7a8e76a3
SR
292rb_event_data(struct ring_buffer_event *event)
293{
334d4169 294 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
7a8e76a3 295 /* If length is in len field, then array[0] has the data */
334d4169 296 if (event->type_len)
7a8e76a3
SR
297 return (void *)&event->array[0];
298 /* Otherwise length is in array[0] and array[1] has the data */
299 return (void *)&event->array[1];
300}
301
302/**
303 * ring_buffer_event_data - return the data of the event
304 * @event: the event to get the data from
305 */
306void *ring_buffer_event_data(struct ring_buffer_event *event)
307{
308 return rb_event_data(event);
309}
c4f50183 310EXPORT_SYMBOL_GPL(ring_buffer_event_data);
7a8e76a3
SR
311
312#define for_each_buffer_cpu(buffer, cpu) \
9e01c1b7 313 for_each_cpu(cpu, buffer->cpumask)
7a8e76a3
SR
314
315#define TS_SHIFT 27
316#define TS_MASK ((1ULL << TS_SHIFT) - 1)
317#define TS_DELTA_TEST (~TS_MASK)
318
abc9b56d 319struct buffer_data_page {
e4c2ce82 320 u64 time_stamp; /* page time stamp */
c3706f00 321 local_t commit; /* write committed index */
abc9b56d
SR
322 unsigned char data[]; /* data of buffer page */
323};
324
77ae365e
SR
325/*
326 * Note, the buffer_page list must be first. The buffer pages
327 * are allocated in cache lines, which means that each buffer
328 * page will be at the beginning of a cache line, and thus
329 * the least significant bits will be zero. We use this to
330 * add flags in the list struct pointers, to make the ring buffer
331 * lockless.
332 */
abc9b56d 333struct buffer_page {
778c55d4 334 struct list_head list; /* list of buffer pages */
abc9b56d 335 local_t write; /* index for next write */
6f807acd 336 unsigned read; /* index for next read */
778c55d4 337 local_t entries; /* entries on this page */
abc9b56d 338 struct buffer_data_page *page; /* Actual data page */
7a8e76a3
SR
339};
340
77ae365e
SR
341/*
342 * The buffer page counters, write and entries, must be reset
343 * atomically when crossing page boundaries. To synchronize this
344 * update, two counters are inserted into the number. One is
345 * the actual counter for the write position or count on the page.
346 *
347 * The other is a counter of updaters. Before an update happens
348 * the update partition of the counter is incremented. This will
349 * allow the updater to update the counter atomically.
350 *
351 * The counter is 20 bits, and the state data is 12.
352 */
353#define RB_WRITE_MASK 0xfffff
354#define RB_WRITE_INTCNT (1 << 20)
355
044fa782 356static void rb_init_page(struct buffer_data_page *bpage)
abc9b56d 357{
044fa782 358 local_set(&bpage->commit, 0);
abc9b56d
SR
359}
360
474d32b6
SR
361/**
362 * ring_buffer_page_len - the size of data on the page.
363 * @page: The page to read
364 *
365 * Returns the amount of data on the page, including buffer page header.
366 */
ef7a4a16
SR
367size_t ring_buffer_page_len(void *page)
368{
474d32b6
SR
369 return local_read(&((struct buffer_data_page *)page)->commit)
370 + BUF_PAGE_HDR_SIZE;
ef7a4a16
SR
371}
372
ed56829c
SR
373/*
374 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
375 * this issue out.
376 */
34a148bf 377static void free_buffer_page(struct buffer_page *bpage)
ed56829c 378{
34a148bf 379 free_page((unsigned long)bpage->page);
e4c2ce82 380 kfree(bpage);
ed56829c
SR
381}
382
7a8e76a3
SR
383/*
384 * We need to fit the time_stamp delta into 27 bits.
385 */
386static inline int test_time_stamp(u64 delta)
387{
388 if (delta & TS_DELTA_TEST)
389 return 1;
390 return 0;
391}
392
474d32b6 393#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
7a8e76a3 394
be957c44
SR
395/* Max payload is BUF_PAGE_SIZE - header (8bytes) */
396#define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
397
ea05b57c
SR
398/* Max number of timestamps that can fit on a page */
399#define RB_TIMESTAMPS_PER_PAGE (BUF_PAGE_SIZE / RB_LEN_TIME_STAMP)
400
d1b182a8
SR
401int ring_buffer_print_page_header(struct trace_seq *s)
402{
403 struct buffer_data_page field;
404 int ret;
405
406 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
407 "offset:0;\tsize:%u;\n",
408 (unsigned int)sizeof(field.time_stamp));
409
410 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
411 "offset:%u;\tsize:%u;\n",
412 (unsigned int)offsetof(typeof(field), commit),
413 (unsigned int)sizeof(field.commit));
414
415 ret = trace_seq_printf(s, "\tfield: char data;\t"
416 "offset:%u;\tsize:%u;\n",
417 (unsigned int)offsetof(typeof(field), data),
418 (unsigned int)BUF_PAGE_SIZE);
419
420 return ret;
421}
422
7a8e76a3
SR
423/*
424 * head_page == tail_page && head == tail then buffer is empty.
425 */
426struct ring_buffer_per_cpu {
427 int cpu;
428 struct ring_buffer *buffer;
77ae365e 429 spinlock_t reader_lock; /* serialize readers */
3e03fb7f 430 raw_spinlock_t lock;
7a8e76a3 431 struct lock_class_key lock_key;
3adc54fa 432 struct list_head *pages;
6f807acd
SR
433 struct buffer_page *head_page; /* read from head */
434 struct buffer_page *tail_page; /* write to tail */
c3706f00 435 struct buffer_page *commit_page; /* committed pages */
d769041f 436 struct buffer_page *reader_page;
77ae365e
SR
437 local_t commit_overrun;
438 local_t overrun;
e4906eff 439 local_t entries;
fa743953
SR
440 local_t committing;
441 local_t commits;
77ae365e 442 unsigned long read;
7a8e76a3
SR
443 u64 write_stamp;
444 u64 read_stamp;
445 atomic_t record_disabled;
446};
447
448struct ring_buffer {
7a8e76a3
SR
449 unsigned pages;
450 unsigned flags;
451 int cpus;
7a8e76a3 452 atomic_t record_disabled;
00f62f61 453 cpumask_var_t cpumask;
7a8e76a3 454
1f8a6a10
PZ
455 struct lock_class_key *reader_lock_key;
456
7a8e76a3
SR
457 struct mutex mutex;
458
459 struct ring_buffer_per_cpu **buffers;
554f786e 460
59222efe 461#ifdef CONFIG_HOTPLUG_CPU
554f786e
SR
462 struct notifier_block cpu_notify;
463#endif
37886f6a 464 u64 (*clock)(void);
7a8e76a3
SR
465};
466
467struct ring_buffer_iter {
468 struct ring_buffer_per_cpu *cpu_buffer;
469 unsigned long head;
470 struct buffer_page *head_page;
471 u64 read_stamp;
472};
473
f536aafc 474/* buffer may be either ring_buffer or ring_buffer_per_cpu */
bf41a158 475#define RB_WARN_ON(buffer, cond) \
3e89c7bb
SR
476 ({ \
477 int _____ret = unlikely(cond); \
478 if (_____ret) { \
bf41a158
SR
479 atomic_inc(&buffer->record_disabled); \
480 WARN_ON(1); \
481 } \
3e89c7bb
SR
482 _____ret; \
483 })
f536aafc 484
37886f6a
SR
485/* Up this if you want to test the TIME_EXTENTS and normalization */
486#define DEBUG_SHIFT 0
487
88eb0125
SR
488static inline u64 rb_time_stamp(struct ring_buffer *buffer, int cpu)
489{
490 /* shift to debug/test normalization and TIME_EXTENTS */
491 return buffer->clock() << DEBUG_SHIFT;
492}
493
37886f6a
SR
494u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
495{
496 u64 time;
497
498 preempt_disable_notrace();
88eb0125 499 time = rb_time_stamp(buffer, cpu);
37886f6a
SR
500 preempt_enable_no_resched_notrace();
501
502 return time;
503}
504EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
505
506void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
507 int cpu, u64 *ts)
508{
509 /* Just stupid testing the normalize function and deltas */
510 *ts >>= DEBUG_SHIFT;
511}
512EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
513
77ae365e
SR
514/*
515 * Making the ring buffer lockless makes things tricky.
516 * Although writes only happen on the CPU that they are on,
517 * and they only need to worry about interrupts. Reads can
518 * happen on any CPU.
519 *
520 * The reader page is always off the ring buffer, but when the
521 * reader finishes with a page, it needs to swap its page with
522 * a new one from the buffer. The reader needs to take from
523 * the head (writes go to the tail). But if a writer is in overwrite
524 * mode and wraps, it must push the head page forward.
525 *
526 * Here lies the problem.
527 *
528 * The reader must be careful to replace only the head page, and
529 * not another one. As described at the top of the file in the
530 * ASCII art, the reader sets its old page to point to the next
531 * page after head. It then sets the page after head to point to
532 * the old reader page. But if the writer moves the head page
533 * during this operation, the reader could end up with the tail.
534 *
535 * We use cmpxchg to help prevent this race. We also do something
536 * special with the page before head. We set the LSB to 1.
537 *
538 * When the writer must push the page forward, it will clear the
539 * bit that points to the head page, move the head, and then set
540 * the bit that points to the new head page.
541 *
542 * We also don't want an interrupt coming in and moving the head
543 * page on another writer. Thus we use the second LSB to catch
544 * that too. Thus:
545 *
546 * head->list->prev->next bit 1 bit 0
547 * ------- -------
548 * Normal page 0 0
549 * Points to head page 0 1
550 * New head page 1 0
551 *
552 * Note we can not trust the prev pointer of the head page, because:
553 *
554 * +----+ +-----+ +-----+
555 * | |------>| T |---X--->| N |
556 * | |<------| | | |
557 * +----+ +-----+ +-----+
558 * ^ ^ |
559 * | +-----+ | |
560 * +----------| R |----------+ |
561 * | |<-----------+
562 * +-----+
563 *
564 * Key: ---X--> HEAD flag set in pointer
565 * T Tail page
566 * R Reader page
567 * N Next page
568 *
569 * (see __rb_reserve_next() to see where this happens)
570 *
571 * What the above shows is that the reader just swapped out
572 * the reader page with a page in the buffer, but before it
573 * could make the new header point back to the new page added
574 * it was preempted by a writer. The writer moved forward onto
575 * the new page added by the reader and is about to move forward
576 * again.
577 *
578 * You can see, it is legitimate for the previous pointer of
579 * the head (or any page) not to point back to itself. But only
580 * temporarially.
581 */
582
583#define RB_PAGE_NORMAL 0UL
584#define RB_PAGE_HEAD 1UL
585#define RB_PAGE_UPDATE 2UL
586
587
588#define RB_FLAG_MASK 3UL
589
590/* PAGE_MOVED is not part of the mask */
591#define RB_PAGE_MOVED 4UL
592
593/*
594 * rb_list_head - remove any bit
595 */
596static struct list_head *rb_list_head(struct list_head *list)
597{
598 unsigned long val = (unsigned long)list;
599
600 return (struct list_head *)(val & ~RB_FLAG_MASK);
601}
602
603/*
604 * rb_is_head_page - test if the give page is the head page
605 *
606 * Because the reader may move the head_page pointer, we can
607 * not trust what the head page is (it may be pointing to
608 * the reader page). But if the next page is a header page,
609 * its flags will be non zero.
610 */
611static int inline
612rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
613 struct buffer_page *page, struct list_head *list)
614{
615 unsigned long val;
616
617 val = (unsigned long)list->next;
618
619 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
620 return RB_PAGE_MOVED;
621
622 return val & RB_FLAG_MASK;
623}
624
625/*
626 * rb_is_reader_page
627 *
628 * The unique thing about the reader page, is that, if the
629 * writer is ever on it, the previous pointer never points
630 * back to the reader page.
631 */
632static int rb_is_reader_page(struct buffer_page *page)
633{
634 struct list_head *list = page->list.prev;
635
636 return rb_list_head(list->next) != &page->list;
637}
638
639/*
640 * rb_set_list_to_head - set a list_head to be pointing to head.
641 */
642static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
643 struct list_head *list)
644{
645 unsigned long *ptr;
646
647 ptr = (unsigned long *)&list->next;
648 *ptr |= RB_PAGE_HEAD;
649 *ptr &= ~RB_PAGE_UPDATE;
650}
651
652/*
653 * rb_head_page_activate - sets up head page
654 */
655static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
656{
657 struct buffer_page *head;
658
659 head = cpu_buffer->head_page;
660 if (!head)
661 return;
662
663 /*
664 * Set the previous list pointer to have the HEAD flag.
665 */
666 rb_set_list_to_head(cpu_buffer, head->list.prev);
667}
668
669static void rb_list_head_clear(struct list_head *list)
670{
671 unsigned long *ptr = (unsigned long *)&list->next;
672
673 *ptr &= ~RB_FLAG_MASK;
674}
675
676/*
677 * rb_head_page_dactivate - clears head page ptr (for free list)
678 */
679static void
680rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
681{
682 struct list_head *hd;
683
684 /* Go through the whole list and clear any pointers found. */
685 rb_list_head_clear(cpu_buffer->pages);
686
687 list_for_each(hd, cpu_buffer->pages)
688 rb_list_head_clear(hd);
689}
690
691static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
692 struct buffer_page *head,
693 struct buffer_page *prev,
694 int old_flag, int new_flag)
695{
696 struct list_head *list;
697 unsigned long val = (unsigned long)&head->list;
698 unsigned long ret;
699
700 list = &prev->list;
701
702 val &= ~RB_FLAG_MASK;
703
704 ret = (unsigned long)cmpxchg(&list->next,
705 val | old_flag, val | new_flag);
706
707 /* check if the reader took the page */
708 if ((ret & ~RB_FLAG_MASK) != val)
709 return RB_PAGE_MOVED;
710
711 return ret & RB_FLAG_MASK;
712}
713
714static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
715 struct buffer_page *head,
716 struct buffer_page *prev,
717 int old_flag)
718{
719 return rb_head_page_set(cpu_buffer, head, prev,
720 old_flag, RB_PAGE_UPDATE);
721}
722
723static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
724 struct buffer_page *head,
725 struct buffer_page *prev,
726 int old_flag)
727{
728 return rb_head_page_set(cpu_buffer, head, prev,
729 old_flag, RB_PAGE_HEAD);
730}
731
732static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
733 struct buffer_page *head,
734 struct buffer_page *prev,
735 int old_flag)
736{
737 return rb_head_page_set(cpu_buffer, head, prev,
738 old_flag, RB_PAGE_NORMAL);
739}
740
741static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
742 struct buffer_page **bpage)
743{
744 struct list_head *p = rb_list_head((*bpage)->list.next);
745
746 *bpage = list_entry(p, struct buffer_page, list);
747}
748
749static struct buffer_page *
750rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
751{
752 struct buffer_page *head;
753 struct buffer_page *page;
754 struct list_head *list;
755 int i;
756
757 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
758 return NULL;
759
760 /* sanity check */
761 list = cpu_buffer->pages;
762 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
763 return NULL;
764
765 page = head = cpu_buffer->head_page;
766 /*
767 * It is possible that the writer moves the header behind
768 * where we started, and we miss in one loop.
769 * A second loop should grab the header, but we'll do
770 * three loops just because I'm paranoid.
771 */
772 for (i = 0; i < 3; i++) {
773 do {
774 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
775 cpu_buffer->head_page = page;
776 return page;
777 }
778 rb_inc_page(cpu_buffer, &page);
779 } while (page != head);
780 }
781
782 RB_WARN_ON(cpu_buffer, 1);
783
784 return NULL;
785}
786
787static int rb_head_page_replace(struct buffer_page *old,
788 struct buffer_page *new)
789{
790 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
791 unsigned long val;
792 unsigned long ret;
793
794 val = *ptr & ~RB_FLAG_MASK;
795 val |= RB_PAGE_HEAD;
796
797 ret = cmpxchg(ptr, val, &new->list);
798
799 return ret == val;
800}
801
802/*
803 * rb_tail_page_update - move the tail page forward
804 *
805 * Returns 1 if moved tail page, 0 if someone else did.
806 */
807static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
808 struct buffer_page *tail_page,
809 struct buffer_page *next_page)
810{
811 struct buffer_page *old_tail;
812 unsigned long old_entries;
813 unsigned long old_write;
814 int ret = 0;
815
816 /*
817 * The tail page now needs to be moved forward.
818 *
819 * We need to reset the tail page, but without messing
820 * with possible erasing of data brought in by interrupts
821 * that have moved the tail page and are currently on it.
822 *
823 * We add a counter to the write field to denote this.
824 */
825 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
826 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
827
828 /*
829 * Just make sure we have seen our old_write and synchronize
830 * with any interrupts that come in.
831 */
832 barrier();
833
834 /*
835 * If the tail page is still the same as what we think
836 * it is, then it is up to us to update the tail
837 * pointer.
838 */
839 if (tail_page == cpu_buffer->tail_page) {
840 /* Zero the write counter */
841 unsigned long val = old_write & ~RB_WRITE_MASK;
842 unsigned long eval = old_entries & ~RB_WRITE_MASK;
843
844 /*
845 * This will only succeed if an interrupt did
846 * not come in and change it. In which case, we
847 * do not want to modify it.
848 */
849 local_cmpxchg(&next_page->write, old_write, val);
850 local_cmpxchg(&next_page->entries, old_entries, eval);
851
852 /*
853 * No need to worry about races with clearing out the commit.
854 * it only can increment when a commit takes place. But that
855 * only happens in the outer most nested commit.
856 */
857 local_set(&next_page->page->commit, 0);
858
859 old_tail = cmpxchg(&cpu_buffer->tail_page,
860 tail_page, next_page);
861
862 if (old_tail == tail_page)
863 ret = 1;
864 }
865
866 return ret;
867}
868
869static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
870 struct buffer_page *bpage)
871{
872 unsigned long val = (unsigned long)bpage;
873
874 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
875 return 1;
876
877 return 0;
878}
879
880/**
881 * rb_check_list - make sure a pointer to a list has the last bits zero
882 */
883static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
884 struct list_head *list)
885{
886 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
887 return 1;
888 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
889 return 1;
890 return 0;
891}
892
7a8e76a3
SR
893/**
894 * check_pages - integrity check of buffer pages
895 * @cpu_buffer: CPU buffer with pages to test
896 *
c3706f00 897 * As a safety measure we check to make sure the data pages have not
7a8e76a3
SR
898 * been corrupted.
899 */
900static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
901{
3adc54fa 902 struct list_head *head = cpu_buffer->pages;
044fa782 903 struct buffer_page *bpage, *tmp;
7a8e76a3 904
77ae365e
SR
905 rb_head_page_deactivate(cpu_buffer);
906
3e89c7bb
SR
907 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
908 return -1;
909 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
910 return -1;
7a8e76a3 911
77ae365e
SR
912 if (rb_check_list(cpu_buffer, head))
913 return -1;
914
044fa782 915 list_for_each_entry_safe(bpage, tmp, head, list) {
3e89c7bb 916 if (RB_WARN_ON(cpu_buffer,
044fa782 917 bpage->list.next->prev != &bpage->list))
3e89c7bb
SR
918 return -1;
919 if (RB_WARN_ON(cpu_buffer,
044fa782 920 bpage->list.prev->next != &bpage->list))
3e89c7bb 921 return -1;
77ae365e
SR
922 if (rb_check_list(cpu_buffer, &bpage->list))
923 return -1;
7a8e76a3
SR
924 }
925
77ae365e
SR
926 rb_head_page_activate(cpu_buffer);
927
7a8e76a3
SR
928 return 0;
929}
930
7a8e76a3
SR
931static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
932 unsigned nr_pages)
933{
044fa782 934 struct buffer_page *bpage, *tmp;
7a8e76a3
SR
935 unsigned long addr;
936 LIST_HEAD(pages);
937 unsigned i;
938
3adc54fa
SR
939 WARN_ON(!nr_pages);
940
7a8e76a3 941 for (i = 0; i < nr_pages; i++) {
044fa782 942 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
aa1e0e3b 943 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
044fa782 944 if (!bpage)
e4c2ce82 945 goto free_pages;
77ae365e
SR
946
947 rb_check_bpage(cpu_buffer, bpage);
948
044fa782 949 list_add(&bpage->list, &pages);
e4c2ce82 950
7a8e76a3
SR
951 addr = __get_free_page(GFP_KERNEL);
952 if (!addr)
953 goto free_pages;
044fa782
SR
954 bpage->page = (void *)addr;
955 rb_init_page(bpage->page);
7a8e76a3
SR
956 }
957
3adc54fa
SR
958 /*
959 * The ring buffer page list is a circular list that does not
960 * start and end with a list head. All page list items point to
961 * other pages.
962 */
963 cpu_buffer->pages = pages.next;
964 list_del(&pages);
7a8e76a3
SR
965
966 rb_check_pages(cpu_buffer);
967
968 return 0;
969
970 free_pages:
044fa782
SR
971 list_for_each_entry_safe(bpage, tmp, &pages, list) {
972 list_del_init(&bpage->list);
973 free_buffer_page(bpage);
7a8e76a3
SR
974 }
975 return -ENOMEM;
976}
977
978static struct ring_buffer_per_cpu *
979rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
980{
981 struct ring_buffer_per_cpu *cpu_buffer;
044fa782 982 struct buffer_page *bpage;
d769041f 983 unsigned long addr;
7a8e76a3
SR
984 int ret;
985
986 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
987 GFP_KERNEL, cpu_to_node(cpu));
988 if (!cpu_buffer)
989 return NULL;
990
991 cpu_buffer->cpu = cpu;
992 cpu_buffer->buffer = buffer;
f83c9d0f 993 spin_lock_init(&cpu_buffer->reader_lock);
1f8a6a10 994 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
3e03fb7f 995 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
7a8e76a3 996
044fa782 997 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
e4c2ce82 998 GFP_KERNEL, cpu_to_node(cpu));
044fa782 999 if (!bpage)
e4c2ce82
SR
1000 goto fail_free_buffer;
1001
77ae365e
SR
1002 rb_check_bpage(cpu_buffer, bpage);
1003
044fa782 1004 cpu_buffer->reader_page = bpage;
d769041f
SR
1005 addr = __get_free_page(GFP_KERNEL);
1006 if (!addr)
e4c2ce82 1007 goto fail_free_reader;
044fa782
SR
1008 bpage->page = (void *)addr;
1009 rb_init_page(bpage->page);
e4c2ce82 1010
d769041f 1011 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
d769041f 1012
7a8e76a3
SR
1013 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
1014 if (ret < 0)
d769041f 1015 goto fail_free_reader;
7a8e76a3
SR
1016
1017 cpu_buffer->head_page
3adc54fa 1018 = list_entry(cpu_buffer->pages, struct buffer_page, list);
bf41a158 1019 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
7a8e76a3 1020
77ae365e
SR
1021 rb_head_page_activate(cpu_buffer);
1022
7a8e76a3
SR
1023 return cpu_buffer;
1024
d769041f
SR
1025 fail_free_reader:
1026 free_buffer_page(cpu_buffer->reader_page);
1027
7a8e76a3
SR
1028 fail_free_buffer:
1029 kfree(cpu_buffer);
1030 return NULL;
1031}
1032
1033static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1034{
3adc54fa 1035 struct list_head *head = cpu_buffer->pages;
044fa782 1036 struct buffer_page *bpage, *tmp;
7a8e76a3 1037
d769041f
SR
1038 free_buffer_page(cpu_buffer->reader_page);
1039
77ae365e
SR
1040 rb_head_page_deactivate(cpu_buffer);
1041
3adc54fa
SR
1042 if (head) {
1043 list_for_each_entry_safe(bpage, tmp, head, list) {
1044 list_del_init(&bpage->list);
1045 free_buffer_page(bpage);
1046 }
1047 bpage = list_entry(head, struct buffer_page, list);
044fa782 1048 free_buffer_page(bpage);
7a8e76a3 1049 }
3adc54fa 1050
7a8e76a3
SR
1051 kfree(cpu_buffer);
1052}
1053
59222efe 1054#ifdef CONFIG_HOTPLUG_CPU
09c9e84d
FW
1055static int rb_cpu_notify(struct notifier_block *self,
1056 unsigned long action, void *hcpu);
554f786e
SR
1057#endif
1058
7a8e76a3
SR
1059/**
1060 * ring_buffer_alloc - allocate a new ring_buffer
68814b58 1061 * @size: the size in bytes per cpu that is needed.
7a8e76a3
SR
1062 * @flags: attributes to set for the ring buffer.
1063 *
1064 * Currently the only flag that is available is the RB_FL_OVERWRITE
1065 * flag. This flag means that the buffer will overwrite old data
1066 * when the buffer wraps. If this flag is not set, the buffer will
1067 * drop data when the tail hits the head.
1068 */
1f8a6a10
PZ
1069struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1070 struct lock_class_key *key)
7a8e76a3
SR
1071{
1072 struct ring_buffer *buffer;
1073 int bsize;
1074 int cpu;
1075
1076 /* keep it in its own cache line */
1077 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1078 GFP_KERNEL);
1079 if (!buffer)
1080 return NULL;
1081
9e01c1b7
RR
1082 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1083 goto fail_free_buffer;
1084
7a8e76a3
SR
1085 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1086 buffer->flags = flags;
37886f6a 1087 buffer->clock = trace_clock_local;
1f8a6a10 1088 buffer->reader_lock_key = key;
7a8e76a3
SR
1089
1090 /* need at least two pages */
5f78abee
SR
1091 if (buffer->pages < 2)
1092 buffer->pages = 2;
7a8e76a3 1093
3bf832ce
FW
1094 /*
1095 * In case of non-hotplug cpu, if the ring-buffer is allocated
1096 * in early initcall, it will not be notified of secondary cpus.
1097 * In that off case, we need to allocate for all possible cpus.
1098 */
1099#ifdef CONFIG_HOTPLUG_CPU
554f786e
SR
1100 get_online_cpus();
1101 cpumask_copy(buffer->cpumask, cpu_online_mask);
3bf832ce
FW
1102#else
1103 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1104#endif
7a8e76a3
SR
1105 buffer->cpus = nr_cpu_ids;
1106
1107 bsize = sizeof(void *) * nr_cpu_ids;
1108 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1109 GFP_KERNEL);
1110 if (!buffer->buffers)
9e01c1b7 1111 goto fail_free_cpumask;
7a8e76a3
SR
1112
1113 for_each_buffer_cpu(buffer, cpu) {
1114 buffer->buffers[cpu] =
1115 rb_allocate_cpu_buffer(buffer, cpu);
1116 if (!buffer->buffers[cpu])
1117 goto fail_free_buffers;
1118 }
1119
59222efe 1120#ifdef CONFIG_HOTPLUG_CPU
554f786e
SR
1121 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1122 buffer->cpu_notify.priority = 0;
1123 register_cpu_notifier(&buffer->cpu_notify);
1124#endif
1125
1126 put_online_cpus();
7a8e76a3
SR
1127 mutex_init(&buffer->mutex);
1128
1129 return buffer;
1130
1131 fail_free_buffers:
1132 for_each_buffer_cpu(buffer, cpu) {
1133 if (buffer->buffers[cpu])
1134 rb_free_cpu_buffer(buffer->buffers[cpu]);
1135 }
1136 kfree(buffer->buffers);
1137
9e01c1b7
RR
1138 fail_free_cpumask:
1139 free_cpumask_var(buffer->cpumask);
554f786e 1140 put_online_cpus();
9e01c1b7 1141
7a8e76a3
SR
1142 fail_free_buffer:
1143 kfree(buffer);
1144 return NULL;
1145}
1f8a6a10 1146EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
7a8e76a3
SR
1147
1148/**
1149 * ring_buffer_free - free a ring buffer.
1150 * @buffer: the buffer to free.
1151 */
1152void
1153ring_buffer_free(struct ring_buffer *buffer)
1154{
1155 int cpu;
1156
554f786e
SR
1157 get_online_cpus();
1158
59222efe 1159#ifdef CONFIG_HOTPLUG_CPU
554f786e
SR
1160 unregister_cpu_notifier(&buffer->cpu_notify);
1161#endif
1162
7a8e76a3
SR
1163 for_each_buffer_cpu(buffer, cpu)
1164 rb_free_cpu_buffer(buffer->buffers[cpu]);
1165
554f786e
SR
1166 put_online_cpus();
1167
9e01c1b7
RR
1168 free_cpumask_var(buffer->cpumask);
1169
7a8e76a3
SR
1170 kfree(buffer);
1171}
c4f50183 1172EXPORT_SYMBOL_GPL(ring_buffer_free);
7a8e76a3 1173
37886f6a
SR
1174void ring_buffer_set_clock(struct ring_buffer *buffer,
1175 u64 (*clock)(void))
1176{
1177 buffer->clock = clock;
1178}
1179
7a8e76a3
SR
1180static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1181
1182static void
1183rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
1184{
044fa782 1185 struct buffer_page *bpage;
7a8e76a3
SR
1186 struct list_head *p;
1187 unsigned i;
1188
1189 atomic_inc(&cpu_buffer->record_disabled);
1190 synchronize_sched();
1191
77ae365e
SR
1192 rb_head_page_deactivate(cpu_buffer);
1193
7a8e76a3 1194 for (i = 0; i < nr_pages; i++) {
3adc54fa 1195 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
3e89c7bb 1196 return;
3adc54fa 1197 p = cpu_buffer->pages->next;
044fa782
SR
1198 bpage = list_entry(p, struct buffer_page, list);
1199 list_del_init(&bpage->list);
1200 free_buffer_page(bpage);
7a8e76a3 1201 }
3adc54fa 1202 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
3e89c7bb 1203 return;
7a8e76a3
SR
1204
1205 rb_reset_cpu(cpu_buffer);
1206
1207 rb_check_pages(cpu_buffer);
1208
1209 atomic_dec(&cpu_buffer->record_disabled);
1210
1211}
1212
1213static void
1214rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
1215 struct list_head *pages, unsigned nr_pages)
1216{
044fa782 1217 struct buffer_page *bpage;
7a8e76a3
SR
1218 struct list_head *p;
1219 unsigned i;
1220
1221 atomic_inc(&cpu_buffer->record_disabled);
1222 synchronize_sched();
1223
77ae365e
SR
1224 spin_lock_irq(&cpu_buffer->reader_lock);
1225 rb_head_page_deactivate(cpu_buffer);
1226
7a8e76a3 1227 for (i = 0; i < nr_pages; i++) {
3e89c7bb
SR
1228 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
1229 return;
7a8e76a3 1230 p = pages->next;
044fa782
SR
1231 bpage = list_entry(p, struct buffer_page, list);
1232 list_del_init(&bpage->list);
3adc54fa 1233 list_add_tail(&bpage->list, cpu_buffer->pages);
7a8e76a3
SR
1234 }
1235 rb_reset_cpu(cpu_buffer);
77ae365e 1236 spin_unlock_irq(&cpu_buffer->reader_lock);
7a8e76a3
SR
1237
1238 rb_check_pages(cpu_buffer);
1239
1240 atomic_dec(&cpu_buffer->record_disabled);
1241}
1242
1243/**
1244 * ring_buffer_resize - resize the ring buffer
1245 * @buffer: the buffer to resize.
1246 * @size: the new size.
1247 *
1248 * The tracer is responsible for making sure that the buffer is
1249 * not being used while changing the size.
1250 * Note: We may be able to change the above requirement by using
1251 * RCU synchronizations.
1252 *
1253 * Minimum size is 2 * BUF_PAGE_SIZE.
1254 *
1255 * Returns -1 on failure.
1256 */
1257int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
1258{
1259 struct ring_buffer_per_cpu *cpu_buffer;
1260 unsigned nr_pages, rm_pages, new_pages;
044fa782 1261 struct buffer_page *bpage, *tmp;
7a8e76a3
SR
1262 unsigned long buffer_size;
1263 unsigned long addr;
1264 LIST_HEAD(pages);
1265 int i, cpu;
1266
ee51a1de
IM
1267 /*
1268 * Always succeed at resizing a non-existent buffer:
1269 */
1270 if (!buffer)
1271 return size;
1272
7a8e76a3
SR
1273 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1274 size *= BUF_PAGE_SIZE;
1275 buffer_size = buffer->pages * BUF_PAGE_SIZE;
1276
1277 /* we need a minimum of two pages */
1278 if (size < BUF_PAGE_SIZE * 2)
1279 size = BUF_PAGE_SIZE * 2;
1280
1281 if (size == buffer_size)
1282 return size;
1283
1284 mutex_lock(&buffer->mutex);
554f786e 1285 get_online_cpus();
7a8e76a3
SR
1286
1287 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1288
1289 if (size < buffer_size) {
1290
1291 /* easy case, just free pages */
554f786e
SR
1292 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
1293 goto out_fail;
7a8e76a3
SR
1294
1295 rm_pages = buffer->pages - nr_pages;
1296
1297 for_each_buffer_cpu(buffer, cpu) {
1298 cpu_buffer = buffer->buffers[cpu];
1299 rb_remove_pages(cpu_buffer, rm_pages);
1300 }
1301 goto out;
1302 }
1303
1304 /*
1305 * This is a bit more difficult. We only want to add pages
1306 * when we can allocate enough for all CPUs. We do this
1307 * by allocating all the pages and storing them on a local
1308 * link list. If we succeed in our allocation, then we
1309 * add these pages to the cpu_buffers. Otherwise we just free
1310 * them all and return -ENOMEM;
1311 */
554f786e
SR
1312 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
1313 goto out_fail;
f536aafc 1314
7a8e76a3
SR
1315 new_pages = nr_pages - buffer->pages;
1316
1317 for_each_buffer_cpu(buffer, cpu) {
1318 for (i = 0; i < new_pages; i++) {
044fa782 1319 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
e4c2ce82
SR
1320 cache_line_size()),
1321 GFP_KERNEL, cpu_to_node(cpu));
044fa782 1322 if (!bpage)
e4c2ce82 1323 goto free_pages;
044fa782 1324 list_add(&bpage->list, &pages);
7a8e76a3
SR
1325 addr = __get_free_page(GFP_KERNEL);
1326 if (!addr)
1327 goto free_pages;
044fa782
SR
1328 bpage->page = (void *)addr;
1329 rb_init_page(bpage->page);
7a8e76a3
SR
1330 }
1331 }
1332
1333 for_each_buffer_cpu(buffer, cpu) {
1334 cpu_buffer = buffer->buffers[cpu];
1335 rb_insert_pages(cpu_buffer, &pages, new_pages);
1336 }
1337
554f786e
SR
1338 if (RB_WARN_ON(buffer, !list_empty(&pages)))
1339 goto out_fail;
7a8e76a3
SR
1340
1341 out:
1342 buffer->pages = nr_pages;
554f786e 1343 put_online_cpus();
7a8e76a3
SR
1344 mutex_unlock(&buffer->mutex);
1345
1346 return size;
1347
1348 free_pages:
044fa782
SR
1349 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1350 list_del_init(&bpage->list);
1351 free_buffer_page(bpage);
7a8e76a3 1352 }
554f786e 1353 put_online_cpus();
641d2f63 1354 mutex_unlock(&buffer->mutex);
7a8e76a3 1355 return -ENOMEM;
554f786e
SR
1356
1357 /*
1358 * Something went totally wrong, and we are too paranoid
1359 * to even clean up the mess.
1360 */
1361 out_fail:
1362 put_online_cpus();
1363 mutex_unlock(&buffer->mutex);
1364 return -1;
7a8e76a3 1365}
c4f50183 1366EXPORT_SYMBOL_GPL(ring_buffer_resize);
7a8e76a3 1367
8789a9e7 1368static inline void *
044fa782 1369__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
8789a9e7 1370{
044fa782 1371 return bpage->data + index;
8789a9e7
SR
1372}
1373
044fa782 1374static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
7a8e76a3 1375{
044fa782 1376 return bpage->page->data + index;
7a8e76a3
SR
1377}
1378
1379static inline struct ring_buffer_event *
d769041f 1380rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
7a8e76a3 1381{
6f807acd
SR
1382 return __rb_page_index(cpu_buffer->reader_page,
1383 cpu_buffer->reader_page->read);
1384}
1385
7a8e76a3
SR
1386static inline struct ring_buffer_event *
1387rb_iter_head_event(struct ring_buffer_iter *iter)
1388{
6f807acd 1389 return __rb_page_index(iter->head_page, iter->head);
7a8e76a3
SR
1390}
1391
77ae365e 1392static inline unsigned long rb_page_write(struct buffer_page *bpage)
bf41a158 1393{
77ae365e 1394 return local_read(&bpage->write) & RB_WRITE_MASK;
bf41a158
SR
1395}
1396
1397static inline unsigned rb_page_commit(struct buffer_page *bpage)
1398{
abc9b56d 1399 return local_read(&bpage->page->commit);
bf41a158
SR
1400}
1401
77ae365e
SR
1402static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1403{
1404 return local_read(&bpage->entries) & RB_WRITE_MASK;
1405}
1406
bf41a158
SR
1407/* Size is determined by what has been commited */
1408static inline unsigned rb_page_size(struct buffer_page *bpage)
1409{
1410 return rb_page_commit(bpage);
1411}
1412
1413static inline unsigned
1414rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1415{
1416 return rb_page_commit(cpu_buffer->commit_page);
1417}
1418
bf41a158
SR
1419static inline unsigned
1420rb_event_index(struct ring_buffer_event *event)
1421{
1422 unsigned long addr = (unsigned long)event;
1423
22f470f8 1424 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
bf41a158
SR
1425}
1426
0f0c85fc 1427static inline int
fa743953
SR
1428rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1429 struct ring_buffer_event *event)
bf41a158
SR
1430{
1431 unsigned long addr = (unsigned long)event;
1432 unsigned long index;
1433
1434 index = rb_event_index(event);
1435 addr &= PAGE_MASK;
1436
1437 return cpu_buffer->commit_page->page == (void *)addr &&
1438 rb_commit_index(cpu_buffer) == index;
1439}
1440
34a148bf 1441static void
bf41a158 1442rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
7a8e76a3 1443{
77ae365e
SR
1444 unsigned long max_count;
1445
bf41a158
SR
1446 /*
1447 * We only race with interrupts and NMIs on this CPU.
1448 * If we own the commit event, then we can commit
1449 * all others that interrupted us, since the interruptions
1450 * are in stack format (they finish before they come
1451 * back to us). This allows us to do a simple loop to
1452 * assign the commit to the tail.
1453 */
a8ccf1d6 1454 again:
77ae365e
SR
1455 max_count = cpu_buffer->buffer->pages * 100;
1456
bf41a158 1457 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
77ae365e
SR
1458 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1459 return;
1460 if (RB_WARN_ON(cpu_buffer,
1461 rb_is_reader_page(cpu_buffer->tail_page)))
1462 return;
1463 local_set(&cpu_buffer->commit_page->page->commit,
1464 rb_page_write(cpu_buffer->commit_page));
bf41a158 1465 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
abc9b56d
SR
1466 cpu_buffer->write_stamp =
1467 cpu_buffer->commit_page->page->time_stamp;
bf41a158
SR
1468 /* add barrier to keep gcc from optimizing too much */
1469 barrier();
1470 }
1471 while (rb_commit_index(cpu_buffer) !=
1472 rb_page_write(cpu_buffer->commit_page)) {
77ae365e
SR
1473
1474 local_set(&cpu_buffer->commit_page->page->commit,
1475 rb_page_write(cpu_buffer->commit_page));
1476 RB_WARN_ON(cpu_buffer,
1477 local_read(&cpu_buffer->commit_page->page->commit) &
1478 ~RB_WRITE_MASK);
bf41a158
SR
1479 barrier();
1480 }
a8ccf1d6
SR
1481
1482 /* again, keep gcc from optimizing */
1483 barrier();
1484
1485 /*
1486 * If an interrupt came in just after the first while loop
1487 * and pushed the tail page forward, we will be left with
1488 * a dangling commit that will never go forward.
1489 */
1490 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1491 goto again;
7a8e76a3
SR
1492}
1493
d769041f 1494static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
7a8e76a3 1495{
abc9b56d 1496 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
6f807acd 1497 cpu_buffer->reader_page->read = 0;
d769041f
SR
1498}
1499
34a148bf 1500static void rb_inc_iter(struct ring_buffer_iter *iter)
d769041f
SR
1501{
1502 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1503
1504 /*
1505 * The iterator could be on the reader page (it starts there).
1506 * But the head could have moved, since the reader was
1507 * found. Check for this case and assign the iterator
1508 * to the head page instead of next.
1509 */
1510 if (iter->head_page == cpu_buffer->reader_page)
77ae365e 1511 iter->head_page = rb_set_head_page(cpu_buffer);
d769041f
SR
1512 else
1513 rb_inc_page(cpu_buffer, &iter->head_page);
1514
abc9b56d 1515 iter->read_stamp = iter->head_page->page->time_stamp;
7a8e76a3
SR
1516 iter->head = 0;
1517}
1518
1519/**
1520 * ring_buffer_update_event - update event type and data
1521 * @event: the even to update
1522 * @type: the type of event
1523 * @length: the size of the event field in the ring buffer
1524 *
1525 * Update the type and data fields of the event. The length
1526 * is the actual size that is written to the ring buffer,
1527 * and with this, we can determine what to place into the
1528 * data field.
1529 */
34a148bf 1530static void
7a8e76a3
SR
1531rb_update_event(struct ring_buffer_event *event,
1532 unsigned type, unsigned length)
1533{
334d4169 1534 event->type_len = type;
7a8e76a3
SR
1535
1536 switch (type) {
1537
1538 case RINGBUF_TYPE_PADDING:
7a8e76a3 1539 case RINGBUF_TYPE_TIME_EXTEND:
7a8e76a3 1540 case RINGBUF_TYPE_TIME_STAMP:
7a8e76a3
SR
1541 break;
1542
334d4169 1543 case 0:
7a8e76a3 1544 length -= RB_EVNT_HDR_SIZE;
334d4169 1545 if (length > RB_MAX_SMALL_DATA)
7a8e76a3 1546 event->array[0] = length;
334d4169
LJ
1547 else
1548 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
7a8e76a3
SR
1549 break;
1550 default:
1551 BUG();
1552 }
1553}
1554
77ae365e
SR
1555/*
1556 * rb_handle_head_page - writer hit the head page
1557 *
1558 * Returns: +1 to retry page
1559 * 0 to continue
1560 * -1 on error
1561 */
1562static int
1563rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1564 struct buffer_page *tail_page,
1565 struct buffer_page *next_page)
1566{
1567 struct buffer_page *new_head;
1568 int entries;
1569 int type;
1570 int ret;
1571
1572 entries = rb_page_entries(next_page);
1573
1574 /*
1575 * The hard part is here. We need to move the head
1576 * forward, and protect against both readers on
1577 * other CPUs and writers coming in via interrupts.
1578 */
1579 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1580 RB_PAGE_HEAD);
1581
1582 /*
1583 * type can be one of four:
1584 * NORMAL - an interrupt already moved it for us
1585 * HEAD - we are the first to get here.
1586 * UPDATE - we are the interrupt interrupting
1587 * a current move.
1588 * MOVED - a reader on another CPU moved the next
1589 * pointer to its reader page. Give up
1590 * and try again.
1591 */
1592
1593 switch (type) {
1594 case RB_PAGE_HEAD:
1595 /*
1596 * We changed the head to UPDATE, thus
1597 * it is our responsibility to update
1598 * the counters.
1599 */
1600 local_add(entries, &cpu_buffer->overrun);
1601
1602 /*
1603 * The entries will be zeroed out when we move the
1604 * tail page.
1605 */
1606
1607 /* still more to do */
1608 break;
1609
1610 case RB_PAGE_UPDATE:
1611 /*
1612 * This is an interrupt that interrupt the
1613 * previous update. Still more to do.
1614 */
1615 break;
1616 case RB_PAGE_NORMAL:
1617 /*
1618 * An interrupt came in before the update
1619 * and processed this for us.
1620 * Nothing left to do.
1621 */
1622 return 1;
1623 case RB_PAGE_MOVED:
1624 /*
1625 * The reader is on another CPU and just did
1626 * a swap with our next_page.
1627 * Try again.
1628 */
1629 return 1;
1630 default:
1631 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1632 return -1;
1633 }
1634
1635 /*
1636 * Now that we are here, the old head pointer is
1637 * set to UPDATE. This will keep the reader from
1638 * swapping the head page with the reader page.
1639 * The reader (on another CPU) will spin till
1640 * we are finished.
1641 *
1642 * We just need to protect against interrupts
1643 * doing the job. We will set the next pointer
1644 * to HEAD. After that, we set the old pointer
1645 * to NORMAL, but only if it was HEAD before.
1646 * otherwise we are an interrupt, and only
1647 * want the outer most commit to reset it.
1648 */
1649 new_head = next_page;
1650 rb_inc_page(cpu_buffer, &new_head);
1651
1652 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
1653 RB_PAGE_NORMAL);
1654
1655 /*
1656 * Valid returns are:
1657 * HEAD - an interrupt came in and already set it.
1658 * NORMAL - One of two things:
1659 * 1) We really set it.
1660 * 2) A bunch of interrupts came in and moved
1661 * the page forward again.
1662 */
1663 switch (ret) {
1664 case RB_PAGE_HEAD:
1665 case RB_PAGE_NORMAL:
1666 /* OK */
1667 break;
1668 default:
1669 RB_WARN_ON(cpu_buffer, 1);
1670 return -1;
1671 }
1672
1673 /*
1674 * It is possible that an interrupt came in,
1675 * set the head up, then more interrupts came in
1676 * and moved it again. When we get back here,
1677 * the page would have been set to NORMAL but we
1678 * just set it back to HEAD.
1679 *
1680 * How do you detect this? Well, if that happened
1681 * the tail page would have moved.
1682 */
1683 if (ret == RB_PAGE_NORMAL) {
1684 /*
1685 * If the tail had moved passed next, then we need
1686 * to reset the pointer.
1687 */
1688 if (cpu_buffer->tail_page != tail_page &&
1689 cpu_buffer->tail_page != next_page)
1690 rb_head_page_set_normal(cpu_buffer, new_head,
1691 next_page,
1692 RB_PAGE_HEAD);
1693 }
1694
1695 /*
1696 * If this was the outer most commit (the one that
1697 * changed the original pointer from HEAD to UPDATE),
1698 * then it is up to us to reset it to NORMAL.
1699 */
1700 if (type == RB_PAGE_HEAD) {
1701 ret = rb_head_page_set_normal(cpu_buffer, next_page,
1702 tail_page,
1703 RB_PAGE_UPDATE);
1704 if (RB_WARN_ON(cpu_buffer,
1705 ret != RB_PAGE_UPDATE))
1706 return -1;
1707 }
1708
1709 return 0;
1710}
1711
34a148bf 1712static unsigned rb_calculate_event_length(unsigned length)
7a8e76a3
SR
1713{
1714 struct ring_buffer_event event; /* Used only for sizeof array */
1715
1716 /* zero length can cause confusions */
1717 if (!length)
1718 length = 1;
1719
1720 if (length > RB_MAX_SMALL_DATA)
1721 length += sizeof(event.array[0]);
1722
1723 length += RB_EVNT_HDR_SIZE;
1724 length = ALIGN(length, RB_ALIGNMENT);
1725
1726 return length;
1727}
1728
c7b09308
SR
1729static inline void
1730rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
1731 struct buffer_page *tail_page,
1732 unsigned long tail, unsigned long length)
1733{
1734 struct ring_buffer_event *event;
1735
1736 /*
1737 * Only the event that crossed the page boundary
1738 * must fill the old tail_page with padding.
1739 */
1740 if (tail >= BUF_PAGE_SIZE) {
1741 local_sub(length, &tail_page->write);
1742 return;
1743 }
1744
1745 event = __rb_page_index(tail_page, tail);
b0b7065b 1746 kmemcheck_annotate_bitfield(event, bitfield);
c7b09308
SR
1747
1748 /*
1749 * If this event is bigger than the minimum size, then
1750 * we need to be careful that we don't subtract the
1751 * write counter enough to allow another writer to slip
1752 * in on this page.
1753 * We put in a discarded commit instead, to make sure
1754 * that this space is not used again.
1755 *
1756 * If we are less than the minimum size, we don't need to
1757 * worry about it.
1758 */
1759 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
1760 /* No room for any events */
1761
1762 /* Mark the rest of the page with padding */
1763 rb_event_set_padding(event);
1764
1765 /* Set the write back to the previous setting */
1766 local_sub(length, &tail_page->write);
1767 return;
1768 }
1769
1770 /* Put in a discarded event */
1771 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
1772 event->type_len = RINGBUF_TYPE_PADDING;
1773 /* time delta must be non zero */
1774 event->time_delta = 1;
1775 /* Account for this as an entry */
1776 local_inc(&tail_page->entries);
1777 local_inc(&cpu_buffer->entries);
1778
1779 /* Set write to end of buffer */
1780 length = (tail + length) - BUF_PAGE_SIZE;
1781 local_sub(length, &tail_page->write);
1782}
6634ff26 1783
7a8e76a3 1784static struct ring_buffer_event *
6634ff26
SR
1785rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1786 unsigned long length, unsigned long tail,
1787 struct buffer_page *commit_page,
1788 struct buffer_page *tail_page, u64 *ts)
7a8e76a3 1789{
7a8e76a3 1790 struct ring_buffer *buffer = cpu_buffer->buffer;
77ae365e
SR
1791 struct buffer_page *next_page;
1792 int ret;
aa20ae84
SR
1793
1794 next_page = tail_page;
1795
aa20ae84
SR
1796 rb_inc_page(cpu_buffer, &next_page);
1797
aa20ae84
SR
1798 /*
1799 * If for some reason, we had an interrupt storm that made
1800 * it all the way around the buffer, bail, and warn
1801 * about it.
1802 */
1803 if (unlikely(next_page == commit_page)) {
77ae365e 1804 local_inc(&cpu_buffer->commit_overrun);
aa20ae84
SR
1805 goto out_reset;
1806 }
1807
77ae365e
SR
1808 /*
1809 * This is where the fun begins!
1810 *
1811 * We are fighting against races between a reader that
1812 * could be on another CPU trying to swap its reader
1813 * page with the buffer head.
1814 *
1815 * We are also fighting against interrupts coming in and
1816 * moving the head or tail on us as well.
1817 *
1818 * If the next page is the head page then we have filled
1819 * the buffer, unless the commit page is still on the
1820 * reader page.
1821 */
1822 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
aa20ae84 1823
77ae365e
SR
1824 /*
1825 * If the commit is not on the reader page, then
1826 * move the header page.
1827 */
1828 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
1829 /*
1830 * If we are not in overwrite mode,
1831 * this is easy, just stop here.
1832 */
1833 if (!(buffer->flags & RB_FL_OVERWRITE))
1834 goto out_reset;
1835
1836 ret = rb_handle_head_page(cpu_buffer,
1837 tail_page,
1838 next_page);
1839 if (ret < 0)
1840 goto out_reset;
1841 if (ret)
1842 goto out_again;
1843 } else {
1844 /*
1845 * We need to be careful here too. The
1846 * commit page could still be on the reader
1847 * page. We could have a small buffer, and
1848 * have filled up the buffer with events
1849 * from interrupts and such, and wrapped.
1850 *
1851 * Note, if the tail page is also the on the
1852 * reader_page, we let it move out.
1853 */
1854 if (unlikely((cpu_buffer->commit_page !=
1855 cpu_buffer->tail_page) &&
1856 (cpu_buffer->commit_page ==
1857 cpu_buffer->reader_page))) {
1858 local_inc(&cpu_buffer->commit_overrun);
1859 goto out_reset;
1860 }
aa20ae84
SR
1861 }
1862 }
1863
77ae365e
SR
1864 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
1865 if (ret) {
1866 /*
1867 * Nested commits always have zero deltas, so
1868 * just reread the time stamp
1869 */
88eb0125 1870 *ts = rb_time_stamp(buffer, cpu_buffer->cpu);
77ae365e 1871 next_page->page->time_stamp = *ts;
aa20ae84
SR
1872 }
1873
77ae365e 1874 out_again:
aa20ae84 1875
77ae365e 1876 rb_reset_tail(cpu_buffer, tail_page, tail, length);
aa20ae84
SR
1877
1878 /* fail and let the caller try again */
1879 return ERR_PTR(-EAGAIN);
1880
45141d46 1881 out_reset:
6f3b3440 1882 /* reset write */
c7b09308 1883 rb_reset_tail(cpu_buffer, tail_page, tail, length);
6f3b3440 1884
bf41a158 1885 return NULL;
7a8e76a3
SR
1886}
1887
6634ff26
SR
1888static struct ring_buffer_event *
1889__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1890 unsigned type, unsigned long length, u64 *ts)
1891{
1892 struct buffer_page *tail_page, *commit_page;
1893 struct ring_buffer_event *event;
1894 unsigned long tail, write;
1895
1896 commit_page = cpu_buffer->commit_page;
1897 /* we just need to protect against interrupts */
1898 barrier();
1899 tail_page = cpu_buffer->tail_page;
1900 write = local_add_return(length, &tail_page->write);
77ae365e
SR
1901
1902 /* set write to only the index of the write */
1903 write &= RB_WRITE_MASK;
6634ff26
SR
1904 tail = write - length;
1905
1906 /* See if we shot pass the end of this buffer page */
1907 if (write > BUF_PAGE_SIZE)
1908 return rb_move_tail(cpu_buffer, length, tail,
1909 commit_page, tail_page, ts);
1910
1911 /* We reserved something on the buffer */
1912
6634ff26 1913 event = __rb_page_index(tail_page, tail);
1744a21d 1914 kmemcheck_annotate_bitfield(event, bitfield);
6634ff26
SR
1915 rb_update_event(event, type, length);
1916
1917 /* The passed in type is zero for DATA */
1918 if (likely(!type))
1919 local_inc(&tail_page->entries);
1920
1921 /*
fa743953
SR
1922 * If this is the first commit on the page, then update
1923 * its timestamp.
6634ff26 1924 */
fa743953
SR
1925 if (!tail)
1926 tail_page->page->time_stamp = *ts;
6634ff26
SR
1927
1928 return event;
1929}
1930
edd813bf
SR
1931static inline int
1932rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
1933 struct ring_buffer_event *event)
1934{
1935 unsigned long new_index, old_index;
1936 struct buffer_page *bpage;
1937 unsigned long index;
1938 unsigned long addr;
1939
1940 new_index = rb_event_index(event);
1941 old_index = new_index + rb_event_length(event);
1942 addr = (unsigned long)event;
1943 addr &= PAGE_MASK;
1944
1945 bpage = cpu_buffer->tail_page;
1946
1947 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
77ae365e
SR
1948 unsigned long write_mask =
1949 local_read(&bpage->write) & ~RB_WRITE_MASK;
edd813bf
SR
1950 /*
1951 * This is on the tail page. It is possible that
1952 * a write could come in and move the tail page
1953 * and write to the next page. That is fine
1954 * because we just shorten what is on this page.
1955 */
77ae365e
SR
1956 old_index += write_mask;
1957 new_index += write_mask;
edd813bf
SR
1958 index = local_cmpxchg(&bpage->write, old_index, new_index);
1959 if (index == old_index)
1960 return 1;
1961 }
1962
1963 /* could not discard */
1964 return 0;
1965}
1966
7a8e76a3
SR
1967static int
1968rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1969 u64 *ts, u64 *delta)
1970{
1971 struct ring_buffer_event *event;
1972 static int once;
bf41a158 1973 int ret;
7a8e76a3
SR
1974
1975 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1976 printk(KERN_WARNING "Delta way too big! %llu"
1977 " ts=%llu write stamp = %llu\n",
e2862c94
SR
1978 (unsigned long long)*delta,
1979 (unsigned long long)*ts,
1980 (unsigned long long)cpu_buffer->write_stamp);
7a8e76a3
SR
1981 WARN_ON(1);
1982 }
1983
1984 /*
1985 * The delta is too big, we to add a
1986 * new timestamp.
1987 */
1988 event = __rb_reserve_next(cpu_buffer,
1989 RINGBUF_TYPE_TIME_EXTEND,
1990 RB_LEN_TIME_EXTEND,
1991 ts);
1992 if (!event)
bf41a158 1993 return -EBUSY;
7a8e76a3 1994
bf41a158
SR
1995 if (PTR_ERR(event) == -EAGAIN)
1996 return -EAGAIN;
1997
1998 /* Only a commited time event can update the write stamp */
fa743953 1999 if (rb_event_is_commit(cpu_buffer, event)) {
bf41a158 2000 /*
fa743953
SR
2001 * If this is the first on the page, then it was
2002 * updated with the page itself. Try to discard it
2003 * and if we can't just make it zero.
bf41a158
SR
2004 */
2005 if (rb_event_index(event)) {
2006 event->time_delta = *delta & TS_MASK;
2007 event->array[0] = *delta >> TS_SHIFT;
2008 } else {
ea05b57c
SR
2009 /* try to discard, since we do not need this */
2010 if (!rb_try_to_discard(cpu_buffer, event)) {
2011 /* nope, just zero it */
2012 event->time_delta = 0;
2013 event->array[0] = 0;
2014 }
bf41a158 2015 }
7a8e76a3 2016 cpu_buffer->write_stamp = *ts;
bf41a158
SR
2017 /* let the caller know this was the commit */
2018 ret = 1;
2019 } else {
edd813bf
SR
2020 /* Try to discard the event */
2021 if (!rb_try_to_discard(cpu_buffer, event)) {
2022 /* Darn, this is just wasted space */
2023 event->time_delta = 0;
2024 event->array[0] = 0;
edd813bf 2025 }
f57a8a19 2026 ret = 0;
7a8e76a3
SR
2027 }
2028
bf41a158
SR
2029 *delta = 0;
2030
2031 return ret;
7a8e76a3
SR
2032}
2033
fa743953
SR
2034static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2035{
2036 local_inc(&cpu_buffer->committing);
2037 local_inc(&cpu_buffer->commits);
2038}
2039
2040static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
2041{
2042 unsigned long commits;
2043
2044 if (RB_WARN_ON(cpu_buffer,
2045 !local_read(&cpu_buffer->committing)))
2046 return;
2047
2048 again:
2049 commits = local_read(&cpu_buffer->commits);
2050 /* synchronize with interrupts */
2051 barrier();
2052 if (local_read(&cpu_buffer->committing) == 1)
2053 rb_set_commit_to_write(cpu_buffer);
2054
2055 local_dec(&cpu_buffer->committing);
2056
2057 /* synchronize with interrupts */
2058 barrier();
2059
2060 /*
2061 * Need to account for interrupts coming in between the
2062 * updating of the commit page and the clearing of the
2063 * committing counter.
2064 */
2065 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2066 !local_read(&cpu_buffer->committing)) {
2067 local_inc(&cpu_buffer->committing);
2068 goto again;
2069 }
2070}
2071
7a8e76a3
SR
2072static struct ring_buffer_event *
2073rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1cd8d735 2074 unsigned long length)
7a8e76a3
SR
2075{
2076 struct ring_buffer_event *event;
168b6b1d 2077 u64 ts, delta = 0;
bf41a158 2078 int commit = 0;
818e3dd3 2079 int nr_loops = 0;
7a8e76a3 2080
fa743953
SR
2081 rb_start_commit(cpu_buffer);
2082
be957c44 2083 length = rb_calculate_event_length(length);
bf41a158 2084 again:
818e3dd3
SR
2085 /*
2086 * We allow for interrupts to reenter here and do a trace.
2087 * If one does, it will cause this original code to loop
2088 * back here. Even with heavy interrupts happening, this
2089 * should only happen a few times in a row. If this happens
2090 * 1000 times in a row, there must be either an interrupt
2091 * storm or we have something buggy.
2092 * Bail!
2093 */
3e89c7bb 2094 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
fa743953 2095 goto out_fail;
818e3dd3 2096
88eb0125 2097 ts = rb_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
7a8e76a3 2098
bf41a158
SR
2099 /*
2100 * Only the first commit can update the timestamp.
2101 * Yes there is a race here. If an interrupt comes in
2102 * just after the conditional and it traces too, then it
2103 * will also check the deltas. More than one timestamp may
2104 * also be made. But only the entry that did the actual
2105 * commit will be something other than zero.
2106 */
0f0c85fc
SR
2107 if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
2108 rb_page_write(cpu_buffer->tail_page) ==
2109 rb_commit_index(cpu_buffer))) {
168b6b1d 2110 u64 diff;
bf41a158 2111
168b6b1d 2112 diff = ts - cpu_buffer->write_stamp;
7a8e76a3 2113
168b6b1d 2114 /* make sure this diff is calculated here */
bf41a158
SR
2115 barrier();
2116
2117 /* Did the write stamp get updated already? */
2118 if (unlikely(ts < cpu_buffer->write_stamp))
168b6b1d 2119 goto get_event;
bf41a158 2120
168b6b1d
SR
2121 delta = diff;
2122 if (unlikely(test_time_stamp(delta))) {
7a8e76a3 2123
bf41a158 2124 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
bf41a158 2125 if (commit == -EBUSY)
fa743953 2126 goto out_fail;
bf41a158
SR
2127
2128 if (commit == -EAGAIN)
2129 goto again;
2130
2131 RB_WARN_ON(cpu_buffer, commit < 0);
7a8e76a3 2132 }
168b6b1d 2133 }
7a8e76a3 2134
168b6b1d 2135 get_event:
1cd8d735 2136 event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
168b6b1d 2137 if (unlikely(PTR_ERR(event) == -EAGAIN))
bf41a158
SR
2138 goto again;
2139
fa743953
SR
2140 if (!event)
2141 goto out_fail;
7a8e76a3 2142
fa743953 2143 if (!rb_event_is_commit(cpu_buffer, event))
7a8e76a3
SR
2144 delta = 0;
2145
2146 event->time_delta = delta;
2147
2148 return event;
fa743953
SR
2149
2150 out_fail:
2151 rb_end_commit(cpu_buffer);
2152 return NULL;
7a8e76a3
SR
2153}
2154
1155de47
PM
2155#ifdef CONFIG_TRACING
2156
aa18efb2 2157#define TRACE_RECURSIVE_DEPTH 16
261842b7
SR
2158
2159static int trace_recursive_lock(void)
2160{
aa18efb2 2161 current->trace_recursion++;
261842b7 2162
aa18efb2
SR
2163 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
2164 return 0;
e057a5e5 2165
aa18efb2
SR
2166 /* Disable all tracing before we do anything else */
2167 tracing_off_permanent();
261842b7 2168
7d7d2b80 2169 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
aa18efb2
SR
2170 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
2171 current->trace_recursion,
2172 hardirq_count() >> HARDIRQ_SHIFT,
2173 softirq_count() >> SOFTIRQ_SHIFT,
2174 in_nmi());
261842b7 2175
aa18efb2
SR
2176 WARN_ON_ONCE(1);
2177 return -1;
261842b7
SR
2178}
2179
2180static void trace_recursive_unlock(void)
2181{
aa18efb2 2182 WARN_ON_ONCE(!current->trace_recursion);
261842b7 2183
aa18efb2 2184 current->trace_recursion--;
261842b7
SR
2185}
2186
1155de47
PM
2187#else
2188
2189#define trace_recursive_lock() (0)
2190#define trace_recursive_unlock() do { } while (0)
2191
2192#endif
2193
bf41a158
SR
2194static DEFINE_PER_CPU(int, rb_need_resched);
2195
7a8e76a3
SR
2196/**
2197 * ring_buffer_lock_reserve - reserve a part of the buffer
2198 * @buffer: the ring buffer to reserve from
2199 * @length: the length of the data to reserve (excluding event header)
7a8e76a3
SR
2200 *
2201 * Returns a reseverd event on the ring buffer to copy directly to.
2202 * The user of this interface will need to get the body to write into
2203 * and can use the ring_buffer_event_data() interface.
2204 *
2205 * The length is the length of the data needed, not the event length
2206 * which also includes the event header.
2207 *
2208 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2209 * If NULL is returned, then nothing has been allocated or locked.
2210 */
2211struct ring_buffer_event *
0a987751 2212ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
7a8e76a3
SR
2213{
2214 struct ring_buffer_per_cpu *cpu_buffer;
2215 struct ring_buffer_event *event;
bf41a158 2216 int cpu, resched;
7a8e76a3 2217
033601a3 2218 if (ring_buffer_flags != RB_BUFFERS_ON)
a3583244
SR
2219 return NULL;
2220
7a8e76a3
SR
2221 if (atomic_read(&buffer->record_disabled))
2222 return NULL;
2223
bf41a158 2224 /* If we are tracing schedule, we don't want to recurse */
182e9f5f 2225 resched = ftrace_preempt_disable();
bf41a158 2226
261842b7
SR
2227 if (trace_recursive_lock())
2228 goto out_nocheck;
2229
7a8e76a3
SR
2230 cpu = raw_smp_processor_id();
2231
9e01c1b7 2232 if (!cpumask_test_cpu(cpu, buffer->cpumask))
d769041f 2233 goto out;
7a8e76a3
SR
2234
2235 cpu_buffer = buffer->buffers[cpu];
7a8e76a3
SR
2236
2237 if (atomic_read(&cpu_buffer->record_disabled))
d769041f 2238 goto out;
7a8e76a3 2239
be957c44 2240 if (length > BUF_MAX_DATA_SIZE)
bf41a158 2241 goto out;
7a8e76a3 2242
1cd8d735 2243 event = rb_reserve_next_event(cpu_buffer, length);
7a8e76a3 2244 if (!event)
d769041f 2245 goto out;
7a8e76a3 2246
bf41a158
SR
2247 /*
2248 * Need to store resched state on this cpu.
2249 * Only the first needs to.
2250 */
2251
2252 if (preempt_count() == 1)
2253 per_cpu(rb_need_resched, cpu) = resched;
2254
7a8e76a3
SR
2255 return event;
2256
d769041f 2257 out:
261842b7
SR
2258 trace_recursive_unlock();
2259
2260 out_nocheck:
182e9f5f 2261 ftrace_preempt_enable(resched);
7a8e76a3
SR
2262 return NULL;
2263}
c4f50183 2264EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
7a8e76a3
SR
2265
2266static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2267 struct ring_buffer_event *event)
2268{
e4906eff 2269 local_inc(&cpu_buffer->entries);
bf41a158 2270
fa743953
SR
2271 /*
2272 * The event first in the commit queue updates the
2273 * time stamp.
2274 */
2275 if (rb_event_is_commit(cpu_buffer, event))
2276 cpu_buffer->write_stamp += event->time_delta;
bf41a158 2277
fa743953 2278 rb_end_commit(cpu_buffer);
7a8e76a3
SR
2279}
2280
2281/**
2282 * ring_buffer_unlock_commit - commit a reserved
2283 * @buffer: The buffer to commit to
2284 * @event: The event pointer to commit.
7a8e76a3
SR
2285 *
2286 * This commits the data to the ring buffer, and releases any locks held.
2287 *
2288 * Must be paired with ring_buffer_lock_reserve.
2289 */
2290int ring_buffer_unlock_commit(struct ring_buffer *buffer,
0a987751 2291 struct ring_buffer_event *event)
7a8e76a3
SR
2292{
2293 struct ring_buffer_per_cpu *cpu_buffer;
2294 int cpu = raw_smp_processor_id();
2295
2296 cpu_buffer = buffer->buffers[cpu];
2297
7a8e76a3
SR
2298 rb_commit(cpu_buffer, event);
2299
261842b7
SR
2300 trace_recursive_unlock();
2301
bf41a158
SR
2302 /*
2303 * Only the last preempt count needs to restore preemption.
2304 */
182e9f5f
SR
2305 if (preempt_count() == 1)
2306 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2307 else
bf41a158 2308 preempt_enable_no_resched_notrace();
7a8e76a3
SR
2309
2310 return 0;
2311}
c4f50183 2312EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
7a8e76a3 2313
f3b9aae1
FW
2314static inline void rb_event_discard(struct ring_buffer_event *event)
2315{
334d4169
LJ
2316 /* array[0] holds the actual length for the discarded event */
2317 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2318 event->type_len = RINGBUF_TYPE_PADDING;
f3b9aae1
FW
2319 /* time delta must be non zero */
2320 if (!event->time_delta)
2321 event->time_delta = 1;
2322}
2323
fa1b47dd
SR
2324/**
2325 * ring_buffer_event_discard - discard any event in the ring buffer
2326 * @event: the event to discard
2327 *
2328 * Sometimes a event that is in the ring buffer needs to be ignored.
2329 * This function lets the user discard an event in the ring buffer
2330 * and then that event will not be read later.
2331 *
2332 * Note, it is up to the user to be careful with this, and protect
2333 * against races. If the user discards an event that has been consumed
2334 * it is possible that it could corrupt the ring buffer.
2335 */
2336void ring_buffer_event_discard(struct ring_buffer_event *event)
2337{
f3b9aae1 2338 rb_event_discard(event);
fa1b47dd
SR
2339}
2340EXPORT_SYMBOL_GPL(ring_buffer_event_discard);
2341
2342/**
2343 * ring_buffer_commit_discard - discard an event that has not been committed
2344 * @buffer: the ring buffer
2345 * @event: non committed event to discard
2346 *
2347 * This is similar to ring_buffer_event_discard but must only be
2348 * performed on an event that has not been committed yet. The difference
2349 * is that this will also try to free the event from the ring buffer
2350 * if another event has not been added behind it.
2351 *
2352 * If another event has been added behind it, it will set the event
2353 * up as discarded, and perform the commit.
2354 *
2355 * If this function is called, do not call ring_buffer_unlock_commit on
2356 * the event.
2357 */
2358void ring_buffer_discard_commit(struct ring_buffer *buffer,
2359 struct ring_buffer_event *event)
2360{
2361 struct ring_buffer_per_cpu *cpu_buffer;
fa1b47dd
SR
2362 int cpu;
2363
2364 /* The event is discarded regardless */
f3b9aae1 2365 rb_event_discard(event);
fa1b47dd 2366
fa743953
SR
2367 cpu = smp_processor_id();
2368 cpu_buffer = buffer->buffers[cpu];
2369
fa1b47dd
SR
2370 /*
2371 * This must only be called if the event has not been
2372 * committed yet. Thus we can assume that preemption
2373 * is still disabled.
2374 */
fa743953 2375 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
fa1b47dd 2376
edd813bf
SR
2377 if (!rb_try_to_discard(cpu_buffer, event))
2378 goto out;
fa1b47dd
SR
2379
2380 /*
2381 * The commit is still visible by the reader, so we
2382 * must increment entries.
2383 */
e4906eff 2384 local_inc(&cpu_buffer->entries);
fa1b47dd 2385 out:
fa743953 2386 rb_end_commit(cpu_buffer);
fa1b47dd 2387
f3b9aae1
FW
2388 trace_recursive_unlock();
2389
fa1b47dd
SR
2390 /*
2391 * Only the last preempt count needs to restore preemption.
2392 */
2393 if (preempt_count() == 1)
2394 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2395 else
2396 preempt_enable_no_resched_notrace();
2397
2398}
2399EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2400
7a8e76a3
SR
2401/**
2402 * ring_buffer_write - write data to the buffer without reserving
2403 * @buffer: The ring buffer to write to.
2404 * @length: The length of the data being written (excluding the event header)
2405 * @data: The data to write to the buffer.
2406 *
2407 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2408 * one function. If you already have the data to write to the buffer, it
2409 * may be easier to simply call this function.
2410 *
2411 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2412 * and not the length of the event which would hold the header.
2413 */
2414int ring_buffer_write(struct ring_buffer *buffer,
2415 unsigned long length,
2416 void *data)
2417{
2418 struct ring_buffer_per_cpu *cpu_buffer;
2419 struct ring_buffer_event *event;
7a8e76a3
SR
2420 void *body;
2421 int ret = -EBUSY;
bf41a158 2422 int cpu, resched;
7a8e76a3 2423
033601a3 2424 if (ring_buffer_flags != RB_BUFFERS_ON)
a3583244
SR
2425 return -EBUSY;
2426
7a8e76a3
SR
2427 if (atomic_read(&buffer->record_disabled))
2428 return -EBUSY;
2429
182e9f5f 2430 resched = ftrace_preempt_disable();
bf41a158 2431
7a8e76a3
SR
2432 cpu = raw_smp_processor_id();
2433
9e01c1b7 2434 if (!cpumask_test_cpu(cpu, buffer->cpumask))
d769041f 2435 goto out;
7a8e76a3
SR
2436
2437 cpu_buffer = buffer->buffers[cpu];
7a8e76a3
SR
2438
2439 if (atomic_read(&cpu_buffer->record_disabled))
2440 goto out;
2441
be957c44
SR
2442 if (length > BUF_MAX_DATA_SIZE)
2443 goto out;
2444
2445 event = rb_reserve_next_event(cpu_buffer, length);
7a8e76a3
SR
2446 if (!event)
2447 goto out;
2448
2449 body = rb_event_data(event);
2450
2451 memcpy(body, data, length);
2452
2453 rb_commit(cpu_buffer, event);
2454
2455 ret = 0;
2456 out:
182e9f5f 2457 ftrace_preempt_enable(resched);
7a8e76a3
SR
2458
2459 return ret;
2460}
c4f50183 2461EXPORT_SYMBOL_GPL(ring_buffer_write);
7a8e76a3 2462
34a148bf 2463static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
bf41a158
SR
2464{
2465 struct buffer_page *reader = cpu_buffer->reader_page;
77ae365e 2466 struct buffer_page *head = rb_set_head_page(cpu_buffer);
bf41a158
SR
2467 struct buffer_page *commit = cpu_buffer->commit_page;
2468
77ae365e
SR
2469 /* In case of error, head will be NULL */
2470 if (unlikely(!head))
2471 return 1;
2472
bf41a158
SR
2473 return reader->read == rb_page_commit(reader) &&
2474 (commit == reader ||
2475 (commit == head &&
2476 head->read == rb_page_commit(commit)));
2477}
2478
7a8e76a3
SR
2479/**
2480 * ring_buffer_record_disable - stop all writes into the buffer
2481 * @buffer: The ring buffer to stop writes to.
2482 *
2483 * This prevents all writes to the buffer. Any attempt to write
2484 * to the buffer after this will fail and return NULL.
2485 *
2486 * The caller should call synchronize_sched() after this.
2487 */
2488void ring_buffer_record_disable(struct ring_buffer *buffer)
2489{
2490 atomic_inc(&buffer->record_disabled);
2491}
c4f50183 2492EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
7a8e76a3
SR
2493
2494/**
2495 * ring_buffer_record_enable - enable writes to the buffer
2496 * @buffer: The ring buffer to enable writes
2497 *
2498 * Note, multiple disables will need the same number of enables
2499 * to truely enable the writing (much like preempt_disable).
2500 */
2501void ring_buffer_record_enable(struct ring_buffer *buffer)
2502{
2503 atomic_dec(&buffer->record_disabled);
2504}
c4f50183 2505EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
7a8e76a3
SR
2506
2507/**
2508 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
2509 * @buffer: The ring buffer to stop writes to.
2510 * @cpu: The CPU buffer to stop
2511 *
2512 * This prevents all writes to the buffer. Any attempt to write
2513 * to the buffer after this will fail and return NULL.
2514 *
2515 * The caller should call synchronize_sched() after this.
2516 */
2517void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
2518{
2519 struct ring_buffer_per_cpu *cpu_buffer;
2520
9e01c1b7 2521 if (!cpumask_test_cpu(cpu, buffer->cpumask))
8aabee57 2522 return;
7a8e76a3
SR
2523
2524 cpu_buffer = buffer->buffers[cpu];
2525 atomic_inc(&cpu_buffer->record_disabled);
2526}
c4f50183 2527EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
7a8e76a3
SR
2528
2529/**
2530 * ring_buffer_record_enable_cpu - enable writes to the buffer
2531 * @buffer: The ring buffer to enable writes
2532 * @cpu: The CPU to enable.
2533 *
2534 * Note, multiple disables will need the same number of enables
2535 * to truely enable the writing (much like preempt_disable).
2536 */
2537void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
2538{
2539 struct ring_buffer_per_cpu *cpu_buffer;
2540
9e01c1b7 2541 if (!cpumask_test_cpu(cpu, buffer->cpumask))
8aabee57 2542 return;
7a8e76a3
SR
2543
2544 cpu_buffer = buffer->buffers[cpu];
2545 atomic_dec(&cpu_buffer->record_disabled);
2546}
c4f50183 2547EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
7a8e76a3
SR
2548
2549/**
2550 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
2551 * @buffer: The ring buffer
2552 * @cpu: The per CPU buffer to get the entries from.
2553 */
2554unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
2555{
2556 struct ring_buffer_per_cpu *cpu_buffer;
8aabee57 2557 unsigned long ret;
7a8e76a3 2558
9e01c1b7 2559 if (!cpumask_test_cpu(cpu, buffer->cpumask))
8aabee57 2560 return 0;
7a8e76a3
SR
2561
2562 cpu_buffer = buffer->buffers[cpu];
77ae365e 2563 ret = (local_read(&cpu_buffer->entries) - local_read(&cpu_buffer->overrun))
e4906eff 2564 - cpu_buffer->read;
554f786e
SR
2565
2566 return ret;
7a8e76a3 2567}
c4f50183 2568EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
7a8e76a3
SR
2569
2570/**
2571 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
2572 * @buffer: The ring buffer
2573 * @cpu: The per CPU buffer to get the number of overruns from
2574 */
2575unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
2576{
2577 struct ring_buffer_per_cpu *cpu_buffer;
8aabee57 2578 unsigned long ret;
7a8e76a3 2579
9e01c1b7 2580 if (!cpumask_test_cpu(cpu, buffer->cpumask))
8aabee57 2581 return 0;
7a8e76a3
SR
2582
2583 cpu_buffer = buffer->buffers[cpu];
77ae365e 2584 ret = local_read(&cpu_buffer->overrun);
554f786e
SR
2585
2586 return ret;
7a8e76a3 2587}
c4f50183 2588EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
7a8e76a3 2589
f0d2c681
SR
2590/**
2591 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
2592 * @buffer: The ring buffer
2593 * @cpu: The per CPU buffer to get the number of overruns from
2594 */
2595unsigned long
2596ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
2597{
2598 struct ring_buffer_per_cpu *cpu_buffer;
2599 unsigned long ret;
2600
2601 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2602 return 0;
2603
2604 cpu_buffer = buffer->buffers[cpu];
77ae365e 2605 ret = local_read(&cpu_buffer->commit_overrun);
f0d2c681
SR
2606
2607 return ret;
2608}
2609EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
2610
7a8e76a3
SR
2611/**
2612 * ring_buffer_entries - get the number of entries in a buffer
2613 * @buffer: The ring buffer
2614 *
2615 * Returns the total number of entries in the ring buffer
2616 * (all CPU entries)
2617 */
2618unsigned long ring_buffer_entries(struct ring_buffer *buffer)
2619{
2620 struct ring_buffer_per_cpu *cpu_buffer;
2621 unsigned long entries = 0;
2622 int cpu;
2623
2624 /* if you care about this being correct, lock the buffer */
2625 for_each_buffer_cpu(buffer, cpu) {
2626 cpu_buffer = buffer->buffers[cpu];
e4906eff 2627 entries += (local_read(&cpu_buffer->entries) -
77ae365e 2628 local_read(&cpu_buffer->overrun)) - cpu_buffer->read;
7a8e76a3
SR
2629 }
2630
2631 return entries;
2632}
c4f50183 2633EXPORT_SYMBOL_GPL(ring_buffer_entries);
7a8e76a3
SR
2634
2635/**
2636 * ring_buffer_overrun_cpu - get the number of overruns in buffer
2637 * @buffer: The ring buffer
2638 *
2639 * Returns the total number of overruns in the ring buffer
2640 * (all CPU entries)
2641 */
2642unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2643{
2644 struct ring_buffer_per_cpu *cpu_buffer;
2645 unsigned long overruns = 0;
2646 int cpu;
2647
2648 /* if you care about this being correct, lock the buffer */
2649 for_each_buffer_cpu(buffer, cpu) {
2650 cpu_buffer = buffer->buffers[cpu];
77ae365e 2651 overruns += local_read(&cpu_buffer->overrun);
7a8e76a3
SR
2652 }
2653
2654 return overruns;
2655}
c4f50183 2656EXPORT_SYMBOL_GPL(ring_buffer_overruns);
7a8e76a3 2657
642edba5 2658static void rb_iter_reset(struct ring_buffer_iter *iter)
7a8e76a3
SR
2659{
2660 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2661
d769041f
SR
2662 /* Iterator usage is expected to have record disabled */
2663 if (list_empty(&cpu_buffer->reader_page->list)) {
77ae365e
SR
2664 iter->head_page = rb_set_head_page(cpu_buffer);
2665 if (unlikely(!iter->head_page))
2666 return;
2667 iter->head = iter->head_page->read;
d769041f
SR
2668 } else {
2669 iter->head_page = cpu_buffer->reader_page;
6f807acd 2670 iter->head = cpu_buffer->reader_page->read;
d769041f
SR
2671 }
2672 if (iter->head)
2673 iter->read_stamp = cpu_buffer->read_stamp;
2674 else
abc9b56d 2675 iter->read_stamp = iter->head_page->page->time_stamp;
642edba5 2676}
f83c9d0f 2677
642edba5
SR
2678/**
2679 * ring_buffer_iter_reset - reset an iterator
2680 * @iter: The iterator to reset
2681 *
2682 * Resets the iterator, so that it will start from the beginning
2683 * again.
2684 */
2685void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2686{
554f786e 2687 struct ring_buffer_per_cpu *cpu_buffer;
642edba5
SR
2688 unsigned long flags;
2689
554f786e
SR
2690 if (!iter)
2691 return;
2692
2693 cpu_buffer = iter->cpu_buffer;
2694
642edba5
SR
2695 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2696 rb_iter_reset(iter);
f83c9d0f 2697 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
7a8e76a3 2698}
c4f50183 2699EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
7a8e76a3
SR
2700
2701/**
2702 * ring_buffer_iter_empty - check if an iterator has no more to read
2703 * @iter: The iterator to check
2704 */
2705int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2706{
2707 struct ring_buffer_per_cpu *cpu_buffer;
2708
2709 cpu_buffer = iter->cpu_buffer;
2710
bf41a158
SR
2711 return iter->head_page == cpu_buffer->commit_page &&
2712 iter->head == rb_commit_index(cpu_buffer);
7a8e76a3 2713}
c4f50183 2714EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
7a8e76a3
SR
2715
2716static void
2717rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2718 struct ring_buffer_event *event)
2719{
2720 u64 delta;
2721
334d4169 2722 switch (event->type_len) {
7a8e76a3
SR
2723 case RINGBUF_TYPE_PADDING:
2724 return;
2725
2726 case RINGBUF_TYPE_TIME_EXTEND:
2727 delta = event->array[0];
2728 delta <<= TS_SHIFT;
2729 delta += event->time_delta;
2730 cpu_buffer->read_stamp += delta;
2731 return;
2732
2733 case RINGBUF_TYPE_TIME_STAMP:
2734 /* FIXME: not implemented */
2735 return;
2736
2737 case RINGBUF_TYPE_DATA:
2738 cpu_buffer->read_stamp += event->time_delta;
2739 return;
2740
2741 default:
2742 BUG();
2743 }
2744 return;
2745}
2746
2747static void
2748rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2749 struct ring_buffer_event *event)
2750{
2751 u64 delta;
2752
334d4169 2753 switch (event->type_len) {
7a8e76a3
SR
2754 case RINGBUF_TYPE_PADDING:
2755 return;
2756
2757 case RINGBUF_TYPE_TIME_EXTEND:
2758 delta = event->array[0];
2759 delta <<= TS_SHIFT;
2760 delta += event->time_delta;
2761 iter->read_stamp += delta;
2762 return;
2763
2764 case RINGBUF_TYPE_TIME_STAMP:
2765 /* FIXME: not implemented */
2766 return;
2767
2768 case RINGBUF_TYPE_DATA:
2769 iter->read_stamp += event->time_delta;
2770 return;
2771
2772 default:
2773 BUG();
2774 }
2775 return;
2776}
2777
d769041f
SR
2778static struct buffer_page *
2779rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
7a8e76a3 2780{
d769041f
SR
2781 struct buffer_page *reader = NULL;
2782 unsigned long flags;
818e3dd3 2783 int nr_loops = 0;
77ae365e 2784 int ret;
d769041f 2785
3e03fb7f
SR
2786 local_irq_save(flags);
2787 __raw_spin_lock(&cpu_buffer->lock);
d769041f
SR
2788
2789 again:
818e3dd3
SR
2790 /*
2791 * This should normally only loop twice. But because the
2792 * start of the reader inserts an empty page, it causes
2793 * a case where we will loop three times. There should be no
2794 * reason to loop four times (that I know of).
2795 */
3e89c7bb 2796 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
818e3dd3
SR
2797 reader = NULL;
2798 goto out;
2799 }
2800
d769041f
SR
2801 reader = cpu_buffer->reader_page;
2802
2803 /* If there's more to read, return this page */
bf41a158 2804 if (cpu_buffer->reader_page->read < rb_page_size(reader))
d769041f
SR
2805 goto out;
2806
2807 /* Never should we have an index greater than the size */
3e89c7bb
SR
2808 if (RB_WARN_ON(cpu_buffer,
2809 cpu_buffer->reader_page->read > rb_page_size(reader)))
2810 goto out;
d769041f
SR
2811
2812 /* check if we caught up to the tail */
2813 reader = NULL;
bf41a158 2814 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
d769041f 2815 goto out;
7a8e76a3
SR
2816
2817 /*
d769041f 2818 * Reset the reader page to size zero.
7a8e76a3 2819 */
77ae365e
SR
2820 local_set(&cpu_buffer->reader_page->write, 0);
2821 local_set(&cpu_buffer->reader_page->entries, 0);
2822 local_set(&cpu_buffer->reader_page->page->commit, 0);
7a8e76a3 2823
77ae365e
SR
2824 spin:
2825 /*
2826 * Splice the empty reader page into the list around the head.
2827 */
2828 reader = rb_set_head_page(cpu_buffer);
d769041f
SR
2829 cpu_buffer->reader_page->list.next = reader->list.next;
2830 cpu_buffer->reader_page->list.prev = reader->list.prev;
bf41a158 2831
3adc54fa
SR
2832 /*
2833 * cpu_buffer->pages just needs to point to the buffer, it
2834 * has no specific buffer page to point to. Lets move it out
2835 * of our way so we don't accidently swap it.
2836 */
2837 cpu_buffer->pages = reader->list.prev;
2838
77ae365e
SR
2839 /* The reader page will be pointing to the new head */
2840 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
7a8e76a3 2841
77ae365e
SR
2842 /*
2843 * Here's the tricky part.
2844 *
2845 * We need to move the pointer past the header page.
2846 * But we can only do that if a writer is not currently
2847 * moving it. The page before the header page has the
2848 * flag bit '1' set if it is pointing to the page we want.
2849 * but if the writer is in the process of moving it
2850 * than it will be '2' or already moved '0'.
2851 */
2852
2853 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
7a8e76a3
SR
2854
2855 /*
77ae365e 2856 * If we did not convert it, then we must try again.
7a8e76a3 2857 */
77ae365e
SR
2858 if (!ret)
2859 goto spin;
7a8e76a3 2860
77ae365e
SR
2861 /*
2862 * Yeah! We succeeded in replacing the page.
2863 *
2864 * Now make the new head point back to the reader page.
2865 */
2866 reader->list.next->prev = &cpu_buffer->reader_page->list;
2867 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
d769041f
SR
2868
2869 /* Finally update the reader page to the new head */
2870 cpu_buffer->reader_page = reader;
2871 rb_reset_reader_page(cpu_buffer);
2872
2873 goto again;
2874
2875 out:
3e03fb7f
SR
2876 __raw_spin_unlock(&cpu_buffer->lock);
2877 local_irq_restore(flags);
d769041f
SR
2878
2879 return reader;
2880}
2881
2882static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2883{
2884 struct ring_buffer_event *event;
2885 struct buffer_page *reader;
2886 unsigned length;
2887
2888 reader = rb_get_reader_page(cpu_buffer);
7a8e76a3 2889
d769041f 2890 /* This function should not be called when buffer is empty */
3e89c7bb
SR
2891 if (RB_WARN_ON(cpu_buffer, !reader))
2892 return;
7a8e76a3 2893
d769041f
SR
2894 event = rb_reader_event(cpu_buffer);
2895
334d4169
LJ
2896 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX
2897 || rb_discarded_event(event))
e4906eff 2898 cpu_buffer->read++;
d769041f
SR
2899
2900 rb_update_read_stamp(cpu_buffer, event);
2901
2902 length = rb_event_length(event);
6f807acd 2903 cpu_buffer->reader_page->read += length;
7a8e76a3
SR
2904}
2905
2906static void rb_advance_iter(struct ring_buffer_iter *iter)
2907{
2908 struct ring_buffer *buffer;
2909 struct ring_buffer_per_cpu *cpu_buffer;
2910 struct ring_buffer_event *event;
2911 unsigned length;
2912
2913 cpu_buffer = iter->cpu_buffer;
2914 buffer = cpu_buffer->buffer;
2915
2916 /*
2917 * Check if we are at the end of the buffer.
2918 */
bf41a158 2919 if (iter->head >= rb_page_size(iter->head_page)) {
ea05b57c
SR
2920 /* discarded commits can make the page empty */
2921 if (iter->head_page == cpu_buffer->commit_page)
3e89c7bb 2922 return;
d769041f 2923 rb_inc_iter(iter);
7a8e76a3
SR
2924 return;
2925 }
2926
2927 event = rb_iter_head_event(iter);
2928
2929 length = rb_event_length(event);
2930
2931 /*
2932 * This should not be called to advance the header if we are
2933 * at the tail of the buffer.
2934 */
3e89c7bb 2935 if (RB_WARN_ON(cpu_buffer,
f536aafc 2936 (iter->head_page == cpu_buffer->commit_page) &&
3e89c7bb
SR
2937 (iter->head + length > rb_commit_index(cpu_buffer))))
2938 return;
7a8e76a3
SR
2939
2940 rb_update_iter_read_stamp(iter, event);
2941
2942 iter->head += length;
2943
2944 /* check for end of page padding */
bf41a158
SR
2945 if ((iter->head >= rb_page_size(iter->head_page)) &&
2946 (iter->head_page != cpu_buffer->commit_page))
7a8e76a3
SR
2947 rb_advance_iter(iter);
2948}
2949
f83c9d0f
SR
2950static struct ring_buffer_event *
2951rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
7a8e76a3
SR
2952{
2953 struct ring_buffer_per_cpu *cpu_buffer;
2954 struct ring_buffer_event *event;
d769041f 2955 struct buffer_page *reader;
818e3dd3 2956 int nr_loops = 0;
7a8e76a3 2957
7a8e76a3
SR
2958 cpu_buffer = buffer->buffers[cpu];
2959
2960 again:
818e3dd3
SR
2961 /*
2962 * We repeat when a timestamp is encountered. It is possible
2963 * to get multiple timestamps from an interrupt entering just
ea05b57c
SR
2964 * as one timestamp is about to be written, or from discarded
2965 * commits. The most that we can have is the number on a single page.
818e3dd3 2966 */
ea05b57c 2967 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
818e3dd3 2968 return NULL;
818e3dd3 2969
d769041f
SR
2970 reader = rb_get_reader_page(cpu_buffer);
2971 if (!reader)
7a8e76a3
SR
2972 return NULL;
2973
d769041f 2974 event = rb_reader_event(cpu_buffer);
7a8e76a3 2975
334d4169 2976 switch (event->type_len) {
7a8e76a3 2977 case RINGBUF_TYPE_PADDING:
2d622719
TZ
2978 if (rb_null_event(event))
2979 RB_WARN_ON(cpu_buffer, 1);
2980 /*
2981 * Because the writer could be discarding every
2982 * event it creates (which would probably be bad)
2983 * if we were to go back to "again" then we may never
2984 * catch up, and will trigger the warn on, or lock
2985 * the box. Return the padding, and we will release
2986 * the current locks, and try again.
2987 */
d769041f 2988 rb_advance_reader(cpu_buffer);
2d622719 2989 return event;
7a8e76a3
SR
2990
2991 case RINGBUF_TYPE_TIME_EXTEND:
2992 /* Internal data, OK to advance */
d769041f 2993 rb_advance_reader(cpu_buffer);
7a8e76a3
SR
2994 goto again;
2995
2996 case RINGBUF_TYPE_TIME_STAMP:
2997 /* FIXME: not implemented */
d769041f 2998 rb_advance_reader(cpu_buffer);
7a8e76a3
SR
2999 goto again;
3000
3001 case RINGBUF_TYPE_DATA:
3002 if (ts) {
3003 *ts = cpu_buffer->read_stamp + event->time_delta;
37886f6a
SR
3004 ring_buffer_normalize_time_stamp(buffer,
3005 cpu_buffer->cpu, ts);
7a8e76a3
SR
3006 }
3007 return event;
3008
3009 default:
3010 BUG();
3011 }
3012
3013 return NULL;
3014}
c4f50183 3015EXPORT_SYMBOL_GPL(ring_buffer_peek);
7a8e76a3 3016
f83c9d0f
SR
3017static struct ring_buffer_event *
3018rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
7a8e76a3
SR
3019{
3020 struct ring_buffer *buffer;
3021 struct ring_buffer_per_cpu *cpu_buffer;
3022 struct ring_buffer_event *event;
818e3dd3 3023 int nr_loops = 0;
7a8e76a3
SR
3024
3025 if (ring_buffer_iter_empty(iter))
3026 return NULL;
3027
3028 cpu_buffer = iter->cpu_buffer;
3029 buffer = cpu_buffer->buffer;
3030
3031 again:
818e3dd3 3032 /*
ea05b57c
SR
3033 * We repeat when a timestamp is encountered.
3034 * We can get multiple timestamps by nested interrupts or also
3035 * if filtering is on (discarding commits). Since discarding
3036 * commits can be frequent we can get a lot of timestamps.
3037 * But we limit them by not adding timestamps if they begin
3038 * at the start of a page.
818e3dd3 3039 */
ea05b57c 3040 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
818e3dd3 3041 return NULL;
818e3dd3 3042
7a8e76a3
SR
3043 if (rb_per_cpu_empty(cpu_buffer))
3044 return NULL;
3045
3046 event = rb_iter_head_event(iter);
3047
334d4169 3048 switch (event->type_len) {
7a8e76a3 3049 case RINGBUF_TYPE_PADDING:
2d622719
TZ
3050 if (rb_null_event(event)) {
3051 rb_inc_iter(iter);
3052 goto again;
3053 }
3054 rb_advance_iter(iter);
3055 return event;
7a8e76a3
SR
3056
3057 case RINGBUF_TYPE_TIME_EXTEND:
3058 /* Internal data, OK to advance */
3059 rb_advance_iter(iter);
3060 goto again;
3061
3062 case RINGBUF_TYPE_TIME_STAMP:
3063 /* FIXME: not implemented */
3064 rb_advance_iter(iter);
3065 goto again;
3066
3067 case RINGBUF_TYPE_DATA:
3068 if (ts) {
3069 *ts = iter->read_stamp + event->time_delta;
37886f6a
SR
3070 ring_buffer_normalize_time_stamp(buffer,
3071 cpu_buffer->cpu, ts);
7a8e76a3
SR
3072 }
3073 return event;
3074
3075 default:
3076 BUG();
3077 }
3078
3079 return NULL;
3080}
c4f50183 3081EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
7a8e76a3 3082
8d707e8e
SR
3083static inline int rb_ok_to_lock(void)
3084{
3085 /*
3086 * If an NMI die dumps out the content of the ring buffer
3087 * do not grab locks. We also permanently disable the ring
3088 * buffer too. A one time deal is all you get from reading
3089 * the ring buffer from an NMI.
3090 */
3091 if (likely(!in_nmi() && !oops_in_progress))
3092 return 1;
3093
3094 tracing_off_permanent();
3095 return 0;
3096}
3097
f83c9d0f
SR
3098/**
3099 * ring_buffer_peek - peek at the next event to be read
3100 * @buffer: The ring buffer to read
3101 * @cpu: The cpu to peak at
3102 * @ts: The timestamp counter of this event.
3103 *
3104 * This will return the event that will be read next, but does
3105 * not consume the data.
3106 */
3107struct ring_buffer_event *
3108ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
3109{
3110 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
8aabee57 3111 struct ring_buffer_event *event;
f83c9d0f 3112 unsigned long flags;
8d707e8e 3113 int dolock;
f83c9d0f 3114
554f786e 3115 if (!cpumask_test_cpu(cpu, buffer->cpumask))
8aabee57 3116 return NULL;
554f786e 3117
8d707e8e 3118 dolock = rb_ok_to_lock();
2d622719 3119 again:
8d707e8e
SR
3120 local_irq_save(flags);
3121 if (dolock)
3122 spin_lock(&cpu_buffer->reader_lock);
f83c9d0f 3123 event = rb_buffer_peek(buffer, cpu, ts);
8d707e8e
SR
3124 if (dolock)
3125 spin_unlock(&cpu_buffer->reader_lock);
3126 local_irq_restore(flags);
f83c9d0f 3127
334d4169 3128 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2d622719
TZ
3129 cpu_relax();
3130 goto again;
3131 }
3132
f83c9d0f
SR
3133 return event;
3134}
3135
3136/**
3137 * ring_buffer_iter_peek - peek at the next event to be read
3138 * @iter: The ring buffer iterator
3139 * @ts: The timestamp counter of this event.
3140 *
3141 * This will return the event that will be read next, but does
3142 * not increment the iterator.
3143 */
3144struct ring_buffer_event *
3145ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3146{
3147 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3148 struct ring_buffer_event *event;
3149 unsigned long flags;
3150
2d622719 3151 again:
f83c9d0f
SR
3152 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3153 event = rb_iter_peek(iter, ts);
3154 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3155
334d4169 3156 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2d622719
TZ
3157 cpu_relax();
3158 goto again;
3159 }
3160
f83c9d0f
SR
3161 return event;
3162}
3163
7a8e76a3
SR
3164/**
3165 * ring_buffer_consume - return an event and consume it
3166 * @buffer: The ring buffer to get the next event from
3167 *
3168 * Returns the next event in the ring buffer, and that event is consumed.
3169 * Meaning, that sequential reads will keep returning a different event,
3170 * and eventually empty the ring buffer if the producer is slower.
3171 */
3172struct ring_buffer_event *
3173ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
3174{
554f786e
SR
3175 struct ring_buffer_per_cpu *cpu_buffer;
3176 struct ring_buffer_event *event = NULL;
f83c9d0f 3177 unsigned long flags;
8d707e8e
SR
3178 int dolock;
3179
3180 dolock = rb_ok_to_lock();
7a8e76a3 3181
2d622719 3182 again:
554f786e
SR
3183 /* might be called in atomic */
3184 preempt_disable();
3185
9e01c1b7 3186 if (!cpumask_test_cpu(cpu, buffer->cpumask))
554f786e 3187 goto out;
7a8e76a3 3188
554f786e 3189 cpu_buffer = buffer->buffers[cpu];
8d707e8e
SR
3190 local_irq_save(flags);
3191 if (dolock)
3192 spin_lock(&cpu_buffer->reader_lock);
f83c9d0f
SR
3193
3194 event = rb_buffer_peek(buffer, cpu, ts);
7a8e76a3 3195 if (!event)
554f786e 3196 goto out_unlock;
7a8e76a3 3197
d769041f 3198 rb_advance_reader(cpu_buffer);
7a8e76a3 3199
554f786e 3200 out_unlock:
8d707e8e
SR
3201 if (dolock)
3202 spin_unlock(&cpu_buffer->reader_lock);
3203 local_irq_restore(flags);
f83c9d0f 3204
554f786e
SR
3205 out:
3206 preempt_enable();
3207
334d4169 3208 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2d622719
TZ
3209 cpu_relax();
3210 goto again;
3211 }
3212
7a8e76a3
SR
3213 return event;
3214}
c4f50183 3215EXPORT_SYMBOL_GPL(ring_buffer_consume);
7a8e76a3
SR
3216
3217/**
3218 * ring_buffer_read_start - start a non consuming read of the buffer
3219 * @buffer: The ring buffer to read from
3220 * @cpu: The cpu buffer to iterate over
3221 *
3222 * This starts up an iteration through the buffer. It also disables
3223 * the recording to the buffer until the reading is finished.
3224 * This prevents the reading from being corrupted. This is not
3225 * a consuming read, so a producer is not expected.
3226 *
3227 * Must be paired with ring_buffer_finish.
3228 */
3229struct ring_buffer_iter *
3230ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
3231{
3232 struct ring_buffer_per_cpu *cpu_buffer;
8aabee57 3233 struct ring_buffer_iter *iter;
d769041f 3234 unsigned long flags;
7a8e76a3 3235
9e01c1b7 3236 if (!cpumask_test_cpu(cpu, buffer->cpumask))
8aabee57 3237 return NULL;
7a8e76a3
SR
3238
3239 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3240 if (!iter)
8aabee57 3241 return NULL;
7a8e76a3
SR
3242
3243 cpu_buffer = buffer->buffers[cpu];
3244
3245 iter->cpu_buffer = cpu_buffer;
3246
3247 atomic_inc(&cpu_buffer->record_disabled);
3248 synchronize_sched();
3249
f83c9d0f 3250 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3e03fb7f 3251 __raw_spin_lock(&cpu_buffer->lock);
642edba5 3252 rb_iter_reset(iter);
3e03fb7f 3253 __raw_spin_unlock(&cpu_buffer->lock);
f83c9d0f 3254 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
7a8e76a3
SR
3255
3256 return iter;
3257}
c4f50183 3258EXPORT_SYMBOL_GPL(ring_buffer_read_start);
7a8e76a3
SR
3259
3260/**
3261 * ring_buffer_finish - finish reading the iterator of the buffer
3262 * @iter: The iterator retrieved by ring_buffer_start
3263 *
3264 * This re-enables the recording to the buffer, and frees the
3265 * iterator.
3266 */
3267void
3268ring_buffer_read_finish(struct ring_buffer_iter *iter)
3269{
3270 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3271
3272 atomic_dec(&cpu_buffer->record_disabled);
3273 kfree(iter);
3274}
c4f50183 3275EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
7a8e76a3
SR
3276
3277/**
3278 * ring_buffer_read - read the next item in the ring buffer by the iterator
3279 * @iter: The ring buffer iterator
3280 * @ts: The time stamp of the event read.
3281 *
3282 * This reads the next event in the ring buffer and increments the iterator.
3283 */
3284struct ring_buffer_event *
3285ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
3286{
3287 struct ring_buffer_event *event;
f83c9d0f
SR
3288 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3289 unsigned long flags;
7a8e76a3 3290
2d622719 3291 again:
f83c9d0f
SR
3292 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3293 event = rb_iter_peek(iter, ts);
7a8e76a3 3294 if (!event)
f83c9d0f 3295 goto out;
7a8e76a3
SR
3296
3297 rb_advance_iter(iter);
f83c9d0f
SR
3298 out:
3299 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
7a8e76a3 3300
334d4169 3301 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2d622719
TZ
3302 cpu_relax();
3303 goto again;
3304 }
3305
7a8e76a3
SR
3306 return event;
3307}
c4f50183 3308EXPORT_SYMBOL_GPL(ring_buffer_read);
7a8e76a3
SR
3309
3310/**
3311 * ring_buffer_size - return the size of the ring buffer (in bytes)
3312 * @buffer: The ring buffer.
3313 */
3314unsigned long ring_buffer_size(struct ring_buffer *buffer)
3315{
3316 return BUF_PAGE_SIZE * buffer->pages;
3317}
c4f50183 3318EXPORT_SYMBOL_GPL(ring_buffer_size);
7a8e76a3
SR
3319
3320static void
3321rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
3322{
77ae365e
SR
3323 rb_head_page_deactivate(cpu_buffer);
3324
7a8e76a3 3325 cpu_buffer->head_page
3adc54fa 3326 = list_entry(cpu_buffer->pages, struct buffer_page, list);
bf41a158 3327 local_set(&cpu_buffer->head_page->write, 0);
778c55d4 3328 local_set(&cpu_buffer->head_page->entries, 0);
abc9b56d 3329 local_set(&cpu_buffer->head_page->page->commit, 0);
d769041f 3330
6f807acd 3331 cpu_buffer->head_page->read = 0;
bf41a158
SR
3332
3333 cpu_buffer->tail_page = cpu_buffer->head_page;
3334 cpu_buffer->commit_page = cpu_buffer->head_page;
3335
3336 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
3337 local_set(&cpu_buffer->reader_page->write, 0);
778c55d4 3338 local_set(&cpu_buffer->reader_page->entries, 0);
abc9b56d 3339 local_set(&cpu_buffer->reader_page->page->commit, 0);
6f807acd 3340 cpu_buffer->reader_page->read = 0;
7a8e76a3 3341
77ae365e
SR
3342 local_set(&cpu_buffer->commit_overrun, 0);
3343 local_set(&cpu_buffer->overrun, 0);
e4906eff 3344 local_set(&cpu_buffer->entries, 0);
fa743953
SR
3345 local_set(&cpu_buffer->committing, 0);
3346 local_set(&cpu_buffer->commits, 0);
77ae365e 3347 cpu_buffer->read = 0;
69507c06
SR
3348
3349 cpu_buffer->write_stamp = 0;
3350 cpu_buffer->read_stamp = 0;
77ae365e
SR
3351
3352 rb_head_page_activate(cpu_buffer);
7a8e76a3
SR
3353}
3354
3355/**
3356 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
3357 * @buffer: The ring buffer to reset a per cpu buffer of
3358 * @cpu: The CPU buffer to be reset
3359 */
3360void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
3361{
3362 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3363 unsigned long flags;
3364
9e01c1b7 3365 if (!cpumask_test_cpu(cpu, buffer->cpumask))
8aabee57 3366 return;
7a8e76a3 3367
41ede23e
SR
3368 atomic_inc(&cpu_buffer->record_disabled);
3369
f83c9d0f
SR
3370 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3371
3e03fb7f 3372 __raw_spin_lock(&cpu_buffer->lock);
7a8e76a3
SR
3373
3374 rb_reset_cpu(cpu_buffer);
3375
3e03fb7f 3376 __raw_spin_unlock(&cpu_buffer->lock);
f83c9d0f
SR
3377
3378 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
41ede23e
SR
3379
3380 atomic_dec(&cpu_buffer->record_disabled);
7a8e76a3 3381}
c4f50183 3382EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
7a8e76a3
SR
3383
3384/**
3385 * ring_buffer_reset - reset a ring buffer
3386 * @buffer: The ring buffer to reset all cpu buffers
3387 */
3388void ring_buffer_reset(struct ring_buffer *buffer)
3389{
7a8e76a3
SR
3390 int cpu;
3391
7a8e76a3 3392 for_each_buffer_cpu(buffer, cpu)
d769041f 3393 ring_buffer_reset_cpu(buffer, cpu);
7a8e76a3 3394}
c4f50183 3395EXPORT_SYMBOL_GPL(ring_buffer_reset);
7a8e76a3
SR
3396
3397/**
3398 * rind_buffer_empty - is the ring buffer empty?
3399 * @buffer: The ring buffer to test
3400 */
3401int ring_buffer_empty(struct ring_buffer *buffer)
3402{
3403 struct ring_buffer_per_cpu *cpu_buffer;
d4788207 3404 unsigned long flags;
8d707e8e 3405 int dolock;
7a8e76a3 3406 int cpu;
d4788207 3407 int ret;
7a8e76a3 3408
8d707e8e 3409 dolock = rb_ok_to_lock();
7a8e76a3
SR
3410
3411 /* yes this is racy, but if you don't like the race, lock the buffer */
3412 for_each_buffer_cpu(buffer, cpu) {
3413 cpu_buffer = buffer->buffers[cpu];
8d707e8e
SR
3414 local_irq_save(flags);
3415 if (dolock)
3416 spin_lock(&cpu_buffer->reader_lock);
d4788207 3417 ret = rb_per_cpu_empty(cpu_buffer);
8d707e8e
SR
3418 if (dolock)
3419 spin_unlock(&cpu_buffer->reader_lock);
3420 local_irq_restore(flags);
3421
d4788207 3422 if (!ret)
7a8e76a3
SR
3423 return 0;
3424 }
554f786e 3425
7a8e76a3
SR
3426 return 1;
3427}
c4f50183 3428EXPORT_SYMBOL_GPL(ring_buffer_empty);
7a8e76a3
SR
3429
3430/**
3431 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
3432 * @buffer: The ring buffer
3433 * @cpu: The CPU buffer to test
3434 */
3435int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
3436{
3437 struct ring_buffer_per_cpu *cpu_buffer;
d4788207 3438 unsigned long flags;
8d707e8e 3439 int dolock;
8aabee57 3440 int ret;
7a8e76a3 3441
9e01c1b7 3442 if (!cpumask_test_cpu(cpu, buffer->cpumask))
8aabee57 3443 return 1;
7a8e76a3 3444
8d707e8e
SR
3445 dolock = rb_ok_to_lock();
3446
7a8e76a3 3447 cpu_buffer = buffer->buffers[cpu];
8d707e8e
SR
3448 local_irq_save(flags);
3449 if (dolock)
3450 spin_lock(&cpu_buffer->reader_lock);
554f786e 3451 ret = rb_per_cpu_empty(cpu_buffer);
8d707e8e
SR
3452 if (dolock)
3453 spin_unlock(&cpu_buffer->reader_lock);
3454 local_irq_restore(flags);
554f786e
SR
3455
3456 return ret;
7a8e76a3 3457}
c4f50183 3458EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
7a8e76a3
SR
3459
3460/**
3461 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
3462 * @buffer_a: One buffer to swap with
3463 * @buffer_b: The other buffer to swap with
3464 *
3465 * This function is useful for tracers that want to take a "snapshot"
3466 * of a CPU buffer and has another back up buffer lying around.
3467 * it is expected that the tracer handles the cpu buffer not being
3468 * used at the moment.
3469 */
3470int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
3471 struct ring_buffer *buffer_b, int cpu)
3472{
3473 struct ring_buffer_per_cpu *cpu_buffer_a;
3474 struct ring_buffer_per_cpu *cpu_buffer_b;
554f786e
SR
3475 int ret = -EINVAL;
3476
9e01c1b7
RR
3477 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
3478 !cpumask_test_cpu(cpu, buffer_b->cpumask))
554f786e 3479 goto out;
7a8e76a3
SR
3480
3481 /* At least make sure the two buffers are somewhat the same */
6d102bc6 3482 if (buffer_a->pages != buffer_b->pages)
554f786e
SR
3483 goto out;
3484
3485 ret = -EAGAIN;
7a8e76a3 3486
97b17efe 3487 if (ring_buffer_flags != RB_BUFFERS_ON)
554f786e 3488 goto out;
97b17efe
SR
3489
3490 if (atomic_read(&buffer_a->record_disabled))
554f786e 3491 goto out;
97b17efe
SR
3492
3493 if (atomic_read(&buffer_b->record_disabled))
554f786e 3494 goto out;
97b17efe 3495
7a8e76a3
SR
3496 cpu_buffer_a = buffer_a->buffers[cpu];
3497 cpu_buffer_b = buffer_b->buffers[cpu];
3498
97b17efe 3499 if (atomic_read(&cpu_buffer_a->record_disabled))
554f786e 3500 goto out;
97b17efe
SR
3501
3502 if (atomic_read(&cpu_buffer_b->record_disabled))
554f786e 3503 goto out;
97b17efe 3504
7a8e76a3
SR
3505 /*
3506 * We can't do a synchronize_sched here because this
3507 * function can be called in atomic context.
3508 * Normally this will be called from the same CPU as cpu.
3509 * If not it's up to the caller to protect this.
3510 */
3511 atomic_inc(&cpu_buffer_a->record_disabled);
3512 atomic_inc(&cpu_buffer_b->record_disabled);
3513
3514 buffer_a->buffers[cpu] = cpu_buffer_b;
3515 buffer_b->buffers[cpu] = cpu_buffer_a;
3516
3517 cpu_buffer_b->buffer = buffer_a;
3518 cpu_buffer_a->buffer = buffer_b;
3519
3520 atomic_dec(&cpu_buffer_a->record_disabled);
3521 atomic_dec(&cpu_buffer_b->record_disabled);
3522
554f786e
SR
3523 ret = 0;
3524out:
554f786e 3525 return ret;
7a8e76a3 3526}
c4f50183 3527EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
7a8e76a3 3528
8789a9e7
SR
3529/**
3530 * ring_buffer_alloc_read_page - allocate a page to read from buffer
3531 * @buffer: the buffer to allocate for.
3532 *
3533 * This function is used in conjunction with ring_buffer_read_page.
3534 * When reading a full page from the ring buffer, these functions
3535 * can be used to speed up the process. The calling function should
3536 * allocate a few pages first with this function. Then when it
3537 * needs to get pages from the ring buffer, it passes the result
3538 * of this function into ring_buffer_read_page, which will swap
3539 * the page that was allocated, with the read page of the buffer.
3540 *
3541 * Returns:
3542 * The page allocated, or NULL on error.
3543 */
3544void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
3545{
044fa782 3546 struct buffer_data_page *bpage;
ef7a4a16 3547 unsigned long addr;
8789a9e7
SR
3548
3549 addr = __get_free_page(GFP_KERNEL);
3550 if (!addr)
3551 return NULL;
3552
044fa782 3553 bpage = (void *)addr;
8789a9e7 3554
ef7a4a16
SR
3555 rb_init_page(bpage);
3556
044fa782 3557 return bpage;
8789a9e7 3558}
d6ce96da 3559EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
8789a9e7
SR
3560
3561/**
3562 * ring_buffer_free_read_page - free an allocated read page
3563 * @buffer: the buffer the page was allocate for
3564 * @data: the page to free
3565 *
3566 * Free a page allocated from ring_buffer_alloc_read_page.
3567 */
3568void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
3569{
3570 free_page((unsigned long)data);
3571}
d6ce96da 3572EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
8789a9e7
SR
3573
3574/**
3575 * ring_buffer_read_page - extract a page from the ring buffer
3576 * @buffer: buffer to extract from
3577 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
ef7a4a16 3578 * @len: amount to extract
8789a9e7
SR
3579 * @cpu: the cpu of the buffer to extract
3580 * @full: should the extraction only happen when the page is full.
3581 *
3582 * This function will pull out a page from the ring buffer and consume it.
3583 * @data_page must be the address of the variable that was returned
3584 * from ring_buffer_alloc_read_page. This is because the page might be used
3585 * to swap with a page in the ring buffer.
3586 *
3587 * for example:
b85fa01e 3588 * rpage = ring_buffer_alloc_read_page(buffer);
8789a9e7
SR
3589 * if (!rpage)
3590 * return error;
ef7a4a16 3591 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
667d2412
LJ
3592 * if (ret >= 0)
3593 * process_page(rpage, ret);
8789a9e7
SR
3594 *
3595 * When @full is set, the function will not return true unless
3596 * the writer is off the reader page.
3597 *
3598 * Note: it is up to the calling functions to handle sleeps and wakeups.
3599 * The ring buffer can be used anywhere in the kernel and can not
3600 * blindly call wake_up. The layer that uses the ring buffer must be
3601 * responsible for that.
3602 *
3603 * Returns:
667d2412
LJ
3604 * >=0 if data has been transferred, returns the offset of consumed data.
3605 * <0 if no data has been transferred.
8789a9e7
SR
3606 */
3607int ring_buffer_read_page(struct ring_buffer *buffer,
ef7a4a16 3608 void **data_page, size_t len, int cpu, int full)
8789a9e7
SR
3609{
3610 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3611 struct ring_buffer_event *event;
044fa782 3612 struct buffer_data_page *bpage;
ef7a4a16 3613 struct buffer_page *reader;
8789a9e7 3614 unsigned long flags;
ef7a4a16 3615 unsigned int commit;
667d2412 3616 unsigned int read;
4f3640f8 3617 u64 save_timestamp;
667d2412 3618 int ret = -1;
8789a9e7 3619
554f786e
SR
3620 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3621 goto out;
3622
474d32b6
SR
3623 /*
3624 * If len is not big enough to hold the page header, then
3625 * we can not copy anything.
3626 */
3627 if (len <= BUF_PAGE_HDR_SIZE)
554f786e 3628 goto out;
474d32b6
SR
3629
3630 len -= BUF_PAGE_HDR_SIZE;
3631
8789a9e7 3632 if (!data_page)
554f786e 3633 goto out;
8789a9e7 3634
044fa782
SR
3635 bpage = *data_page;
3636 if (!bpage)
554f786e 3637 goto out;
8789a9e7
SR
3638
3639 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3640
ef7a4a16
SR
3641 reader = rb_get_reader_page(cpu_buffer);
3642 if (!reader)
554f786e 3643 goto out_unlock;
8789a9e7 3644
ef7a4a16
SR
3645 event = rb_reader_event(cpu_buffer);
3646
3647 read = reader->read;
3648 commit = rb_page_commit(reader);
667d2412 3649
8789a9e7 3650 /*
474d32b6
SR
3651 * If this page has been partially read or
3652 * if len is not big enough to read the rest of the page or
3653 * a writer is still on the page, then
3654 * we must copy the data from the page to the buffer.
3655 * Otherwise, we can simply swap the page with the one passed in.
8789a9e7 3656 */
474d32b6 3657 if (read || (len < (commit - read)) ||
ef7a4a16 3658 cpu_buffer->reader_page == cpu_buffer->commit_page) {
667d2412 3659 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
474d32b6
SR
3660 unsigned int rpos = read;
3661 unsigned int pos = 0;
ef7a4a16 3662 unsigned int size;
8789a9e7
SR
3663
3664 if (full)
554f786e 3665 goto out_unlock;
8789a9e7 3666
ef7a4a16
SR
3667 if (len > (commit - read))
3668 len = (commit - read);
3669
3670 size = rb_event_length(event);
3671
3672 if (len < size)
554f786e 3673 goto out_unlock;
ef7a4a16 3674
4f3640f8
SR
3675 /* save the current timestamp, since the user will need it */
3676 save_timestamp = cpu_buffer->read_stamp;
3677
ef7a4a16
SR
3678 /* Need to copy one event at a time */
3679 do {
474d32b6 3680 memcpy(bpage->data + pos, rpage->data + rpos, size);
ef7a4a16
SR
3681
3682 len -= size;
3683
3684 rb_advance_reader(cpu_buffer);
474d32b6
SR
3685 rpos = reader->read;
3686 pos += size;
ef7a4a16
SR
3687
3688 event = rb_reader_event(cpu_buffer);
3689 size = rb_event_length(event);
3690 } while (len > size);
667d2412
LJ
3691
3692 /* update bpage */
ef7a4a16 3693 local_set(&bpage->commit, pos);
4f3640f8 3694 bpage->time_stamp = save_timestamp;
ef7a4a16 3695
474d32b6
SR
3696 /* we copied everything to the beginning */
3697 read = 0;
8789a9e7 3698 } else {
afbab76a 3699 /* update the entry counter */
77ae365e 3700 cpu_buffer->read += rb_page_entries(reader);
afbab76a 3701
8789a9e7 3702 /* swap the pages */
044fa782 3703 rb_init_page(bpage);
ef7a4a16
SR
3704 bpage = reader->page;
3705 reader->page = *data_page;
3706 local_set(&reader->write, 0);
778c55d4 3707 local_set(&reader->entries, 0);
ef7a4a16 3708 reader->read = 0;
044fa782 3709 *data_page = bpage;
8789a9e7 3710 }
667d2412 3711 ret = read;
8789a9e7 3712
554f786e 3713 out_unlock:
8789a9e7
SR
3714 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3715
554f786e 3716 out:
8789a9e7
SR
3717 return ret;
3718}
d6ce96da 3719EXPORT_SYMBOL_GPL(ring_buffer_read_page);
8789a9e7 3720
1155de47 3721#ifdef CONFIG_TRACING
a3583244
SR
3722static ssize_t
3723rb_simple_read(struct file *filp, char __user *ubuf,
3724 size_t cnt, loff_t *ppos)
3725{
5e39841c 3726 unsigned long *p = filp->private_data;
a3583244
SR
3727 char buf[64];
3728 int r;
3729
033601a3
SR
3730 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3731 r = sprintf(buf, "permanently disabled\n");
3732 else
3733 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
a3583244
SR
3734
3735 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3736}
3737
3738static ssize_t
3739rb_simple_write(struct file *filp, const char __user *ubuf,
3740 size_t cnt, loff_t *ppos)
3741{
5e39841c 3742 unsigned long *p = filp->private_data;
a3583244 3743 char buf[64];
5e39841c 3744 unsigned long val;
a3583244
SR
3745 int ret;
3746
3747 if (cnt >= sizeof(buf))
3748 return -EINVAL;
3749
3750 if (copy_from_user(&buf, ubuf, cnt))
3751 return -EFAULT;
3752
3753 buf[cnt] = 0;
3754
3755 ret = strict_strtoul(buf, 10, &val);
3756 if (ret < 0)
3757 return ret;
3758
033601a3
SR
3759 if (val)
3760 set_bit(RB_BUFFERS_ON_BIT, p);
3761 else
3762 clear_bit(RB_BUFFERS_ON_BIT, p);
a3583244
SR
3763
3764 (*ppos)++;
3765
3766 return cnt;
3767}
3768
5e2336a0 3769static const struct file_operations rb_simple_fops = {
a3583244
SR
3770 .open = tracing_open_generic,
3771 .read = rb_simple_read,
3772 .write = rb_simple_write,
3773};
3774
3775
3776static __init int rb_init_debugfs(void)
3777{
3778 struct dentry *d_tracer;
a3583244
SR
3779
3780 d_tracer = tracing_init_dentry();
3781
5452af66
FW
3782 trace_create_file("tracing_on", 0644, d_tracer,
3783 &ring_buffer_flags, &rb_simple_fops);
a3583244
SR
3784
3785 return 0;
3786}
3787
3788fs_initcall(rb_init_debugfs);
1155de47 3789#endif
554f786e 3790
59222efe 3791#ifdef CONFIG_HOTPLUG_CPU
09c9e84d
FW
3792static int rb_cpu_notify(struct notifier_block *self,
3793 unsigned long action, void *hcpu)
554f786e
SR
3794{
3795 struct ring_buffer *buffer =
3796 container_of(self, struct ring_buffer, cpu_notify);
3797 long cpu = (long)hcpu;
3798
3799 switch (action) {
3800 case CPU_UP_PREPARE:
3801 case CPU_UP_PREPARE_FROZEN:
3f237a79 3802 if (cpumask_test_cpu(cpu, buffer->cpumask))
554f786e
SR
3803 return NOTIFY_OK;
3804
3805 buffer->buffers[cpu] =
3806 rb_allocate_cpu_buffer(buffer, cpu);
3807 if (!buffer->buffers[cpu]) {
3808 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3809 cpu);
3810 return NOTIFY_OK;
3811 }
3812 smp_wmb();
3f237a79 3813 cpumask_set_cpu(cpu, buffer->cpumask);
554f786e
SR
3814 break;
3815 case CPU_DOWN_PREPARE:
3816 case CPU_DOWN_PREPARE_FROZEN:
3817 /*
3818 * Do nothing.
3819 * If we were to free the buffer, then the user would
3820 * lose any trace that was in the buffer.
3821 */
3822 break;
3823 default:
3824 break;
3825 }
3826 return NOTIFY_OK;
3827}
3828#endif