ring_buffer.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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. */
  361. split_page(page, order);
  362. SetPagePrivate(page);
  363. set_page_private(page, order);
  364. }
  365. return page;
  366. }
  367. static void rb_free_aux_page(struct ring_buffer *rb, int idx)
  368. {
  369. struct page *page = virt_to_page(rb->aux_pages[idx]);
  370. ClearPagePrivate(page);
  371. page->mapping = NULL;
  372. __free_page(page);
  373. }
  374. int rb_alloc_aux(struct ring_buffer *rb, struct perf_event *event,
  375. pgoff_t pgoff, int nr_pages, long watermark, int flags)
  376. {
  377. bool overwrite = !(flags & RING_BUFFER_WRITABLE);
  378. int node = (event->cpu == -1) ? -1 : cpu_to_node(event->cpu);
  379. int ret = -ENOMEM, max_order = 0;
  380. if (!has_aux(event))
  381. return -ENOTSUPP;
  382. if (event->pmu->capabilities & PERF_PMU_CAP_AUX_NO_SG) {
  383. /*
  384. * We need to start with the max_order that fits in nr_pages,
  385. * not the other way around, hence ilog2() and not get_order.
  386. */
  387. max_order = ilog2(nr_pages);
  388. /*
  389. * PMU requests more than one contiguous chunks of memory
  390. * for SW double buffering
  391. */
  392. if ((event->pmu->capabilities & PERF_PMU_CAP_AUX_SW_DOUBLEBUF) &&
  393. !overwrite) {
  394. if (!max_order)
  395. return -EINVAL;
  396. max_order--;
  397. }
  398. }
  399. rb->aux_pages = kzalloc_node(nr_pages * sizeof(void *), GFP_KERNEL, node);
  400. if (!rb->aux_pages)
  401. return -ENOMEM;
  402. rb->free_aux = event->pmu->free_aux;
  403. for (rb->aux_nr_pages = 0; rb->aux_nr_pages < nr_pages;) {
  404. struct page *page;
  405. int last, order;
  406. order = min(max_order, ilog2(nr_pages - rb->aux_nr_pages));
  407. page = rb_alloc_aux_page(node, order);
  408. if (!page)
  409. goto out;
  410. for (last = rb->aux_nr_pages + (1 << page_private(page));
  411. last > rb->aux_nr_pages; rb->aux_nr_pages++)
  412. rb->aux_pages[rb->aux_nr_pages] = page_address(page++);
  413. }
  414. /*
  415. * In overwrite mode, PMUs that don't support SG may not handle more
  416. * than one contiguous allocation, since they rely on PMI to do double
  417. * buffering. In this case, the entire buffer has to be one contiguous
  418. * chunk.
  419. */
  420. if ((event->pmu->capabilities & PERF_PMU_CAP_AUX_NO_SG) &&
  421. overwrite) {
  422. struct page *page = virt_to_page(rb->aux_pages[0]);
  423. if (page_private(page) != max_order)
  424. goto out;
  425. }
  426. rb->aux_priv = event->pmu->setup_aux(event->cpu, rb->aux_pages, nr_pages,
  427. overwrite);
  428. if (!rb->aux_priv)
  429. goto out;
  430. ret = 0;
  431. /*
  432. * aux_pages (and pmu driver's private data, aux_priv) will be
  433. * referenced in both producer's and consumer's contexts, thus
  434. * we keep a refcount here to make sure either of the two can
  435. * reference them safely.
  436. */
  437. atomic_set(&rb->aux_refcount, 1);
  438. rb->aux_overwrite = overwrite;
  439. rb->aux_watermark = watermark;
  440. if (!rb->aux_watermark && !rb->aux_overwrite)
  441. rb->aux_watermark = nr_pages << (PAGE_SHIFT - 1);
  442. out:
  443. if (!ret)
  444. rb->aux_pgoff = pgoff;
  445. else
  446. rb_free_aux(rb);
  447. return ret;
  448. }
  449. static void __rb_free_aux(struct ring_buffer *rb)
  450. {
  451. int pg;
  452. if (rb->aux_priv) {
  453. rb->free_aux(rb->aux_priv);
  454. rb->free_aux = NULL;
  455. rb->aux_priv = NULL;
  456. }
  457. for (pg = 0; pg < rb->aux_nr_pages; pg++)
  458. rb_free_aux_page(rb, pg);
  459. kfree(rb->aux_pages);
  460. rb->aux_nr_pages = 0;
  461. }
  462. void rb_free_aux(struct ring_buffer *rb)
  463. {
  464. if (atomic_dec_and_test(&rb->aux_refcount))
  465. irq_work_queue(&rb->irq_work);
  466. }
  467. static void rb_irq_work(struct irq_work *work)
  468. {
  469. struct ring_buffer *rb = container_of(work, struct ring_buffer, irq_work);
  470. if (!atomic_read(&rb->aux_refcount))
  471. __rb_free_aux(rb);
  472. if (rb->rcu_head.next == (void *)rb)
  473. call_rcu(&rb->rcu_head, rb_free_rcu);
  474. }
  475. #ifndef CONFIG_PERF_USE_VMALLOC
  476. /*
  477. * Back perf_mmap() with regular GFP_KERNEL-0 pages.
  478. */
  479. static struct page *
  480. __perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff)
  481. {
  482. if (pgoff > rb->nr_pages)
  483. return NULL;
  484. if (pgoff == 0)
  485. return virt_to_page(rb->user_page);
  486. return virt_to_page(rb->data_pages[pgoff - 1]);
  487. }
  488. static void *perf_mmap_alloc_page(int cpu)
  489. {
  490. struct page *page;
  491. int node;
  492. node = (cpu == -1) ? cpu : cpu_to_node(cpu);
  493. page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
  494. if (!page)
  495. return NULL;
  496. return page_address(page);
  497. }
  498. struct ring_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags)
  499. {
  500. struct ring_buffer *rb;
  501. unsigned long size;
  502. int i;
  503. size = sizeof(struct ring_buffer);
  504. size += nr_pages * sizeof(void *);
  505. rb = kzalloc(size, GFP_KERNEL);
  506. if (!rb)
  507. goto fail;
  508. rb->user_page = perf_mmap_alloc_page(cpu);
  509. if (!rb->user_page)
  510. goto fail_user_page;
  511. for (i = 0; i < nr_pages; i++) {
  512. rb->data_pages[i] = perf_mmap_alloc_page(cpu);
  513. if (!rb->data_pages[i])
  514. goto fail_data_pages;
  515. }
  516. rb->nr_pages = nr_pages;
  517. ring_buffer_init(rb, watermark, flags);
  518. return rb;
  519. fail_data_pages:
  520. for (i--; i >= 0; i--)
  521. free_page((unsigned long)rb->data_pages[i]);
  522. free_page((unsigned long)rb->user_page);
  523. fail_user_page:
  524. kfree(rb);
  525. fail:
  526. return NULL;
  527. }
  528. static void perf_mmap_free_page(unsigned long addr)
  529. {
  530. struct page *page = virt_to_page((void *)addr);
  531. page->mapping = NULL;
  532. __free_page(page);
  533. }
  534. void rb_free(struct ring_buffer *rb)
  535. {
  536. int i;
  537. perf_mmap_free_page((unsigned long)rb->user_page);
  538. for (i = 0; i < rb->nr_pages; i++)
  539. perf_mmap_free_page((unsigned long)rb->data_pages[i]);
  540. kfree(rb);
  541. }
  542. #else
  543. static int data_page_nr(struct ring_buffer *rb)
  544. {
  545. return rb->nr_pages << page_order(rb);
  546. }
  547. static struct page *
  548. __perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff)
  549. {
  550. /* The '>' counts in the user page. */
  551. if (pgoff > data_page_nr(rb))
  552. return NULL;
  553. return vmalloc_to_page((void *)rb->user_page + pgoff * PAGE_SIZE);
  554. }
  555. static void perf_mmap_unmark_page(void *addr)
  556. {
  557. struct page *page = vmalloc_to_page(addr);
  558. page->mapping = NULL;
  559. }
  560. static void rb_free_work(struct work_struct *work)
  561. {
  562. struct ring_buffer *rb;
  563. void *base;
  564. int i, nr;
  565. rb = container_of(work, struct ring_buffer, work);
  566. nr = data_page_nr(rb);
  567. base = rb->user_page;
  568. /* The '<=' counts in the user page. */
  569. for (i = 0; i <= nr; i++)
  570. perf_mmap_unmark_page(base + (i * PAGE_SIZE));
  571. vfree(base);
  572. kfree(rb);
  573. }
  574. void rb_free(struct ring_buffer *rb)
  575. {
  576. schedule_work(&rb->work);
  577. }
  578. struct ring_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags)
  579. {
  580. struct ring_buffer *rb;
  581. unsigned long size;
  582. void *all_buf;
  583. size = sizeof(struct ring_buffer);
  584. size += sizeof(void *);
  585. rb = kzalloc(size, GFP_KERNEL);
  586. if (!rb)
  587. goto fail;
  588. INIT_WORK(&rb->work, rb_free_work);
  589. all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
  590. if (!all_buf)
  591. goto fail_all_buf;
  592. rb->user_page = all_buf;
  593. rb->data_pages[0] = all_buf + PAGE_SIZE;
  594. rb->page_order = ilog2(nr_pages);
  595. rb->nr_pages = !!nr_pages;
  596. ring_buffer_init(rb, watermark, flags);
  597. return rb;
  598. fail_all_buf:
  599. kfree(rb);
  600. fail:
  601. return NULL;
  602. }
  603. #endif
  604. struct page *
  605. perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff)
  606. {
  607. if (rb->aux_nr_pages) {
  608. /* above AUX space */
  609. if (pgoff > rb->aux_pgoff + rb->aux_nr_pages)
  610. return NULL;
  611. /* AUX space */
  612. if (pgoff >= rb->aux_pgoff)
  613. return virt_to_page(rb->aux_pages[pgoff - rb->aux_pgoff]);
  614. }
  615. return __perf_mmap_to_page(rb, pgoff);
  616. }