blob: 6ed16ecfd0a38dc70681690674d73ebd5ff4584a [file] [log] [blame]
Frederic Weisbecker76369132011-05-19 19:55:04 +02001/*
2 * Performance events ring-buffer code:
3 *
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>
Al Virod36b6912011-12-29 17:09:01 -05007 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
Frederic Weisbecker76369132011-05-19 19:55:04 +02008 *
9 * For licensing details see kernel-base/COPYING
10 */
11
12#include <linux/perf_event.h>
13#include <linux/vmalloc.h>
14#include <linux/slab.h>
Peter Zijlstra26c86da2013-10-31 10:19:59 +010015#include <linux/circ_buf.h>
Frederic Weisbecker76369132011-05-19 19:55:04 +020016
17#include "internal.h"
18
Frederic Weisbecker76369132011-05-19 19:55:04 +020019static void perf_output_wakeup(struct perf_output_handle *handle)
20{
21 atomic_set(&handle->rb->poll, POLL_IN);
22
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +020023 handle->event->pending_wakeup = 1;
24 irq_work_queue(&handle->event->pending);
Frederic Weisbecker76369132011-05-19 19:55:04 +020025}
26
27/*
28 * We need to ensure a later event_id doesn't publish a head when a former
29 * event isn't done writing. However since we need to deal with NMIs we
30 * cannot fully serialize things.
31 *
32 * We only publish the head (and generate a wakeup) when the outer-most
33 * event completes.
34 */
35static void perf_output_get_handle(struct perf_output_handle *handle)
36{
37 struct ring_buffer *rb = handle->rb;
38
39 preempt_disable();
40 local_inc(&rb->nest);
41 handle->wakeup = local_read(&rb->wakeup);
42}
43
44static void perf_output_put_handle(struct perf_output_handle *handle)
45{
46 struct ring_buffer *rb = handle->rb;
47 unsigned long head;
48
49again:
50 head = local_read(&rb->head);
51
52 /*
53 * IRQ/NMI can happen here, which means we can miss a head update.
54 */
55
56 if (!local_dec_and_test(&rb->nest))
57 goto out;
58
59 /*
Peter Zijlstrabf378d32013-10-28 13:55:29 +010060 * Since the mmap() consumer (userspace) can run on a different CPU:
61 *
62 * kernel user
63 *
64 * READ ->data_tail READ ->data_head
65 * smp_mb() (A) smp_rmb() (C)
66 * WRITE $data READ $data
67 * smp_wmb() (B) smp_mb() (D)
68 * STORE ->data_head WRITE ->data_tail
69 *
70 * Where A pairs with D, and B pairs with C.
71 *
72 * I don't think A needs to be a full barrier because we won't in fact
73 * write data until we see the store from userspace. So we simply don't
74 * issue the data WRITE until we observe it. Be conservative for now.
75 *
76 * OTOH, D needs to be a full barrier since it separates the data READ
77 * from the tail WRITE.
78 *
79 * For B a WMB is sufficient since it separates two WRITEs, and for C
80 * an RMB is sufficient since it separates two READs.
81 *
82 * See perf_output_begin().
Frederic Weisbecker76369132011-05-19 19:55:04 +020083 */
Peter Zijlstrabf378d32013-10-28 13:55:29 +010084 smp_wmb();
Frederic Weisbecker76369132011-05-19 19:55:04 +020085 rb->user_page->data_head = head;
86
87 /*
88 * Now check if we missed an update, rely on the (compiler)
89 * barrier in atomic_dec_and_test() to re-read rb->head.
90 */
91 if (unlikely(head != local_read(&rb->head))) {
92 local_inc(&rb->nest);
93 goto again;
94 }
95
96 if (handle->wakeup != local_read(&rb->wakeup))
97 perf_output_wakeup(handle);
98
99out:
100 preempt_enable();
101}
102
103int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +0200104 struct perf_event *event, unsigned int size)
Frederic Weisbecker76369132011-05-19 19:55:04 +0200105{
106 struct ring_buffer *rb;
107 unsigned long tail, offset, head;
108 int have_lost;
109 struct perf_sample_data sample_data;
110 struct {
111 struct perf_event_header header;
112 u64 id;
113 u64 lost;
114 } lost_event;
115
116 rcu_read_lock();
117 /*
118 * For inherited events we send all the output towards the parent.
119 */
120 if (event->parent)
121 event = event->parent;
122
123 rb = rcu_dereference(event->rb);
Peter Zijlstrac72b42a2013-10-31 17:20:25 +0100124 if (unlikely(!rb))
Frederic Weisbecker76369132011-05-19 19:55:04 +0200125 goto out;
126
Peter Zijlstrac72b42a2013-10-31 17:20:25 +0100127 if (unlikely(!rb->nr_pages))
Frederic Weisbecker76369132011-05-19 19:55:04 +0200128 goto out;
129
Peter Zijlstrac72b42a2013-10-31 17:20:25 +0100130 handle->rb = rb;
131 handle->event = event;
132
Frederic Weisbecker76369132011-05-19 19:55:04 +0200133 have_lost = local_read(&rb->lost);
Peter Zijlstrac72b42a2013-10-31 17:20:25 +0100134 if (unlikely(have_lost)) {
Frederic Weisbecker76369132011-05-19 19:55:04 +0200135 lost_event.header.size = sizeof(lost_event);
136 perf_event_header__init_id(&lost_event.header, &sample_data,
137 event);
138 size += lost_event.header.size;
139 }
140
141 perf_output_get_handle(handle);
142
143 do {
Frederic Weisbecker76369132011-05-19 19:55:04 +0200144 tail = ACCESS_ONCE(rb->user_page->data_tail);
Frederic Weisbecker76369132011-05-19 19:55:04 +0200145 offset = head = local_read(&rb->head);
Peter Zijlstra26c86da2013-10-31 10:19:59 +0100146 if (!rb->overwrite &&
147 unlikely(CIRC_SPACE(head, tail, perf_data_size(rb)) < size))
Frederic Weisbecker76369132011-05-19 19:55:04 +0200148 goto fail;
Peter Zijlstra26c86da2013-10-31 10:19:59 +0100149 head += size;
Frederic Weisbecker76369132011-05-19 19:55:04 +0200150 } while (local_cmpxchg(&rb->head, offset, head) != offset);
151
Peter Zijlstra85f59ed2013-10-31 17:25:38 +0100152 /*
153 * Separate the userpage->tail read from the data stores below.
154 * Matches the MB userspace SHOULD issue after reading the data
155 * and before storing the new tail position.
156 *
157 * See perf_output_put_handle().
158 */
159 smp_mb();
160
Peter Zijlstrac72b42a2013-10-31 17:20:25 +0100161 if (unlikely(head - local_read(&rb->wakeup) > rb->watermark))
Frederic Weisbecker76369132011-05-19 19:55:04 +0200162 local_add(rb->watermark, &rb->wakeup);
163
164 handle->page = offset >> (PAGE_SHIFT + page_order(rb));
165 handle->page &= rb->nr_pages - 1;
166 handle->size = offset & ((PAGE_SIZE << page_order(rb)) - 1);
167 handle->addr = rb->data_pages[handle->page];
168 handle->addr += handle->size;
169 handle->size = (PAGE_SIZE << page_order(rb)) - handle->size;
170
Peter Zijlstrac72b42a2013-10-31 17:20:25 +0100171 if (unlikely(have_lost)) {
Frederic Weisbecker76369132011-05-19 19:55:04 +0200172 lost_event.header.type = PERF_RECORD_LOST;
173 lost_event.header.misc = 0;
174 lost_event.id = event->id;
175 lost_event.lost = local_xchg(&rb->lost, 0);
176
177 perf_output_put(handle, lost_event);
178 perf_event__output_id_sample(event, handle, &sample_data);
179 }
180
181 return 0;
182
183fail:
184 local_inc(&rb->lost);
185 perf_output_put_handle(handle);
186out:
187 rcu_read_unlock();
188
189 return -ENOSPC;
190}
191
Frederic Weisbecker91d77532012-08-07 15:20:38 +0200192unsigned int perf_output_copy(struct perf_output_handle *handle,
Frederic Weisbecker76369132011-05-19 19:55:04 +0200193 const void *buf, unsigned int len)
194{
Frederic Weisbecker91d77532012-08-07 15:20:38 +0200195 return __output_copy(handle, buf, len);
Frederic Weisbecker76369132011-05-19 19:55:04 +0200196}
197
Jiri Olsa5685e0f2012-08-07 15:20:39 +0200198unsigned int perf_output_skip(struct perf_output_handle *handle,
199 unsigned int len)
200{
201 return __output_skip(handle, NULL, len);
202}
203
Frederic Weisbecker76369132011-05-19 19:55:04 +0200204void perf_output_end(struct perf_output_handle *handle)
205{
Frederic Weisbecker76369132011-05-19 19:55:04 +0200206 perf_output_put_handle(handle);
207 rcu_read_unlock();
208}
209
210static void
211ring_buffer_init(struct ring_buffer *rb, long watermark, int flags)
212{
213 long max_size = perf_data_size(rb);
214
215 if (watermark)
216 rb->watermark = min(max_size, watermark);
217
218 if (!rb->watermark)
219 rb->watermark = max_size / 2;
220
221 if (flags & RING_BUFFER_WRITABLE)
Stephane Eraniandd9c0862013-03-18 14:33:28 +0100222 rb->overwrite = 0;
223 else
224 rb->overwrite = 1;
Frederic Weisbecker76369132011-05-19 19:55:04 +0200225
226 atomic_set(&rb->refcount, 1);
Peter Zijlstra10c6db12011-11-26 02:47:31 +0100227
228 INIT_LIST_HEAD(&rb->event_list);
229 spin_lock_init(&rb->event_lock);
Frederic Weisbecker76369132011-05-19 19:55:04 +0200230}
231
232#ifndef CONFIG_PERF_USE_VMALLOC
233
234/*
235 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
236 */
237
238struct page *
239perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff)
240{
241 if (pgoff > rb->nr_pages)
242 return NULL;
243
244 if (pgoff == 0)
245 return virt_to_page(rb->user_page);
246
247 return virt_to_page(rb->data_pages[pgoff - 1]);
248}
249
250static void *perf_mmap_alloc_page(int cpu)
251{
252 struct page *page;
253 int node;
254
255 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
256 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
257 if (!page)
258 return NULL;
259
260 return page_address(page);
261}
262
263struct ring_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags)
264{
265 struct ring_buffer *rb;
266 unsigned long size;
267 int i;
268
269 size = sizeof(struct ring_buffer);
270 size += nr_pages * sizeof(void *);
271
272 rb = kzalloc(size, GFP_KERNEL);
273 if (!rb)
274 goto fail;
275
276 rb->user_page = perf_mmap_alloc_page(cpu);
277 if (!rb->user_page)
278 goto fail_user_page;
279
280 for (i = 0; i < nr_pages; i++) {
281 rb->data_pages[i] = perf_mmap_alloc_page(cpu);
282 if (!rb->data_pages[i])
283 goto fail_data_pages;
284 }
285
286 rb->nr_pages = nr_pages;
287
288 ring_buffer_init(rb, watermark, flags);
289
290 return rb;
291
292fail_data_pages:
293 for (i--; i >= 0; i--)
294 free_page((unsigned long)rb->data_pages[i]);
295
296 free_page((unsigned long)rb->user_page);
297
298fail_user_page:
299 kfree(rb);
300
301fail:
302 return NULL;
303}
304
305static void perf_mmap_free_page(unsigned long addr)
306{
307 struct page *page = virt_to_page((void *)addr);
308
309 page->mapping = NULL;
310 __free_page(page);
311}
312
313void rb_free(struct ring_buffer *rb)
314{
315 int i;
316
317 perf_mmap_free_page((unsigned long)rb->user_page);
318 for (i = 0; i < rb->nr_pages; i++)
319 perf_mmap_free_page((unsigned long)rb->data_pages[i]);
320 kfree(rb);
321}
322
323#else
Jiri Olsa5919b302013-03-19 15:35:09 +0100324static int data_page_nr(struct ring_buffer *rb)
325{
326 return rb->nr_pages << page_order(rb);
327}
Frederic Weisbecker76369132011-05-19 19:55:04 +0200328
329struct page *
330perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff)
331{
Jiri Olsa5919b302013-03-19 15:35:09 +0100332 /* The '>' counts in the user page. */
333 if (pgoff > data_page_nr(rb))
Frederic Weisbecker76369132011-05-19 19:55:04 +0200334 return NULL;
335
336 return vmalloc_to_page((void *)rb->user_page + pgoff * PAGE_SIZE);
337}
338
339static void perf_mmap_unmark_page(void *addr)
340{
341 struct page *page = vmalloc_to_page(addr);
342
343 page->mapping = NULL;
344}
345
346static void rb_free_work(struct work_struct *work)
347{
348 struct ring_buffer *rb;
349 void *base;
350 int i, nr;
351
352 rb = container_of(work, struct ring_buffer, work);
Jiri Olsa5919b302013-03-19 15:35:09 +0100353 nr = data_page_nr(rb);
Frederic Weisbecker76369132011-05-19 19:55:04 +0200354
355 base = rb->user_page;
Jiri Olsa5919b302013-03-19 15:35:09 +0100356 /* The '<=' counts in the user page. */
357 for (i = 0; i <= nr; i++)
Frederic Weisbecker76369132011-05-19 19:55:04 +0200358 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
359
360 vfree(base);
361 kfree(rb);
362}
363
364void rb_free(struct ring_buffer *rb)
365{
366 schedule_work(&rb->work);
367}
368
369struct ring_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags)
370{
371 struct ring_buffer *rb;
372 unsigned long size;
373 void *all_buf;
374
375 size = sizeof(struct ring_buffer);
376 size += sizeof(void *);
377
378 rb = kzalloc(size, GFP_KERNEL);
379 if (!rb)
380 goto fail;
381
382 INIT_WORK(&rb->work, rb_free_work);
383
384 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
385 if (!all_buf)
386 goto fail_all_buf;
387
388 rb->user_page = all_buf;
389 rb->data_pages[0] = all_buf + PAGE_SIZE;
390 rb->page_order = ilog2(nr_pages);
Jiri Olsa5919b302013-03-19 15:35:09 +0100391 rb->nr_pages = !!nr_pages;
Frederic Weisbecker76369132011-05-19 19:55:04 +0200392
393 ring_buffer_init(rb, watermark, flags);
394
395 return rb;
396
397fail_all_buf:
398 kfree(rb);
399
400fail:
401 return NULL;
402}
403
404#endif