ring_buffer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 "hyperv_vmbus.h"
  32. /*
  33. * When we write to the ring buffer, check if the host needs to
  34. * be signaled. Here is the details of this protocol:
  35. *
  36. * 1. The host guarantees that while it is draining the
  37. * ring buffer, it will set the interrupt_mask to
  38. * indicate it does not need to be interrupted when
  39. * new data is placed.
  40. *
  41. * 2. The host guarantees that it will completely drain
  42. * the ring buffer before exiting the read loop. Further,
  43. * once the ring buffer is empty, it will clear the
  44. * interrupt_mask and re-check to see if new data has
  45. * arrived.
  46. *
  47. * KYS: Oct. 30, 2016:
  48. * It looks like Windows hosts have logic to deal with DOS attacks that
  49. * can be triggered if it receives interrupts when it is not expecting
  50. * the interrupt. The host expects interrupts only when the ring
  51. * transitions from empty to non-empty (or full to non full on the guest
  52. * to host ring).
  53. * So, base the signaling decision solely on the ring state until the
  54. * host logic is fixed.
  55. */
  56. static void hv_signal_on_write(u32 old_write, struct vmbus_channel *channel)
  57. {
  58. struct hv_ring_buffer_info *rbi = &channel->outbound;
  59. virt_mb();
  60. if (READ_ONCE(rbi->ring_buffer->interrupt_mask))
  61. return;
  62. /* check interrupt_mask before read_index */
  63. virt_rmb();
  64. /*
  65. * This is the only case we need to signal when the
  66. * ring transitions from being empty to non-empty.
  67. */
  68. if (old_write == READ_ONCE(rbi->ring_buffer->read_index))
  69. vmbus_setevent(channel);
  70. }
  71. /* Get the next write location for the specified ring buffer. */
  72. static inline u32
  73. hv_get_next_write_location(struct hv_ring_buffer_info *ring_info)
  74. {
  75. u32 next = ring_info->ring_buffer->write_index;
  76. return next;
  77. }
  78. /* Set the next write location for the specified ring buffer. */
  79. static inline void
  80. hv_set_next_write_location(struct hv_ring_buffer_info *ring_info,
  81. u32 next_write_location)
  82. {
  83. ring_info->ring_buffer->write_index = next_write_location;
  84. }
  85. /* Get the next read location for the specified ring buffer. */
  86. static inline u32
  87. hv_get_next_read_location(const struct hv_ring_buffer_info *ring_info)
  88. {
  89. return ring_info->ring_buffer->read_index;
  90. }
  91. /*
  92. * Get the next read location + offset for the specified ring buffer.
  93. * This allows the caller to skip.
  94. */
  95. static inline u32
  96. hv_get_next_readlocation_withoffset(const struct hv_ring_buffer_info *ring_info,
  97. u32 offset)
  98. {
  99. u32 next = ring_info->ring_buffer->read_index;
  100. next += offset;
  101. if (next >= ring_info->ring_datasize)
  102. next -= ring_info->ring_datasize;
  103. return next;
  104. }
  105. /* Set the next read location for the specified ring buffer. */
  106. static inline void
  107. hv_set_next_read_location(struct hv_ring_buffer_info *ring_info,
  108. u32 next_read_location)
  109. {
  110. ring_info->ring_buffer->read_index = next_read_location;
  111. ring_info->priv_read_index = next_read_location;
  112. }
  113. /* Get the size of the ring buffer. */
  114. static inline u32
  115. hv_get_ring_buffersize(const struct hv_ring_buffer_info *ring_info)
  116. {
  117. return ring_info->ring_datasize;
  118. }
  119. /* Get the read and write indices as u64 of the specified ring buffer. */
  120. static inline u64
  121. hv_get_ring_bufferindices(struct hv_ring_buffer_info *ring_info)
  122. {
  123. return (u64)ring_info->ring_buffer->write_index << 32;
  124. }
  125. /*
  126. * Helper routine to copy to source from ring buffer.
  127. * Assume there is enough room. Handles wrap-around in src case only!!
  128. */
  129. static u32 hv_copyfrom_ringbuffer(
  130. const struct hv_ring_buffer_info *ring_info,
  131. void *dest,
  132. u32 destlen,
  133. u32 start_read_offset)
  134. {
  135. void *ring_buffer = hv_get_ring_buffer(ring_info);
  136. u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
  137. memcpy(dest, ring_buffer + start_read_offset, destlen);
  138. start_read_offset += destlen;
  139. if (start_read_offset >= ring_buffer_size)
  140. start_read_offset -= ring_buffer_size;
  141. return start_read_offset;
  142. }
  143. /*
  144. * Helper routine to copy from source to ring buffer.
  145. * Assume there is enough room. Handles wrap-around in dest case only!!
  146. */
  147. static u32 hv_copyto_ringbuffer(
  148. struct hv_ring_buffer_info *ring_info,
  149. u32 start_write_offset,
  150. const void *src,
  151. u32 srclen)
  152. {
  153. void *ring_buffer = hv_get_ring_buffer(ring_info);
  154. u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
  155. memcpy(ring_buffer + start_write_offset, src, srclen);
  156. start_write_offset += srclen;
  157. if (start_write_offset >= ring_buffer_size)
  158. start_write_offset -= ring_buffer_size;
  159. return start_write_offset;
  160. }
  161. /* Get various debug metrics for the specified ring buffer. */
  162. void hv_ringbuffer_get_debuginfo(const struct hv_ring_buffer_info *ring_info,
  163. struct hv_ring_buffer_debug_info *debug_info)
  164. {
  165. u32 bytes_avail_towrite;
  166. u32 bytes_avail_toread;
  167. if (ring_info->ring_buffer) {
  168. hv_get_ringbuffer_availbytes(ring_info,
  169. &bytes_avail_toread,
  170. &bytes_avail_towrite);
  171. debug_info->bytes_avail_toread = bytes_avail_toread;
  172. debug_info->bytes_avail_towrite = bytes_avail_towrite;
  173. debug_info->current_read_index =
  174. ring_info->ring_buffer->read_index;
  175. debug_info->current_write_index =
  176. ring_info->ring_buffer->write_index;
  177. debug_info->current_interrupt_mask =
  178. ring_info->ring_buffer->interrupt_mask;
  179. }
  180. }
  181. EXPORT_SYMBOL_GPL(hv_ringbuffer_get_debuginfo);
  182. /* Initialize the ring buffer. */
  183. int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
  184. struct page *pages, u32 page_cnt)
  185. {
  186. int i;
  187. struct page **pages_wraparound;
  188. BUILD_BUG_ON((sizeof(struct hv_ring_buffer) != PAGE_SIZE));
  189. memset(ring_info, 0, sizeof(struct hv_ring_buffer_info));
  190. /*
  191. * First page holds struct hv_ring_buffer, do wraparound mapping for
  192. * the rest.
  193. */
  194. pages_wraparound = kzalloc(sizeof(struct page *) * (page_cnt * 2 - 1),
  195. GFP_KERNEL);
  196. if (!pages_wraparound)
  197. return -ENOMEM;
  198. pages_wraparound[0] = pages;
  199. for (i = 0; i < 2 * (page_cnt - 1); i++)
  200. pages_wraparound[i + 1] = &pages[i % (page_cnt - 1) + 1];
  201. ring_info->ring_buffer = (struct hv_ring_buffer *)
  202. vmap(pages_wraparound, page_cnt * 2 - 1, VM_MAP, PAGE_KERNEL);
  203. kfree(pages_wraparound);
  204. if (!ring_info->ring_buffer)
  205. return -ENOMEM;
  206. ring_info->ring_buffer->read_index =
  207. ring_info->ring_buffer->write_index = 0;
  208. /* Set the feature bit for enabling flow control. */
  209. ring_info->ring_buffer->feature_bits.value = 1;
  210. ring_info->ring_size = page_cnt << PAGE_SHIFT;
  211. ring_info->ring_datasize = ring_info->ring_size -
  212. sizeof(struct hv_ring_buffer);
  213. spin_lock_init(&ring_info->ring_lock);
  214. return 0;
  215. }
  216. /* Cleanup the ring buffer. */
  217. void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
  218. {
  219. vunmap(ring_info->ring_buffer);
  220. }
  221. /* Write to the ring buffer. */
  222. int hv_ringbuffer_write(struct vmbus_channel *channel,
  223. const struct kvec *kv_list, u32 kv_count)
  224. {
  225. int i;
  226. u32 bytes_avail_towrite;
  227. u32 totalbytes_towrite = sizeof(u64);
  228. u32 next_write_location;
  229. u32 old_write;
  230. u64 prev_indices;
  231. unsigned long flags;
  232. struct hv_ring_buffer_info *outring_info = &channel->outbound;
  233. if (channel->rescind)
  234. return -ENODEV;
  235. for (i = 0; i < kv_count; i++)
  236. totalbytes_towrite += kv_list[i].iov_len;
  237. spin_lock_irqsave(&outring_info->ring_lock, flags);
  238. bytes_avail_towrite = hv_get_bytes_to_write(outring_info);
  239. /*
  240. * If there is only room for the packet, assume it is full.
  241. * Otherwise, the next time around, we think the ring buffer
  242. * is empty since the read index == write index.
  243. */
  244. if (bytes_avail_towrite <= totalbytes_towrite) {
  245. spin_unlock_irqrestore(&outring_info->ring_lock, flags);
  246. return -EAGAIN;
  247. }
  248. /* Write to the ring buffer */
  249. next_write_location = hv_get_next_write_location(outring_info);
  250. old_write = next_write_location;
  251. for (i = 0; i < kv_count; i++) {
  252. next_write_location = hv_copyto_ringbuffer(outring_info,
  253. next_write_location,
  254. kv_list[i].iov_base,
  255. kv_list[i].iov_len);
  256. }
  257. /* Set previous packet start */
  258. prev_indices = hv_get_ring_bufferindices(outring_info);
  259. next_write_location = hv_copyto_ringbuffer(outring_info,
  260. next_write_location,
  261. &prev_indices,
  262. sizeof(u64));
  263. /* Issue a full memory barrier before updating the write index */
  264. virt_mb();
  265. /* Now, update the write location */
  266. hv_set_next_write_location(outring_info, next_write_location);
  267. spin_unlock_irqrestore(&outring_info->ring_lock, flags);
  268. hv_signal_on_write(old_write, channel);
  269. if (channel->rescind)
  270. return -ENODEV;
  271. return 0;
  272. }
  273. int hv_ringbuffer_read(struct vmbus_channel *channel,
  274. void *buffer, u32 buflen, u32 *buffer_actual_len,
  275. u64 *requestid, bool raw)
  276. {
  277. u32 bytes_avail_toread;
  278. u32 next_read_location;
  279. u64 prev_indices = 0;
  280. struct vmpacket_descriptor desc;
  281. u32 offset;
  282. u32 packetlen;
  283. struct hv_ring_buffer_info *inring_info = &channel->inbound;
  284. if (buflen <= 0)
  285. return -EINVAL;
  286. *buffer_actual_len = 0;
  287. *requestid = 0;
  288. bytes_avail_toread = hv_get_bytes_to_read(inring_info);
  289. /* Make sure there is something to read */
  290. if (bytes_avail_toread < sizeof(desc)) {
  291. /*
  292. * No error is set when there is even no header, drivers are
  293. * supposed to analyze buffer_actual_len.
  294. */
  295. return 0;
  296. }
  297. init_cached_read_index(channel);
  298. next_read_location = hv_get_next_read_location(inring_info);
  299. next_read_location = hv_copyfrom_ringbuffer(inring_info, &desc,
  300. sizeof(desc),
  301. next_read_location);
  302. offset = raw ? 0 : (desc.offset8 << 3);
  303. packetlen = (desc.len8 << 3) - offset;
  304. *buffer_actual_len = packetlen;
  305. *requestid = desc.trans_id;
  306. if (bytes_avail_toread < packetlen + offset)
  307. return -EAGAIN;
  308. if (packetlen > buflen)
  309. return -ENOBUFS;
  310. next_read_location =
  311. hv_get_next_readlocation_withoffset(inring_info, offset);
  312. next_read_location = hv_copyfrom_ringbuffer(inring_info,
  313. buffer,
  314. packetlen,
  315. next_read_location);
  316. next_read_location = hv_copyfrom_ringbuffer(inring_info,
  317. &prev_indices,
  318. sizeof(u64),
  319. next_read_location);
  320. /*
  321. * Make sure all reads are done before we update the read index since
  322. * the writer may start writing to the read area once the read index
  323. * is updated.
  324. */
  325. virt_mb();
  326. /* Update the read index */
  327. hv_set_next_read_location(inring_info, next_read_location);
  328. hv_signal_on_read(channel);
  329. return 0;
  330. }