ring_buffer.c 11 KB

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