ring_buffer.c 11 KB

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