ring_buffer.c 20 KB

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