perf/ring_buffer: Introduce new ioctl options to pause and resume the ring-buffer
[linux-2.6/btrfs-unstable.git] / kernel / events / ring_buffer.c
blob72d8127bb8fde9588e3fd0281c168ae03e580e75
1 /*
2 * Performance events ring-buffer code:
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra
7 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
9 * For licensing details see kernel-base/COPYING
12 #include <linux/perf_event.h>
13 #include <linux/vmalloc.h>
14 #include <linux/slab.h>
15 #include <linux/circ_buf.h>
16 #include <linux/poll.h>
18 #include "internal.h"
20 static void perf_output_wakeup(struct perf_output_handle *handle)
22 atomic_set(&handle->rb->poll, POLLIN);
24 handle->event->pending_wakeup = 1;
25 irq_work_queue(&handle->event->pending);
29 * We need to ensure a later event_id doesn't publish a head when a former
30 * event isn't done writing. However since we need to deal with NMIs we
31 * cannot fully serialize things.
33 * We only publish the head (and generate a wakeup) when the outer-most
34 * event completes.
36 static void perf_output_get_handle(struct perf_output_handle *handle)
38 struct ring_buffer *rb = handle->rb;
40 preempt_disable();
41 local_inc(&rb->nest);
42 handle->wakeup = local_read(&rb->wakeup);
45 static void perf_output_put_handle(struct perf_output_handle *handle)
47 struct ring_buffer *rb = handle->rb;
48 unsigned long head;
50 again:
51 head = local_read(&rb->head);
54 * IRQ/NMI can happen here, which means we can miss a head update.
57 if (!local_dec_and_test(&rb->nest))
58 goto out;
61 * Since the mmap() consumer (userspace) can run on a different CPU:
63 * kernel user
65 * if (LOAD ->data_tail) { LOAD ->data_head
66 * (A) smp_rmb() (C)
67 * STORE $data LOAD $data
68 * smp_wmb() (B) smp_mb() (D)
69 * STORE ->data_head STORE ->data_tail
70 * }
72 * Where A pairs with D, and B pairs with C.
74 * In our case (A) is a control dependency that separates the load of
75 * the ->data_tail and the stores of $data. In case ->data_tail
76 * indicates there is no room in the buffer to store $data we do not.
78 * D needs to be a full barrier since it separates the data READ
79 * from the tail WRITE.
81 * For B a WMB is sufficient since it separates two WRITEs, and for C
82 * an RMB is sufficient since it separates two READs.
84 * See perf_output_begin().
86 smp_wmb(); /* B, matches C */
87 rb->user_page->data_head = head;
90 * Now check if we missed an update -- rely on previous implied
91 * compiler barriers to force a re-read.
93 if (unlikely(head != local_read(&rb->head))) {
94 local_inc(&rb->nest);
95 goto again;
98 if (handle->wakeup != local_read(&rb->wakeup))
99 perf_output_wakeup(handle);
101 out:
102 preempt_enable();
105 int perf_output_begin(struct perf_output_handle *handle,
106 struct perf_event *event, unsigned int size)
108 struct ring_buffer *rb;
109 unsigned long tail, offset, head;
110 int have_lost, page_shift;
111 struct {
112 struct perf_event_header header;
113 u64 id;
114 u64 lost;
115 } lost_event;
117 rcu_read_lock();
119 * For inherited events we send all the output towards the parent.
121 if (event->parent)
122 event = event->parent;
124 rb = rcu_dereference(event->rb);
125 if (unlikely(!rb))
126 goto out;
128 if (unlikely(rb->paused)) {
129 if (rb->nr_pages)
130 local_inc(&rb->lost);
131 goto out;
134 handle->rb = rb;
135 handle->event = event;
137 have_lost = local_read(&rb->lost);
138 if (unlikely(have_lost)) {
139 size += sizeof(lost_event);
140 if (event->attr.sample_id_all)
141 size += event->id_header_size;
144 perf_output_get_handle(handle);
146 do {
147 tail = READ_ONCE(rb->user_page->data_tail);
148 offset = head = local_read(&rb->head);
149 if (!rb->overwrite &&
150 unlikely(CIRC_SPACE(head, tail, perf_data_size(rb)) < size))
151 goto fail;
154 * The above forms a control dependency barrier separating the
155 * @tail load above from the data stores below. Since the @tail
156 * load is required to compute the branch to fail below.
158 * A, matches D; the full memory barrier userspace SHOULD issue
159 * after reading the data and before storing the new tail
160 * position.
162 * See perf_output_put_handle().
165 head += size;
166 } while (local_cmpxchg(&rb->head, offset, head) != offset);
169 * We rely on the implied barrier() by local_cmpxchg() to ensure
170 * none of the data stores below can be lifted up by the compiler.
173 if (unlikely(head - local_read(&rb->wakeup) > rb->watermark))
174 local_add(rb->watermark, &rb->wakeup);
176 page_shift = PAGE_SHIFT + page_order(rb);
178 handle->page = (offset >> page_shift) & (rb->nr_pages - 1);
179 offset &= (1UL << page_shift) - 1;
180 handle->addr = rb->data_pages[handle->page] + offset;
181 handle->size = (1UL << page_shift) - offset;
183 if (unlikely(have_lost)) {
184 struct perf_sample_data sample_data;
186 lost_event.header.size = sizeof(lost_event);
187 lost_event.header.type = PERF_RECORD_LOST;
188 lost_event.header.misc = 0;
189 lost_event.id = event->id;
190 lost_event.lost = local_xchg(&rb->lost, 0);
192 perf_event_header__init_id(&lost_event.header,
193 &sample_data, event);
194 perf_output_put(handle, lost_event);
195 perf_event__output_id_sample(event, handle, &sample_data);
198 return 0;
200 fail:
201 local_inc(&rb->lost);
202 perf_output_put_handle(handle);
203 out:
204 rcu_read_unlock();
206 return -ENOSPC;
209 unsigned int perf_output_copy(struct perf_output_handle *handle,
210 const void *buf, unsigned int len)
212 return __output_copy(handle, buf, len);
215 unsigned int perf_output_skip(struct perf_output_handle *handle,
216 unsigned int len)
218 return __output_skip(handle, NULL, len);
221 void perf_output_end(struct perf_output_handle *handle)
223 perf_output_put_handle(handle);
224 rcu_read_unlock();
227 static void
228 ring_buffer_init(struct ring_buffer *rb, long watermark, int flags)
230 long max_size = perf_data_size(rb);
232 if (watermark)
233 rb->watermark = min(max_size, watermark);
235 if (!rb->watermark)
236 rb->watermark = max_size / 2;
238 if (flags & RING_BUFFER_WRITABLE)
239 rb->overwrite = 0;
240 else
241 rb->overwrite = 1;
243 atomic_set(&rb->refcount, 1);
245 INIT_LIST_HEAD(&rb->event_list);
246 spin_lock_init(&rb->event_lock);
249 * perf_output_begin() only checks rb->paused, therefore
250 * rb->paused must be true if we have no pages for output.
252 if (!rb->nr_pages)
253 rb->paused = 1;
257 * This is called before hardware starts writing to the AUX area to
258 * obtain an output handle and make sure there's room in the buffer.
259 * When the capture completes, call perf_aux_output_end() to commit
260 * the recorded data to the buffer.
262 * The ordering is similar to that of perf_output_{begin,end}, with
263 * the exception of (B), which should be taken care of by the pmu
264 * driver, since ordering rules will differ depending on hardware.
266 * Call this from pmu::start(); see the comment in perf_aux_output_end()
267 * about its use in pmu callbacks. Both can also be called from the PMI
268 * handler if needed.
270 void *perf_aux_output_begin(struct perf_output_handle *handle,
271 struct perf_event *event)
273 struct perf_event *output_event = event;
274 unsigned long aux_head, aux_tail;
275 struct ring_buffer *rb;
277 if (output_event->parent)
278 output_event = output_event->parent;
281 * Since this will typically be open across pmu::add/pmu::del, we
282 * grab ring_buffer's refcount instead of holding rcu read lock
283 * to make sure it doesn't disappear under us.
285 rb = ring_buffer_get(output_event);
286 if (!rb)
287 return NULL;
289 if (!rb_has_aux(rb) || !atomic_inc_not_zero(&rb->aux_refcount))
290 goto err;
293 * If rb::aux_mmap_count is zero (and rb_has_aux() above went through),
294 * the aux buffer is in perf_mmap_close(), about to get freed.
296 if (!atomic_read(&rb->aux_mmap_count))
297 goto err_put;
300 * Nesting is not supported for AUX area, make sure nested
301 * writers are caught early
303 if (WARN_ON_ONCE(local_xchg(&rb->aux_nest, 1)))
304 goto err_put;
306 aux_head = local_read(&rb->aux_head);
308 handle->rb = rb;
309 handle->event = event;
310 handle->head = aux_head;
311 handle->size = 0;
314 * In overwrite mode, AUX data stores do not depend on aux_tail,
315 * therefore (A) control dependency barrier does not exist. The
316 * (B) <-> (C) ordering is still observed by the pmu driver.
318 if (!rb->aux_overwrite) {
319 aux_tail = ACCESS_ONCE(rb->user_page->aux_tail);
320 handle->wakeup = local_read(&rb->aux_wakeup) + rb->aux_watermark;
321 if (aux_head - aux_tail < perf_aux_size(rb))
322 handle->size = CIRC_SPACE(aux_head, aux_tail, perf_aux_size(rb));
325 * handle->size computation depends on aux_tail load; this forms a
326 * control dependency barrier separating aux_tail load from aux data
327 * store that will be enabled on successful return
329 if (!handle->size) { /* A, matches D */
330 event->pending_disable = 1;
331 perf_output_wakeup(handle);
332 local_set(&rb->aux_nest, 0);
333 goto err_put;
337 return handle->rb->aux_priv;
339 err_put:
340 /* can't be last */
341 rb_free_aux(rb);
343 err:
344 ring_buffer_put(rb);
345 handle->event = NULL;
347 return NULL;
351 * Commit the data written by hardware into the ring buffer by adjusting
352 * aux_head and posting a PERF_RECORD_AUX into the perf buffer. It is the
353 * pmu driver's responsibility to observe ordering rules of the hardware,
354 * so that all the data is externally visible before this is called.
356 * Note: this has to be called from pmu::stop() callback, as the assumption
357 * of the AUX buffer management code is that after pmu::stop(), the AUX
358 * transaction must be stopped and therefore drop the AUX reference count.
360 void perf_aux_output_end(struct perf_output_handle *handle, unsigned long size,
361 bool truncated)
363 struct ring_buffer *rb = handle->rb;
364 unsigned long aux_head;
365 u64 flags = 0;
367 if (truncated)
368 flags |= PERF_AUX_FLAG_TRUNCATED;
370 /* in overwrite mode, driver provides aux_head via handle */
371 if (rb->aux_overwrite) {
372 flags |= PERF_AUX_FLAG_OVERWRITE;
374 aux_head = handle->head;
375 local_set(&rb->aux_head, aux_head);
376 } else {
377 aux_head = local_read(&rb->aux_head);
378 local_add(size, &rb->aux_head);
381 if (size || flags) {
383 * Only send RECORD_AUX if we have something useful to communicate
386 perf_event_aux_event(handle->event, aux_head, size, flags);
389 aux_head = rb->user_page->aux_head = local_read(&rb->aux_head);
391 if (aux_head - local_read(&rb->aux_wakeup) >= rb->aux_watermark) {
392 perf_output_wakeup(handle);
393 local_add(rb->aux_watermark, &rb->aux_wakeup);
395 handle->event = NULL;
397 local_set(&rb->aux_nest, 0);
398 /* can't be last */
399 rb_free_aux(rb);
400 ring_buffer_put(rb);
404 * Skip over a given number of bytes in the AUX buffer, due to, for example,
405 * hardware's alignment constraints.
407 int perf_aux_output_skip(struct perf_output_handle *handle, unsigned long size)
409 struct ring_buffer *rb = handle->rb;
410 unsigned long aux_head;
412 if (size > handle->size)
413 return -ENOSPC;
415 local_add(size, &rb->aux_head);
417 aux_head = rb->user_page->aux_head = local_read(&rb->aux_head);
418 if (aux_head - local_read(&rb->aux_wakeup) >= rb->aux_watermark) {
419 perf_output_wakeup(handle);
420 local_add(rb->aux_watermark, &rb->aux_wakeup);
421 handle->wakeup = local_read(&rb->aux_wakeup) +
422 rb->aux_watermark;
425 handle->head = aux_head;
426 handle->size -= size;
428 return 0;
431 void *perf_get_aux(struct perf_output_handle *handle)
433 /* this is only valid between perf_aux_output_begin and *_end */
434 if (!handle->event)
435 return NULL;
437 return handle->rb->aux_priv;
440 #define PERF_AUX_GFP (GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY)
442 static struct page *rb_alloc_aux_page(int node, int order)
444 struct page *page;
446 if (order > MAX_ORDER)
447 order = MAX_ORDER;
449 do {
450 page = alloc_pages_node(node, PERF_AUX_GFP, order);
451 } while (!page && order--);
453 if (page && order) {
455 * Communicate the allocation size to the driver:
456 * if we managed to secure a high-order allocation,
457 * set its first page's private to this order;
458 * !PagePrivate(page) means it's just a normal page.
460 split_page(page, order);
461 SetPagePrivate(page);
462 set_page_private(page, order);
465 return page;
468 static void rb_free_aux_page(struct ring_buffer *rb, int idx)
470 struct page *page = virt_to_page(rb->aux_pages[idx]);
472 ClearPagePrivate(page);
473 page->mapping = NULL;
474 __free_page(page);
477 static void __rb_free_aux(struct ring_buffer *rb)
479 int pg;
482 * Should never happen, the last reference should be dropped from
483 * perf_mmap_close() path, which first stops aux transactions (which
484 * in turn are the atomic holders of aux_refcount) and then does the
485 * last rb_free_aux().
487 WARN_ON_ONCE(in_atomic());
489 if (rb->aux_priv) {
490 rb->free_aux(rb->aux_priv);
491 rb->free_aux = NULL;
492 rb->aux_priv = NULL;
495 if (rb->aux_nr_pages) {
496 for (pg = 0; pg < rb->aux_nr_pages; pg++)
497 rb_free_aux_page(rb, pg);
499 kfree(rb->aux_pages);
500 rb->aux_nr_pages = 0;
504 int rb_alloc_aux(struct ring_buffer *rb, struct perf_event *event,
505 pgoff_t pgoff, int nr_pages, long watermark, int flags)
507 bool overwrite = !(flags & RING_BUFFER_WRITABLE);
508 int node = (event->cpu == -1) ? -1 : cpu_to_node(event->cpu);
509 int ret = -ENOMEM, max_order = 0;
511 if (!has_aux(event))
512 return -ENOTSUPP;
514 if (event->pmu->capabilities & PERF_PMU_CAP_AUX_NO_SG) {
516 * We need to start with the max_order that fits in nr_pages,
517 * not the other way around, hence ilog2() and not get_order.
519 max_order = ilog2(nr_pages);
522 * PMU requests more than one contiguous chunks of memory
523 * for SW double buffering
525 if ((event->pmu->capabilities & PERF_PMU_CAP_AUX_SW_DOUBLEBUF) &&
526 !overwrite) {
527 if (!max_order)
528 return -EINVAL;
530 max_order--;
534 rb->aux_pages = kzalloc_node(nr_pages * sizeof(void *), GFP_KERNEL, node);
535 if (!rb->aux_pages)
536 return -ENOMEM;
538 rb->free_aux = event->pmu->free_aux;
539 for (rb->aux_nr_pages = 0; rb->aux_nr_pages < nr_pages;) {
540 struct page *page;
541 int last, order;
543 order = min(max_order, ilog2(nr_pages - rb->aux_nr_pages));
544 page = rb_alloc_aux_page(node, order);
545 if (!page)
546 goto out;
548 for (last = rb->aux_nr_pages + (1 << page_private(page));
549 last > rb->aux_nr_pages; rb->aux_nr_pages++)
550 rb->aux_pages[rb->aux_nr_pages] = page_address(page++);
554 * In overwrite mode, PMUs that don't support SG may not handle more
555 * than one contiguous allocation, since they rely on PMI to do double
556 * buffering. In this case, the entire buffer has to be one contiguous
557 * chunk.
559 if ((event->pmu->capabilities & PERF_PMU_CAP_AUX_NO_SG) &&
560 overwrite) {
561 struct page *page = virt_to_page(rb->aux_pages[0]);
563 if (page_private(page) != max_order)
564 goto out;
567 rb->aux_priv = event->pmu->setup_aux(event->cpu, rb->aux_pages, nr_pages,
568 overwrite);
569 if (!rb->aux_priv)
570 goto out;
572 ret = 0;
575 * aux_pages (and pmu driver's private data, aux_priv) will be
576 * referenced in both producer's and consumer's contexts, thus
577 * we keep a refcount here to make sure either of the two can
578 * reference them safely.
580 atomic_set(&rb->aux_refcount, 1);
582 rb->aux_overwrite = overwrite;
583 rb->aux_watermark = watermark;
585 if (!rb->aux_watermark && !rb->aux_overwrite)
586 rb->aux_watermark = nr_pages << (PAGE_SHIFT - 1);
588 out:
589 if (!ret)
590 rb->aux_pgoff = pgoff;
591 else
592 __rb_free_aux(rb);
594 return ret;
597 void rb_free_aux(struct ring_buffer *rb)
599 if (atomic_dec_and_test(&rb->aux_refcount))
600 __rb_free_aux(rb);
603 #ifndef CONFIG_PERF_USE_VMALLOC
606 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
609 static struct page *
610 __perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff)
612 if (pgoff > rb->nr_pages)
613 return NULL;
615 if (pgoff == 0)
616 return virt_to_page(rb->user_page);
618 return virt_to_page(rb->data_pages[pgoff - 1]);
621 static void *perf_mmap_alloc_page(int cpu)
623 struct page *page;
624 int node;
626 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
627 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
628 if (!page)
629 return NULL;
631 return page_address(page);
634 struct ring_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags)
636 struct ring_buffer *rb;
637 unsigned long size;
638 int i;
640 size = sizeof(struct ring_buffer);
641 size += nr_pages * sizeof(void *);
643 rb = kzalloc(size, GFP_KERNEL);
644 if (!rb)
645 goto fail;
647 rb->user_page = perf_mmap_alloc_page(cpu);
648 if (!rb->user_page)
649 goto fail_user_page;
651 for (i = 0; i < nr_pages; i++) {
652 rb->data_pages[i] = perf_mmap_alloc_page(cpu);
653 if (!rb->data_pages[i])
654 goto fail_data_pages;
657 rb->nr_pages = nr_pages;
659 ring_buffer_init(rb, watermark, flags);
661 return rb;
663 fail_data_pages:
664 for (i--; i >= 0; i--)
665 free_page((unsigned long)rb->data_pages[i]);
667 free_page((unsigned long)rb->user_page);
669 fail_user_page:
670 kfree(rb);
672 fail:
673 return NULL;
676 static void perf_mmap_free_page(unsigned long addr)
678 struct page *page = virt_to_page((void *)addr);
680 page->mapping = NULL;
681 __free_page(page);
684 void rb_free(struct ring_buffer *rb)
686 int i;
688 perf_mmap_free_page((unsigned long)rb->user_page);
689 for (i = 0; i < rb->nr_pages; i++)
690 perf_mmap_free_page((unsigned long)rb->data_pages[i]);
691 kfree(rb);
694 #else
695 static int data_page_nr(struct ring_buffer *rb)
697 return rb->nr_pages << page_order(rb);
700 static struct page *
701 __perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff)
703 /* The '>' counts in the user page. */
704 if (pgoff > data_page_nr(rb))
705 return NULL;
707 return vmalloc_to_page((void *)rb->user_page + pgoff * PAGE_SIZE);
710 static void perf_mmap_unmark_page(void *addr)
712 struct page *page = vmalloc_to_page(addr);
714 page->mapping = NULL;
717 static void rb_free_work(struct work_struct *work)
719 struct ring_buffer *rb;
720 void *base;
721 int i, nr;
723 rb = container_of(work, struct ring_buffer, work);
724 nr = data_page_nr(rb);
726 base = rb->user_page;
727 /* The '<=' counts in the user page. */
728 for (i = 0; i <= nr; i++)
729 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
731 vfree(base);
732 kfree(rb);
735 void rb_free(struct ring_buffer *rb)
737 schedule_work(&rb->work);
740 struct ring_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags)
742 struct ring_buffer *rb;
743 unsigned long size;
744 void *all_buf;
746 size = sizeof(struct ring_buffer);
747 size += sizeof(void *);
749 rb = kzalloc(size, GFP_KERNEL);
750 if (!rb)
751 goto fail;
753 INIT_WORK(&rb->work, rb_free_work);
755 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
756 if (!all_buf)
757 goto fail_all_buf;
759 rb->user_page = all_buf;
760 rb->data_pages[0] = all_buf + PAGE_SIZE;
761 if (nr_pages) {
762 rb->nr_pages = 1;
763 rb->page_order = ilog2(nr_pages);
766 ring_buffer_init(rb, watermark, flags);
768 return rb;
770 fail_all_buf:
771 kfree(rb);
773 fail:
774 return NULL;
777 #endif
779 struct page *
780 perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff)
782 if (rb->aux_nr_pages) {
783 /* above AUX space */
784 if (pgoff > rb->aux_pgoff + rb->aux_nr_pages)
785 return NULL;
787 /* AUX space */
788 if (pgoff >= rb->aux_pgoff)
789 return virt_to_page(rb->aux_pages[pgoff - rb->aux_pgoff]);
792 return __perf_mmap_to_page(rb, pgoff);