ring_buffer.c 21 KB

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