spi-topcliff-pch: supports a spi mode setup and bit order setup by IO control
[zen-stable.git] / kernel / events / ring_buffer.c
blob6ddaba43fb7adf1b792a97bf45baead52cee1fb5
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 <pzijlstr@redhat.com>
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>
16 #include "internal.h"
18 static bool perf_output_space(struct ring_buffer *rb, unsigned long tail,
19 unsigned long offset, unsigned long head)
21 unsigned long mask;
23 if (!rb->writable)
24 return true;
26 mask = perf_data_size(rb) - 1;
28 offset = (offset - tail) & mask;
29 head = (head - tail) & mask;
31 if ((int)(head - offset) < 0)
32 return false;
34 return true;
37 static void perf_output_wakeup(struct perf_output_handle *handle)
39 atomic_set(&handle->rb->poll, POLL_IN);
41 handle->event->pending_wakeup = 1;
42 irq_work_queue(&handle->event->pending);
46 * We need to ensure a later event_id doesn't publish a head when a former
47 * event isn't done writing. However since we need to deal with NMIs we
48 * cannot fully serialize things.
50 * We only publish the head (and generate a wakeup) when the outer-most
51 * event completes.
53 static void perf_output_get_handle(struct perf_output_handle *handle)
55 struct ring_buffer *rb = handle->rb;
57 preempt_disable();
58 local_inc(&rb->nest);
59 handle->wakeup = local_read(&rb->wakeup);
62 static void perf_output_put_handle(struct perf_output_handle *handle)
64 struct ring_buffer *rb = handle->rb;
65 unsigned long head;
67 again:
68 head = local_read(&rb->head);
71 * IRQ/NMI can happen here, which means we can miss a head update.
74 if (!local_dec_and_test(&rb->nest))
75 goto out;
78 * Publish the known good head. Rely on the full barrier implied
79 * by atomic_dec_and_test() order the rb->head read and this
80 * write.
82 rb->user_page->data_head = head;
85 * Now check if we missed an update, rely on the (compiler)
86 * barrier in atomic_dec_and_test() to re-read rb->head.
88 if (unlikely(head != local_read(&rb->head))) {
89 local_inc(&rb->nest);
90 goto again;
93 if (handle->wakeup != local_read(&rb->wakeup))
94 perf_output_wakeup(handle);
96 out:
97 preempt_enable();
100 int perf_output_begin(struct perf_output_handle *handle,
101 struct perf_event *event, unsigned int size)
103 struct ring_buffer *rb;
104 unsigned long tail, offset, head;
105 int have_lost;
106 struct perf_sample_data sample_data;
107 struct {
108 struct perf_event_header header;
109 u64 id;
110 u64 lost;
111 } lost_event;
113 rcu_read_lock();
115 * For inherited events we send all the output towards the parent.
117 if (event->parent)
118 event = event->parent;
120 rb = rcu_dereference(event->rb);
121 if (!rb)
122 goto out;
124 handle->rb = rb;
125 handle->event = event;
127 if (!rb->nr_pages)
128 goto out;
130 have_lost = local_read(&rb->lost);
131 if (have_lost) {
132 lost_event.header.size = sizeof(lost_event);
133 perf_event_header__init_id(&lost_event.header, &sample_data,
134 event);
135 size += lost_event.header.size;
138 perf_output_get_handle(handle);
140 do {
142 * Userspace could choose to issue a mb() before updating the
143 * tail pointer. So that all reads will be completed before the
144 * write is issued.
146 tail = ACCESS_ONCE(rb->user_page->data_tail);
147 smp_rmb();
148 offset = head = local_read(&rb->head);
149 head += size;
150 if (unlikely(!perf_output_space(rb, tail, offset, head)))
151 goto fail;
152 } while (local_cmpxchg(&rb->head, offset, head) != offset);
154 if (head - local_read(&rb->wakeup) > rb->watermark)
155 local_add(rb->watermark, &rb->wakeup);
157 handle->page = offset >> (PAGE_SHIFT + page_order(rb));
158 handle->page &= rb->nr_pages - 1;
159 handle->size = offset & ((PAGE_SIZE << page_order(rb)) - 1);
160 handle->addr = rb->data_pages[handle->page];
161 handle->addr += handle->size;
162 handle->size = (PAGE_SIZE << page_order(rb)) - handle->size;
164 if (have_lost) {
165 lost_event.header.type = PERF_RECORD_LOST;
166 lost_event.header.misc = 0;
167 lost_event.id = event->id;
168 lost_event.lost = local_xchg(&rb->lost, 0);
170 perf_output_put(handle, lost_event);
171 perf_event__output_id_sample(event, handle, &sample_data);
174 return 0;
176 fail:
177 local_inc(&rb->lost);
178 perf_output_put_handle(handle);
179 out:
180 rcu_read_unlock();
182 return -ENOSPC;
185 void perf_output_copy(struct perf_output_handle *handle,
186 const void *buf, unsigned int len)
188 __output_copy(handle, buf, len);
191 void perf_output_end(struct perf_output_handle *handle)
193 perf_output_put_handle(handle);
194 rcu_read_unlock();
197 static void
198 ring_buffer_init(struct ring_buffer *rb, long watermark, int flags)
200 long max_size = perf_data_size(rb);
202 if (watermark)
203 rb->watermark = min(max_size, watermark);
205 if (!rb->watermark)
206 rb->watermark = max_size / 2;
208 if (flags & RING_BUFFER_WRITABLE)
209 rb->writable = 1;
211 atomic_set(&rb->refcount, 1);
213 INIT_LIST_HEAD(&rb->event_list);
214 spin_lock_init(&rb->event_lock);
217 #ifndef CONFIG_PERF_USE_VMALLOC
220 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
223 struct page *
224 perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff)
226 if (pgoff > rb->nr_pages)
227 return NULL;
229 if (pgoff == 0)
230 return virt_to_page(rb->user_page);
232 return virt_to_page(rb->data_pages[pgoff - 1]);
235 static void *perf_mmap_alloc_page(int cpu)
237 struct page *page;
238 int node;
240 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
241 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
242 if (!page)
243 return NULL;
245 return page_address(page);
248 struct ring_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags)
250 struct ring_buffer *rb;
251 unsigned long size;
252 int i;
254 size = sizeof(struct ring_buffer);
255 size += nr_pages * sizeof(void *);
257 rb = kzalloc(size, GFP_KERNEL);
258 if (!rb)
259 goto fail;
261 rb->user_page = perf_mmap_alloc_page(cpu);
262 if (!rb->user_page)
263 goto fail_user_page;
265 for (i = 0; i < nr_pages; i++) {
266 rb->data_pages[i] = perf_mmap_alloc_page(cpu);
267 if (!rb->data_pages[i])
268 goto fail_data_pages;
271 rb->nr_pages = nr_pages;
273 ring_buffer_init(rb, watermark, flags);
275 return rb;
277 fail_data_pages:
278 for (i--; i >= 0; i--)
279 free_page((unsigned long)rb->data_pages[i]);
281 free_page((unsigned long)rb->user_page);
283 fail_user_page:
284 kfree(rb);
286 fail:
287 return NULL;
290 static void perf_mmap_free_page(unsigned long addr)
292 struct page *page = virt_to_page((void *)addr);
294 page->mapping = NULL;
295 __free_page(page);
298 void rb_free(struct ring_buffer *rb)
300 int i;
302 perf_mmap_free_page((unsigned long)rb->user_page);
303 for (i = 0; i < rb->nr_pages; i++)
304 perf_mmap_free_page((unsigned long)rb->data_pages[i]);
305 kfree(rb);
308 #else
310 struct page *
311 perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff)
313 if (pgoff > (1UL << page_order(rb)))
314 return NULL;
316 return vmalloc_to_page((void *)rb->user_page + pgoff * PAGE_SIZE);
319 static void perf_mmap_unmark_page(void *addr)
321 struct page *page = vmalloc_to_page(addr);
323 page->mapping = NULL;
326 static void rb_free_work(struct work_struct *work)
328 struct ring_buffer *rb;
329 void *base;
330 int i, nr;
332 rb = container_of(work, struct ring_buffer, work);
333 nr = 1 << page_order(rb);
335 base = rb->user_page;
336 for (i = 0; i < nr + 1; i++)
337 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
339 vfree(base);
340 kfree(rb);
343 void rb_free(struct ring_buffer *rb)
345 schedule_work(&rb->work);
348 struct ring_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags)
350 struct ring_buffer *rb;
351 unsigned long size;
352 void *all_buf;
354 size = sizeof(struct ring_buffer);
355 size += sizeof(void *);
357 rb = kzalloc(size, GFP_KERNEL);
358 if (!rb)
359 goto fail;
361 INIT_WORK(&rb->work, rb_free_work);
363 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
364 if (!all_buf)
365 goto fail_all_buf;
367 rb->user_page = all_buf;
368 rb->data_pages[0] = all_buf + PAGE_SIZE;
369 rb->page_order = ilog2(nr_pages);
370 rb->nr_pages = 1;
372 ring_buffer_init(rb, watermark, flags);
374 return rb;
376 fail_all_buf:
377 kfree(rb);
379 fail:
380 return NULL;
383 #endif