1.. SPDX-License-Identifier: GPL-2.0 2 3VMBus 4===== 5VMBus is a software construct provided by Hyper-V to guest VMs. It 6consists of a control path and common facilities used by synthetic 7devices that Hyper-V presents to guest VMs. The control path is 8used to offer synthetic devices to the guest VM and, in some cases, 9to rescind those devices. The common facilities include software 10channels for communicating between the device driver in the guest VM 11and the synthetic device implementation that is part of Hyper-V, and 12signaling primitives to allow Hyper-V and the guest to interrupt 13each other. 14 15VMBus is modeled in Linux as a bus, with the expected /sys/bus/vmbus 16entry in a running Linux guest. The VMBus driver (drivers/hv/vmbus_drv.c) 17establishes the VMBus control path with the Hyper-V host, then 18registers itself as a Linux bus driver. It implements the standard 19bus functions for adding and removing devices to/from the bus. 20 21Most synthetic devices offered by Hyper-V have a corresponding Linux 22device driver. These devices include: 23 24* SCSI controller 25* NIC 26* Graphics frame buffer 27* Keyboard 28* Mouse 29* PCI device pass-thru 30* Heartbeat 31* Time Sync 32* Shutdown 33* Memory balloon 34* Key/Value Pair (KVP) exchange with Hyper-V 35* Hyper-V online backup (a.k.a. VSS) 36 37Guest VMs may have multiple instances of the synthetic SCSI 38controller, synthetic NIC, and PCI pass-thru devices. Other 39synthetic devices are limited to a single instance per VM. Not 40listed above are a small number of synthetic devices offered by 41Hyper-V that are used only by Windows guests and for which Linux 42does not have a driver. 43 44Hyper-V uses the terms "VSP" and "VSC" in describing synthetic 45devices. "VSP" refers to the Hyper-V code that implements a 46particular synthetic device, while "VSC" refers to the driver for 47the device in the guest VM. For example, the Linux driver for the 48synthetic NIC is referred to as "netvsc" and the Linux driver for 49the synthetic SCSI controller is "storvsc". These drivers contain 50functions with names like "storvsc_connect_to_vsp". 51 52VMBus channels 53-------------- 54An instance of a synthetic device uses VMBus channels to communicate 55between the VSP and the VSC. Channels are bi-directional and used 56for passing messages. Most synthetic devices use a single channel, 57but the synthetic SCSI controller and synthetic NIC may use multiple 58channels to achieve higher performance and greater parallelism. 59 60Each channel consists of two ring buffers. These are classic ring 61buffers from a university data structures textbook. If the read 62and writes pointers are equal, the ring buffer is considered to be 63empty, so a full ring buffer always has at least one byte unused. 64The "in" ring buffer is for messages from the Hyper-V host to the 65guest, and the "out" ring buffer is for messages from the guest to 66the Hyper-V host. In Linux, the "in" and "out" designations are as 67viewed by the guest side. The ring buffers are memory that is 68shared between the guest and the host, and they follow the standard 69paradigm where the memory is allocated by the guest, with the list 70of GPAs that make up the ring buffer communicated to the host. Each 71ring buffer consists of a header page (4 Kbytes) with the read and 72write indices and some control flags, followed by the memory for the 73actual ring. The size of the ring is determined by the VSC in the 74guest and is specific to each synthetic device. The list of GPAs 75making up the ring is communicated to the Hyper-V host over the 76VMBus control path as a GPA Descriptor List (GPADL). See function 77vmbus_establish_gpadl(). 78 79Each ring buffer is mapped into contiguous Linux kernel virtual 80space in three parts: 1) the 4 Kbyte header page, 2) the memory 81that makes up the ring itself, and 3) a second mapping of the memory 82that makes up the ring itself. Because (2) and (3) are contiguous 83in kernel virtual space, the code that copies data to and from the 84ring buffer need not be concerned with ring buffer wrap-around. 85Once a copy operation has completed, the read or write index may 86need to be reset to point back into the first mapping, but the 87actual data copy does not need to be broken into two parts. This 88approach also allows complex data structures to be easily accessed 89directly in the ring without handling wrap-around. 90 91On arm64 with page sizes > 4 Kbytes, the header page must still be 92passed to Hyper-V as a 4 Kbyte area. But the memory for the actual 93ring must be aligned to PAGE_SIZE and have a size that is a multiple 94of PAGE_SIZE so that the duplicate mapping trick can be done. Hence 95a portion of the header page is unused and not communicated to 96Hyper-V. This case is handled by vmbus_establish_gpadl(). 97 98Hyper-V enforces a limit on the aggregate amount of guest memory 99that can be shared with the host via GPADLs. This limit ensures 100that a rogue guest can't force the consumption of excessive host 101resources. For Windows Server 2019 and later, this limit is 102approximately 1280 Mbytes. For versions prior to Windows Server 1032019, the limit is approximately 384 Mbytes. 104 105VMBus channel messages 106---------------------- 107All messages sent in a VMBus channel have a standard header that includes 108the message length, the offset of the message payload, some flags, and a 109transactionID. The portion of the message after the header is 110unique to each VSP/VSC pair. 111 112Messages follow one of two patterns: 113 114* Unidirectional: Either side sends a message and does not 115 expect a response message 116* Request/response: One side (usually the guest) sends a message 117 and expects a response 118 119The transactionID (a.k.a. "requestID") is for matching requests & 120responses. Some synthetic devices allow multiple requests to be in- 121flight simultaneously, so the guest specifies a transactionID when 122sending a request. Hyper-V sends back the same transactionID in the 123matching response. 124 125Messages passed between the VSP and VSC are control messages. For 126example, a message sent from the storvsc driver might be "execute 127this SCSI command". If a message also implies some data transfer 128between the guest and the Hyper-V host, the actual data to be 129transferred may be embedded with the control message, or it may be 130specified as a separate data buffer that the Hyper-V host will 131access as a DMA operation. The former case is used when the size of 132the data is small and the cost of copying the data to and from the 133ring buffer is minimal. For example, time sync messages from the 134Hyper-V host to the guest contain the actual time value. When the 135data is larger, a separate data buffer is used. In this case, the 136control message contains a list of GPAs that describe the data 137buffer. For example, the storvsc driver uses this approach to 138specify the data buffers to/from which disk I/O is done. 139 140Three functions exist to send VMBus channel messages: 141 1421. vmbus_sendpacket(): Control-only messages and messages with 143 embedded data -- no GPAs 1442. vmbus_sendpacket_getid(): Similar to vmbus_sendpacket() but returns 145 a transaction ID that is matched against a reply from the Hyper-V 146 host. Useful when the struct completion (or other rendezvous data) 147 must reside on the caller's stack. By invalidating the transaction 148 ID before exiting the function due to a timeout or other error, a 149 late arriving interrupt is assured to not trash the stack. 1503. vmbus_sendpacket_mpb_desc(): Message with one or more data ranges 151 to target. Each range consists of a single offset and length plus 152 a list of GPAs that describe a logical area of guest memory. 153 154Historically, Linux guests have trusted Hyper-V to send well-formed 155and valid messages, and Linux drivers for synthetic devices did not 156fully validate messages. With the introduction of processor 157technologies that fully encrypt guest memory and that allow the 158guest to not trust the hypervisor (AMD SEV-SNP, Intel TDX), trusting 159the Hyper-V host is no longer a valid assumption. The drivers for 160VMBus synthetic devices are being updated to fully validate any 161values read from memory that is shared with Hyper-V, which includes 162messages from VMBus devices. To facilitate such validation, 163messages read by the guest from the "in" ring buffer are copied to a 164temporary buffer that is not shared with Hyper-V. Validation is 165performed in this temporary buffer without the risk of Hyper-V 166maliciously modifying the message after it is validated but before 167it is used. 168 169Synthetic Interrupt Controller (synic) 170-------------------------------------- 171Hyper-V provides each guest CPU with a synthetic interrupt controller 172that is used by VMBus for host-guest communication. While each synic 173defines 16 synthetic interrupts (SINT), Linux uses only one of the 16 174(VMBUS_MESSAGE_SINT). All interrupts related to communication between 175the Hyper-V host and a guest CPU use that SINT. 176 177The SINT is mapped to a single per-CPU architectural interrupt (i.e, 178an 8-bit x86/x64 interrupt vector, or an arm64 PPI INTID). Because 179each CPU in the guest has a synic and may receive VMBus interrupts, 180they are best modeled in Linux as per-CPU interrupts. This model works 181well on arm64 where a single per-CPU Linux IRQ is allocated for 182VMBUS_MESSAGE_SINT. This IRQ appears in /proc/interrupts as an IRQ labelled 183"Hyper-V VMbus". Since x86/x64 lacks support for per-CPU IRQs, an x86 184interrupt vector is statically allocated (HYPERVISOR_CALLBACK_VECTOR) 185across all CPUs and explicitly coded to call vmbus_isr(). In this case, 186there's no Linux IRQ, and the interrupts are visible in aggregate in 187/proc/interrupts on the "HYP" line. 188 189The synic provides the means to demultiplex the architectural interrupt into 190one or more logical interrupts and route the logical interrupt to the proper 191VMBus handler in Linux. This demultiplexing is done by vmbus_isr() and 192related functions that access synic data structures. 193 194The synic is not modeled in Linux as an irq chip or irq domain, 195and the demultiplexed logical interrupts are not Linux IRQs. As such, 196they don't appear in /proc/interrupts or /proc/irq. The CPU 197affinity for one of these logical interrupts is controlled via an 198entry under /sys/bus/vmbus as described below. 199 200VMBus interrupts 201---------------- 202VMBus provides a mechanism for the guest to interrupt the host when 203the guest has queued new messages in a ring buffer. The host 204expects that the guest will send an interrupt only when an "out" 205ring buffer transitions from empty to non-empty. If the guest sends 206interrupts at other times, the host deems such interrupts to be 207unnecessary. If a guest sends an excessive number of unnecessary 208interrupts, the host may throttle that guest by suspending its 209execution for a few seconds to prevent a denial-of-service attack. 210 211Similarly, the host will interrupt the guest via the synic when 212it sends a new message on the VMBus control path, or when a VMBus 213channel "in" ring buffer transitions from empty to non-empty due to 214the host inserting a new VMBus channel message. The control message stream 215and each VMBus channel "in" ring buffer are separate logical interrupts 216that are demultiplexed by vmbus_isr(). It demultiplexes by first checking 217for channel interrupts by calling vmbus_chan_sched(), which looks at a synic 218bitmap to determine which channels have pending interrupts on this CPU. 219If multiple channels have pending interrupts for this CPU, they are 220processed sequentially. When all channel interrupts have been processed, 221vmbus_isr() checks for and processes any messages received on the VMBus 222control path. 223 224The guest CPU that a VMBus channel will interrupt is selected by the 225guest when the channel is created, and the host is informed of that 226selection. VMBus devices are broadly grouped into two categories: 227 2281. "Slow" devices that need only one VMBus channel. The devices 229 (such as keyboard, mouse, heartbeat, and timesync) generate 230 relatively few interrupts. Their VMBus channels are all 231 assigned to interrupt the VMBUS_CONNECT_CPU, which is always 232 CPU 0. 233 2342. "High speed" devices that may use multiple VMBus channels for 235 higher parallelism and performance. These devices include the 236 synthetic SCSI controller and synthetic NIC. Their VMBus 237 channels interrupts are assigned to CPUs that are spread out 238 among the available CPUs in the VM so that interrupts on 239 multiple channels can be processed in parallel. 240 241The assignment of VMBus channel interrupts to CPUs is done in the 242function init_vp_index(). This assignment is done outside of the 243normal Linux interrupt affinity mechanism, so the interrupts are 244neither "unmanaged" nor "managed" interrupts. 245 246The CPU that a VMBus channel will interrupt can be seen in 247/sys/bus/vmbus/devices/<deviceGUID>/ channels/<channelRelID>/cpu. 248When running on later versions of Hyper-V, the CPU can be changed 249by writing a new value to this sysfs entry. Because VMBus channel 250interrupts are not Linux IRQs, there are no entries in /proc/interrupts 251or /proc/irq corresponding to individual VMBus channel interrupts. 252 253An online CPU in a Linux guest may not be taken offline if it has 254VMBus channel interrupts assigned to it. Starting in kernel v6.15, 255any such interrupts are automatically reassigned to some other CPU 256at the time of offlining. The "other" CPU is chosen by the 257implementation and is not load balanced or otherwise intelligently 258determined. If the CPU is onlined again, channel interrupts previously 259assigned to it are not moved back. As a result, after multiple CPUs 260have been offlined, and perhaps onlined again, the interrupt-to-CPU 261mapping may be scrambled and non-optimal. In such a case, optimal 262assignments must be re-established manually. For kernels v6.14 and 263earlier, any conflicting channel interrupts must first be manually 264reassigned to another CPU as described above. Then when no channel 265interrupts are assigned to the CPU, it can be taken offline. 266 267The VMBus channel interrupt handling code is designed to work 268correctly even if an interrupt is received on a CPU other than the 269CPU assigned to the channel. Specifically, the code does not use 270CPU-based exclusion for correctness. In normal operation, Hyper-V 271will interrupt the assigned CPU. But when the CPU assigned to a 272channel is being changed via sysfs, the guest doesn't know exactly 273when Hyper-V will make the transition. The code must work correctly 274even if there is a time lag before Hyper-V starts interrupting the 275new CPU. See comments in target_cpu_store(). 276 277VMBus device creation/deletion 278------------------------------ 279Hyper-V and the Linux guest have a separate message-passing path 280that is used for synthetic device creation and deletion. This 281path does not use a VMBus channel. See vmbus_post_msg() and 282vmbus_on_msg_dpc(). 283 284The first step is for the guest to connect to the generic 285Hyper-V VMBus mechanism. As part of establishing this connection, 286the guest and Hyper-V agree on a VMBus protocol version they will 287use. This negotiation allows newer Linux kernels to run on older 288Hyper-V versions, and vice versa. 289 290The guest then tells Hyper-V to "send offers". Hyper-V sends an 291offer message to the guest for each synthetic device that the VM 292is configured to have. Each VMBus device type has a fixed GUID 293known as the "class ID", and each VMBus device instance is also 294identified by a GUID. The offer message from Hyper-V contains 295both GUIDs to uniquely (within the VM) identify the device. 296There is one offer message for each device instance, so a VM with 297two synthetic NICs will get two offers messages with the NIC 298class ID. The ordering of offer messages can vary from boot-to-boot 299and must not be assumed to be consistent in Linux code. Offer 300messages may also arrive long after Linux has initially booted 301because Hyper-V supports adding devices, such as synthetic NICs, 302to running VMs. A new offer message is processed by 303vmbus_process_offer(), which indirectly invokes vmbus_add_channel_work(). 304 305Upon receipt of an offer message, the guest identifies the device 306type based on the class ID, and invokes the correct driver to set up 307the device. Driver/device matching is performed using the standard 308Linux mechanism. 309 310The device driver probe function opens the primary VMBus channel to 311the corresponding VSP. It allocates guest memory for the channel 312ring buffers and shares the ring buffer with the Hyper-V host by 313giving the host a list of GPAs for the ring buffer memory. See 314vmbus_establish_gpadl(). 315 316Once the ring buffer is set up, the device driver and VSP exchange 317setup messages via the primary channel. These messages may include 318negotiating the device protocol version to be used between the Linux 319VSC and the VSP on the Hyper-V host. The setup messages may also 320include creating additional VMBus channels, which are somewhat 321mis-named as "sub-channels" since they are functionally 322equivalent to the primary channel once they are created. 323 324Finally, the device driver may create entries in /dev as with 325any device driver. 326 327The Hyper-V host can send a "rescind" message to the guest to 328remove a device that was previously offered. Linux drivers must 329handle such a rescind message at any time. Rescinding a device 330invokes the device driver "remove" function to cleanly shut 331down the device and remove it. Once a synthetic device is 332rescinded, neither Hyper-V nor Linux retains any state about 333its previous existence. Such a device might be re-added later, 334in which case it is treated as an entirely new device. See 335vmbus_onoffer_rescind(). 336 337For some devices, such as the KVP device, Hyper-V automatically 338sends a rescind message when the primary channel is closed, 339likely as a result of unbinding the device from its driver. 340The rescind causes Linux to remove the device. But then Hyper-V 341immediately reoffers the device to the guest, causing a new 342instance of the device to be created in Linux. For other 343devices, such as the synthetic SCSI and NIC devices, closing the 344primary channel does *not* result in Hyper-V sending a rescind 345message. The device continues to exist in Linux on the VMBus, 346but with no driver bound to it. The same driver or a new driver 347can subsequently be bound to the existing instance of the device. 348