ring_buffer.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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, POLLIN);
  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 bool __always_inline
  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 int __always_inline
  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. /*
  251. * This is called before hardware starts writing to the AUX area to
  252. * obtain an output handle and make sure there's room in the buffer.
  253. * When the capture completes, call perf_aux_output_end() to commit
  254. * the recorded data to the buffer.
  255. *
  256. * The ordering is similar to that of perf_output_{begin,end}, with
  257. * the exception of (B), which should be taken care of by the pmu
  258. * driver, since ordering rules will differ depending on hardware.
  259. *
  260. * Call this from pmu::start(); see the comment in perf_aux_output_end()
  261. * about its use in pmu callbacks. Both can also be called from the PMI
  262. * handler if needed.
  263. */
  264. void *perf_aux_output_begin(struct perf_output_handle *handle,
  265. struct perf_event *event)
  266. {
  267. struct perf_event *output_event = event;
  268. unsigned long aux_head, aux_tail;
  269. struct ring_buffer *rb;
  270. if (output_event->parent)
  271. output_event = output_event->parent;
  272. /*
  273. * Since this will typically be open across pmu::add/pmu::del, we
  274. * grab ring_buffer's refcount instead of holding rcu read lock
  275. * to make sure it doesn't disappear under us.
  276. */
  277. rb = ring_buffer_get(output_event);
  278. if (!rb)
  279. return NULL;
  280. if (!rb_has_aux(rb))
  281. goto err;
  282. /*
  283. * If aux_mmap_count is zero, the aux buffer is in perf_mmap_close(),
  284. * about to get freed, so we leave immediately.
  285. *
  286. * Checking rb::aux_mmap_count and rb::refcount has to be done in
  287. * the same order, see perf_mmap_close. Otherwise we end up freeing
  288. * aux pages in this path, which is a bug, because in_atomic().
  289. */
  290. if (!atomic_read(&rb->aux_mmap_count))
  291. goto err;
  292. if (!atomic_inc_not_zero(&rb->aux_refcount))
  293. goto err;
  294. /*
  295. * Nesting is not supported for AUX area, make sure nested
  296. * writers are caught early
  297. */
  298. if (WARN_ON_ONCE(local_xchg(&rb->aux_nest, 1)))
  299. goto err_put;
  300. aux_head = local_read(&rb->aux_head);
  301. handle->rb = rb;
  302. handle->event = event;
  303. handle->head = aux_head;
  304. handle->size = 0;
  305. /*
  306. * In overwrite mode, AUX data stores do not depend on aux_tail,
  307. * therefore (A) control dependency barrier does not exist. The
  308. * (B) <-> (C) ordering is still observed by the pmu driver.
  309. */
  310. if (!rb->aux_overwrite) {
  311. aux_tail = ACCESS_ONCE(rb->user_page->aux_tail);
  312. handle->wakeup = local_read(&rb->aux_wakeup) + rb->aux_watermark;
  313. if (aux_head - aux_tail < perf_aux_size(rb))
  314. handle->size = CIRC_SPACE(aux_head, aux_tail, perf_aux_size(rb));
  315. /*
  316. * handle->size computation depends on aux_tail load; this forms a
  317. * control dependency barrier separating aux_tail load from aux data
  318. * store that will be enabled on successful return
  319. */
  320. if (!handle->size) { /* A, matches D */
  321. event->pending_disable = 1;
  322. perf_output_wakeup(handle);
  323. local_set(&rb->aux_nest, 0);
  324. goto err_put;
  325. }
  326. }
  327. return handle->rb->aux_priv;
  328. err_put:
  329. /* can't be last */
  330. rb_free_aux(rb);
  331. err:
  332. ring_buffer_put(rb);
  333. handle->event = NULL;
  334. return NULL;
  335. }
  336. /*
  337. * Commit the data written by hardware into the ring buffer by adjusting
  338. * aux_head and posting a PERF_RECORD_AUX into the perf buffer. It is the
  339. * pmu driver's responsibility to observe ordering rules of the hardware,
  340. * so that all the data is externally visible before this is called.
  341. *
  342. * Note: this has to be called from pmu::stop() callback, as the assumption
  343. * of the AUX buffer management code is that after pmu::stop(), the AUX
  344. * transaction must be stopped and therefore drop the AUX reference count.
  345. */
  346. void perf_aux_output_end(struct perf_output_handle *handle, unsigned long size,
  347. bool truncated)
  348. {
  349. struct ring_buffer *rb = handle->rb;
  350. bool wakeup = truncated;
  351. unsigned long aux_head;
  352. u64 flags = 0;
  353. if (truncated)
  354. flags |= PERF_AUX_FLAG_TRUNCATED;
  355. /* in overwrite mode, driver provides aux_head via handle */
  356. if (rb->aux_overwrite) {
  357. flags |= PERF_AUX_FLAG_OVERWRITE;
  358. aux_head = handle->head;
  359. local_set(&rb->aux_head, aux_head);
  360. } else {
  361. aux_head = local_read(&rb->aux_head);
  362. local_add(size, &rb->aux_head);
  363. }
  364. if (size || flags) {
  365. /*
  366. * Only send RECORD_AUX if we have something useful to communicate
  367. */
  368. perf_event_aux_event(handle->event, aux_head, size, flags);
  369. }
  370. aux_head = rb->user_page->aux_head = local_read(&rb->aux_head);
  371. if (aux_head - local_read(&rb->aux_wakeup) >= rb->aux_watermark) {
  372. wakeup = true;
  373. local_add(rb->aux_watermark, &rb->aux_wakeup);
  374. }
  375. if (wakeup) {
  376. if (truncated)
  377. handle->event->pending_disable = 1;
  378. perf_output_wakeup(handle);
  379. }
  380. handle->event = NULL;
  381. local_set(&rb->aux_nest, 0);
  382. /* can't be last */
  383. rb_free_aux(rb);
  384. ring_buffer_put(rb);
  385. }
  386. /*
  387. * Skip over a given number of bytes in the AUX buffer, due to, for example,
  388. * hardware's alignment constraints.
  389. */
  390. int perf_aux_output_skip(struct perf_output_handle *handle, unsigned long size)
  391. {
  392. struct ring_buffer *rb = handle->rb;
  393. unsigned long aux_head;
  394. if (size > handle->size)
  395. return -ENOSPC;
  396. local_add(size, &rb->aux_head);
  397. aux_head = rb->user_page->aux_head = local_read(&rb->aux_head);
  398. if (aux_head - local_read(&rb->aux_wakeup) >= rb->aux_watermark) {
  399. perf_output_wakeup(handle);
  400. local_add(rb->aux_watermark, &rb->aux_wakeup);
  401. handle->wakeup = local_read(&rb->aux_wakeup) +
  402. rb->aux_watermark;
  403. }
  404. handle->head = aux_head;
  405. handle->size -= size;
  406. return 0;
  407. }
  408. void *perf_get_aux(struct perf_output_handle *handle)
  409. {
  410. /* this is only valid between perf_aux_output_begin and *_end */
  411. if (!handle->event)
  412. return NULL;
  413. return handle->rb->aux_priv;
  414. }
  415. #define PERF_AUX_GFP (GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY)
  416. static struct page *rb_alloc_aux_page(int node, int order)
  417. {
  418. struct page *page;
  419. if (order > MAX_ORDER)
  420. order = MAX_ORDER;
  421. do {
  422. page = alloc_pages_node(node, PERF_AUX_GFP, order);
  423. } while (!page && order--);
  424. if (page && order) {
  425. /*
  426. * Communicate the allocation size to the driver:
  427. * if we managed to secure a high-order allocation,
  428. * set its first page's private to this order;
  429. * !PagePrivate(page) means it's just a normal page.
  430. */
  431. split_page(page, order);
  432. SetPagePrivate(page);
  433. set_page_private(page, order);
  434. }
  435. return page;
  436. }
  437. static void rb_free_aux_page(struct ring_buffer *rb, int idx)
  438. {
  439. struct page *page = virt_to_page(rb->aux_pages[idx]);
  440. ClearPagePrivate(page);
  441. page->mapping = NULL;
  442. __free_page(page);
  443. }
  444. static void __rb_free_aux(struct ring_buffer *rb)
  445. {
  446. int pg;
  447. /*
  448. * Should never happen, the last reference should be dropped from
  449. * perf_mmap_close() path, which first stops aux transactions (which
  450. * in turn are the atomic holders of aux_refcount) and then does the
  451. * last rb_free_aux().
  452. */
  453. WARN_ON_ONCE(in_atomic());
  454. if (rb->aux_priv) {
  455. rb->free_aux(rb->aux_priv);
  456. rb->free_aux = NULL;
  457. rb->aux_priv = NULL;
  458. }
  459. if (rb->aux_nr_pages) {
  460. for (pg = 0; pg < rb->aux_nr_pages; pg++)
  461. rb_free_aux_page(rb, pg);
  462. kfree(rb->aux_pages);
  463. rb->aux_nr_pages = 0;
  464. }
  465. }
  466. int rb_alloc_aux(struct ring_buffer *rb, struct perf_event *event,
  467. pgoff_t pgoff, int nr_pages, long watermark, int flags)
  468. {
  469. bool overwrite = !(flags & RING_BUFFER_WRITABLE);
  470. int node = (event->cpu == -1) ? -1 : cpu_to_node(event->cpu);
  471. int ret = -ENOMEM, max_order = 0;
  472. if (!has_aux(event))
  473. return -ENOTSUPP;
  474. if (event->pmu->capabilities & PERF_PMU_CAP_AUX_NO_SG) {
  475. /*
  476. * We need to start with the max_order that fits in nr_pages,
  477. * not the other way around, hence ilog2() and not get_order.
  478. */
  479. max_order = ilog2(nr_pages);
  480. /*
  481. * PMU requests more than one contiguous chunks of memory
  482. * for SW double buffering
  483. */
  484. if ((event->pmu->capabilities & PERF_PMU_CAP_AUX_SW_DOUBLEBUF) &&
  485. !overwrite) {
  486. if (!max_order)
  487. return -EINVAL;
  488. max_order--;
  489. }
  490. }
  491. rb->aux_pages = kzalloc_node(nr_pages * sizeof(void *), GFP_KERNEL, node);
  492. if (!rb->aux_pages)
  493. return -ENOMEM;
  494. rb->free_aux = event->pmu->free_aux;
  495. for (rb->aux_nr_pages = 0; rb->aux_nr_pages < nr_pages;) {
  496. struct page *page;
  497. int last, order;
  498. order = min(max_order, ilog2(nr_pages - rb->aux_nr_pages));
  499. page = rb_alloc_aux_page(node, order);
  500. if (!page)
  501. goto out;
  502. for (last = rb->aux_nr_pages + (1 << page_private(page));
  503. last > rb->aux_nr_pages; rb->aux_nr_pages++)
  504. rb->aux_pages[rb->aux_nr_pages] = page_address(page++);
  505. }
  506. /*
  507. * In overwrite mode, PMUs that don't support SG may not handle more
  508. * than one contiguous allocation, since they rely on PMI to do double
  509. * buffering. In this case, the entire buffer has to be one contiguous
  510. * chunk.
  511. */
  512. if ((event->pmu->capabilities & PERF_PMU_CAP_AUX_NO_SG) &&
  513. overwrite) {
  514. struct page *page = virt_to_page(rb->aux_pages[0]);
  515. if (page_private(page) != max_order)
  516. goto out;
  517. }
  518. rb->aux_priv = event->pmu->setup_aux(event->cpu, rb->aux_pages, nr_pages,
  519. overwrite);
  520. if (!rb->aux_priv)
  521. goto out;
  522. ret = 0;
  523. /*
  524. * aux_pages (and pmu driver's private data, aux_priv) will be
  525. * referenced in both producer's and consumer's contexts, thus
  526. * we keep a refcount here to make sure either of the two can
  527. * reference them safely.
  528. */
  529. atomic_set(&rb->aux_refcount, 1);
  530. rb->aux_overwrite = overwrite;
  531. rb->aux_watermark = watermark;
  532. if (!rb->aux_watermark && !rb->aux_overwrite)
  533. rb->aux_watermark = nr_pages << (PAGE_SHIFT - 1);
  534. out:
  535. if (!ret)
  536. rb->aux_pgoff = pgoff;
  537. else
  538. __rb_free_aux(rb);
  539. return ret;
  540. }
  541. void rb_free_aux(struct ring_buffer *rb)
  542. {
  543. if (atomic_dec_and_test(&rb->aux_refcount))
  544. __rb_free_aux(rb);
  545. }
  546. #ifndef CONFIG_PERF_USE_VMALLOC
  547. /*
  548. * Back perf_mmap() with regular GFP_KERNEL-0 pages.
  549. */
  550. static struct page *
  551. __perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff)
  552. {
  553. if (pgoff > rb->nr_pages)
  554. return NULL;
  555. if (pgoff == 0)
  556. return virt_to_page(rb->user_page);
  557. return virt_to_page(rb->data_pages[pgoff - 1]);
  558. }
  559. static void *perf_mmap_alloc_page(int cpu)
  560. {
  561. struct page *page;
  562. int node;
  563. node = (cpu == -1) ? cpu : cpu_to_node(cpu);
  564. page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
  565. if (!page)
  566. return NULL;
  567. return page_address(page);
  568. }
  569. struct ring_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags)
  570. {
  571. struct ring_buffer *rb;
  572. unsigned long size;
  573. int i;
  574. size = sizeof(struct ring_buffer);
  575. size += nr_pages * sizeof(void *);
  576. rb = kzalloc(size, GFP_KERNEL);
  577. if (!rb)
  578. goto fail;
  579. rb->user_page = perf_mmap_alloc_page(cpu);
  580. if (!rb->user_page)
  581. goto fail_user_page;
  582. for (i = 0; i < nr_pages; i++) {
  583. rb->data_pages[i] = perf_mmap_alloc_page(cpu);
  584. if (!rb->data_pages[i])
  585. goto fail_data_pages;
  586. }
  587. rb->nr_pages = nr_pages;
  588. ring_buffer_init(rb, watermark, flags);
  589. return rb;
  590. fail_data_pages:
  591. for (i--; i >= 0; i--)
  592. free_page((unsigned long)rb->data_pages[i]);
  593. free_page((unsigned long)rb->user_page);
  594. fail_user_page:
  595. kfree(rb);
  596. fail:
  597. return NULL;
  598. }
  599. static void perf_mmap_free_page(unsigned long addr)
  600. {
  601. struct page *page = virt_to_page((void *)addr);
  602. page->mapping = NULL;
  603. __free_page(page);
  604. }
  605. void rb_free(struct ring_buffer *rb)
  606. {
  607. int i;
  608. perf_mmap_free_page((unsigned long)rb->user_page);
  609. for (i = 0; i < rb->nr_pages; i++)
  610. perf_mmap_free_page((unsigned long)rb->data_pages[i]);
  611. kfree(rb);
  612. }
  613. #else
  614. static int data_page_nr(struct ring_buffer *rb)
  615. {
  616. return rb->nr_pages << page_order(rb);
  617. }
  618. static struct page *
  619. __perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff)
  620. {
  621. /* The '>' counts in the user page. */
  622. if (pgoff > data_page_nr(rb))
  623. return NULL;
  624. return vmalloc_to_page((void *)rb->user_page + pgoff * PAGE_SIZE);
  625. }
  626. static void perf_mmap_unmark_page(void *addr)
  627. {
  628. struct page *page = vmalloc_to_page(addr);
  629. page->mapping = NULL;
  630. }
  631. static void rb_free_work(struct work_struct *work)
  632. {
  633. struct ring_buffer *rb;
  634. void *base;
  635. int i, nr;
  636. rb = container_of(work, struct ring_buffer, work);
  637. nr = data_page_nr(rb);
  638. base = rb->user_page;
  639. /* The '<=' counts in the user page. */
  640. for (i = 0; i <= nr; i++)
  641. perf_mmap_unmark_page(base + (i * PAGE_SIZE));
  642. vfree(base);
  643. kfree(rb);
  644. }
  645. void rb_free(struct ring_buffer *rb)
  646. {
  647. schedule_work(&rb->work);
  648. }
  649. struct ring_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags)
  650. {
  651. struct ring_buffer *rb;
  652. unsigned long size;
  653. void *all_buf;
  654. size = sizeof(struct ring_buffer);
  655. size += sizeof(void *);
  656. rb = kzalloc(size, GFP_KERNEL);
  657. if (!rb)
  658. goto fail;
  659. INIT_WORK(&rb->work, rb_free_work);
  660. all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
  661. if (!all_buf)
  662. goto fail_all_buf;
  663. rb->user_page = all_buf;
  664. rb->data_pages[0] = all_buf + PAGE_SIZE;
  665. if (nr_pages) {
  666. rb->nr_pages = 1;
  667. rb->page_order = ilog2(nr_pages);
  668. }
  669. ring_buffer_init(rb, watermark, flags);
  670. return rb;
  671. fail_all_buf:
  672. kfree(rb);
  673. fail:
  674. return NULL;
  675. }
  676. #endif
  677. struct page *
  678. perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff)
  679. {
  680. if (rb->aux_nr_pages) {
  681. /* above AUX space */
  682. if (pgoff > rb->aux_pgoff + rb->aux_nr_pages)
  683. return NULL;
  684. /* AUX space */
  685. if (pgoff >= rb->aux_pgoff) {
  686. int aux_pgoff = array_index_nospec(pgoff - rb->aux_pgoff, rb->aux_nr_pages);
  687. return virt_to_page(rb->aux_pages[aux_pgoff]);
  688. }
  689. }
  690. return __perf_mmap_to_page(rb, pgoff);
  691. }