ring_buffer.c 23 KB

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