ring_buffer.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. void perf_output_copy(struct perf_output_handle *handle,
  188. const void *buf, unsigned int len)
  189. {
  190. __output_copy(handle, buf, len);
  191. }
  192. void perf_output_end(struct perf_output_handle *handle)
  193. {
  194. perf_output_put_handle(handle);
  195. rcu_read_unlock();
  196. }
  197. static void
  198. ring_buffer_init(struct ring_buffer *rb, long watermark, int flags)
  199. {
  200. long max_size = perf_data_size(rb);
  201. if (watermark)
  202. rb->watermark = min(max_size, watermark);
  203. if (!rb->watermark)
  204. rb->watermark = max_size / 2;
  205. if (flags & RING_BUFFER_WRITABLE)
  206. rb->writable = 1;
  207. atomic_set(&rb->refcount, 1);
  208. INIT_LIST_HEAD(&rb->event_list);
  209. spin_lock_init(&rb->event_lock);
  210. }
  211. #ifndef CONFIG_PERF_USE_VMALLOC
  212. /*
  213. * Back perf_mmap() with regular GFP_KERNEL-0 pages.
  214. */
  215. struct page *
  216. perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff)
  217. {
  218. if (pgoff > rb->nr_pages)
  219. return NULL;
  220. if (pgoff == 0)
  221. return virt_to_page(rb->user_page);
  222. return virt_to_page(rb->data_pages[pgoff - 1]);
  223. }
  224. static void *perf_mmap_alloc_page(int cpu)
  225. {
  226. struct page *page;
  227. int node;
  228. node = (cpu == -1) ? cpu : cpu_to_node(cpu);
  229. page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
  230. if (!page)
  231. return NULL;
  232. return page_address(page);
  233. }
  234. struct ring_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags)
  235. {
  236. struct ring_buffer *rb;
  237. unsigned long size;
  238. int i;
  239. size = sizeof(struct ring_buffer);
  240. size += nr_pages * sizeof(void *);
  241. rb = kzalloc(size, GFP_KERNEL);
  242. if (!rb)
  243. goto fail;
  244. rb->user_page = perf_mmap_alloc_page(cpu);
  245. if (!rb->user_page)
  246. goto fail_user_page;
  247. for (i = 0; i < nr_pages; i++) {
  248. rb->data_pages[i] = perf_mmap_alloc_page(cpu);
  249. if (!rb->data_pages[i])
  250. goto fail_data_pages;
  251. }
  252. rb->nr_pages = nr_pages;
  253. ring_buffer_init(rb, watermark, flags);
  254. return rb;
  255. fail_data_pages:
  256. for (i--; i >= 0; i--)
  257. free_page((unsigned long)rb->data_pages[i]);
  258. free_page((unsigned long)rb->user_page);
  259. fail_user_page:
  260. kfree(rb);
  261. fail:
  262. return NULL;
  263. }
  264. static void perf_mmap_free_page(unsigned long addr)
  265. {
  266. struct page *page = virt_to_page((void *)addr);
  267. page->mapping = NULL;
  268. __free_page(page);
  269. }
  270. void rb_free(struct ring_buffer *rb)
  271. {
  272. int i;
  273. perf_mmap_free_page((unsigned long)rb->user_page);
  274. for (i = 0; i < rb->nr_pages; i++)
  275. perf_mmap_free_page((unsigned long)rb->data_pages[i]);
  276. kfree(rb);
  277. }
  278. #else
  279. struct page *
  280. perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff)
  281. {
  282. if (pgoff > (1UL << page_order(rb)))
  283. return NULL;
  284. return vmalloc_to_page((void *)rb->user_page + pgoff * PAGE_SIZE);
  285. }
  286. static void perf_mmap_unmark_page(void *addr)
  287. {
  288. struct page *page = vmalloc_to_page(addr);
  289. page->mapping = NULL;
  290. }
  291. static void rb_free_work(struct work_struct *work)
  292. {
  293. struct ring_buffer *rb;
  294. void *base;
  295. int i, nr;
  296. rb = container_of(work, struct ring_buffer, work);
  297. nr = 1 << page_order(rb);
  298. base = rb->user_page;
  299. for (i = 0; i < nr + 1; i++)
  300. perf_mmap_unmark_page(base + (i * PAGE_SIZE));
  301. vfree(base);
  302. kfree(rb);
  303. }
  304. void rb_free(struct ring_buffer *rb)
  305. {
  306. schedule_work(&rb->work);
  307. }
  308. struct ring_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags)
  309. {
  310. struct ring_buffer *rb;
  311. unsigned long size;
  312. void *all_buf;
  313. size = sizeof(struct ring_buffer);
  314. size += sizeof(void *);
  315. rb = kzalloc(size, GFP_KERNEL);
  316. if (!rb)
  317. goto fail;
  318. INIT_WORK(&rb->work, rb_free_work);
  319. all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
  320. if (!all_buf)
  321. goto fail_all_buf;
  322. rb->user_page = all_buf;
  323. rb->data_pages[0] = all_buf + PAGE_SIZE;
  324. rb->page_order = ilog2(nr_pages);
  325. rb->nr_pages = 1;
  326. ring_buffer_init(rb, watermark, flags);
  327. return rb;
  328. fail_all_buf:
  329. kfree(rb);
  330. fail:
  331. return NULL;
  332. }
  333. #endif