ring_buffer.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. }
  72. /* Get the next write location for the specified ring buffer. */
  73. static inline u32
  74. hv_get_next_write_location(struct hv_ring_buffer_info *ring_info)
  75. {
  76. u32 next = ring_info->ring_buffer->write_index;
  77. return next;
  78. }
  79. /* Set the next write location for the specified ring buffer. */
  80. static inline void
  81. hv_set_next_write_location(struct hv_ring_buffer_info *ring_info,
  82. u32 next_write_location)
  83. {
  84. ring_info->ring_buffer->write_index = next_write_location;
  85. }
  86. /* Get the next read location for the specified ring buffer. */
  87. static inline u32
  88. hv_get_next_read_location(const struct hv_ring_buffer_info *ring_info)
  89. {
  90. return ring_info->ring_buffer->read_index;
  91. }
  92. /*
  93. * Get the next read location + offset for the specified ring buffer.
  94. * This allows the caller to skip.
  95. */
  96. static inline u32
  97. hv_get_next_readlocation_withoffset(const struct hv_ring_buffer_info *ring_info,
  98. u32 offset)
  99. {
  100. u32 next = ring_info->ring_buffer->read_index;
  101. next += offset;
  102. if (next >= ring_info->ring_datasize)
  103. next -= ring_info->ring_datasize;
  104. return next;
  105. }
  106. /* Set the next read location for the specified ring buffer. */
  107. static inline void
  108. hv_set_next_read_location(struct hv_ring_buffer_info *ring_info,
  109. u32 next_read_location)
  110. {
  111. ring_info->ring_buffer->read_index = next_read_location;
  112. ring_info->priv_read_index = next_read_location;
  113. }
  114. /* Get the size of the ring buffer. */
  115. static inline u32
  116. hv_get_ring_buffersize(const struct hv_ring_buffer_info *ring_info)
  117. {
  118. return ring_info->ring_datasize;
  119. }
  120. /* Get the read and write indices as u64 of the specified ring buffer. */
  121. static inline u64
  122. hv_get_ring_bufferindices(struct hv_ring_buffer_info *ring_info)
  123. {
  124. return (u64)ring_info->ring_buffer->write_index << 32;
  125. }
  126. /*
  127. * Helper routine to copy to source from ring buffer.
  128. * Assume there is enough room. Handles wrap-around in src case only!!
  129. */
  130. static u32 hv_copyfrom_ringbuffer(
  131. const struct hv_ring_buffer_info *ring_info,
  132. void *dest,
  133. u32 destlen,
  134. u32 start_read_offset)
  135. {
  136. void *ring_buffer = hv_get_ring_buffer(ring_info);
  137. u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
  138. memcpy(dest, ring_buffer + start_read_offset, destlen);
  139. start_read_offset += destlen;
  140. if (start_read_offset >= ring_buffer_size)
  141. start_read_offset -= ring_buffer_size;
  142. return start_read_offset;
  143. }
  144. /*
  145. * Helper routine to copy from source to ring buffer.
  146. * Assume there is enough room. Handles wrap-around in dest case only!!
  147. */
  148. static u32 hv_copyto_ringbuffer(
  149. struct hv_ring_buffer_info *ring_info,
  150. u32 start_write_offset,
  151. const void *src,
  152. u32 srclen)
  153. {
  154. void *ring_buffer = hv_get_ring_buffer(ring_info);
  155. u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
  156. memcpy(ring_buffer + start_write_offset, src, srclen);
  157. start_write_offset += srclen;
  158. if (start_write_offset >= ring_buffer_size)
  159. start_write_offset -= ring_buffer_size;
  160. return start_write_offset;
  161. }
  162. /* Get various debug metrics for the specified ring buffer. */
  163. void hv_ringbuffer_get_debuginfo(const struct hv_ring_buffer_info *ring_info,
  164. struct hv_ring_buffer_debug_info *debug_info)
  165. {
  166. u32 bytes_avail_towrite;
  167. u32 bytes_avail_toread;
  168. if (ring_info->ring_buffer) {
  169. hv_get_ringbuffer_availbytes(ring_info,
  170. &bytes_avail_toread,
  171. &bytes_avail_towrite);
  172. debug_info->bytes_avail_toread = bytes_avail_toread;
  173. debug_info->bytes_avail_towrite = bytes_avail_towrite;
  174. debug_info->current_read_index =
  175. ring_info->ring_buffer->read_index;
  176. debug_info->current_write_index =
  177. ring_info->ring_buffer->write_index;
  178. debug_info->current_interrupt_mask =
  179. ring_info->ring_buffer->interrupt_mask;
  180. }
  181. }
  182. EXPORT_SYMBOL_GPL(hv_ringbuffer_get_debuginfo);
  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;
  227. u32 bytes_avail_towrite;
  228. u32 totalbytes_towrite = sizeof(u64);
  229. u32 next_write_location;
  230. u32 old_write;
  231. u64 prev_indices;
  232. unsigned long flags;
  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. spin_lock_irqsave(&outring_info->ring_lock, flags);
  239. bytes_avail_towrite = hv_get_bytes_to_write(outring_info);
  240. /*
  241. * If there is only room for the packet, assume it is full.
  242. * Otherwise, the next time around, we think the ring buffer
  243. * is empty since the read index == write index.
  244. */
  245. if (bytes_avail_towrite <= totalbytes_towrite) {
  246. spin_unlock_irqrestore(&outring_info->ring_lock, flags);
  247. return -EAGAIN;
  248. }
  249. /* Write to the ring buffer */
  250. next_write_location = hv_get_next_write_location(outring_info);
  251. old_write = next_write_location;
  252. for (i = 0; i < kv_count; i++) {
  253. next_write_location = hv_copyto_ringbuffer(outring_info,
  254. next_write_location,
  255. kv_list[i].iov_base,
  256. kv_list[i].iov_len);
  257. }
  258. /* Set previous packet start */
  259. prev_indices = hv_get_ring_bufferindices(outring_info);
  260. next_write_location = hv_copyto_ringbuffer(outring_info,
  261. next_write_location,
  262. &prev_indices,
  263. sizeof(u64));
  264. /* Issue a full memory barrier before updating the write index */
  265. virt_mb();
  266. /* Now, update the write location */
  267. hv_set_next_write_location(outring_info, next_write_location);
  268. spin_unlock_irqrestore(&outring_info->ring_lock, flags);
  269. hv_signal_on_write(old_write, channel);
  270. if (channel->rescind)
  271. return -ENODEV;
  272. return 0;
  273. }
  274. static inline void
  275. init_cached_read_index(struct hv_ring_buffer_info *rbi)
  276. {
  277. rbi->cached_read_index = rbi->ring_buffer->read_index;
  278. }
  279. int hv_ringbuffer_read(struct vmbus_channel *channel,
  280. void *buffer, u32 buflen, u32 *buffer_actual_len,
  281. u64 *requestid, bool raw)
  282. {
  283. u32 bytes_avail_toread;
  284. u32 next_read_location;
  285. u64 prev_indices = 0;
  286. struct vmpacket_descriptor desc;
  287. u32 offset;
  288. u32 packetlen;
  289. struct hv_ring_buffer_info *inring_info = &channel->inbound;
  290. if (buflen <= 0)
  291. return -EINVAL;
  292. *buffer_actual_len = 0;
  293. *requestid = 0;
  294. bytes_avail_toread = hv_get_bytes_to_read(inring_info);
  295. /* Make sure there is something to read */
  296. if (bytes_avail_toread < sizeof(desc)) {
  297. /*
  298. * No error is set when there is even no header, drivers are
  299. * supposed to analyze buffer_actual_len.
  300. */
  301. return 0;
  302. }
  303. init_cached_read_index(inring_info);
  304. next_read_location = hv_get_next_read_location(inring_info);
  305. next_read_location = hv_copyfrom_ringbuffer(inring_info, &desc,
  306. sizeof(desc),
  307. next_read_location);
  308. offset = raw ? 0 : (desc.offset8 << 3);
  309. packetlen = (desc.len8 << 3) - offset;
  310. *buffer_actual_len = packetlen;
  311. *requestid = desc.trans_id;
  312. if (bytes_avail_toread < packetlen + offset)
  313. return -EAGAIN;
  314. if (packetlen > buflen)
  315. return -ENOBUFS;
  316. next_read_location =
  317. hv_get_next_readlocation_withoffset(inring_info, offset);
  318. next_read_location = hv_copyfrom_ringbuffer(inring_info,
  319. buffer,
  320. packetlen,
  321. next_read_location);
  322. next_read_location = hv_copyfrom_ringbuffer(inring_info,
  323. &prev_indices,
  324. sizeof(u64),
  325. next_read_location);
  326. /*
  327. * Make sure all reads are done before we update the read index since
  328. * the writer may start writing to the read area once the read index
  329. * is updated.
  330. */
  331. virt_mb();
  332. /* Update the read index */
  333. hv_set_next_read_location(inring_info, next_read_location);
  334. hv_signal_on_read(channel);
  335. return 0;
  336. }
  337. /*
  338. * Determine number of bytes available in ring buffer after
  339. * the current iterator (priv_read_index) location.
  340. *
  341. * This is similar to hv_get_bytes_to_read but with private
  342. * read index instead.
  343. */
  344. static u32 hv_pkt_iter_avail(const struct hv_ring_buffer_info *rbi)
  345. {
  346. u32 priv_read_loc = rbi->priv_read_index;
  347. u32 write_loc = READ_ONCE(rbi->ring_buffer->write_index);
  348. if (write_loc >= priv_read_loc)
  349. return write_loc - priv_read_loc;
  350. else
  351. return (rbi->ring_datasize - priv_read_loc) + write_loc;
  352. }
  353. /*
  354. * Get first vmbus packet from ring buffer after read_index
  355. *
  356. * If ring buffer is empty, returns NULL and no other action needed.
  357. */
  358. struct vmpacket_descriptor *hv_pkt_iter_first(struct vmbus_channel *channel)
  359. {
  360. struct hv_ring_buffer_info *rbi = &channel->inbound;
  361. /* set state for later hv_signal_on_read() */
  362. init_cached_read_index(rbi);
  363. if (hv_pkt_iter_avail(rbi) < sizeof(struct vmpacket_descriptor))
  364. return NULL;
  365. return hv_get_ring_buffer(rbi) + rbi->priv_read_index;
  366. }
  367. EXPORT_SYMBOL_GPL(hv_pkt_iter_first);
  368. /*
  369. * Get next vmbus packet from ring buffer.
  370. *
  371. * Advances the current location (priv_read_index) and checks for more
  372. * data. If the end of the ring buffer is reached, then return NULL.
  373. */
  374. struct vmpacket_descriptor *
  375. __hv_pkt_iter_next(struct vmbus_channel *channel,
  376. const struct vmpacket_descriptor *desc)
  377. {
  378. struct hv_ring_buffer_info *rbi = &channel->inbound;
  379. u32 packetlen = desc->len8 << 3;
  380. u32 dsize = rbi->ring_datasize;
  381. /* bump offset to next potential packet */
  382. rbi->priv_read_index += packetlen + VMBUS_PKT_TRAILER;
  383. if (rbi->priv_read_index >= dsize)
  384. rbi->priv_read_index -= dsize;
  385. /* more data? */
  386. if (hv_pkt_iter_avail(rbi) < sizeof(struct vmpacket_descriptor))
  387. return NULL;
  388. else
  389. return hv_get_ring_buffer(rbi) + rbi->priv_read_index;
  390. }
  391. EXPORT_SYMBOL_GPL(__hv_pkt_iter_next);
  392. /*
  393. * Update host ring buffer after iterating over packets.
  394. */
  395. void hv_pkt_iter_close(struct vmbus_channel *channel)
  396. {
  397. struct hv_ring_buffer_info *rbi = &channel->inbound;
  398. /*
  399. * Make sure all reads are done before we update the read index since
  400. * the writer may start writing to the read area once the read index
  401. * is updated.
  402. */
  403. virt_rmb();
  404. rbi->ring_buffer->read_index = rbi->priv_read_index;
  405. hv_signal_on_read(channel);
  406. }
  407. EXPORT_SYMBOL_GPL(hv_pkt_iter_close);