ring_buffer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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 "internal.h"
  15. static bool perf_output_space(struct ring_buffer *rb, unsigned long tail,
  16. unsigned long offset, unsigned long head)
  17. {
  18. unsigned long mask;
  19. if (!rb->writable)
  20. return true;
  21. mask = perf_data_size(rb) - 1;
  22. offset = (offset - tail) & mask;
  23. head = (head - tail) & mask;
  24. if ((int)(head - offset) < 0)
  25. return false;
  26. return true;
  27. }
  28. static void perf_output_wakeup(struct perf_output_handle *handle)
  29. {
  30. atomic_set(&handle->rb->poll, POLL_IN);
  31. handle->event->pending_wakeup = 1;
  32. irq_work_queue(&handle->event->pending);
  33. }
  34. /*
  35. * We need to ensure a later event_id doesn't publish a head when a former
  36. * event isn't done writing. However since we need to deal with NMIs we
  37. * cannot fully serialize things.
  38. *
  39. * We only publish the head (and generate a wakeup) when the outer-most
  40. * event completes.
  41. */
  42. static void perf_output_get_handle(struct perf_output_handle *handle)
  43. {
  44. struct ring_buffer *rb = handle->rb;
  45. preempt_disable();
  46. local_inc(&rb->nest);
  47. handle->wakeup = local_read(&rb->wakeup);
  48. }
  49. static void perf_output_put_handle(struct perf_output_handle *handle)
  50. {
  51. struct ring_buffer *rb = handle->rb;
  52. unsigned long head;
  53. again:
  54. head = local_read(&rb->head);
  55. /*
  56. * IRQ/NMI can happen here and advance @rb->head, causing our
  57. * load above to be stale.
  58. */
  59. /*
  60. * If this isn't the outermost nesting, we don't have to update
  61. * @rb->user_page->data_head.
  62. */
  63. if (local_read(&rb->nest) > 1) {
  64. local_dec(&rb->nest);
  65. goto out;
  66. }
  67. /*
  68. * Since the mmap() consumer (userspace) can run on a different CPU:
  69. *
  70. * kernel user
  71. *
  72. * READ ->data_tail READ ->data_head
  73. * smp_mb() (A) smp_rmb() (C)
  74. * WRITE $data READ $data
  75. * smp_wmb() (B) smp_mb() (D)
  76. * STORE ->data_head WRITE ->data_tail
  77. *
  78. * Where A pairs with D, and B pairs with C.
  79. *
  80. * I don't think A needs to be a full barrier because we won't in fact
  81. * write data until we see the store from userspace. So we simply don't
  82. * issue the data WRITE until we observe it. Be conservative for now.
  83. *
  84. * OTOH, D needs to be a full barrier since it separates the data READ
  85. * from the tail WRITE.
  86. *
  87. * For B a WMB is sufficient since it separates two WRITEs, and for C
  88. * an RMB is sufficient since it separates two READs.
  89. *
  90. * See perf_output_begin().
  91. */
  92. smp_wmb();
  93. rb->user_page->data_head = head;
  94. /*
  95. * We must publish the head before decrementing the nest count,
  96. * otherwise an IRQ/NMI can publish a more recent head value and our
  97. * write will (temporarily) publish a stale value.
  98. */
  99. barrier();
  100. local_set(&rb->nest, 0);
  101. /*
  102. * Ensure we decrement @rb->nest before we validate the @rb->head.
  103. * Otherwise we cannot be sure we caught the 'last' nested update.
  104. */
  105. barrier();
  106. if (unlikely(head != local_read(&rb->head))) {
  107. local_inc(&rb->nest);
  108. goto again;
  109. }
  110. if (handle->wakeup != local_read(&rb->wakeup))
  111. perf_output_wakeup(handle);
  112. out:
  113. preempt_enable();
  114. }
  115. int perf_output_begin(struct perf_output_handle *handle,
  116. struct perf_event *event, unsigned int size)
  117. {
  118. struct ring_buffer *rb;
  119. unsigned long tail, offset, head;
  120. int have_lost;
  121. struct perf_sample_data sample_data;
  122. struct {
  123. struct perf_event_header header;
  124. u64 id;
  125. u64 lost;
  126. } lost_event;
  127. rcu_read_lock();
  128. /*
  129. * For inherited events we send all the output towards the parent.
  130. */
  131. if (event->parent)
  132. event = event->parent;
  133. rb = rcu_dereference(event->rb);
  134. if (!rb)
  135. goto out;
  136. handle->rb = rb;
  137. handle->event = event;
  138. if (!rb->nr_pages)
  139. goto out;
  140. have_lost = local_read(&rb->lost);
  141. if (have_lost) {
  142. lost_event.header.size = sizeof(lost_event);
  143. perf_event_header__init_id(&lost_event.header, &sample_data,
  144. event);
  145. size += lost_event.header.size;
  146. }
  147. perf_output_get_handle(handle);
  148. do {
  149. /*
  150. * Userspace could choose to issue a mb() before updating the
  151. * tail pointer. So that all reads will be completed before the
  152. * write is issued.
  153. *
  154. * See perf_output_put_handle().
  155. */
  156. tail = ACCESS_ONCE(rb->user_page->data_tail);
  157. smp_mb();
  158. offset = head = local_read(&rb->head);
  159. head += size;
  160. if (unlikely(!perf_output_space(rb, tail, offset, head)))
  161. goto fail;
  162. } while (local_cmpxchg(&rb->head, offset, head) != offset);
  163. if (head - local_read(&rb->wakeup) > rb->watermark)
  164. local_add(rb->watermark, &rb->wakeup);
  165. handle->page = offset >> (PAGE_SHIFT + page_order(rb));
  166. handle->page &= rb->nr_pages - 1;
  167. handle->size = offset & ((PAGE_SIZE << page_order(rb)) - 1);
  168. handle->addr = rb->data_pages[handle->page];
  169. handle->addr += handle->size;
  170. handle->size = (PAGE_SIZE << page_order(rb)) - handle->size;
  171. if (have_lost) {
  172. lost_event.header.type = PERF_RECORD_LOST;
  173. lost_event.header.misc = 0;
  174. lost_event.id = event->id;
  175. lost_event.lost = local_xchg(&rb->lost, 0);
  176. perf_output_put(handle, lost_event);
  177. perf_event__output_id_sample(event, handle, &sample_data);
  178. }
  179. return 0;
  180. fail:
  181. local_inc(&rb->lost);
  182. perf_output_put_handle(handle);
  183. out:
  184. rcu_read_unlock();
  185. return -ENOSPC;
  186. }
  187. unsigned int perf_output_copy(struct perf_output_handle *handle,
  188. const void *buf, unsigned int len)
  189. {
  190. return __output_copy(handle, buf, len);
  191. }
  192. unsigned int perf_output_skip(struct perf_output_handle *handle,
  193. unsigned int len)
  194. {
  195. return __output_skip(handle, NULL, len);
  196. }
  197. void perf_output_end(struct perf_output_handle *handle)
  198. {
  199. perf_output_put_handle(handle);
  200. rcu_read_unlock();
  201. }
  202. static void
  203. ring_buffer_init(struct ring_buffer *rb, long watermark, int flags)
  204. {
  205. long max_size = perf_data_size(rb);
  206. if (watermark)
  207. rb->watermark = min(max_size, watermark);
  208. if (!rb->watermark)
  209. rb->watermark = max_size / 2;
  210. if (flags & RING_BUFFER_WRITABLE)
  211. rb->writable = 1;
  212. atomic_set(&rb->refcount, 1);
  213. INIT_LIST_HEAD(&rb->event_list);
  214. spin_lock_init(&rb->event_lock);
  215. }
  216. int rb_alloc_aux(struct ring_buffer *rb, struct perf_event *event,
  217. pgoff_t pgoff, int nr_pages, long watermark, int flags)
  218. {
  219. bool overwrite = !(flags & RING_BUFFER_WRITABLE);
  220. int node = (event->cpu == -1) ? -1 : cpu_to_node(event->cpu);
  221. int ret = -ENOMEM;
  222. if (!has_aux(event))
  223. return -ENOTSUPP;
  224. rb->aux_pages = kzalloc_node(nr_pages * sizeof(void *), GFP_KERNEL, node);
  225. if (!rb->aux_pages)
  226. return -ENOMEM;
  227. rb->free_aux = event->pmu->free_aux;
  228. for (rb->aux_nr_pages = 0; rb->aux_nr_pages < nr_pages;
  229. rb->aux_nr_pages++) {
  230. struct page *page;
  231. page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
  232. if (!page)
  233. goto out;
  234. rb->aux_pages[rb->aux_nr_pages] = page_address(page);
  235. }
  236. rb->aux_priv = event->pmu->setup_aux(event->cpu, rb->aux_pages, nr_pages,
  237. overwrite);
  238. if (!rb->aux_priv)
  239. goto out;
  240. ret = 0;
  241. /*
  242. * aux_pages (and pmu driver's private data, aux_priv) will be
  243. * referenced in both producer's and consumer's contexts, thus
  244. * we keep a refcount here to make sure either of the two can
  245. * reference them safely.
  246. */
  247. atomic_set(&rb->aux_refcount, 1);
  248. rb->aux_watermark = watermark;
  249. if (!rb->aux_watermark && !rb->aux_overwrite)
  250. rb->aux_watermark = nr_pages << (PAGE_SHIFT - 1);
  251. out:
  252. if (!ret)
  253. rb->aux_pgoff = pgoff;
  254. else
  255. rb_free_aux(rb);
  256. return ret;
  257. }
  258. static void __rb_free_aux(struct ring_buffer *rb)
  259. {
  260. int pg;
  261. /*
  262. * Should never happen, the last reference should be dropped from
  263. * perf_mmap_close() path, which first stops aux transactions (which
  264. * in turn are the atomic holders of aux_refcount) and then does the
  265. * last rb_free_aux().
  266. */
  267. WARN_ON_ONCE(in_atomic());
  268. if (rb->aux_priv) {
  269. rb->free_aux(rb->aux_priv);
  270. rb->free_aux = NULL;
  271. rb->aux_priv = NULL;
  272. }
  273. for (pg = 0; pg < rb->aux_nr_pages; pg++)
  274. free_page((unsigned long)rb->aux_pages[pg]);
  275. kfree(rb->aux_pages);
  276. rb->aux_nr_pages = 0;
  277. }
  278. void rb_free_aux(struct ring_buffer *rb)
  279. {
  280. if (atomic_dec_and_test(&rb->aux_refcount))
  281. __rb_free_aux(rb);
  282. }
  283. #ifndef CONFIG_PERF_USE_VMALLOC
  284. /*
  285. * Back perf_mmap() with regular GFP_KERNEL-0 pages.
  286. */
  287. static struct page *
  288. __perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff)
  289. {
  290. if (pgoff > rb->nr_pages)
  291. return NULL;
  292. if (pgoff == 0)
  293. return virt_to_page(rb->user_page);
  294. return virt_to_page(rb->data_pages[pgoff - 1]);
  295. }
  296. static void *perf_mmap_alloc_page(int cpu)
  297. {
  298. struct page *page;
  299. int node;
  300. node = (cpu == -1) ? cpu : cpu_to_node(cpu);
  301. page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
  302. if (!page)
  303. return NULL;
  304. return page_address(page);
  305. }
  306. struct ring_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags)
  307. {
  308. struct ring_buffer *rb;
  309. unsigned long size;
  310. int i;
  311. size = sizeof(struct ring_buffer);
  312. size += nr_pages * sizeof(void *);
  313. rb = kzalloc(size, GFP_KERNEL);
  314. if (!rb)
  315. goto fail;
  316. rb->user_page = perf_mmap_alloc_page(cpu);
  317. if (!rb->user_page)
  318. goto fail_user_page;
  319. for (i = 0; i < nr_pages; i++) {
  320. rb->data_pages[i] = perf_mmap_alloc_page(cpu);
  321. if (!rb->data_pages[i])
  322. goto fail_data_pages;
  323. }
  324. rb->nr_pages = nr_pages;
  325. ring_buffer_init(rb, watermark, flags);
  326. return rb;
  327. fail_data_pages:
  328. for (i--; i >= 0; i--)
  329. free_page((unsigned long)rb->data_pages[i]);
  330. free_page((unsigned long)rb->user_page);
  331. fail_user_page:
  332. kfree(rb);
  333. fail:
  334. return NULL;
  335. }
  336. static void perf_mmap_free_page(unsigned long addr)
  337. {
  338. struct page *page = virt_to_page((void *)addr);
  339. page->mapping = NULL;
  340. __free_page(page);
  341. }
  342. void rb_free(struct ring_buffer *rb)
  343. {
  344. int i;
  345. perf_mmap_free_page((unsigned long)rb->user_page);
  346. for (i = 0; i < rb->nr_pages; i++)
  347. perf_mmap_free_page((unsigned long)rb->data_pages[i]);
  348. kfree(rb);
  349. }
  350. #else
  351. static int data_page_nr(struct ring_buffer *rb)
  352. {
  353. return rb->nr_pages << page_order(rb);
  354. }
  355. static struct page *
  356. __perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff)
  357. {
  358. /* The '>' counts in the user page. */
  359. if (pgoff > data_page_nr(rb))
  360. return NULL;
  361. return vmalloc_to_page((void *)rb->user_page + pgoff * PAGE_SIZE);
  362. }
  363. static void perf_mmap_unmark_page(void *addr)
  364. {
  365. struct page *page = vmalloc_to_page(addr);
  366. page->mapping = NULL;
  367. }
  368. static void rb_free_work(struct work_struct *work)
  369. {
  370. struct ring_buffer *rb;
  371. void *base;
  372. int i, nr;
  373. rb = container_of(work, struct ring_buffer, work);
  374. nr = data_page_nr(rb);
  375. base = rb->user_page;
  376. /* The '<=' counts in the user page. */
  377. for (i = 0; i <= nr; i++)
  378. perf_mmap_unmark_page(base + (i * PAGE_SIZE));
  379. vfree(base);
  380. kfree(rb);
  381. }
  382. void rb_free(struct ring_buffer *rb)
  383. {
  384. schedule_work(&rb->work);
  385. }
  386. struct ring_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags)
  387. {
  388. struct ring_buffer *rb;
  389. unsigned long size;
  390. void *all_buf;
  391. size = sizeof(struct ring_buffer);
  392. size += sizeof(void *);
  393. rb = kzalloc(size, GFP_KERNEL);
  394. if (!rb)
  395. goto fail;
  396. INIT_WORK(&rb->work, rb_free_work);
  397. all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
  398. if (!all_buf)
  399. goto fail_all_buf;
  400. rb->user_page = all_buf;
  401. rb->data_pages[0] = all_buf + PAGE_SIZE;
  402. rb->page_order = ilog2(nr_pages);
  403. rb->nr_pages = !!nr_pages;
  404. ring_buffer_init(rb, watermark, flags);
  405. return rb;
  406. fail_all_buf:
  407. kfree(rb);
  408. fail:
  409. return NULL;
  410. }
  411. #endif
  412. struct page *
  413. perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff)
  414. {
  415. if (rb->aux_nr_pages) {
  416. /* above AUX space */
  417. if (pgoff > rb->aux_pgoff + rb->aux_nr_pages)
  418. return NULL;
  419. /* AUX space */
  420. if (pgoff >= rb->aux_pgoff)
  421. return virt_to_page(rb->aux_pages[pgoff - rb->aux_pgoff]);
  422. }
  423. return __perf_mmap_to_page(rb, pgoff);
  424. }