ring_buffer.c 18 KB

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