ring_buffer.c 13 KB

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