ring_buffer.c 23 KB

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