Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'hyperv-next-signed-20210629' of git://git.kernel.org/pub/scm/linux/kernel/git/hyperv/linux

Pull hyperv updates from Wei Liu:
"Just a few minor enhancement patches and bug fixes"

* tag 'hyperv-next-signed-20210629' of git://git.kernel.org/pub/scm/linux/kernel/git/hyperv/linux:
PCI: hv: Add check for hyperv_initialized in init_hv_pci_drv()
Drivers: hv: Move Hyper-V extended capability check to arch neutral code
drivers: hv: Fix missing error code in vmbus_connect()
x86/hyperv: fix logical processor creation
hv_utils: Fix passing zero to 'PTR_ERR' warning
scsi: storvsc: Use blk_mq_unique_tag() to generate requestIDs
Drivers: hv: vmbus: Copy packets sent by Hyper-V out of the ring buffer
hv_balloon: Remove redundant assignment to region_start

+317 -127
-47
arch/x86/hyperv/hv_init.c
··· 614 614 return hv_get_isolation_type() != HV_ISOLATION_TYPE_NONE; 615 615 } 616 616 EXPORT_SYMBOL_GPL(hv_is_isolation_supported); 617 - 618 - /* Bit mask of the extended capability to query: see HV_EXT_CAPABILITY_xxx */ 619 - bool hv_query_ext_cap(u64 cap_query) 620 - { 621 - /* 622 - * The address of the 'hv_extended_cap' variable will be used as an 623 - * output parameter to the hypercall below and so it should be 624 - * compatible with 'virt_to_phys'. Which means, it's address should be 625 - * directly mapped. Use 'static' to keep it compatible; stack variables 626 - * can be virtually mapped, making them incompatible with 627 - * 'virt_to_phys'. 628 - * Hypercall input/output addresses should also be 8-byte aligned. 629 - */ 630 - static u64 hv_extended_cap __aligned(8); 631 - static bool hv_extended_cap_queried; 632 - u64 status; 633 - 634 - /* 635 - * Querying extended capabilities is an extended hypercall. Check if the 636 - * partition supports extended hypercall, first. 637 - */ 638 - if (!(ms_hyperv.priv_high & HV_ENABLE_EXTENDED_HYPERCALLS)) 639 - return false; 640 - 641 - /* Extended capabilities do not change at runtime. */ 642 - if (hv_extended_cap_queried) 643 - return hv_extended_cap & cap_query; 644 - 645 - status = hv_do_hypercall(HV_EXT_CALL_QUERY_CAPABILITIES, NULL, 646 - &hv_extended_cap); 647 - 648 - /* 649 - * The query extended capabilities hypercall should not fail under 650 - * any normal circumstances. Avoid repeatedly making the hypercall, on 651 - * error. 652 - */ 653 - hv_extended_cap_queried = true; 654 - status &= HV_HYPERCALL_RESULT_MASK; 655 - if (status != HV_STATUS_SUCCESS) { 656 - pr_err("Hyper-V: Extended query capabilities hypercall failed 0x%llx\n", 657 - status); 658 - return false; 659 - } 660 - 661 - return hv_extended_cap & cap_query; 662 - } 663 - EXPORT_SYMBOL_GPL(hv_query_ext_cap);
+1 -1
arch/x86/kernel/cpu/mshyperv.c
··· 236 236 for_each_present_cpu(i) { 237 237 if (i == 0) 238 238 continue; 239 - ret = hv_call_add_logical_proc(numa_cpu_node(i), i, cpu_physical_id(i)); 239 + ret = hv_call_add_logical_proc(numa_cpu_node(i), i, i); 240 240 BUG_ON(ret); 241 241 } 242 242
+1 -1
drivers/Makefile
··· 160 160 161 161 # Virtualization drivers 162 162 obj-$(CONFIG_VIRT_DRIVERS) += virt/ 163 - obj-$(CONFIG_HYPERV) += hv/ 163 + obj-$(subst m,y,$(CONFIG_HYPERV)) += hv/ 164 164 165 165 obj-$(CONFIG_PM_DEVFREQ) += devfreq/ 166 166 obj-$(CONFIG_EXTCON) += extcon/
+3
drivers/hv/Makefile
··· 11 11 channel_mgmt.o ring_buffer.o hv_trace.o 12 12 hv_vmbus-$(CONFIG_HYPERV_TESTING) += hv_debugfs.o 13 13 hv_utils-y := hv_util.o hv_kvp.o hv_snapshot.o hv_fcopy.o hv_utils_transport.o 14 + 15 + # Code that must be built-in 16 + obj-$(subst m,y,$(CONFIG_HYPERV)) += hv_common.o
+12 -11
drivers/hv/channel.c
··· 662 662 newchannel->onchannel_callback = onchannelcallback; 663 663 newchannel->channel_callback_context = context; 664 664 665 - err = hv_ringbuffer_init(&newchannel->outbound, page, send_pages); 665 + if (!newchannel->max_pkt_size) 666 + newchannel->max_pkt_size = VMBUS_DEFAULT_MAX_PKT_SIZE; 667 + 668 + err = hv_ringbuffer_init(&newchannel->outbound, page, send_pages, 0); 666 669 if (err) 667 670 goto error_clean_ring; 668 671 669 - err = hv_ringbuffer_init(&newchannel->inbound, 670 - &page[send_pages], recv_pages); 672 + err = hv_ringbuffer_init(&newchannel->inbound, &page[send_pages], 673 + recv_pages, newchannel->max_pkt_size); 671 674 if (err) 672 675 goto error_clean_ring; 673 676 ··· 1189 1186 * vmbus_next_request_id - Returns a new request id. It is also 1190 1187 * the index at which the guest memory address is stored. 1191 1188 * Uses a spin lock to avoid race conditions. 1192 - * @rqstor: Pointer to the requestor struct 1189 + * @channel: Pointer to the VMbus channel struct 1193 1190 * @rqst_add: Guest memory address to be stored in the array 1194 1191 */ 1195 - u64 vmbus_next_request_id(struct vmbus_requestor *rqstor, u64 rqst_addr) 1192 + u64 vmbus_next_request_id(struct vmbus_channel *channel, u64 rqst_addr) 1196 1193 { 1194 + struct vmbus_requestor *rqstor = &channel->requestor; 1197 1195 unsigned long flags; 1198 1196 u64 current_id; 1199 - const struct vmbus_channel *channel = 1200 - container_of(rqstor, const struct vmbus_channel, requestor); 1201 1197 1202 1198 /* Check rqstor has been initialized */ 1203 1199 if (!channel->rqstor_size) ··· 1230 1228 /* 1231 1229 * vmbus_request_addr - Returns the memory address stored at @trans_id 1232 1230 * in @rqstor. Uses a spin lock to avoid race conditions. 1233 - * @rqstor: Pointer to the requestor struct 1231 + * @channel: Pointer to the VMbus channel struct 1234 1232 * @trans_id: Request id sent back from Hyper-V. Becomes the requestor's 1235 1233 * next request id. 1236 1234 */ 1237 - u64 vmbus_request_addr(struct vmbus_requestor *rqstor, u64 trans_id) 1235 + u64 vmbus_request_addr(struct vmbus_channel *channel, u64 trans_id) 1238 1236 { 1237 + struct vmbus_requestor *rqstor = &channel->requestor; 1239 1238 unsigned long flags; 1240 1239 u64 req_addr; 1241 - const struct vmbus_channel *channel = 1242 - container_of(rqstor, const struct vmbus_channel, requestor); 1243 1240 1244 1241 /* Check rqstor has been initialized */ 1245 1242 if (!channel->rqstor_size)
+3 -1
drivers/hv/connection.c
··· 232 232 */ 233 233 234 234 for (i = 0; ; i++) { 235 - if (i == ARRAY_SIZE(vmbus_versions)) 235 + if (i == ARRAY_SIZE(vmbus_versions)) { 236 + ret = -EDOM; 236 237 goto cleanup; 238 + } 237 239 238 240 version = vmbus_versions[i]; 239 241 if (version > max_version)
-1
drivers/hv/hv_balloon.c
··· 1010 1010 * that need to be hot-added while ensuring the alignment 1011 1011 * and size requirements of Linux as it relates to hot-add. 1012 1012 */ 1013 - region_start = pg_start; 1014 1013 region_size = (pfn_cnt / HA_CHUNK) * HA_CHUNK; 1015 1014 if (pfn_cnt % HA_CHUNK) 1016 1015 region_size += HA_CHUNK;
+66
drivers/hv/hv_common.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + 3 + /* 4 + * Architecture neutral utility routines for interacting with 5 + * Hyper-V. This file is specifically for code that must be 6 + * built-in to the kernel image when CONFIG_HYPERV is set 7 + * (vs. being in a module) because it is called from architecture 8 + * specific code under arch/. 9 + * 10 + * Copyright (C) 2021, Microsoft, Inc. 11 + * 12 + * Author : Michael Kelley <mikelley@microsoft.com> 13 + */ 14 + 15 + #include <linux/types.h> 16 + #include <linux/export.h> 17 + #include <linux/bitfield.h> 18 + #include <asm/hyperv-tlfs.h> 19 + #include <asm/mshyperv.h> 20 + 21 + 22 + /* Bit mask of the extended capability to query: see HV_EXT_CAPABILITY_xxx */ 23 + bool hv_query_ext_cap(u64 cap_query) 24 + { 25 + /* 26 + * The address of the 'hv_extended_cap' variable will be used as an 27 + * output parameter to the hypercall below and so it should be 28 + * compatible with 'virt_to_phys'. Which means, it's address should be 29 + * directly mapped. Use 'static' to keep it compatible; stack variables 30 + * can be virtually mapped, making them incompatible with 31 + * 'virt_to_phys'. 32 + * Hypercall input/output addresses should also be 8-byte aligned. 33 + */ 34 + static u64 hv_extended_cap __aligned(8); 35 + static bool hv_extended_cap_queried; 36 + u64 status; 37 + 38 + /* 39 + * Querying extended capabilities is an extended hypercall. Check if the 40 + * partition supports extended hypercall, first. 41 + */ 42 + if (!(ms_hyperv.priv_high & HV_ENABLE_EXTENDED_HYPERCALLS)) 43 + return false; 44 + 45 + /* Extended capabilities do not change at runtime. */ 46 + if (hv_extended_cap_queried) 47 + return hv_extended_cap & cap_query; 48 + 49 + status = hv_do_hypercall(HV_EXT_CALL_QUERY_CAPABILITIES, NULL, 50 + &hv_extended_cap); 51 + 52 + /* 53 + * The query extended capabilities hypercall should not fail under 54 + * any normal circumstances. Avoid repeatedly making the hypercall, on 55 + * error. 56 + */ 57 + hv_extended_cap_queried = true; 58 + if (!hv_result_success(status)) { 59 + pr_err("Hyper-V: Extended query capabilities hypercall failed 0x%llx\n", 60 + status); 61 + return false; 62 + } 63 + 64 + return hv_extended_cap & cap_query; 65 + } 66 + EXPORT_SYMBOL_GPL(hv_query_ext_cap);
+1
drivers/hv/hv_fcopy.c
··· 349 349 { 350 350 recv_buffer = srv->recv_buffer; 351 351 fcopy_transaction.recv_channel = srv->channel; 352 + fcopy_transaction.recv_channel->max_pkt_size = HV_HYP_PAGE_SIZE * 2; 352 353 353 354 /* 354 355 * When this driver loads, the user level daemon that
+1
drivers/hv/hv_kvp.c
··· 757 757 { 758 758 recv_buffer = srv->recv_buffer; 759 759 kvp_transaction.recv_channel = srv->channel; 760 + kvp_transaction.recv_channel->max_pkt_size = HV_HYP_PAGE_SIZE * 4; 760 761 761 762 /* 762 763 * When this driver loads, the user level daemon that
+2 -2
drivers/hv/hv_util.c
··· 750 750 */ 751 751 hv_ptp_clock = ptp_clock_register(&ptp_hyperv_info, NULL); 752 752 if (IS_ERR_OR_NULL(hv_ptp_clock)) { 753 - pr_err("cannot register PTP clock: %ld\n", 754 - PTR_ERR(hv_ptp_clock)); 753 + pr_err("cannot register PTP clock: %d\n", 754 + PTR_ERR_OR_ZERO(hv_ptp_clock)); 755 755 hv_ptp_clock = NULL; 756 756 } 757 757
+1 -1
drivers/hv/hyperv_vmbus.h
··· 174 174 void hv_ringbuffer_pre_init(struct vmbus_channel *channel); 175 175 176 176 int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info, 177 - struct page *pages, u32 pagecnt); 177 + struct page *pages, u32 pagecnt, u32 max_pkt_size); 178 178 179 179 void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info); 180 180
+79 -16
drivers/hv/ring_buffer.c
··· 181 181 182 182 /* Initialize the ring buffer. */ 183 183 int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info, 184 - struct page *pages, u32 page_cnt) 184 + struct page *pages, u32 page_cnt, u32 max_pkt_size) 185 185 { 186 186 int i; 187 187 struct page **pages_wraparound; ··· 223 223 sizeof(struct hv_ring_buffer); 224 224 ring_info->priv_read_index = 0; 225 225 226 + /* Initialize buffer that holds copies of incoming packets */ 227 + if (max_pkt_size) { 228 + ring_info->pkt_buffer = kzalloc(max_pkt_size, GFP_KERNEL); 229 + if (!ring_info->pkt_buffer) 230 + return -ENOMEM; 231 + ring_info->pkt_buffer_size = max_pkt_size; 232 + } 233 + 226 234 spin_lock_init(&ring_info->ring_lock); 227 235 228 236 return 0; ··· 243 235 vunmap(ring_info->ring_buffer); 244 236 ring_info->ring_buffer = NULL; 245 237 mutex_unlock(&ring_info->ring_buffer_mutex); 238 + 239 + kfree(ring_info->pkt_buffer); 240 + ring_info->pkt_buffer_size = 0; 246 241 } 247 242 248 243 /* Write to the ring buffer. */ ··· 312 301 */ 313 302 314 303 if (desc->flags == VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED) { 315 - rqst_id = vmbus_next_request_id(&channel->requestor, requestid); 316 - if (rqst_id == VMBUS_RQST_ERROR) { 317 - spin_unlock_irqrestore(&outring_info->ring_lock, flags); 318 - return -EAGAIN; 304 + if (channel->next_request_id_callback != NULL) { 305 + rqst_id = channel->next_request_id_callback(channel, requestid); 306 + if (rqst_id == VMBUS_RQST_ERROR) { 307 + spin_unlock_irqrestore(&outring_info->ring_lock, flags); 308 + return -EAGAIN; 309 + } 319 310 } 320 311 } 321 312 desc = hv_get_ring_buffer(outring_info) + old_write; ··· 345 332 if (channel->rescind) { 346 333 if (rqst_id != VMBUS_NO_RQSTOR) { 347 334 /* Reclaim request ID to avoid leak of IDs */ 348 - vmbus_request_addr(&channel->requestor, rqst_id); 335 + if (channel->request_addr_callback != NULL) 336 + channel->request_addr_callback(channel, rqst_id); 349 337 } 350 338 return -ENODEV; 351 339 } ··· 389 375 memcpy(buffer, (const char *)desc + offset, packetlen); 390 376 391 377 /* Advance ring index to next packet descriptor */ 392 - __hv_pkt_iter_next(channel, desc); 378 + __hv_pkt_iter_next(channel, desc, true); 393 379 394 380 /* Notify host of update */ 395 381 hv_pkt_iter_close(channel); ··· 416 402 } 417 403 418 404 /* 405 + * Get first vmbus packet without copying it out of the ring buffer 406 + */ 407 + struct vmpacket_descriptor *hv_pkt_iter_first_raw(struct vmbus_channel *channel) 408 + { 409 + struct hv_ring_buffer_info *rbi = &channel->inbound; 410 + 411 + hv_debug_delay_test(channel, MESSAGE_DELAY); 412 + 413 + if (hv_pkt_iter_avail(rbi) < sizeof(struct vmpacket_descriptor)) 414 + return NULL; 415 + 416 + return (struct vmpacket_descriptor *)(hv_get_ring_buffer(rbi) + rbi->priv_read_index); 417 + } 418 + EXPORT_SYMBOL_GPL(hv_pkt_iter_first_raw); 419 + 420 + /* 419 421 * Get first vmbus packet from ring buffer after read_index 420 422 * 421 423 * If ring buffer is empty, returns NULL and no other action needed. ··· 439 409 struct vmpacket_descriptor *hv_pkt_iter_first(struct vmbus_channel *channel) 440 410 { 441 411 struct hv_ring_buffer_info *rbi = &channel->inbound; 442 - struct vmpacket_descriptor *desc; 412 + struct vmpacket_descriptor *desc, *desc_copy; 413 + u32 bytes_avail, pkt_len, pkt_offset; 443 414 444 - hv_debug_delay_test(channel, MESSAGE_DELAY); 445 - if (hv_pkt_iter_avail(rbi) < sizeof(struct vmpacket_descriptor)) 415 + desc = hv_pkt_iter_first_raw(channel); 416 + if (!desc) 446 417 return NULL; 447 418 448 - desc = hv_get_ring_buffer(rbi) + rbi->priv_read_index; 449 - if (desc) 450 - prefetch((char *)desc + (desc->len8 << 3)); 419 + bytes_avail = min(rbi->pkt_buffer_size, hv_pkt_iter_avail(rbi)); 451 420 452 - return desc; 421 + /* 422 + * Ensure the compiler does not use references to incoming Hyper-V values (which 423 + * could change at any moment) when reading local variables later in the code 424 + */ 425 + pkt_len = READ_ONCE(desc->len8) << 3; 426 + pkt_offset = READ_ONCE(desc->offset8) << 3; 427 + 428 + /* 429 + * If pkt_len is invalid, set it to the smaller of hv_pkt_iter_avail() and 430 + * rbi->pkt_buffer_size 431 + */ 432 + if (pkt_len < sizeof(struct vmpacket_descriptor) || pkt_len > bytes_avail) 433 + pkt_len = bytes_avail; 434 + 435 + /* 436 + * If pkt_offset is invalid, arbitrarily set it to 437 + * the size of vmpacket_descriptor 438 + */ 439 + if (pkt_offset < sizeof(struct vmpacket_descriptor) || pkt_offset > pkt_len) 440 + pkt_offset = sizeof(struct vmpacket_descriptor); 441 + 442 + /* Copy the Hyper-V packet out of the ring buffer */ 443 + desc_copy = (struct vmpacket_descriptor *)rbi->pkt_buffer; 444 + memcpy(desc_copy, desc, pkt_len); 445 + 446 + /* 447 + * Hyper-V could still change len8 and offset8 after the earlier read. 448 + * Ensure that desc_copy has legal values for len8 and offset8 that 449 + * are consistent with the copy we just made 450 + */ 451 + desc_copy->len8 = pkt_len >> 3; 452 + desc_copy->offset8 = pkt_offset >> 3; 453 + 454 + return desc_copy; 453 455 } 454 456 EXPORT_SYMBOL_GPL(hv_pkt_iter_first); 455 457 ··· 493 431 */ 494 432 struct vmpacket_descriptor * 495 433 __hv_pkt_iter_next(struct vmbus_channel *channel, 496 - const struct vmpacket_descriptor *desc) 434 + const struct vmpacket_descriptor *desc, 435 + bool copy) 497 436 { 498 437 struct hv_ring_buffer_info *rbi = &channel->inbound; 499 438 u32 packetlen = desc->len8 << 3; ··· 507 444 rbi->priv_read_index -= dsize; 508 445 509 446 /* more data? */ 510 - return hv_pkt_iter_first(channel); 447 + return copy ? hv_pkt_iter_first(channel) : hv_pkt_iter_first_raw(channel); 511 448 } 512 449 EXPORT_SYMBOL_GPL(__hv_pkt_iter_next); 513 450
+7
drivers/net/hyperv/hyperv_net.h
··· 895 895 ringbytes / NETVSC_MIN_IN_MSG_SIZE; 896 896 } 897 897 898 + /* XFER PAGE packets can specify a maximum of 375 ranges for NDIS >= 6.0 899 + * and a maximum of 64 ranges for NDIS < 6.0 with no RSC; with RSC, this 900 + * limit is raised to 562 (= NVSP_RSC_MAX). 901 + */ 902 + #define NETVSC_MAX_XFER_PAGE_RANGES NVSP_RSC_MAX 898 903 #define NETVSC_XFER_HEADER_SIZE(rng_cnt) \ 899 904 (offsetof(struct vmtransfer_page_packet_header, ranges) + \ 900 905 (rng_cnt) * sizeof(struct vmtransfer_page_range)) 906 + #define NETVSC_MAX_PKT_SIZE (NETVSC_XFER_HEADER_SIZE(NETVSC_MAX_XFER_PAGE_RANGES) + \ 907 + sizeof(struct nvsp_message) + (sizeof(u32) * VRSS_SEND_TAB_SIZE)) 901 908 902 909 struct multi_send_data { 903 910 struct sk_buff *skb; /* skb containing the pkt */
+7 -3
drivers/net/hyperv/netvsc.c
··· 757 757 int queue_sends; 758 758 u64 cmd_rqst; 759 759 760 - cmd_rqst = vmbus_request_addr(&channel->requestor, (u64)desc->trans_id); 760 + cmd_rqst = channel->request_addr_callback(channel, (u64)desc->trans_id); 761 761 if (cmd_rqst == VMBUS_RQST_ERROR) { 762 762 netdev_err(ndev, "Incorrect transaction id\n"); 763 763 return; ··· 817 817 818 818 /* First check if this is a VMBUS completion without data payload */ 819 819 if (!msglen) { 820 - cmd_rqst = vmbus_request_addr(&incoming_channel->requestor, 821 - (u64)desc->trans_id); 820 + cmd_rqst = incoming_channel->request_addr_callback(incoming_channel, 821 + (u64)desc->trans_id); 822 822 if (cmd_rqst == VMBUS_RQST_ERROR) { 823 823 netdev_err(ndev, "Invalid transaction id\n"); 824 824 return; ··· 1649 1649 netvsc_poll, NAPI_POLL_WEIGHT); 1650 1650 1651 1651 /* Open the channel */ 1652 + device->channel->next_request_id_callback = vmbus_next_request_id; 1653 + device->channel->request_addr_callback = vmbus_request_addr; 1652 1654 device->channel->rqstor_size = netvsc_rqstor_size(netvsc_ring_bytes); 1655 + device->channel->max_pkt_size = NETVSC_MAX_PKT_SIZE; 1656 + 1653 1657 ret = vmbus_open(device->channel, netvsc_ring_bytes, 1654 1658 netvsc_ring_bytes, NULL, 0, 1655 1659 netvsc_channel_cb, net_device->chan_table);
+4
drivers/net/hyperv/rndis_filter.c
··· 1259 1259 /* Set the channel before opening.*/ 1260 1260 nvchan->channel = new_sc; 1261 1261 1262 + new_sc->next_request_id_callback = vmbus_next_request_id; 1263 + new_sc->request_addr_callback = vmbus_request_addr; 1262 1264 new_sc->rqstor_size = netvsc_rqstor_size(netvsc_ring_bytes); 1265 + new_sc->max_pkt_size = NETVSC_MAX_PKT_SIZE; 1266 + 1263 1267 ret = vmbus_open(new_sc, netvsc_ring_bytes, 1264 1268 netvsc_ring_bytes, NULL, 0, 1265 1269 netvsc_channel_cb, nvchan);
+3
drivers/pci/controller/pci-hyperv.c
··· 3476 3476 3477 3477 static int __init init_hv_pci_drv(void) 3478 3478 { 3479 + if (!hv_is_hyperv_initialized()) 3480 + return -ENODEV; 3481 + 3479 3482 /* Set the invalid domain number's bit, so it will not be used */ 3480 3483 set_bit(HVPCI_DOM_INVALID, hvpci_dom_map); 3481 3484
+73 -31
drivers/scsi/storvsc_drv.c
··· 406 406 #define STORVSC_IDE_MAX_TARGETS 1 407 407 #define STORVSC_IDE_MAX_CHANNELS 1 408 408 409 + /* 410 + * Upper bound on the size of a storvsc packet. vmscsi_size_delta is not 411 + * included in the calculation because it is set after STORVSC_MAX_PKT_SIZE 412 + * is used in storvsc_connect_to_vsp 413 + */ 414 + #define STORVSC_MAX_PKT_SIZE (sizeof(struct vmpacket_descriptor) +\ 415 + sizeof(struct vstor_packet)) 416 + 409 417 struct storvsc_cmd_request { 410 418 struct scsi_cmnd *cmd; 411 419 ··· 696 688 spin_unlock_irqrestore(&stor_device->lock, flags); 697 689 } 698 690 691 + static u64 storvsc_next_request_id(struct vmbus_channel *channel, u64 rqst_addr) 692 + { 693 + struct storvsc_cmd_request *request = 694 + (struct storvsc_cmd_request *)(unsigned long)rqst_addr; 695 + 696 + if (rqst_addr == VMBUS_RQST_INIT) 697 + return VMBUS_RQST_INIT; 698 + if (rqst_addr == VMBUS_RQST_RESET) 699 + return VMBUS_RQST_RESET; 700 + 701 + /* 702 + * Cannot return an ID of 0, which is reserved for an unsolicited 703 + * message from Hyper-V. 704 + */ 705 + return (u64)blk_mq_unique_tag(request->cmd->request) + 1; 706 + } 707 + 699 708 static void handle_sc_creation(struct vmbus_channel *new_sc) 700 709 { 701 710 struct hv_device *device = new_sc->primary_channel->device_obj; ··· 726 701 return; 727 702 728 703 memset(&props, 0, sizeof(struct vmstorage_channel_properties)); 704 + new_sc->max_pkt_size = STORVSC_MAX_PKT_SIZE; 729 705 730 - /* 731 - * The size of vmbus_requestor is an upper bound on the number of requests 732 - * that can be in-progress at any one time across all channels. 733 - */ 734 - new_sc->rqstor_size = scsi_driver.can_queue; 706 + new_sc->next_request_id_callback = storvsc_next_request_id; 735 707 736 708 ret = vmbus_open(new_sc, 737 709 storvsc_ringbuffer_size, ··· 795 773 ret = vmbus_sendpacket(device->channel, vstor_packet, 796 774 (sizeof(struct vstor_packet) - 797 775 stor_device->vmscsi_size_delta), 798 - (unsigned long)request, 776 + VMBUS_RQST_INIT, 799 777 VM_PKT_DATA_INBAND, 800 778 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); 801 779 ··· 864 842 ret = vmbus_sendpacket(device->channel, vstor_packet, 865 843 (sizeof(struct vstor_packet) - 866 844 stor_device->vmscsi_size_delta), 867 - (unsigned long)request, 845 + VMBUS_RQST_INIT, 868 846 VM_PKT_DATA_INBAND, 869 847 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); 870 848 if (ret != 0) ··· 1266 1244 const struct vmpacket_descriptor *desc; 1267 1245 struct hv_device *device; 1268 1246 struct storvsc_device *stor_device; 1247 + struct Scsi_Host *shost; 1269 1248 1270 1249 if (channel->primary_channel != NULL) 1271 1250 device = channel->primary_channel->device_obj; ··· 1277 1254 if (!stor_device) 1278 1255 return; 1279 1256 1257 + shost = stor_device->host; 1258 + 1280 1259 foreach_vmbus_pkt(desc, channel) { 1281 - void *packet = hv_pkt_data(desc); 1282 - struct storvsc_cmd_request *request; 1283 - u64 cmd_rqst; 1284 - 1285 - cmd_rqst = vmbus_request_addr(&channel->requestor, 1286 - desc->trans_id); 1287 - if (cmd_rqst == VMBUS_RQST_ERROR) { 1288 - dev_err(&device->device, 1289 - "Incorrect transaction id\n"); 1290 - continue; 1291 - } 1292 - 1293 - request = (struct storvsc_cmd_request *)(unsigned long)cmd_rqst; 1260 + struct vstor_packet *packet = hv_pkt_data(desc); 1261 + struct storvsc_cmd_request *request = NULL; 1262 + u64 rqst_id = desc->trans_id; 1294 1263 1295 1264 if (hv_pkt_datalen(desc) < sizeof(struct vstor_packet) - 1296 1265 stor_device->vmscsi_size_delta) { ··· 1290 1275 continue; 1291 1276 } 1292 1277 1293 - if (request == &stor_device->init_request || 1294 - request == &stor_device->reset_request) { 1295 - memcpy(&request->vstor_packet, packet, 1296 - (sizeof(struct vstor_packet) - stor_device->vmscsi_size_delta)); 1297 - complete(&request->wait_event); 1278 + if (rqst_id == VMBUS_RQST_INIT) { 1279 + request = &stor_device->init_request; 1280 + } else if (rqst_id == VMBUS_RQST_RESET) { 1281 + request = &stor_device->reset_request; 1298 1282 } else { 1283 + /* Hyper-V can send an unsolicited message with ID of 0 */ 1284 + if (rqst_id == 0) { 1285 + /* 1286 + * storvsc_on_receive() looks at the vstor_packet in the message 1287 + * from the ring buffer. If the operation in the vstor_packet is 1288 + * COMPLETE_IO, then we call storvsc_on_io_completion(), and 1289 + * dereference the guest memory address. Make sure we don't call 1290 + * storvsc_on_io_completion() with a guest memory address that is 1291 + * zero if Hyper-V were to construct and send such a bogus packet. 1292 + */ 1293 + if (packet->operation == VSTOR_OPERATION_COMPLETE_IO) { 1294 + dev_err(&device->device, "Invalid packet with ID of 0\n"); 1295 + continue; 1296 + } 1297 + } else { 1298 + struct scsi_cmnd *scmnd; 1299 + 1300 + /* Transaction 'rqst_id' corresponds to tag 'rqst_id - 1' */ 1301 + scmnd = scsi_host_find_tag(shost, rqst_id - 1); 1302 + if (scmnd == NULL) { 1303 + dev_err(&device->device, "Incorrect transaction ID\n"); 1304 + continue; 1305 + } 1306 + request = (struct storvsc_cmd_request *)scsi_cmd_priv(scmnd); 1307 + } 1308 + 1299 1309 storvsc_on_receive(stor_device, packet, request); 1310 + continue; 1300 1311 } 1312 + 1313 + memcpy(&request->vstor_packet, packet, 1314 + (sizeof(struct vstor_packet) - stor_device->vmscsi_size_delta)); 1315 + complete(&request->wait_event); 1301 1316 } 1302 1317 } 1303 1318 ··· 1339 1294 1340 1295 memset(&props, 0, sizeof(struct vmstorage_channel_properties)); 1341 1296 1342 - /* 1343 - * The size of vmbus_requestor is an upper bound on the number of requests 1344 - * that can be in-progress at any one time across all channels. 1345 - */ 1346 - device->channel->rqstor_size = scsi_driver.can_queue; 1297 + device->channel->max_pkt_size = STORVSC_MAX_PKT_SIZE; 1298 + device->channel->next_request_id_callback = storvsc_next_request_id; 1347 1299 1348 1300 ret = vmbus_open(device->channel, 1349 1301 ring_size, ··· 1666 1624 ret = vmbus_sendpacket(device->channel, vstor_packet, 1667 1625 (sizeof(struct vstor_packet) - 1668 1626 stor_device->vmscsi_size_delta), 1669 - (unsigned long)&stor_device->reset_request, 1627 + VMBUS_RQST_RESET, 1670 1628 VM_PKT_DATA_INBAND, 1671 1629 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); 1672 1630 if (ret != 0)
+51 -10
include/linux/hyperv.h
··· 181 181 * being freed while the ring buffer is being accessed. 182 182 */ 183 183 struct mutex ring_buffer_mutex; 184 + 185 + /* Buffer that holds a copy of an incoming host packet */ 186 + void *pkt_buffer; 187 + u32 pkt_buffer_size; 184 188 }; 185 189 186 190 ··· 794 790 795 791 #define VMBUS_NO_RQSTOR U64_MAX 796 792 #define VMBUS_RQST_ERROR (U64_MAX - 1) 793 + /* NetVSC-specific */ 797 794 #define VMBUS_RQST_ID_NO_RESPONSE (U64_MAX - 2) 795 + /* StorVSC-specific */ 796 + #define VMBUS_RQST_INIT (U64_MAX - 2) 797 + #define VMBUS_RQST_RESET (U64_MAX - 3) 798 798 799 799 struct vmbus_device { 800 800 u16 dev_type; ··· 806 798 bool perf_device; 807 799 bool allowed_in_isolated; 808 800 }; 801 + 802 + #define VMBUS_DEFAULT_MAX_PKT_SIZE 4096 809 803 810 804 struct vmbus_channel { 811 805 struct list_head listentry; ··· 1028 1018 u32 fuzz_testing_interrupt_delay; 1029 1019 u32 fuzz_testing_message_delay; 1030 1020 1021 + /* callback to generate a request ID from a request address */ 1022 + u64 (*next_request_id_callback)(struct vmbus_channel *channel, u64 rqst_addr); 1023 + /* callback to retrieve a request address from a request ID */ 1024 + u64 (*request_addr_callback)(struct vmbus_channel *channel, u64 rqst_id); 1025 + 1031 1026 /* request/transaction ids for VMBus */ 1032 1027 struct vmbus_requestor requestor; 1033 1028 u32 rqstor_size; 1029 + 1030 + /* The max size of a packet on this channel */ 1031 + u32 max_pkt_size; 1034 1032 }; 1035 1033 1036 - u64 vmbus_next_request_id(struct vmbus_requestor *rqstor, u64 rqst_addr); 1037 - u64 vmbus_request_addr(struct vmbus_requestor *rqstor, u64 trans_id); 1034 + u64 vmbus_next_request_id(struct vmbus_channel *channel, u64 rqst_addr); 1035 + u64 vmbus_request_addr(struct vmbus_channel *channel, u64 trans_id); 1038 1036 1039 1037 static inline bool is_hvsock_channel(const struct vmbus_channel *c) 1040 1038 { ··· 1681 1663 1682 1664 1683 1665 struct vmpacket_descriptor * 1666 + hv_pkt_iter_first_raw(struct vmbus_channel *channel); 1667 + 1668 + struct vmpacket_descriptor * 1684 1669 hv_pkt_iter_first(struct vmbus_channel *channel); 1685 1670 1686 1671 struct vmpacket_descriptor * 1687 1672 __hv_pkt_iter_next(struct vmbus_channel *channel, 1688 - const struct vmpacket_descriptor *pkt); 1673 + const struct vmpacket_descriptor *pkt, 1674 + bool copy); 1689 1675 1690 1676 void hv_pkt_iter_close(struct vmbus_channel *channel); 1677 + 1678 + static inline struct vmpacket_descriptor * 1679 + hv_pkt_iter_next_pkt(struct vmbus_channel *channel, 1680 + const struct vmpacket_descriptor *pkt, 1681 + bool copy) 1682 + { 1683 + struct vmpacket_descriptor *nxt; 1684 + 1685 + nxt = __hv_pkt_iter_next(channel, pkt, copy); 1686 + if (!nxt) 1687 + hv_pkt_iter_close(channel); 1688 + 1689 + return nxt; 1690 + } 1691 + 1692 + /* 1693 + * Get next packet descriptor without copying it out of the ring buffer 1694 + * If at end of list, return NULL and update host. 1695 + */ 1696 + static inline struct vmpacket_descriptor * 1697 + hv_pkt_iter_next_raw(struct vmbus_channel *channel, 1698 + const struct vmpacket_descriptor *pkt) 1699 + { 1700 + return hv_pkt_iter_next_pkt(channel, pkt, false); 1701 + } 1691 1702 1692 1703 /* 1693 1704 * Get next packet descriptor from iterator ··· 1726 1679 hv_pkt_iter_next(struct vmbus_channel *channel, 1727 1680 const struct vmpacket_descriptor *pkt) 1728 1681 { 1729 - struct vmpacket_descriptor *nxt; 1730 - 1731 - nxt = __hv_pkt_iter_next(channel, pkt); 1732 - if (!nxt) 1733 - hv_pkt_iter_close(channel); 1734 - 1735 - return nxt; 1682 + return hv_pkt_iter_next_pkt(channel, pkt, true); 1736 1683 } 1737 1684 1738 1685 #define foreach_vmbus_pkt(pkt, channel) \
+2 -2
net/vmw_vsock/hyperv_transport.c
··· 596 596 return -EOPNOTSUPP; 597 597 598 598 if (need_refill) { 599 - hvs->recv_desc = hv_pkt_iter_first(hvs->chan); 599 + hvs->recv_desc = hv_pkt_iter_first_raw(hvs->chan); 600 600 ret = hvs_update_recv_data(hvs); 601 601 if (ret) 602 602 return ret; ··· 610 610 611 611 hvs->recv_data_len -= to_read; 612 612 if (hvs->recv_data_len == 0) { 613 - hvs->recv_desc = hv_pkt_iter_next(hvs->chan, hvs->recv_desc); 613 + hvs->recv_desc = hv_pkt_iter_next_raw(hvs->chan, hvs->recv_desc); 614 614 if (hvs->recv_desc) { 615 615 ret = hvs_update_recv_data(hvs); 616 616 if (ret)