1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * 4 * Copyright (c) 2009, Microsoft Corporation. 5 * 6 * Authors: 7 * Haiyang Zhang <haiyangz@microsoft.com> 8 * Hank Janssen <hjanssen@microsoft.com> 9 * K. Y. Srinivasan <kys@microsoft.com> 10 */ 11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 12 13 #include <linux/kernel.h> 14 #include <linux/mm.h> 15 #include <linux/hyperv.h> 16 #include <linux/uio.h> 17 #include <linux/vmalloc.h> 18 #include <linux/slab.h> 19 #include <linux/prefetch.h> 20 #include <linux/io.h> 21 #include <linux/export.h> 22 #include <asm/mshyperv.h> 23 24 #include "hyperv_vmbus.h" 25 26 #define VMBUS_PKT_TRAILER 8 27 28 /* 29 * When we write to the ring buffer, check if the host needs to 30 * be signaled. Here is the details of this protocol: 31 * 32 * 1. The host guarantees that while it is draining the 33 * ring buffer, it will set the interrupt_mask to 34 * indicate it does not need to be interrupted when 35 * new data is placed. 36 * 37 * 2. The host guarantees that it will completely drain 38 * the ring buffer before exiting the read loop. Further, 39 * once the ring buffer is empty, it will clear the 40 * interrupt_mask and re-check to see if new data has 41 * arrived. 42 * 43 * KYS: Oct. 30, 2016: 44 * It looks like Windows hosts have logic to deal with DOS attacks that 45 * can be triggered if it receives interrupts when it is not expecting 46 * the interrupt. The host expects interrupts only when the ring 47 * transitions from empty to non-empty (or full to non full on the guest 48 * to host ring). 49 * So, base the signaling decision solely on the ring state until the 50 * host logic is fixed. 51 */ 52 53 static void hv_signal_on_write(u32 old_write, struct vmbus_channel *channel) 54 { 55 struct hv_ring_buffer_info *rbi = &channel->outbound; 56 57 virt_mb(); 58 if (READ_ONCE(rbi->ring_buffer->interrupt_mask)) 59 return; 60 61 /* check interrupt_mask before read_index */ 62 virt_rmb(); 63 /* 64 * This is the only case we need to signal when the 65 * ring transitions from being empty to non-empty. 66 */ 67 if (old_write == READ_ONCE(rbi->ring_buffer->read_index)) { 68 ++channel->intr_out_empty; 69 vmbus_setevent(channel); 70 } 71 } 72 73 /* Set the next write location for the specified ring buffer. */ 74 static inline void 75 hv_set_next_write_location(struct hv_ring_buffer_info *ring_info, 76 u32 next_write_location) 77 { 78 WRITE_ONCE(ring_info->ring_buffer->write_index, next_write_location); 79 } 80 81 /* Get the size of the ring buffer. */ 82 static inline u32 83 hv_get_ring_buffersize(const struct hv_ring_buffer_info *ring_info) 84 { 85 return ring_info->ring_datasize; 86 } 87 88 /* 89 * Helper routine to copy from source to ring buffer. 90 * Assume there is enough room. Handles wrap-around in dest case only!! 91 */ 92 static u32 hv_copyto_ringbuffer( 93 struct hv_ring_buffer_info *ring_info, 94 u32 start_write_offset, 95 const void *src, 96 u32 srclen) 97 { 98 void *ring_buffer = hv_get_ring_buffer(ring_info); 99 u32 ring_buffer_size = hv_get_ring_buffersize(ring_info); 100 101 memcpy(ring_buffer + start_write_offset, src, srclen); 102 103 start_write_offset += srclen; 104 if (start_write_offset >= ring_buffer_size) 105 start_write_offset -= ring_buffer_size; 106 107 return start_write_offset; 108 } 109 110 /* Get various debug metrics for the specified ring buffer. */ 111 int hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info, 112 struct hv_ring_buffer_debug_info *debug_info) 113 { 114 u32 read_index; 115 u32 write_index; 116 117 mutex_lock(&ring_info->ring_buffer_mutex); 118 119 if (!ring_info->ring_buffer) { 120 mutex_unlock(&ring_info->ring_buffer_mutex); 121 return -EINVAL; 122 } 123 124 read_index = READ_ONCE(ring_info->ring_buffer->read_index); 125 write_index = READ_ONCE(ring_info->ring_buffer->write_index); 126 debug_info->bytes_avail_toread = 127 hv_ringbuffer_avail_read(ring_info, read_index, write_index); 128 debug_info->bytes_avail_towrite = 129 hv_ringbuffer_avail_write(ring_info, read_index, write_index); 130 debug_info->current_read_index = read_index; 131 debug_info->current_write_index = write_index; 132 debug_info->current_interrupt_mask 133 = ring_info->ring_buffer->interrupt_mask; 134 mutex_unlock(&ring_info->ring_buffer_mutex); 135 136 return 0; 137 } 138 EXPORT_SYMBOL_GPL(hv_ringbuffer_get_debuginfo); 139 140 /* Initialize a channel's ring buffer info mutex locks */ 141 void hv_ringbuffer_pre_init(struct vmbus_channel *channel) 142 { 143 mutex_init(&channel->inbound.ring_buffer_mutex); 144 mutex_init(&channel->outbound.ring_buffer_mutex); 145 } 146 147 /* Initialize the ring buffer. */ 148 int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info, 149 struct page *pages, u32 page_cnt, u32 max_pkt_size, 150 bool confidential) 151 { 152 struct page **pages_wraparound; 153 int i; 154 155 BUILD_BUG_ON((sizeof(struct hv_ring_buffer) != PAGE_SIZE)); 156 157 /* 158 * First page holds struct hv_ring_buffer, do wraparound mapping for 159 * the rest. 160 */ 161 pages_wraparound = kzalloc_objs(struct page *, page_cnt * 2 - 1); 162 if (!pages_wraparound) 163 return -ENOMEM; 164 165 pages_wraparound[0] = pages; 166 for (i = 0; i < 2 * (page_cnt - 1); i++) 167 pages_wraparound[i + 1] = 168 &pages[i % (page_cnt - 1) + 1]; 169 170 ring_info->ring_buffer = (struct hv_ring_buffer *) 171 vmap(pages_wraparound, page_cnt * 2 - 1, VM_MAP, 172 confidential ? PAGE_KERNEL : pgprot_decrypted(PAGE_KERNEL)); 173 174 kfree(pages_wraparound); 175 if (!ring_info->ring_buffer) 176 return -ENOMEM; 177 178 /* 179 * Ensure the header page is zero'ed since 180 * encryption status may have changed. 181 */ 182 memset(ring_info->ring_buffer, 0, HV_HYP_PAGE_SIZE); 183 184 ring_info->ring_buffer->read_index = 185 ring_info->ring_buffer->write_index = 0; 186 187 /* Set the feature bit for enabling flow control. */ 188 ring_info->ring_buffer->feature_bits.value = 1; 189 190 ring_info->ring_size = page_cnt << PAGE_SHIFT; 191 ring_info->ring_size_div10_reciprocal = 192 reciprocal_value(ring_info->ring_size / 10); 193 ring_info->ring_datasize = ring_info->ring_size - 194 sizeof(struct hv_ring_buffer); 195 ring_info->priv_read_index = 0; 196 197 /* Initialize buffer that holds copies of incoming packets */ 198 if (max_pkt_size) { 199 ring_info->pkt_buffer = kzalloc(max_pkt_size, GFP_KERNEL); 200 if (!ring_info->pkt_buffer) 201 return -ENOMEM; 202 ring_info->pkt_buffer_size = max_pkt_size; 203 } 204 205 spin_lock_init(&ring_info->ring_lock); 206 207 return 0; 208 } 209 210 /* Cleanup the ring buffer. */ 211 void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info) 212 { 213 mutex_lock(&ring_info->ring_buffer_mutex); 214 vunmap(ring_info->ring_buffer); 215 ring_info->ring_buffer = NULL; 216 mutex_unlock(&ring_info->ring_buffer_mutex); 217 218 kfree(ring_info->pkt_buffer); 219 ring_info->pkt_buffer = NULL; 220 ring_info->pkt_buffer_size = 0; 221 } 222 223 /* 224 * Check if the ring buffer spinlock is available to take or not; used on 225 * atomic contexts, like panic path (see the Hyper-V framebuffer driver). 226 */ 227 228 bool hv_ringbuffer_spinlock_busy(struct vmbus_channel *channel) 229 { 230 struct hv_ring_buffer_info *rinfo = &channel->outbound; 231 232 return spin_is_locked(&rinfo->ring_lock); 233 } 234 EXPORT_SYMBOL_GPL(hv_ringbuffer_spinlock_busy); 235 236 /* Write to the ring buffer. */ 237 int hv_ringbuffer_write(struct vmbus_channel *channel, 238 const struct kvec *kv_list, u32 kv_count, 239 u64 requestid, u64 *trans_id) 240 { 241 int i; 242 u32 bytes_avail_towrite; 243 u32 totalbytes_towrite = sizeof(u64); 244 u32 next_write_location; 245 u32 old_write; 246 u32 read_index; 247 u64 prev_indices; 248 unsigned long flags; 249 struct hv_ring_buffer_info *outring_info = &channel->outbound; 250 struct vmpacket_descriptor *desc = kv_list[0].iov_base; 251 u64 __trans_id, rqst_id = VMBUS_NO_RQSTOR; 252 253 if (channel->rescind) 254 return -ENODEV; 255 256 for (i = 0; i < kv_count; i++) 257 totalbytes_towrite += kv_list[i].iov_len; 258 259 spin_lock_irqsave(&outring_info->ring_lock, flags); 260 261 read_index = READ_ONCE(outring_info->ring_buffer->read_index); 262 old_write = READ_ONCE(outring_info->ring_buffer->write_index); 263 if (unlikely(hv_ringbuffer_indices_invalid(outring_info, read_index, 264 old_write))) { 265 spin_unlock_irqrestore(&outring_info->ring_lock, flags); 266 pr_err_ratelimited("outbound ring indices out of range: relid %u read %u write %u size %u\n", 267 channel->offermsg.child_relid, read_index, 268 old_write, outring_info->ring_datasize); 269 return -EIO; 270 } 271 272 bytes_avail_towrite = hv_ringbuffer_avail_write(outring_info, read_index, 273 old_write); 274 275 /* 276 * If there is only room for the packet, assume it is full. 277 * Otherwise, the next time around, we think the ring buffer 278 * is empty since the read index == write index. 279 */ 280 if (bytes_avail_towrite <= totalbytes_towrite) { 281 ++channel->out_full_total; 282 283 if (!channel->out_full_flag) { 284 ++channel->out_full_first; 285 channel->out_full_flag = true; 286 } 287 288 spin_unlock_irqrestore(&outring_info->ring_lock, flags); 289 return -EAGAIN; 290 } 291 292 channel->out_full_flag = false; 293 294 /* Write to the ring buffer */ 295 next_write_location = old_write; 296 297 for (i = 0; i < kv_count; i++) { 298 next_write_location = hv_copyto_ringbuffer(outring_info, 299 next_write_location, 300 kv_list[i].iov_base, 301 kv_list[i].iov_len); 302 } 303 304 /* 305 * Allocate the request ID after the data has been copied into the 306 * ring buffer. Once this request ID is allocated, the completion 307 * path could find the data and free it. 308 */ 309 310 if (desc->flags == VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED) { 311 if (channel->next_request_id_callback != NULL) { 312 rqst_id = channel->next_request_id_callback(channel, requestid); 313 if (rqst_id == VMBUS_RQST_ERROR) { 314 spin_unlock_irqrestore(&outring_info->ring_lock, flags); 315 return -EAGAIN; 316 } 317 } 318 } 319 desc = hv_get_ring_buffer(outring_info) + old_write; 320 __trans_id = (rqst_id == VMBUS_NO_RQSTOR) ? requestid : rqst_id; 321 /* 322 * Ensure the compiler doesn't generate code that reads the value of 323 * the transaction ID from the ring buffer, which is shared with the 324 * Hyper-V host and subject to being changed at any time. 325 */ 326 WRITE_ONCE(desc->trans_id, __trans_id); 327 if (trans_id) 328 *trans_id = __trans_id; 329 330 /* Set previous packet start */ 331 prev_indices = (u64)old_write << 32; 332 333 next_write_location = hv_copyto_ringbuffer(outring_info, 334 next_write_location, 335 &prev_indices, 336 sizeof(u64)); 337 338 /* Issue a full memory barrier before updating the write index */ 339 virt_mb(); 340 341 /* Now, update the write location */ 342 hv_set_next_write_location(outring_info, next_write_location); 343 344 345 spin_unlock_irqrestore(&outring_info->ring_lock, flags); 346 347 hv_signal_on_write(old_write, channel); 348 349 if (channel->rescind) { 350 if (rqst_id != VMBUS_NO_RQSTOR) { 351 /* Reclaim request ID to avoid leak of IDs */ 352 if (channel->request_addr_callback != NULL) 353 channel->request_addr_callback(channel, rqst_id); 354 } 355 return -ENODEV; 356 } 357 358 return 0; 359 } 360 361 int hv_ringbuffer_read(struct vmbus_channel *channel, 362 void *buffer, u32 buflen, u32 *buffer_actual_len, 363 u64 *requestid, bool raw) 364 { 365 struct vmpacket_descriptor *desc; 366 u32 packetlen, offset; 367 368 if (unlikely(buflen == 0)) 369 return -EINVAL; 370 371 *buffer_actual_len = 0; 372 *requestid = 0; 373 374 /* Make sure there is something to read */ 375 desc = hv_pkt_iter_first(channel); 376 if (desc == NULL) { 377 /* 378 * No error is set when there is even no header, drivers are 379 * supposed to analyze buffer_actual_len. 380 */ 381 return 0; 382 } 383 384 offset = raw ? 0 : (desc->offset8 << 3); 385 packetlen = (desc->len8 << 3) - offset; 386 *buffer_actual_len = packetlen; 387 *requestid = desc->trans_id; 388 389 if (unlikely(packetlen > buflen)) 390 return -ENOBUFS; 391 392 /* since ring is double mapped, only one copy is necessary */ 393 memcpy(buffer, (const char *)desc + offset, packetlen); 394 395 /* Advance ring index to next packet descriptor */ 396 __hv_pkt_iter_next(channel, desc); 397 398 /* Notify host of update */ 399 hv_pkt_iter_close(channel); 400 401 return 0; 402 } 403 404 /* 405 * Determine number of bytes available in ring buffer after 406 * the current iterator (priv_read_index) location. 407 * 408 * This is similar to hv_get_bytes_to_read but with private 409 * read index instead. 410 */ 411 static u32 hv_pkt_iter_avail(const struct vmbus_channel *channel) 412 { 413 const struct hv_ring_buffer_info *rbi = &channel->inbound; 414 u32 priv_read_loc = rbi->priv_read_index; 415 u32 write_loc; 416 417 /* 418 * The Hyper-V host writes the packet data, then uses 419 * store_release() to update the write_index. Use load_acquire() 420 * here to prevent loads of the packet data from being re-ordered 421 * before the read of the write_index and potentially getting 422 * stale data. 423 */ 424 write_loc = virt_load_acquire(&rbi->ring_buffer->write_index); 425 if (unlikely(hv_ringbuffer_indices_invalid(rbi, priv_read_loc, 426 write_loc))) { 427 pr_err_ratelimited("inbound write index out of range: relid %u write %u size %u\n", 428 channel->offermsg.child_relid, write_loc, 429 rbi->ring_datasize); 430 return 0; 431 } 432 433 return hv_ringbuffer_avail_read(rbi, priv_read_loc, write_loc); 434 } 435 436 /* 437 * Get first vmbus packet from ring buffer after read_index 438 * 439 * If ring buffer is empty, returns NULL and no other action needed. 440 */ 441 struct vmpacket_descriptor *hv_pkt_iter_first(struct vmbus_channel *channel) 442 { 443 struct hv_ring_buffer_info *rbi = &channel->inbound; 444 struct vmpacket_descriptor *desc, *desc_copy; 445 u32 bytes_avail, pkt_len, pkt_offset; 446 447 hv_debug_delay_test(channel, MESSAGE_DELAY); 448 449 bytes_avail = hv_pkt_iter_avail(channel); 450 if (bytes_avail < sizeof(struct vmpacket_descriptor)) 451 return NULL; 452 bytes_avail = min(rbi->pkt_buffer_size, bytes_avail); 453 454 desc = (struct vmpacket_descriptor *)(hv_get_ring_buffer(rbi) + rbi->priv_read_index); 455 456 /* 457 * Ensure the compiler does not use references to incoming Hyper-V values (which 458 * could change at any moment) when reading local variables later in the code 459 */ 460 pkt_len = READ_ONCE(desc->len8) << 3; 461 pkt_offset = READ_ONCE(desc->offset8) << 3; 462 463 /* 464 * If pkt_len is invalid, set it to the smaller of hv_pkt_iter_avail() and 465 * rbi->pkt_buffer_size 466 */ 467 if (pkt_len < sizeof(struct vmpacket_descriptor) || pkt_len > bytes_avail) 468 pkt_len = bytes_avail; 469 470 /* 471 * If pkt_offset is invalid, arbitrarily set it to 472 * the size of vmpacket_descriptor 473 */ 474 if (pkt_offset < sizeof(struct vmpacket_descriptor) || pkt_offset > pkt_len) 475 pkt_offset = sizeof(struct vmpacket_descriptor); 476 477 /* Copy the Hyper-V packet out of the ring buffer */ 478 desc_copy = (struct vmpacket_descriptor *)rbi->pkt_buffer; 479 memcpy(desc_copy, desc, pkt_len); 480 481 /* 482 * Hyper-V could still change len8 and offset8 after the earlier read. 483 * Ensure that desc_copy has legal values for len8 and offset8 that 484 * are consistent with the copy we just made 485 */ 486 desc_copy->len8 = pkt_len >> 3; 487 desc_copy->offset8 = pkt_offset >> 3; 488 489 return desc_copy; 490 } 491 EXPORT_SYMBOL_GPL(hv_pkt_iter_first); 492 493 /* 494 * Get next vmbus packet from ring buffer. 495 * 496 * Advances the current location (priv_read_index) and checks for more 497 * data. If the end of the ring buffer is reached, then return NULL. 498 */ 499 struct vmpacket_descriptor * 500 __hv_pkt_iter_next(struct vmbus_channel *channel, 501 const struct vmpacket_descriptor *desc) 502 { 503 struct hv_ring_buffer_info *rbi = &channel->inbound; 504 u32 packetlen = desc->len8 << 3; 505 u32 dsize = rbi->ring_datasize; 506 507 hv_debug_delay_test(channel, MESSAGE_DELAY); 508 /* bump offset to next potential packet */ 509 rbi->priv_read_index += packetlen + VMBUS_PKT_TRAILER; 510 if (rbi->priv_read_index >= dsize) 511 rbi->priv_read_index -= dsize; 512 513 /* more data? */ 514 return hv_pkt_iter_first(channel); 515 } 516 EXPORT_SYMBOL_GPL(__hv_pkt_iter_next); 517 518 /* How many bytes were read in this iterator cycle */ 519 static u32 hv_pkt_iter_bytes_read(const struct hv_ring_buffer_info *rbi, 520 u32 start_read_index) 521 { 522 if (rbi->priv_read_index >= start_read_index) 523 return rbi->priv_read_index - start_read_index; 524 else 525 return rbi->ring_datasize - start_read_index + 526 rbi->priv_read_index; 527 } 528 529 /* 530 * Update host ring buffer after iterating over packets. If the host has 531 * stopped queuing new entries because it found the ring buffer full, and 532 * sufficient space is being freed up, signal the host. But be careful to 533 * only signal the host when necessary, both for performance reasons and 534 * because Hyper-V protects itself by throttling guests that signal 535 * inappropriately. 536 * 537 * Determining when to signal is tricky. There are three key data inputs 538 * that must be handled in this order to avoid race conditions: 539 * 540 * 1. Update the read_index 541 * 2. Read the pending_send_sz 542 * 3. Read the current write_index 543 * 544 * The interrupt_mask is not used to determine when to signal. The 545 * interrupt_mask is used only on the guest->host ring buffer when 546 * sending requests to the host. The host does not use it on the host-> 547 * guest ring buffer to indicate whether it should be signaled. 548 */ 549 void hv_pkt_iter_close(struct vmbus_channel *channel) 550 { 551 struct hv_ring_buffer_info *rbi = &channel->inbound; 552 u32 curr_write_sz, pending_sz, bytes_read, start_read_index; 553 u32 write_index; 554 555 /* 556 * Make sure all reads are done before we update the read index since 557 * the writer may start writing to the read area once the read index 558 * is updated. 559 */ 560 virt_rmb(); 561 start_read_index = READ_ONCE(rbi->ring_buffer->read_index); 562 WRITE_ONCE(rbi->ring_buffer->read_index, rbi->priv_read_index); 563 564 /* 565 * Issue a full memory barrier before making the signaling decision. 566 * If reading pending_send_sz were to be reordered and happen 567 * before we commit the new read_index, a race could occur. If the 568 * host were to set the pending_send_sz after we have sampled 569 * pending_send_sz, and the ring buffer blocks before we commit the 570 * read index, we could miss sending the interrupt. Issue a full 571 * memory barrier to address this. 572 */ 573 virt_mb(); 574 575 /* 576 * If the pending_send_sz is zero, then the ring buffer is not 577 * blocked and there is no need to signal. This is far by the 578 * most common case, so exit quickly for best performance. 579 */ 580 pending_sz = READ_ONCE(rbi->ring_buffer->pending_send_sz); 581 if (!pending_sz) 582 return; 583 584 /* 585 * Ensure the read of write_index happens after the read of 586 * pending_send_sz. 587 */ 588 virt_rmb(); 589 write_index = READ_ONCE(rbi->ring_buffer->write_index); 590 if (unlikely(hv_ringbuffer_indices_invalid(rbi, start_read_index, 591 write_index))) 592 return; 593 594 curr_write_sz = hv_ringbuffer_avail_write(rbi, rbi->priv_read_index, 595 write_index); 596 bytes_read = hv_pkt_iter_bytes_read(rbi, start_read_index); 597 598 /* 599 * We want to signal the host only if we're transitioning 600 * from a "not enough free space" state to a "enough free 601 * space" state. For example, it's possible that this function 602 * could run and free up enough space to signal the host, and then 603 * run again and free up additional space before the host has a 604 * chance to clear the pending_send_sz. The 2nd invocation would 605 * be a null transition from "enough free space" to "enough free 606 * space", which doesn't warrant a signal. 607 * 608 * Exactly filling the ring buffer is treated as "not enough 609 * space". The ring buffer always must have at least one byte 610 * empty so the empty and full conditions are distinguishable. 611 * curr_write_sz doesn't fully tell the truth in this regard. 612 * 613 * So first check if we were in the "enough free space" state 614 * before we began the iteration. If so, the host was not 615 * blocked, and there's no need to signal. 616 */ 617 if (curr_write_sz - bytes_read > pending_sz) 618 return; 619 620 /* 621 * Similarly, if the new state is "not enough space", then 622 * there's no need to signal. 623 */ 624 if (curr_write_sz <= pending_sz) 625 return; 626 627 ++channel->intr_in_full; 628 vmbus_setevent(channel); 629 } 630 EXPORT_SYMBOL_GPL(hv_pkt_iter_close); 631