ring_buffer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. *
  3. * Copyright (c) 2009, Microsoft Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  16. * Place - Suite 330, Boston, MA 02111-1307 USA.
  17. *
  18. * Authors:
  19. * Haiyang Zhang <haiyangz@microsoft.com>
  20. * Hank Janssen <hjanssen@microsoft.com>
  21. * K. Y. Srinivasan <kys@microsoft.com>
  22. *
  23. */
  24. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  25. #include <linux/kernel.h>
  26. #include <linux/mm.h>
  27. #include <linux/hyperv.h>
  28. #include <linux/uio.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/slab.h>
  31. #include <linux/prefetch.h>
  32. #include "hyperv_vmbus.h"
  33. #define VMBUS_PKT_TRAILER 8
  34. /*
  35. * When we write to the ring buffer, check if the host needs to
  36. * be signaled. Here is the details of this protocol:
  37. *
  38. * 1. The host guarantees that while it is draining the
  39. * ring buffer, it will set the interrupt_mask to
  40. * indicate it does not need to be interrupted when
  41. * new data is placed.
  42. *
  43. * 2. The host guarantees that it will completely drain
  44. * the ring buffer before exiting the read loop. Further,
  45. * once the ring buffer is empty, it will clear the
  46. * interrupt_mask and re-check to see if new data has
  47. * arrived.
  48. *
  49. * KYS: Oct. 30, 2016:
  50. * It looks like Windows hosts have logic to deal with DOS attacks that
  51. * can be triggered if it receives interrupts when it is not expecting
  52. * the interrupt. The host expects interrupts only when the ring
  53. * transitions from empty to non-empty (or full to non full on the guest
  54. * to host ring).
  55. * So, base the signaling decision solely on the ring state until the
  56. * host logic is fixed.
  57. */
  58. static void hv_signal_on_write(u32 old_write, struct vmbus_channel *channel)
  59. {
  60. struct hv_ring_buffer_info *rbi = &channel->outbound;
  61. virt_mb();
  62. if (READ_ONCE(rbi->ring_buffer->interrupt_mask))
  63. return;
  64. /* check interrupt_mask before read_index */
  65. virt_rmb();
  66. /*
  67. * This is the only case we need to signal when the
  68. * ring transitions from being empty to non-empty.
  69. */
  70. if (old_write == READ_ONCE(rbi->ring_buffer->read_index))
  71. vmbus_setevent(channel);
  72. }
  73. /* Get the next write location for the specified ring buffer. */
  74. static inline u32
  75. hv_get_next_write_location(struct hv_ring_buffer_info *ring_info)
  76. {
  77. u32 next = ring_info->ring_buffer->write_index;
  78. return next;
  79. }
  80. /* Set the next write location for the specified ring buffer. */
  81. static inline void
  82. hv_set_next_write_location(struct hv_ring_buffer_info *ring_info,
  83. u32 next_write_location)
  84. {
  85. ring_info->ring_buffer->write_index = next_write_location;
  86. }
  87. /* Set the next read location for the specified ring buffer. */
  88. static inline void
  89. hv_set_next_read_location(struct hv_ring_buffer_info *ring_info,
  90. u32 next_read_location)
  91. {
  92. ring_info->ring_buffer->read_index = next_read_location;
  93. ring_info->priv_read_index = next_read_location;
  94. }
  95. /* Get the size of the ring buffer. */
  96. static inline u32
  97. hv_get_ring_buffersize(const struct hv_ring_buffer_info *ring_info)
  98. {
  99. return ring_info->ring_datasize;
  100. }
  101. /* Get the read and write indices as u64 of the specified ring buffer. */
  102. static inline u64
  103. hv_get_ring_bufferindices(struct hv_ring_buffer_info *ring_info)
  104. {
  105. return (u64)ring_info->ring_buffer->write_index << 32;
  106. }
  107. /*
  108. * Helper routine to copy from source to ring buffer.
  109. * Assume there is enough room. Handles wrap-around in dest case only!!
  110. */
  111. static u32 hv_copyto_ringbuffer(
  112. struct hv_ring_buffer_info *ring_info,
  113. u32 start_write_offset,
  114. const void *src,
  115. u32 srclen)
  116. {
  117. void *ring_buffer = hv_get_ring_buffer(ring_info);
  118. u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
  119. memcpy(ring_buffer + start_write_offset, src, srclen);
  120. start_write_offset += srclen;
  121. if (start_write_offset >= ring_buffer_size)
  122. start_write_offset -= ring_buffer_size;
  123. return start_write_offset;
  124. }
  125. /* Get various debug metrics for the specified ring buffer. */
  126. void hv_ringbuffer_get_debuginfo(const struct hv_ring_buffer_info *ring_info,
  127. struct hv_ring_buffer_debug_info *debug_info)
  128. {
  129. u32 bytes_avail_towrite;
  130. u32 bytes_avail_toread;
  131. if (ring_info->ring_buffer) {
  132. hv_get_ringbuffer_availbytes(ring_info,
  133. &bytes_avail_toread,
  134. &bytes_avail_towrite);
  135. debug_info->bytes_avail_toread = bytes_avail_toread;
  136. debug_info->bytes_avail_towrite = bytes_avail_towrite;
  137. debug_info->current_read_index =
  138. ring_info->ring_buffer->read_index;
  139. debug_info->current_write_index =
  140. ring_info->ring_buffer->write_index;
  141. debug_info->current_interrupt_mask =
  142. ring_info->ring_buffer->interrupt_mask;
  143. }
  144. }
  145. EXPORT_SYMBOL_GPL(hv_ringbuffer_get_debuginfo);
  146. /* Initialize the ring buffer. */
  147. int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
  148. struct page *pages, u32 page_cnt)
  149. {
  150. int i;
  151. struct page **pages_wraparound;
  152. BUILD_BUG_ON((sizeof(struct hv_ring_buffer) != PAGE_SIZE));
  153. memset(ring_info, 0, sizeof(struct hv_ring_buffer_info));
  154. /*
  155. * First page holds struct hv_ring_buffer, do wraparound mapping for
  156. * the rest.
  157. */
  158. pages_wraparound = kzalloc(sizeof(struct page *) * (page_cnt * 2 - 1),
  159. GFP_KERNEL);
  160. if (!pages_wraparound)
  161. return -ENOMEM;
  162. pages_wraparound[0] = pages;
  163. for (i = 0; i < 2 * (page_cnt - 1); i++)
  164. pages_wraparound[i + 1] = &pages[i % (page_cnt - 1) + 1];
  165. ring_info->ring_buffer = (struct hv_ring_buffer *)
  166. vmap(pages_wraparound, page_cnt * 2 - 1, VM_MAP, PAGE_KERNEL);
  167. kfree(pages_wraparound);
  168. if (!ring_info->ring_buffer)
  169. return -ENOMEM;
  170. ring_info->ring_buffer->read_index =
  171. ring_info->ring_buffer->write_index = 0;
  172. /* Set the feature bit for enabling flow control. */
  173. ring_info->ring_buffer->feature_bits.value = 1;
  174. ring_info->ring_size = page_cnt << PAGE_SHIFT;
  175. ring_info->ring_datasize = ring_info->ring_size -
  176. sizeof(struct hv_ring_buffer);
  177. spin_lock_init(&ring_info->ring_lock);
  178. return 0;
  179. }
  180. /* Cleanup the ring buffer. */
  181. void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
  182. {
  183. vunmap(ring_info->ring_buffer);
  184. }
  185. /* Write to the ring buffer. */
  186. int hv_ringbuffer_write(struct vmbus_channel *channel,
  187. const struct kvec *kv_list, u32 kv_count)
  188. {
  189. int i;
  190. u32 bytes_avail_towrite;
  191. u32 totalbytes_towrite = sizeof(u64);
  192. u32 next_write_location;
  193. u32 old_write;
  194. u64 prev_indices;
  195. unsigned long flags;
  196. struct hv_ring_buffer_info *outring_info = &channel->outbound;
  197. if (channel->rescind)
  198. return -ENODEV;
  199. for (i = 0; i < kv_count; i++)
  200. totalbytes_towrite += kv_list[i].iov_len;
  201. spin_lock_irqsave(&outring_info->ring_lock, flags);
  202. bytes_avail_towrite = hv_get_bytes_to_write(outring_info);
  203. /*
  204. * If there is only room for the packet, assume it is full.
  205. * Otherwise, the next time around, we think the ring buffer
  206. * is empty since the read index == write index.
  207. */
  208. if (bytes_avail_towrite <= totalbytes_towrite) {
  209. spin_unlock_irqrestore(&outring_info->ring_lock, flags);
  210. return -EAGAIN;
  211. }
  212. /* Write to the ring buffer */
  213. next_write_location = hv_get_next_write_location(outring_info);
  214. old_write = next_write_location;
  215. for (i = 0; i < kv_count; i++) {
  216. next_write_location = hv_copyto_ringbuffer(outring_info,
  217. next_write_location,
  218. kv_list[i].iov_base,
  219. kv_list[i].iov_len);
  220. }
  221. /* Set previous packet start */
  222. prev_indices = hv_get_ring_bufferindices(outring_info);
  223. next_write_location = hv_copyto_ringbuffer(outring_info,
  224. next_write_location,
  225. &prev_indices,
  226. sizeof(u64));
  227. /* Issue a full memory barrier before updating the write index */
  228. virt_mb();
  229. /* Now, update the write location */
  230. hv_set_next_write_location(outring_info, next_write_location);
  231. spin_unlock_irqrestore(&outring_info->ring_lock, flags);
  232. hv_signal_on_write(old_write, channel);
  233. if (channel->rescind)
  234. return -ENODEV;
  235. return 0;
  236. }
  237. int hv_ringbuffer_read(struct vmbus_channel *channel,
  238. void *buffer, u32 buflen, u32 *buffer_actual_len,
  239. u64 *requestid, bool raw)
  240. {
  241. struct vmpacket_descriptor *desc;
  242. u32 packetlen, offset;
  243. if (unlikely(buflen == 0))
  244. return -EINVAL;
  245. *buffer_actual_len = 0;
  246. *requestid = 0;
  247. /* Make sure there is something to read */
  248. desc = hv_pkt_iter_first(channel);
  249. if (desc == NULL) {
  250. /*
  251. * No error is set when there is even no header, drivers are
  252. * supposed to analyze buffer_actual_len.
  253. */
  254. return 0;
  255. }
  256. offset = raw ? 0 : (desc->offset8 << 3);
  257. packetlen = (desc->len8 << 3) - offset;
  258. *buffer_actual_len = packetlen;
  259. *requestid = desc->trans_id;
  260. if (unlikely(packetlen > buflen))
  261. return -ENOBUFS;
  262. /* since ring is double mapped, only one copy is necessary */
  263. memcpy(buffer, (const char *)desc + offset, packetlen);
  264. /* Advance ring index to next packet descriptor */
  265. __hv_pkt_iter_next(channel, desc);
  266. /* Notify host of update */
  267. hv_pkt_iter_close(channel);
  268. return 0;
  269. }
  270. /*
  271. * Determine number of bytes available in ring buffer after
  272. * the current iterator (priv_read_index) location.
  273. *
  274. * This is similar to hv_get_bytes_to_read but with private
  275. * read index instead.
  276. */
  277. static u32 hv_pkt_iter_avail(const struct hv_ring_buffer_info *rbi)
  278. {
  279. u32 priv_read_loc = rbi->priv_read_index;
  280. u32 write_loc = READ_ONCE(rbi->ring_buffer->write_index);
  281. if (write_loc >= priv_read_loc)
  282. return write_loc - priv_read_loc;
  283. else
  284. return (rbi->ring_datasize - priv_read_loc) + write_loc;
  285. }
  286. /*
  287. * Get first vmbus packet from ring buffer after read_index
  288. *
  289. * If ring buffer is empty, returns NULL and no other action needed.
  290. */
  291. struct vmpacket_descriptor *hv_pkt_iter_first(struct vmbus_channel *channel)
  292. {
  293. struct hv_ring_buffer_info *rbi = &channel->inbound;
  294. struct vmpacket_descriptor *desc;
  295. if (hv_pkt_iter_avail(rbi) < sizeof(struct vmpacket_descriptor))
  296. return NULL;
  297. desc = hv_get_ring_buffer(rbi) + rbi->priv_read_index;
  298. if (desc)
  299. prefetch((char *)desc + (desc->len8 << 3));
  300. return desc;
  301. }
  302. EXPORT_SYMBOL_GPL(hv_pkt_iter_first);
  303. /*
  304. * Get next vmbus packet from ring buffer.
  305. *
  306. * Advances the current location (priv_read_index) and checks for more
  307. * data. If the end of the ring buffer is reached, then return NULL.
  308. */
  309. struct vmpacket_descriptor *
  310. __hv_pkt_iter_next(struct vmbus_channel *channel,
  311. const struct vmpacket_descriptor *desc)
  312. {
  313. struct hv_ring_buffer_info *rbi = &channel->inbound;
  314. u32 packetlen = desc->len8 << 3;
  315. u32 dsize = rbi->ring_datasize;
  316. /* bump offset to next potential packet */
  317. rbi->priv_read_index += packetlen + VMBUS_PKT_TRAILER;
  318. if (rbi->priv_read_index >= dsize)
  319. rbi->priv_read_index -= dsize;
  320. /* more data? */
  321. return hv_pkt_iter_first(channel);
  322. }
  323. EXPORT_SYMBOL_GPL(__hv_pkt_iter_next);
  324. /*
  325. * Update host ring buffer after iterating over packets.
  326. */
  327. void hv_pkt_iter_close(struct vmbus_channel *channel)
  328. {
  329. struct hv_ring_buffer_info *rbi = &channel->inbound;
  330. u32 orig_write_sz = hv_get_bytes_to_write(rbi);
  331. /*
  332. * Make sure all reads are done before we update the read index since
  333. * the writer may start writing to the read area once the read index
  334. * is updated.
  335. */
  336. virt_rmb();
  337. rbi->ring_buffer->read_index = rbi->priv_read_index;
  338. /*
  339. * Issue a full memory barrier before making the signaling decision.
  340. * Here is the reason for having this barrier:
  341. * If the reading of the pend_sz (in this function)
  342. * were to be reordered and read before we commit the new read
  343. * index (in the calling function) we could
  344. * have a problem. If the host were to set the pending_sz after we
  345. * have sampled pending_sz and go to sleep before we commit the
  346. * read index, we could miss sending the interrupt. Issue a full
  347. * memory barrier to address this.
  348. */
  349. virt_mb();
  350. /* If host has disabled notifications then skip */
  351. if (rbi->ring_buffer->interrupt_mask)
  352. return;
  353. if (rbi->ring_buffer->feature_bits.feat_pending_send_sz) {
  354. u32 pending_sz = READ_ONCE(rbi->ring_buffer->pending_send_sz);
  355. /*
  356. * If there was space before we began iteration,
  357. * then host was not blocked. Also handles case where
  358. * pending_sz is zero then host has nothing pending
  359. * and does not need to be signaled.
  360. */
  361. if (orig_write_sz > pending_sz)
  362. return;
  363. /* If pending write will not fit, don't give false hope. */
  364. if (hv_get_bytes_to_write(rbi) < pending_sz)
  365. return;
  366. }
  367. vmbus_setevent(channel);
  368. }
  369. EXPORT_SYMBOL_GPL(hv_pkt_iter_close);