ring_buffer.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 <linux/prefetch.h>
  32. #include "hyperv_vmbus.h"
  33. #define VMBUS_PKT_TRAILER 8
  34. /*
  35. * When we write to the ring buffer, check if the host needs to
  36. * be signaled. Here is the details of this protocol:
  37. *
  38. * 1. The host guarantees that while it is draining the
  39. * ring buffer, it will set the interrupt_mask to
  40. * indicate it does not need to be interrupted when
  41. * new data is placed.
  42. *
  43. * 2. The host guarantees that it will completely drain
  44. * the ring buffer before exiting the read loop. Further,
  45. * once the ring buffer is empty, it will clear the
  46. * interrupt_mask and re-check to see if new data has
  47. * arrived.
  48. *
  49. * KYS: Oct. 30, 2016:
  50. * It looks like Windows hosts have logic to deal with DOS attacks that
  51. * can be triggered if it receives interrupts when it is not expecting
  52. * the interrupt. The host expects interrupts only when the ring
  53. * transitions from empty to non-empty (or full to non full on the guest
  54. * to host ring).
  55. * So, base the signaling decision solely on the ring state until the
  56. * host logic is fixed.
  57. */
  58. static void hv_signal_on_write(u32 old_write, struct vmbus_channel *channel)
  59. {
  60. struct hv_ring_buffer_info *rbi = &channel->outbound;
  61. virt_mb();
  62. if (READ_ONCE(rbi->ring_buffer->interrupt_mask))
  63. return;
  64. /* check interrupt_mask before read_index */
  65. virt_rmb();
  66. /*
  67. * This is the only case we need to signal when the
  68. * ring transitions from being empty to non-empty.
  69. */
  70. if (old_write == READ_ONCE(rbi->ring_buffer->read_index))
  71. vmbus_setevent(channel);
  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. /* Set the next read location for the specified ring buffer. */
  88. static inline void
  89. hv_set_next_read_location(struct hv_ring_buffer_info *ring_info,
  90. u32 next_read_location)
  91. {
  92. ring_info->ring_buffer->read_index = next_read_location;
  93. ring_info->priv_read_index = next_read_location;
  94. }
  95. /* Get the size of the ring buffer. */
  96. static inline u32
  97. hv_get_ring_buffersize(const struct hv_ring_buffer_info *ring_info)
  98. {
  99. return ring_info->ring_datasize;
  100. }
  101. /* Get the read and write indices as u64 of the specified ring buffer. */
  102. static inline u64
  103. hv_get_ring_bufferindices(struct hv_ring_buffer_info *ring_info)
  104. {
  105. return (u64)ring_info->ring_buffer->write_index << 32;
  106. }
  107. /*
  108. * Helper routine to copy from source to ring buffer.
  109. * Assume there is enough room. Handles wrap-around in dest case only!!
  110. */
  111. static u32 hv_copyto_ringbuffer(
  112. struct hv_ring_buffer_info *ring_info,
  113. u32 start_write_offset,
  114. const void *src,
  115. u32 srclen)
  116. {
  117. void *ring_buffer = hv_get_ring_buffer(ring_info);
  118. u32 ring_buffer_size = hv_get_ring_buffersize(ring_info);
  119. memcpy(ring_buffer + start_write_offset, src, srclen);
  120. start_write_offset += srclen;
  121. if (start_write_offset >= ring_buffer_size)
  122. start_write_offset -= ring_buffer_size;
  123. return start_write_offset;
  124. }
  125. /*
  126. *
  127. * hv_get_ringbuffer_availbytes()
  128. *
  129. * Get number of bytes available to read and to write to
  130. * for the specified ring buffer
  131. */
  132. static void
  133. hv_get_ringbuffer_availbytes(const struct hv_ring_buffer_info *rbi,
  134. u32 *read, u32 *write)
  135. {
  136. u32 read_loc, write_loc, dsize;
  137. /* Capture the read/write indices before they changed */
  138. read_loc = READ_ONCE(rbi->ring_buffer->read_index);
  139. write_loc = READ_ONCE(rbi->ring_buffer->write_index);
  140. dsize = rbi->ring_datasize;
  141. *write = write_loc >= read_loc ? dsize - (write_loc - read_loc) :
  142. read_loc - write_loc;
  143. *read = dsize - *write;
  144. }
  145. /* Get various debug metrics for the specified ring buffer. */
  146. void hv_ringbuffer_get_debuginfo(const struct hv_ring_buffer_info *ring_info,
  147. struct hv_ring_buffer_debug_info *debug_info)
  148. {
  149. u32 bytes_avail_towrite;
  150. u32 bytes_avail_toread;
  151. if (ring_info->ring_buffer) {
  152. hv_get_ringbuffer_availbytes(ring_info,
  153. &bytes_avail_toread,
  154. &bytes_avail_towrite);
  155. debug_info->bytes_avail_toread = bytes_avail_toread;
  156. debug_info->bytes_avail_towrite = bytes_avail_towrite;
  157. debug_info->current_read_index =
  158. ring_info->ring_buffer->read_index;
  159. debug_info->current_write_index =
  160. ring_info->ring_buffer->write_index;
  161. debug_info->current_interrupt_mask =
  162. ring_info->ring_buffer->interrupt_mask;
  163. }
  164. }
  165. EXPORT_SYMBOL_GPL(hv_ringbuffer_get_debuginfo);
  166. /* Initialize the ring buffer. */
  167. int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
  168. struct page *pages, u32 page_cnt)
  169. {
  170. int i;
  171. struct page **pages_wraparound;
  172. BUILD_BUG_ON((sizeof(struct hv_ring_buffer) != PAGE_SIZE));
  173. memset(ring_info, 0, sizeof(struct hv_ring_buffer_info));
  174. /*
  175. * First page holds struct hv_ring_buffer, do wraparound mapping for
  176. * the rest.
  177. */
  178. pages_wraparound = kcalloc(page_cnt * 2 - 1, sizeof(struct page *),
  179. GFP_KERNEL);
  180. if (!pages_wraparound)
  181. return -ENOMEM;
  182. pages_wraparound[0] = pages;
  183. for (i = 0; i < 2 * (page_cnt - 1); i++)
  184. pages_wraparound[i + 1] = &pages[i % (page_cnt - 1) + 1];
  185. ring_info->ring_buffer = (struct hv_ring_buffer *)
  186. vmap(pages_wraparound, page_cnt * 2 - 1, VM_MAP, PAGE_KERNEL);
  187. kfree(pages_wraparound);
  188. if (!ring_info->ring_buffer)
  189. return -ENOMEM;
  190. ring_info->ring_buffer->read_index =
  191. ring_info->ring_buffer->write_index = 0;
  192. /* Set the feature bit for enabling flow control. */
  193. ring_info->ring_buffer->feature_bits.value = 1;
  194. ring_info->ring_size = page_cnt << PAGE_SHIFT;
  195. ring_info->ring_size_div10_reciprocal =
  196. reciprocal_value(ring_info->ring_size / 10);
  197. ring_info->ring_datasize = ring_info->ring_size -
  198. sizeof(struct hv_ring_buffer);
  199. spin_lock_init(&ring_info->ring_lock);
  200. return 0;
  201. }
  202. /* Cleanup the ring buffer. */
  203. void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
  204. {
  205. vunmap(ring_info->ring_buffer);
  206. ring_info->ring_buffer = NULL;
  207. }
  208. /* Write to the ring buffer. */
  209. int hv_ringbuffer_write(struct vmbus_channel *channel,
  210. const struct kvec *kv_list, u32 kv_count)
  211. {
  212. int i;
  213. u32 bytes_avail_towrite;
  214. u32 totalbytes_towrite = sizeof(u64);
  215. u32 next_write_location;
  216. u32 old_write;
  217. u64 prev_indices;
  218. unsigned long flags;
  219. struct hv_ring_buffer_info *outring_info = &channel->outbound;
  220. if (channel->rescind)
  221. return -ENODEV;
  222. for (i = 0; i < kv_count; i++)
  223. totalbytes_towrite += kv_list[i].iov_len;
  224. spin_lock_irqsave(&outring_info->ring_lock, flags);
  225. bytes_avail_towrite = hv_get_bytes_to_write(outring_info);
  226. /*
  227. * If there is only room for the packet, assume it is full.
  228. * Otherwise, the next time around, we think the ring buffer
  229. * is empty since the read index == write index.
  230. */
  231. if (bytes_avail_towrite <= totalbytes_towrite) {
  232. spin_unlock_irqrestore(&outring_info->ring_lock, flags);
  233. return -EAGAIN;
  234. }
  235. /* Write to the ring buffer */
  236. next_write_location = hv_get_next_write_location(outring_info);
  237. old_write = next_write_location;
  238. for (i = 0; i < kv_count; i++) {
  239. next_write_location = hv_copyto_ringbuffer(outring_info,
  240. next_write_location,
  241. kv_list[i].iov_base,
  242. kv_list[i].iov_len);
  243. }
  244. /* Set previous packet start */
  245. prev_indices = hv_get_ring_bufferindices(outring_info);
  246. next_write_location = hv_copyto_ringbuffer(outring_info,
  247. next_write_location,
  248. &prev_indices,
  249. sizeof(u64));
  250. /* Issue a full memory barrier before updating the write index */
  251. virt_mb();
  252. /* Now, update the write location */
  253. hv_set_next_write_location(outring_info, next_write_location);
  254. spin_unlock_irqrestore(&outring_info->ring_lock, flags);
  255. hv_signal_on_write(old_write, channel);
  256. if (channel->rescind)
  257. return -ENODEV;
  258. return 0;
  259. }
  260. int hv_ringbuffer_read(struct vmbus_channel *channel,
  261. void *buffer, u32 buflen, u32 *buffer_actual_len,
  262. u64 *requestid, bool raw)
  263. {
  264. struct vmpacket_descriptor *desc;
  265. u32 packetlen, offset;
  266. if (unlikely(buflen == 0))
  267. return -EINVAL;
  268. *buffer_actual_len = 0;
  269. *requestid = 0;
  270. /* Make sure there is something to read */
  271. desc = hv_pkt_iter_first(channel);
  272. if (desc == NULL) {
  273. /*
  274. * No error is set when there is even no header, drivers are
  275. * supposed to analyze buffer_actual_len.
  276. */
  277. return 0;
  278. }
  279. offset = raw ? 0 : (desc->offset8 << 3);
  280. packetlen = (desc->len8 << 3) - offset;
  281. *buffer_actual_len = packetlen;
  282. *requestid = desc->trans_id;
  283. if (unlikely(packetlen > buflen))
  284. return -ENOBUFS;
  285. /* since ring is double mapped, only one copy is necessary */
  286. memcpy(buffer, (const char *)desc + offset, packetlen);
  287. /* Advance ring index to next packet descriptor */
  288. __hv_pkt_iter_next(channel, desc);
  289. /* Notify host of update */
  290. hv_pkt_iter_close(channel);
  291. return 0;
  292. }
  293. /*
  294. * Determine number of bytes available in ring buffer after
  295. * the current iterator (priv_read_index) location.
  296. *
  297. * This is similar to hv_get_bytes_to_read but with private
  298. * read index instead.
  299. */
  300. static u32 hv_pkt_iter_avail(const struct hv_ring_buffer_info *rbi)
  301. {
  302. u32 priv_read_loc = rbi->priv_read_index;
  303. u32 write_loc = READ_ONCE(rbi->ring_buffer->write_index);
  304. if (write_loc >= priv_read_loc)
  305. return write_loc - priv_read_loc;
  306. else
  307. return (rbi->ring_datasize - priv_read_loc) + write_loc;
  308. }
  309. /*
  310. * Get first vmbus packet from ring buffer after read_index
  311. *
  312. * If ring buffer is empty, returns NULL and no other action needed.
  313. */
  314. struct vmpacket_descriptor *hv_pkt_iter_first(struct vmbus_channel *channel)
  315. {
  316. struct hv_ring_buffer_info *rbi = &channel->inbound;
  317. struct vmpacket_descriptor *desc;
  318. if (hv_pkt_iter_avail(rbi) < sizeof(struct vmpacket_descriptor))
  319. return NULL;
  320. desc = hv_get_ring_buffer(rbi) + rbi->priv_read_index;
  321. if (desc)
  322. prefetch((char *)desc + (desc->len8 << 3));
  323. return desc;
  324. }
  325. EXPORT_SYMBOL_GPL(hv_pkt_iter_first);
  326. /*
  327. * Get next vmbus packet from ring buffer.
  328. *
  329. * Advances the current location (priv_read_index) and checks for more
  330. * data. If the end of the ring buffer is reached, then return NULL.
  331. */
  332. struct vmpacket_descriptor *
  333. __hv_pkt_iter_next(struct vmbus_channel *channel,
  334. const struct vmpacket_descriptor *desc)
  335. {
  336. struct hv_ring_buffer_info *rbi = &channel->inbound;
  337. u32 packetlen = desc->len8 << 3;
  338. u32 dsize = rbi->ring_datasize;
  339. /* bump offset to next potential packet */
  340. rbi->priv_read_index += packetlen + VMBUS_PKT_TRAILER;
  341. if (rbi->priv_read_index >= dsize)
  342. rbi->priv_read_index -= dsize;
  343. /* more data? */
  344. return hv_pkt_iter_first(channel);
  345. }
  346. EXPORT_SYMBOL_GPL(__hv_pkt_iter_next);
  347. /* How many bytes were read in this iterator cycle */
  348. static u32 hv_pkt_iter_bytes_read(const struct hv_ring_buffer_info *rbi,
  349. u32 start_read_index)
  350. {
  351. if (rbi->priv_read_index >= start_read_index)
  352. return rbi->priv_read_index - start_read_index;
  353. else
  354. return rbi->ring_datasize - start_read_index +
  355. rbi->priv_read_index;
  356. }
  357. /*
  358. * Update host ring buffer after iterating over packets. If the host has
  359. * stopped queuing new entries because it found the ring buffer full, and
  360. * sufficient space is being freed up, signal the host. But be careful to
  361. * only signal the host when necessary, both for performance reasons and
  362. * because Hyper-V protects itself by throttling guests that signal
  363. * inappropriately.
  364. *
  365. * Determining when to signal is tricky. There are three key data inputs
  366. * that must be handled in this order to avoid race conditions:
  367. *
  368. * 1. Update the read_index
  369. * 2. Read the pending_send_sz
  370. * 3. Read the current write_index
  371. *
  372. * The interrupt_mask is not used to determine when to signal. The
  373. * interrupt_mask is used only on the guest->host ring buffer when
  374. * sending requests to the host. The host does not use it on the host->
  375. * guest ring buffer to indicate whether it should be signaled.
  376. */
  377. void hv_pkt_iter_close(struct vmbus_channel *channel)
  378. {
  379. struct hv_ring_buffer_info *rbi = &channel->inbound;
  380. u32 curr_write_sz, pending_sz, bytes_read, start_read_index;
  381. /*
  382. * Make sure all reads are done before we update the read index since
  383. * the writer may start writing to the read area once the read index
  384. * is updated.
  385. */
  386. virt_rmb();
  387. start_read_index = rbi->ring_buffer->read_index;
  388. rbi->ring_buffer->read_index = rbi->priv_read_index;
  389. /*
  390. * Older versions of Hyper-V (before WS2102 and Win8) do not
  391. * implement pending_send_sz and simply poll if the host->guest
  392. * ring buffer is full. No signaling is needed or expected.
  393. */
  394. if (!rbi->ring_buffer->feature_bits.feat_pending_send_sz)
  395. return;
  396. /*
  397. * Issue a full memory barrier before making the signaling decision.
  398. * If reading pending_send_sz were to be reordered and happen
  399. * before we commit the new read_index, a race could occur. If the
  400. * host were to set the pending_send_sz after we have sampled
  401. * pending_send_sz, and the ring buffer blocks before we commit the
  402. * read index, we could miss sending the interrupt. Issue a full
  403. * memory barrier to address this.
  404. */
  405. virt_mb();
  406. /*
  407. * If the pending_send_sz is zero, then the ring buffer is not
  408. * blocked and there is no need to signal. This is far by the
  409. * most common case, so exit quickly for best performance.
  410. */
  411. pending_sz = READ_ONCE(rbi->ring_buffer->pending_send_sz);
  412. if (!pending_sz)
  413. return;
  414. /*
  415. * Ensure the read of write_index in hv_get_bytes_to_write()
  416. * happens after the read of pending_send_sz.
  417. */
  418. virt_rmb();
  419. curr_write_sz = hv_get_bytes_to_write(rbi);
  420. bytes_read = hv_pkt_iter_bytes_read(rbi, start_read_index);
  421. /*
  422. * We want to signal the host only if we're transitioning
  423. * from a "not enough free space" state to a "enough free
  424. * space" state. For example, it's possible that this function
  425. * could run and free up enough space to signal the host, and then
  426. * run again and free up additional space before the host has a
  427. * chance to clear the pending_send_sz. The 2nd invocation would
  428. * be a null transition from "enough free space" to "enough free
  429. * space", which doesn't warrant a signal.
  430. *
  431. * Exactly filling the ring buffer is treated as "not enough
  432. * space". The ring buffer always must have at least one byte
  433. * empty so the empty and full conditions are distinguishable.
  434. * hv_get_bytes_to_write() doesn't fully tell the truth in
  435. * this regard.
  436. *
  437. * So first check if we were in the "enough free space" state
  438. * before we began the iteration. If so, the host was not
  439. * blocked, and there's no need to signal.
  440. */
  441. if (curr_write_sz - bytes_read > pending_sz)
  442. return;
  443. /*
  444. * Similarly, if the new state is "not enough space", then
  445. * there's no need to signal.
  446. */
  447. if (curr_write_sz <= pending_sz)
  448. return;
  449. vmbus_setevent(channel);
  450. }
  451. EXPORT_SYMBOL_GPL(hv_pkt_iter_close);