ring_buffer.c 23 KB

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