ring_buffer.c 20 KB

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