Commit | Line | Data |
---|---|---|
8e86e015 | 1 | // SPDX-License-Identifier: GPL-2.0 |
76369139 FW |
2 | /* |
3 | * Performance events ring-buffer code: | |
4 | * | |
5 | * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de> | |
6 | * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar | |
90eec103 | 7 | * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra |
d36b6910 | 8 | * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com> |
76369139 FW |
9 | */ |
10 | ||
11 | #include <linux/perf_event.h> | |
12 | #include <linux/vmalloc.h> | |
13 | #include <linux/slab.h> | |
26c86da8 | 14 | #include <linux/circ_buf.h> |
7c60fc0e | 15 | #include <linux/poll.h> |
4411ec1d | 16 | #include <linux/nospec.h> |
76369139 FW |
17 | |
18 | #include "internal.h" | |
19 | ||
76369139 FW |
20 | static void perf_output_wakeup(struct perf_output_handle *handle) |
21 | { | |
a9a08845 | 22 | atomic_set(&handle->rb->poll, EPOLLIN); |
76369139 | 23 | |
a8b0ca17 PZ |
24 | handle->event->pending_wakeup = 1; |
25 | irq_work_queue(&handle->event->pending); | |
76369139 FW |
26 | } |
27 | ||
28 | /* | |
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. | |
32 | * | |
33 | * We only publish the head (and generate a wakeup) when the outer-most | |
34 | * event completes. | |
35 | */ | |
36 | static void perf_output_get_handle(struct perf_output_handle *handle) | |
37 | { | |
38 | struct ring_buffer *rb = handle->rb; | |
39 | ||
40 | preempt_disable(); | |
41 | local_inc(&rb->nest); | |
42 | handle->wakeup = local_read(&rb->wakeup); | |
43 | } | |
44 | ||
45 | static void perf_output_put_handle(struct perf_output_handle *handle) | |
46 | { | |
47 | struct ring_buffer *rb = handle->rb; | |
48 | unsigned long head; | |
49 | ||
50 | again: | |
51 | head = local_read(&rb->head); | |
52 | ||
53 | /* | |
1b038c6e YC |
54 | * IRQ/NMI can happen here and advance @rb->head, causing our |
55 | * load above to be stale. | |
76369139 FW |
56 | */ |
57 | ||
1b038c6e YC |
58 | /* |
59 | * If this isn't the outermost nesting, we don't have to update | |
60 | * @rb->user_page->data_head. | |
61 | */ | |
62 | if (local_read(&rb->nest) > 1) { | |
63 | local_dec(&rb->nest); | |
76369139 | 64 | goto out; |
1b038c6e | 65 | } |
76369139 FW |
66 | |
67 | /* | |
bf378d34 PZ |
68 | * Since the mmap() consumer (userspace) can run on a different CPU: |
69 | * | |
70 | * kernel user | |
71 | * | |
c7f2e3cd PZ |
72 | * if (LOAD ->data_tail) { LOAD ->data_head |
73 | * (A) smp_rmb() (C) | |
74 | * STORE $data LOAD $data | |
75 | * smp_wmb() (B) smp_mb() (D) | |
76 | * STORE ->data_head STORE ->data_tail | |
77 | * } | |
bf378d34 PZ |
78 | * |
79 | * Where A pairs with D, and B pairs with C. | |
80 | * | |
c7f2e3cd PZ |
81 | * In our case (A) is a control dependency that separates the load of |
82 | * the ->data_tail and the stores of $data. In case ->data_tail | |
83 | * indicates there is no room in the buffer to store $data we do not. | |
bf378d34 | 84 | * |
c7f2e3cd | 85 | * D needs to be a full barrier since it separates the data READ |
bf378d34 PZ |
86 | * from the tail WRITE. |
87 | * | |
88 | * For B a WMB is sufficient since it separates two WRITEs, and for C | |
89 | * an RMB is sufficient since it separates two READs. | |
90 | * | |
91 | * See perf_output_begin(). | |
76369139 | 92 | */ |
c7f2e3cd | 93 | smp_wmb(); /* B, matches C */ |
76369139 FW |
94 | rb->user_page->data_head = head; |
95 | ||
96 | /* | |
1b038c6e YC |
97 | * We must publish the head before decrementing the nest count, |
98 | * otherwise an IRQ/NMI can publish a more recent head value and our | |
99 | * write will (temporarily) publish a stale value. | |
100 | */ | |
101 | barrier(); | |
102 | local_set(&rb->nest, 0); | |
103 | ||
104 | /* | |
105 | * Ensure we decrement @rb->nest before we validate the @rb->head. | |
106 | * Otherwise we cannot be sure we caught the 'last' nested update. | |
76369139 | 107 | */ |
1b038c6e | 108 | barrier(); |
76369139 FW |
109 | if (unlikely(head != local_read(&rb->head))) { |
110 | local_inc(&rb->nest); | |
111 | goto again; | |
112 | } | |
113 | ||
114 | if (handle->wakeup != local_read(&rb->wakeup)) | |
115 | perf_output_wakeup(handle); | |
116 | ||
117 | out: | |
118 | preempt_enable(); | |
119 | } | |
120 | ||
57d6a793 | 121 | static __always_inline bool |
d1b26c70 WN |
122 | ring_buffer_has_space(unsigned long head, unsigned long tail, |
123 | unsigned long data_size, unsigned int size, | |
124 | bool backward) | |
125 | { | |
126 | if (!backward) | |
127 | return CIRC_SPACE(head, tail, data_size) >= size; | |
128 | else | |
129 | return CIRC_SPACE(tail, head, data_size) >= size; | |
130 | } | |
131 | ||
57d6a793 | 132 | static __always_inline int |
d1b26c70 WN |
133 | __perf_output_begin(struct perf_output_handle *handle, |
134 | struct perf_event *event, unsigned int size, | |
135 | bool backward) | |
76369139 FW |
136 | { |
137 | struct ring_buffer *rb; | |
138 | unsigned long tail, offset, head; | |
524feca5 | 139 | int have_lost, page_shift; |
76369139 FW |
140 | struct { |
141 | struct perf_event_header header; | |
142 | u64 id; | |
143 | u64 lost; | |
144 | } lost_event; | |
145 | ||
146 | rcu_read_lock(); | |
147 | /* | |
148 | * For inherited events we send all the output towards the parent. | |
149 | */ | |
150 | if (event->parent) | |
151 | event = event->parent; | |
152 | ||
153 | rb = rcu_dereference(event->rb); | |
c72b42a3 | 154 | if (unlikely(!rb)) |
76369139 FW |
155 | goto out; |
156 | ||
86e7972f WN |
157 | if (unlikely(rb->paused)) { |
158 | if (rb->nr_pages) | |
159 | local_inc(&rb->lost); | |
76369139 | 160 | goto out; |
86e7972f | 161 | } |
76369139 | 162 | |
c72b42a3 PZ |
163 | handle->rb = rb; |
164 | handle->event = event; | |
165 | ||
76369139 | 166 | have_lost = local_read(&rb->lost); |
c72b42a3 | 167 | if (unlikely(have_lost)) { |
d20a973f PZ |
168 | size += sizeof(lost_event); |
169 | if (event->attr.sample_id_all) | |
170 | size += event->id_header_size; | |
76369139 FW |
171 | } |
172 | ||
173 | perf_output_get_handle(handle); | |
174 | ||
175 | do { | |
105ff3cb | 176 | tail = READ_ONCE(rb->user_page->data_tail); |
76369139 | 177 | offset = head = local_read(&rb->head); |
d1b26c70 WN |
178 | if (!rb->overwrite) { |
179 | if (unlikely(!ring_buffer_has_space(head, tail, | |
180 | perf_data_size(rb), | |
181 | size, backward))) | |
182 | goto fail; | |
183 | } | |
c7f2e3cd PZ |
184 | |
185 | /* | |
186 | * The above forms a control dependency barrier separating the | |
187 | * @tail load above from the data stores below. Since the @tail | |
188 | * load is required to compute the branch to fail below. | |
189 | * | |
190 | * A, matches D; the full memory barrier userspace SHOULD issue | |
191 | * after reading the data and before storing the new tail | |
192 | * position. | |
193 | * | |
194 | * See perf_output_put_handle(). | |
195 | */ | |
196 | ||
d1b26c70 WN |
197 | if (!backward) |
198 | head += size; | |
199 | else | |
200 | head -= size; | |
76369139 FW |
201 | } while (local_cmpxchg(&rb->head, offset, head) != offset); |
202 | ||
d1b26c70 WN |
203 | if (backward) { |
204 | offset = head; | |
205 | head = (u64)(-head); | |
206 | } | |
207 | ||
85f59edf | 208 | /* |
c7f2e3cd PZ |
209 | * We rely on the implied barrier() by local_cmpxchg() to ensure |
210 | * none of the data stores below can be lifted up by the compiler. | |
85f59edf | 211 | */ |
85f59edf | 212 | |
c72b42a3 | 213 | if (unlikely(head - local_read(&rb->wakeup) > rb->watermark)) |
76369139 FW |
214 | local_add(rb->watermark, &rb->wakeup); |
215 | ||
524feca5 PZ |
216 | page_shift = PAGE_SHIFT + page_order(rb); |
217 | ||
218 | handle->page = (offset >> page_shift) & (rb->nr_pages - 1); | |
219 | offset &= (1UL << page_shift) - 1; | |
220 | handle->addr = rb->data_pages[handle->page] + offset; | |
221 | handle->size = (1UL << page_shift) - offset; | |
76369139 | 222 | |
c72b42a3 | 223 | if (unlikely(have_lost)) { |
d20a973f PZ |
224 | struct perf_sample_data sample_data; |
225 | ||
226 | lost_event.header.size = sizeof(lost_event); | |
76369139 FW |
227 | lost_event.header.type = PERF_RECORD_LOST; |
228 | lost_event.header.misc = 0; | |
229 | lost_event.id = event->id; | |
230 | lost_event.lost = local_xchg(&rb->lost, 0); | |
231 | ||
d20a973f PZ |
232 | perf_event_header__init_id(&lost_event.header, |
233 | &sample_data, event); | |
76369139 FW |
234 | perf_output_put(handle, lost_event); |
235 | perf_event__output_id_sample(event, handle, &sample_data); | |
236 | } | |
237 | ||
238 | return 0; | |
239 | ||
240 | fail: | |
241 | local_inc(&rb->lost); | |
242 | perf_output_put_handle(handle); | |
243 | out: | |
244 | rcu_read_unlock(); | |
245 | ||
246 | return -ENOSPC; | |
247 | } | |
248 | ||
9ecda41a WN |
249 | int perf_output_begin_forward(struct perf_output_handle *handle, |
250 | struct perf_event *event, unsigned int size) | |
251 | { | |
252 | return __perf_output_begin(handle, event, size, false); | |
253 | } | |
254 | ||
255 | int perf_output_begin_backward(struct perf_output_handle *handle, | |
256 | struct perf_event *event, unsigned int size) | |
257 | { | |
258 | return __perf_output_begin(handle, event, size, true); | |
259 | } | |
260 | ||
d1b26c70 WN |
261 | int perf_output_begin(struct perf_output_handle *handle, |
262 | struct perf_event *event, unsigned int size) | |
263 | { | |
9ecda41a WN |
264 | |
265 | return __perf_output_begin(handle, event, size, | |
266 | unlikely(is_write_backward(event))); | |
d1b26c70 WN |
267 | } |
268 | ||
91d7753a | 269 | unsigned int perf_output_copy(struct perf_output_handle *handle, |
76369139 FW |
270 | const void *buf, unsigned int len) |
271 | { | |
91d7753a | 272 | return __output_copy(handle, buf, len); |
76369139 FW |
273 | } |
274 | ||
5685e0ff JO |
275 | unsigned int perf_output_skip(struct perf_output_handle *handle, |
276 | unsigned int len) | |
277 | { | |
278 | return __output_skip(handle, NULL, len); | |
279 | } | |
280 | ||
76369139 FW |
281 | void perf_output_end(struct perf_output_handle *handle) |
282 | { | |
76369139 FW |
283 | perf_output_put_handle(handle); |
284 | rcu_read_unlock(); | |
285 | } | |
286 | ||
287 | static void | |
288 | ring_buffer_init(struct ring_buffer *rb, long watermark, int flags) | |
289 | { | |
290 | long max_size = perf_data_size(rb); | |
291 | ||
292 | if (watermark) | |
293 | rb->watermark = min(max_size, watermark); | |
294 | ||
295 | if (!rb->watermark) | |
296 | rb->watermark = max_size / 2; | |
297 | ||
298 | if (flags & RING_BUFFER_WRITABLE) | |
dd9c086d SE |
299 | rb->overwrite = 0; |
300 | else | |
301 | rb->overwrite = 1; | |
76369139 | 302 | |
fecb8ed2 | 303 | refcount_set(&rb->refcount, 1); |
10c6db11 PZ |
304 | |
305 | INIT_LIST_HEAD(&rb->event_list); | |
306 | spin_lock_init(&rb->event_lock); | |
86e7972f WN |
307 | |
308 | /* | |
309 | * perf_output_begin() only checks rb->paused, therefore | |
310 | * rb->paused must be true if we have no pages for output. | |
311 | */ | |
312 | if (!rb->nr_pages) | |
313 | rb->paused = 1; | |
76369139 FW |
314 | } |
315 | ||
f4c0b0aa WD |
316 | void perf_aux_output_flag(struct perf_output_handle *handle, u64 flags) |
317 | { | |
318 | /* | |
319 | * OVERWRITE is determined by perf_aux_output_end() and can't | |
320 | * be passed in directly. | |
321 | */ | |
322 | if (WARN_ON_ONCE(flags & PERF_AUX_FLAG_OVERWRITE)) | |
323 | return; | |
324 | ||
325 | handle->aux_flags |= flags; | |
326 | } | |
327 | EXPORT_SYMBOL_GPL(perf_aux_output_flag); | |
328 | ||
fdc26706 AS |
329 | /* |
330 | * This is called before hardware starts writing to the AUX area to | |
331 | * obtain an output handle and make sure there's room in the buffer. | |
332 | * When the capture completes, call perf_aux_output_end() to commit | |
333 | * the recorded data to the buffer. | |
334 | * | |
335 | * The ordering is similar to that of perf_output_{begin,end}, with | |
336 | * the exception of (B), which should be taken care of by the pmu | |
337 | * driver, since ordering rules will differ depending on hardware. | |
af5bb4ed AS |
338 | * |
339 | * Call this from pmu::start(); see the comment in perf_aux_output_end() | |
340 | * about its use in pmu callbacks. Both can also be called from the PMI | |
341 | * handler if needed. | |
fdc26706 AS |
342 | */ |
343 | void *perf_aux_output_begin(struct perf_output_handle *handle, | |
344 | struct perf_event *event) | |
345 | { | |
346 | struct perf_event *output_event = event; | |
347 | unsigned long aux_head, aux_tail; | |
348 | struct ring_buffer *rb; | |
349 | ||
350 | if (output_event->parent) | |
351 | output_event = output_event->parent; | |
352 | ||
353 | /* | |
354 | * Since this will typically be open across pmu::add/pmu::del, we | |
355 | * grab ring_buffer's refcount instead of holding rcu read lock | |
356 | * to make sure it doesn't disappear under us. | |
357 | */ | |
358 | rb = ring_buffer_get(output_event); | |
359 | if (!rb) | |
360 | return NULL; | |
361 | ||
b79ccadd | 362 | if (!rb_has_aux(rb)) |
fdc26706 AS |
363 | goto err; |
364 | ||
dcb10a96 | 365 | /* |
b79ccadd AS |
366 | * If aux_mmap_count is zero, the aux buffer is in perf_mmap_close(), |
367 | * about to get freed, so we leave immediately. | |
368 | * | |
369 | * Checking rb::aux_mmap_count and rb::refcount has to be done in | |
370 | * the same order, see perf_mmap_close. Otherwise we end up freeing | |
371 | * aux pages in this path, which is a bug, because in_atomic(). | |
dcb10a96 AS |
372 | */ |
373 | if (!atomic_read(&rb->aux_mmap_count)) | |
b79ccadd AS |
374 | goto err; |
375 | ||
ca3bb3d0 | 376 | if (!refcount_inc_not_zero(&rb->aux_refcount)) |
b79ccadd | 377 | goto err; |
dcb10a96 | 378 | |
fdc26706 AS |
379 | /* |
380 | * Nesting is not supported for AUX area, make sure nested | |
381 | * writers are caught early | |
382 | */ | |
383 | if (WARN_ON_ONCE(local_xchg(&rb->aux_nest, 1))) | |
384 | goto err_put; | |
385 | ||
2ab346cf | 386 | aux_head = rb->aux_head; |
fdc26706 AS |
387 | |
388 | handle->rb = rb; | |
389 | handle->event = event; | |
390 | handle->head = aux_head; | |
2023a0d2 | 391 | handle->size = 0; |
f4c0b0aa | 392 | handle->aux_flags = 0; |
fdc26706 AS |
393 | |
394 | /* | |
2023a0d2 AS |
395 | * In overwrite mode, AUX data stores do not depend on aux_tail, |
396 | * therefore (A) control dependency barrier does not exist. The | |
397 | * (B) <-> (C) ordering is still observed by the pmu driver. | |
fdc26706 | 398 | */ |
2023a0d2 | 399 | if (!rb->aux_overwrite) { |
6aa7de05 | 400 | aux_tail = READ_ONCE(rb->user_page->aux_tail); |
2ab346cf | 401 | handle->wakeup = rb->aux_wakeup + rb->aux_watermark; |
2023a0d2 AS |
402 | if (aux_head - aux_tail < perf_aux_size(rb)) |
403 | handle->size = CIRC_SPACE(aux_head, aux_tail, perf_aux_size(rb)); | |
404 | ||
405 | /* | |
406 | * handle->size computation depends on aux_tail load; this forms a | |
407 | * control dependency barrier separating aux_tail load from aux data | |
408 | * store that will be enabled on successful return | |
409 | */ | |
410 | if (!handle->size) { /* A, matches D */ | |
1d54ad94 | 411 | event->pending_disable = smp_processor_id(); |
2023a0d2 AS |
412 | perf_output_wakeup(handle); |
413 | local_set(&rb->aux_nest, 0); | |
414 | goto err_put; | |
415 | } | |
fdc26706 AS |
416 | } |
417 | ||
418 | return handle->rb->aux_priv; | |
419 | ||
420 | err_put: | |
af5bb4ed | 421 | /* can't be last */ |
fdc26706 AS |
422 | rb_free_aux(rb); |
423 | ||
424 | err: | |
95ff4ca2 | 425 | ring_buffer_put(rb); |
fdc26706 AS |
426 | handle->event = NULL; |
427 | ||
428 | return NULL; | |
429 | } | |
bc1d2020 | 430 | EXPORT_SYMBOL_GPL(perf_aux_output_begin); |
fdc26706 | 431 | |
57d6a793 | 432 | static __always_inline bool rb_need_aux_wakeup(struct ring_buffer *rb) |
441430eb AS |
433 | { |
434 | if (rb->aux_overwrite) | |
435 | return false; | |
436 | ||
437 | if (rb->aux_head - rb->aux_wakeup >= rb->aux_watermark) { | |
438 | rb->aux_wakeup = rounddown(rb->aux_head, rb->aux_watermark); | |
439 | return true; | |
440 | } | |
441 | ||
442 | return false; | |
443 | } | |
444 | ||
fdc26706 AS |
445 | /* |
446 | * Commit the data written by hardware into the ring buffer by adjusting | |
447 | * aux_head and posting a PERF_RECORD_AUX into the perf buffer. It is the | |
448 | * pmu driver's responsibility to observe ordering rules of the hardware, | |
449 | * so that all the data is externally visible before this is called. | |
af5bb4ed AS |
450 | * |
451 | * Note: this has to be called from pmu::stop() callback, as the assumption | |
452 | * of the AUX buffer management code is that after pmu::stop(), the AUX | |
453 | * transaction must be stopped and therefore drop the AUX reference count. | |
fdc26706 | 454 | */ |
f4c0b0aa | 455 | void perf_aux_output_end(struct perf_output_handle *handle, unsigned long size) |
fdc26706 | 456 | { |
ae0c2d99 | 457 | bool wakeup = !!(handle->aux_flags & PERF_AUX_FLAG_TRUNCATED); |
fdc26706 | 458 | struct ring_buffer *rb = handle->rb; |
2023a0d2 | 459 | unsigned long aux_head; |
fdc26706 | 460 | |
2023a0d2 AS |
461 | /* in overwrite mode, driver provides aux_head via handle */ |
462 | if (rb->aux_overwrite) { | |
f4c0b0aa | 463 | handle->aux_flags |= PERF_AUX_FLAG_OVERWRITE; |
2023a0d2 AS |
464 | |
465 | aux_head = handle->head; | |
2ab346cf | 466 | rb->aux_head = aux_head; |
2023a0d2 | 467 | } else { |
f4c0b0aa WD |
468 | handle->aux_flags &= ~PERF_AUX_FLAG_OVERWRITE; |
469 | ||
2ab346cf WD |
470 | aux_head = rb->aux_head; |
471 | rb->aux_head += size; | |
2023a0d2 | 472 | } |
fdc26706 | 473 | |
339bc418 AS |
474 | /* |
475 | * Only send RECORD_AUX if we have something useful to communicate | |
476 | * | |
477 | * Note: the OVERWRITE records by themselves are not considered | |
478 | * useful, as they don't communicate any *new* information, | |
479 | * aside from the short-lived offset, that becomes history at | |
480 | * the next event sched-in and therefore isn't useful. | |
481 | * The userspace that needs to copy out AUX data in overwrite | |
482 | * mode should know to use user_page::aux_head for the actual | |
483 | * offset. So, from now on we don't output AUX records that | |
484 | * have *only* OVERWRITE flag set. | |
485 | */ | |
486 | if (size || (handle->aux_flags & ~(u64)PERF_AUX_FLAG_OVERWRITE)) | |
487 | perf_event_aux_event(handle->event, aux_head, size, | |
488 | handle->aux_flags); | |
fdc26706 | 489 | |
2ab346cf | 490 | rb->user_page->aux_head = rb->aux_head; |
441430eb | 491 | if (rb_need_aux_wakeup(rb)) |
3f56e687 | 492 | wakeup = true; |
3f56e687 AS |
493 | |
494 | if (wakeup) { | |
f4c0b0aa | 495 | if (handle->aux_flags & PERF_AUX_FLAG_TRUNCATED) |
1d54ad94 | 496 | handle->event->pending_disable = smp_processor_id(); |
3f56e687 AS |
497 | perf_output_wakeup(handle); |
498 | } | |
499 | ||
fdc26706 AS |
500 | handle->event = NULL; |
501 | ||
502 | local_set(&rb->aux_nest, 0); | |
af5bb4ed | 503 | /* can't be last */ |
fdc26706 | 504 | rb_free_aux(rb); |
95ff4ca2 | 505 | ring_buffer_put(rb); |
fdc26706 | 506 | } |
bc1d2020 | 507 | EXPORT_SYMBOL_GPL(perf_aux_output_end); |
fdc26706 AS |
508 | |
509 | /* | |
510 | * Skip over a given number of bytes in the AUX buffer, due to, for example, | |
511 | * hardware's alignment constraints. | |
512 | */ | |
513 | int perf_aux_output_skip(struct perf_output_handle *handle, unsigned long size) | |
514 | { | |
515 | struct ring_buffer *rb = handle->rb; | |
fdc26706 AS |
516 | |
517 | if (size > handle->size) | |
518 | return -ENOSPC; | |
519 | ||
2ab346cf | 520 | rb->aux_head += size; |
fdc26706 | 521 | |
2ab346cf | 522 | rb->user_page->aux_head = rb->aux_head; |
441430eb | 523 | if (rb_need_aux_wakeup(rb)) { |
1a594131 | 524 | perf_output_wakeup(handle); |
2ab346cf | 525 | handle->wakeup = rb->aux_wakeup + rb->aux_watermark; |
1a594131 AS |
526 | } |
527 | ||
2ab346cf | 528 | handle->head = rb->aux_head; |
fdc26706 AS |
529 | handle->size -= size; |
530 | ||
531 | return 0; | |
532 | } | |
bc1d2020 | 533 | EXPORT_SYMBOL_GPL(perf_aux_output_skip); |
fdc26706 AS |
534 | |
535 | void *perf_get_aux(struct perf_output_handle *handle) | |
536 | { | |
537 | /* this is only valid between perf_aux_output_begin and *_end */ | |
538 | if (!handle->event) | |
539 | return NULL; | |
540 | ||
541 | return handle->rb->aux_priv; | |
542 | } | |
bc1d2020 | 543 | EXPORT_SYMBOL_GPL(perf_get_aux); |
fdc26706 | 544 | |
0a4e38e6 AS |
545 | #define PERF_AUX_GFP (GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY) |
546 | ||
547 | static struct page *rb_alloc_aux_page(int node, int order) | |
548 | { | |
549 | struct page *page; | |
550 | ||
551 | if (order > MAX_ORDER) | |
552 | order = MAX_ORDER; | |
553 | ||
554 | do { | |
555 | page = alloc_pages_node(node, PERF_AUX_GFP, order); | |
556 | } while (!page && order--); | |
557 | ||
558 | if (page && order) { | |
559 | /* | |
c2ad6b51 AS |
560 | * Communicate the allocation size to the driver: |
561 | * if we managed to secure a high-order allocation, | |
562 | * set its first page's private to this order; | |
563 | * !PagePrivate(page) means it's just a normal page. | |
0a4e38e6 AS |
564 | */ |
565 | split_page(page, order); | |
566 | SetPagePrivate(page); | |
567 | set_page_private(page, order); | |
568 | } | |
569 | ||
570 | return page; | |
571 | } | |
572 | ||
573 | static void rb_free_aux_page(struct ring_buffer *rb, int idx) | |
574 | { | |
575 | struct page *page = virt_to_page(rb->aux_pages[idx]); | |
576 | ||
577 | ClearPagePrivate(page); | |
578 | page->mapping = NULL; | |
579 | __free_page(page); | |
580 | } | |
581 | ||
45c815f0 AS |
582 | static void __rb_free_aux(struct ring_buffer *rb) |
583 | { | |
584 | int pg; | |
585 | ||
95ff4ca2 AS |
586 | /* |
587 | * Should never happen, the last reference should be dropped from | |
588 | * perf_mmap_close() path, which first stops aux transactions (which | |
589 | * in turn are the atomic holders of aux_refcount) and then does the | |
590 | * last rb_free_aux(). | |
591 | */ | |
592 | WARN_ON_ONCE(in_atomic()); | |
593 | ||
45c815f0 AS |
594 | if (rb->aux_priv) { |
595 | rb->free_aux(rb->aux_priv); | |
596 | rb->free_aux = NULL; | |
597 | rb->aux_priv = NULL; | |
598 | } | |
599 | ||
600 | if (rb->aux_nr_pages) { | |
601 | for (pg = 0; pg < rb->aux_nr_pages; pg++) | |
602 | rb_free_aux_page(rb, pg); | |
603 | ||
604 | kfree(rb->aux_pages); | |
605 | rb->aux_nr_pages = 0; | |
606 | } | |
607 | } | |
608 | ||
45bfb2e5 | 609 | int rb_alloc_aux(struct ring_buffer *rb, struct perf_event *event, |
1a594131 | 610 | pgoff_t pgoff, int nr_pages, long watermark, int flags) |
45bfb2e5 PZ |
611 | { |
612 | bool overwrite = !(flags & RING_BUFFER_WRITABLE); | |
613 | int node = (event->cpu == -1) ? -1 : cpu_to_node(event->cpu); | |
5768402f | 614 | int ret = -ENOMEM, max_order; |
45bfb2e5 PZ |
615 | |
616 | if (!has_aux(event)) | |
8a1898db | 617 | return -EOPNOTSUPP; |
45bfb2e5 | 618 | |
5768402f AS |
619 | /* |
620 | * We need to start with the max_order that fits in nr_pages, | |
621 | * not the other way around, hence ilog2() and not get_order. | |
622 | */ | |
623 | max_order = ilog2(nr_pages); | |
0a4e38e6 | 624 | |
5768402f AS |
625 | /* |
626 | * PMU requests more than one contiguous chunks of memory | |
627 | * for SW double buffering | |
628 | */ | |
26ae4f44 | 629 | if (!overwrite) { |
5768402f AS |
630 | if (!max_order) |
631 | return -EINVAL; | |
6a279230 | 632 | |
5768402f | 633 | max_order--; |
6a279230 AS |
634 | } |
635 | ||
590b5b7d KC |
636 | rb->aux_pages = kcalloc_node(nr_pages, sizeof(void *), GFP_KERNEL, |
637 | node); | |
45bfb2e5 PZ |
638 | if (!rb->aux_pages) |
639 | return -ENOMEM; | |
640 | ||
641 | rb->free_aux = event->pmu->free_aux; | |
0a4e38e6 | 642 | for (rb->aux_nr_pages = 0; rb->aux_nr_pages < nr_pages;) { |
45bfb2e5 | 643 | struct page *page; |
0a4e38e6 | 644 | int last, order; |
45bfb2e5 | 645 | |
0a4e38e6 AS |
646 | order = min(max_order, ilog2(nr_pages - rb->aux_nr_pages)); |
647 | page = rb_alloc_aux_page(node, order); | |
45bfb2e5 PZ |
648 | if (!page) |
649 | goto out; | |
650 | ||
0a4e38e6 AS |
651 | for (last = rb->aux_nr_pages + (1 << page_private(page)); |
652 | last > rb->aux_nr_pages; rb->aux_nr_pages++) | |
653 | rb->aux_pages[rb->aux_nr_pages] = page_address(page++); | |
45bfb2e5 PZ |
654 | } |
655 | ||
aa319bcd AS |
656 | /* |
657 | * In overwrite mode, PMUs that don't support SG may not handle more | |
658 | * than one contiguous allocation, since they rely on PMI to do double | |
659 | * buffering. In this case, the entire buffer has to be one contiguous | |
660 | * chunk. | |
661 | */ | |
662 | if ((event->pmu->capabilities & PERF_PMU_CAP_AUX_NO_SG) && | |
663 | overwrite) { | |
664 | struct page *page = virt_to_page(rb->aux_pages[0]); | |
665 | ||
666 | if (page_private(page) != max_order) | |
667 | goto out; | |
668 | } | |
669 | ||
84001866 | 670 | rb->aux_priv = event->pmu->setup_aux(event, rb->aux_pages, nr_pages, |
45bfb2e5 PZ |
671 | overwrite); |
672 | if (!rb->aux_priv) | |
673 | goto out; | |
674 | ||
675 | ret = 0; | |
676 | ||
677 | /* | |
678 | * aux_pages (and pmu driver's private data, aux_priv) will be | |
679 | * referenced in both producer's and consumer's contexts, thus | |
680 | * we keep a refcount here to make sure either of the two can | |
681 | * reference them safely. | |
682 | */ | |
ca3bb3d0 | 683 | refcount_set(&rb->aux_refcount, 1); |
45bfb2e5 | 684 | |
2023a0d2 | 685 | rb->aux_overwrite = overwrite; |
1a594131 AS |
686 | rb->aux_watermark = watermark; |
687 | ||
688 | if (!rb->aux_watermark && !rb->aux_overwrite) | |
689 | rb->aux_watermark = nr_pages << (PAGE_SHIFT - 1); | |
2023a0d2 | 690 | |
45bfb2e5 PZ |
691 | out: |
692 | if (!ret) | |
693 | rb->aux_pgoff = pgoff; | |
694 | else | |
45c815f0 | 695 | __rb_free_aux(rb); |
45bfb2e5 PZ |
696 | |
697 | return ret; | |
698 | } | |
699 | ||
45bfb2e5 PZ |
700 | void rb_free_aux(struct ring_buffer *rb) |
701 | { | |
ca3bb3d0 | 702 | if (refcount_dec_and_test(&rb->aux_refcount)) |
45bfb2e5 PZ |
703 | __rb_free_aux(rb); |
704 | } | |
705 | ||
76369139 FW |
706 | #ifndef CONFIG_PERF_USE_VMALLOC |
707 | ||
708 | /* | |
709 | * Back perf_mmap() with regular GFP_KERNEL-0 pages. | |
710 | */ | |
711 | ||
45bfb2e5 PZ |
712 | static struct page * |
713 | __perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff) | |
76369139 FW |
714 | { |
715 | if (pgoff > rb->nr_pages) | |
716 | return NULL; | |
717 | ||
718 | if (pgoff == 0) | |
719 | return virt_to_page(rb->user_page); | |
720 | ||
721 | return virt_to_page(rb->data_pages[pgoff - 1]); | |
722 | } | |
723 | ||
724 | static void *perf_mmap_alloc_page(int cpu) | |
725 | { | |
726 | struct page *page; | |
727 | int node; | |
728 | ||
729 | node = (cpu == -1) ? cpu : cpu_to_node(cpu); | |
730 | page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0); | |
731 | if (!page) | |
732 | return NULL; | |
733 | ||
734 | return page_address(page); | |
735 | } | |
736 | ||
737 | struct ring_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags) | |
738 | { | |
739 | struct ring_buffer *rb; | |
740 | unsigned long size; | |
741 | int i; | |
742 | ||
743 | size = sizeof(struct ring_buffer); | |
744 | size += nr_pages * sizeof(void *); | |
745 | ||
528871b4 | 746 | if (order_base_2(size) >= PAGE_SHIFT+MAX_ORDER) |
9dff0aa9 MR |
747 | goto fail; |
748 | ||
76369139 FW |
749 | rb = kzalloc(size, GFP_KERNEL); |
750 | if (!rb) | |
751 | goto fail; | |
752 | ||
753 | rb->user_page = perf_mmap_alloc_page(cpu); | |
754 | if (!rb->user_page) | |
755 | goto fail_user_page; | |
756 | ||
757 | for (i = 0; i < nr_pages; i++) { | |
758 | rb->data_pages[i] = perf_mmap_alloc_page(cpu); | |
759 | if (!rb->data_pages[i]) | |
760 | goto fail_data_pages; | |
761 | } | |
762 | ||
763 | rb->nr_pages = nr_pages; | |
764 | ||
765 | ring_buffer_init(rb, watermark, flags); | |
766 | ||
767 | return rb; | |
768 | ||
769 | fail_data_pages: | |
770 | for (i--; i >= 0; i--) | |
771 | free_page((unsigned long)rb->data_pages[i]); | |
772 | ||
773 | free_page((unsigned long)rb->user_page); | |
774 | ||
775 | fail_user_page: | |
776 | kfree(rb); | |
777 | ||
778 | fail: | |
779 | return NULL; | |
780 | } | |
781 | ||
782 | static void perf_mmap_free_page(unsigned long addr) | |
783 | { | |
784 | struct page *page = virt_to_page((void *)addr); | |
785 | ||
786 | page->mapping = NULL; | |
787 | __free_page(page); | |
788 | } | |
789 | ||
790 | void rb_free(struct ring_buffer *rb) | |
791 | { | |
792 | int i; | |
793 | ||
794 | perf_mmap_free_page((unsigned long)rb->user_page); | |
795 | for (i = 0; i < rb->nr_pages; i++) | |
796 | perf_mmap_free_page((unsigned long)rb->data_pages[i]); | |
797 | kfree(rb); | |
798 | } | |
799 | ||
800 | #else | |
5919b309 JO |
801 | static int data_page_nr(struct ring_buffer *rb) |
802 | { | |
803 | return rb->nr_pages << page_order(rb); | |
804 | } | |
76369139 | 805 | |
45bfb2e5 PZ |
806 | static struct page * |
807 | __perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff) | |
76369139 | 808 | { |
5919b309 JO |
809 | /* The '>' counts in the user page. */ |
810 | if (pgoff > data_page_nr(rb)) | |
76369139 FW |
811 | return NULL; |
812 | ||
813 | return vmalloc_to_page((void *)rb->user_page + pgoff * PAGE_SIZE); | |
814 | } | |
815 | ||
816 | static void perf_mmap_unmark_page(void *addr) | |
817 | { | |
818 | struct page *page = vmalloc_to_page(addr); | |
819 | ||
820 | page->mapping = NULL; | |
821 | } | |
822 | ||
823 | static void rb_free_work(struct work_struct *work) | |
824 | { | |
825 | struct ring_buffer *rb; | |
826 | void *base; | |
827 | int i, nr; | |
828 | ||
829 | rb = container_of(work, struct ring_buffer, work); | |
5919b309 | 830 | nr = data_page_nr(rb); |
76369139 FW |
831 | |
832 | base = rb->user_page; | |
5919b309 JO |
833 | /* The '<=' counts in the user page. */ |
834 | for (i = 0; i <= nr; i++) | |
76369139 FW |
835 | perf_mmap_unmark_page(base + (i * PAGE_SIZE)); |
836 | ||
837 | vfree(base); | |
838 | kfree(rb); | |
839 | } | |
840 | ||
841 | void rb_free(struct ring_buffer *rb) | |
842 | { | |
843 | schedule_work(&rb->work); | |
844 | } | |
845 | ||
846 | struct ring_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags) | |
847 | { | |
848 | struct ring_buffer *rb; | |
849 | unsigned long size; | |
850 | void *all_buf; | |
851 | ||
852 | size = sizeof(struct ring_buffer); | |
853 | size += sizeof(void *); | |
854 | ||
855 | rb = kzalloc(size, GFP_KERNEL); | |
856 | if (!rb) | |
857 | goto fail; | |
858 | ||
859 | INIT_WORK(&rb->work, rb_free_work); | |
860 | ||
861 | all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE); | |
862 | if (!all_buf) | |
863 | goto fail_all_buf; | |
864 | ||
865 | rb->user_page = all_buf; | |
866 | rb->data_pages[0] = all_buf + PAGE_SIZE; | |
8184059e PZ |
867 | if (nr_pages) { |
868 | rb->nr_pages = 1; | |
869 | rb->page_order = ilog2(nr_pages); | |
870 | } | |
76369139 FW |
871 | |
872 | ring_buffer_init(rb, watermark, flags); | |
873 | ||
874 | return rb; | |
875 | ||
876 | fail_all_buf: | |
877 | kfree(rb); | |
878 | ||
879 | fail: | |
880 | return NULL; | |
881 | } | |
882 | ||
883 | #endif | |
45bfb2e5 PZ |
884 | |
885 | struct page * | |
886 | perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff) | |
887 | { | |
888 | if (rb->aux_nr_pages) { | |
889 | /* above AUX space */ | |
890 | if (pgoff > rb->aux_pgoff + rb->aux_nr_pages) | |
891 | return NULL; | |
892 | ||
893 | /* AUX space */ | |
4411ec1d PZ |
894 | if (pgoff >= rb->aux_pgoff) { |
895 | int aux_pgoff = array_index_nospec(pgoff - rb->aux_pgoff, rb->aux_nr_pages); | |
896 | return virt_to_page(rb->aux_pages[aux_pgoff]); | |
897 | } | |
45bfb2e5 PZ |
898 | } |
899 | ||
900 | return __perf_mmap_to_page(rb, pgoff); | |
901 | } |