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