ring_buffer.c 18 KB

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