Repository navigation
virt_kvm: save and restore KVM nested virtualization state - #3744
Robert Nowotny (bitranox) wants to merge 5 commits into
Conversation
|
This PR modifies files containing For more on why we check whole files, instead of just diffs, check out the Rustonomicon |
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Enable capturing/restoring KVM nested-virtualization (VMX/SVM) state as part of x86_64 vCPU state, and document updated snapshot requirements/behavior.
Changes:
- Implement KVM nested-state get/set plumbing and expose it through vCPU state accessors.
- Make UEFI platform resolvers register based on device presence (including snapshot restore paths).
- Update snapshot documentation and REPL help text for file-backed memory requirements.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs | Wire nested-state into VP state access and skip empty restore ioctls. |
| vm/kvm/src/lib.rs | Add KVM nested-state ioctls, errors, and Processor get/set APIs. |
| openvmm/openvmm_entry/src/repl.rs | Clarify snapshot CLI prerequisite wording. |
| openvmm/openvmm_core/src/worker/dispatch.rs | Register UEFI platform resolvers whenever the UEFI device exists. |
| Guide/src/user_guide/openvmm/snapshots.md | Document that KVM snapshots include nested-virtualization state. |
36461c3 to
aa5dbc1
Compare
aa5dbc1 to
b7de430
Compare
b7de430 to
0437a2e
Compare
|
Robert Nowotny (@bitranox), parts of this snapshot work may still be useful, but it needs rebasing and splitting from the UEFI work now covered by #4147. Could you refresh the remaining KVM work if you plan to continue? Thanks. |
0437a2e to
bb12758
Compare
|
Ben Hillis (@benhillis) sorry for the slow reply. I rebased this onto current main and dropped the UEFI part, since #4147 covers it. What's left is the KVM nested-state save/restore. While I had it open, I fixed a bug in the original version: a VM reset didn't take the vCPU out of nested operation, so resetting a guest that runs its own hypervisor failed. The description has the details. I also rewrote it to match the smaller scope. |
There was a problem hiding this comment.
🟡 Changes recommended
Missing capability gating and unsaved queued interrupts can produce incomplete snapshots or failed resets.
2 open findings
4 resolved since last review
This dereferences a*const kvm_nested_statecreated from a&[u8], which has the same potential…bufis aVec<u8>, sobuf.as_mut_ptr()is only guaranteed to be aligned foru8. Casting it… The returnedusedlength is trusted without validation. If the kernel returns a size smaller than…get_nested_state()performsKVM_CHECK_EXTENSIONevery call, adding an extra ioctl per vCPU…
🧠 Review effort: Balanced
|
On the SVM point: as the Testing section says, I have no AMD host. The |
The KVM backend could restore only the at-reset nested state, so a VM that runs its own hypervisor could not be snapshotted. Implement save and restore with KVM_GET_NESTED_STATE and KVM_SET_NESTED_STATE. The buffer is sized from KVM_CAP_NESTED_STATE, and both the length the kernel returns and the size stored in a saved blob are bounds-checked before use. An empty blob still means the architectural reset state. A reset also has to take the vCPU out of nested operation before the registers are reset. KVM rejects the at-reset CR4 while the vCPU is in VMX operation, and reset_all writes the nested state last, so resetting a VM whose guest was running a hypervisor failed on the first register write. Call Processor::reset_nested_state() first.
Without the capability, nested state can be neither saved nor cleared, so a reset of a vCPU in nested operation still fails and a snapshot silently drops its nested state. Refuse nested_virt on such hosts.
On a partition with nested virtualization, every PIC interrupt was queued with KVM_INTERRUPT. KVM does not report a queued interrupt through KVM_GET_VCPU_EVENTS, so a VP stopped and saved before the guest took it lost the interrupt, and the PIC kept it in service. Queue the interrupt only while the VP is running a nested guest, where KVM must decide whether it causes an exit from that guest. Otherwise inject it through the injected-interrupt slot, as on other partitions, so that it is saved and restored with the rest of the VP state.
While the VP runs a nested guest, a PIC interrupt is queued with KVM_INTERRUPT. KVM reports a queued interrupt in neither the VP state nor the nested state, and it can keep holding it after a nested exit, until the L1 hypervisor enables interrupts. A save taken in that window lost the interrupt, which the PIC had already acknowledged. Track the queued interrupt in the VP runner until KVM reports the VP ready for another interrupt, which it does only once nothing is queued, and fail the save while it may still be queued.
d25a0ba to
5657720
Compare
There was a problem hiding this comment.
🟡 Changes recommended
AMD nested-hypervisor snapshot restoration loses the guest-programmed MSR_VM_HSAVE_PA address because it is not serialized, requiring save/restore support and a nonzero-address round-trip test.
1 open issue
Review effort: Balanced
KVM keeps the guest's host save area address outside the SVM nested state blob, so a nested hypervisor restored from a snapshot lost it and took a #GP on its next VMRUN. Save it in NestedState, write it back on restore, and zero it on reset.
There was a problem hiding this comment.
🔵 Needs a closer look
Vendor-specific nested-state save, restore, and reset behavior needs hardware-backed validation because the rebased reset path is untested in a booted guest and SVM host-save-area restore lacks AMD-host execution evidence.
No open issues 🎉
1 resolved since last review
Review effort: Balanced



The KVM backend could only put the nested state back to its reset value, so a VM running its own hypervisor could not be snapshotted. This PR implements save and restore with
KVM_GET_NESTED_STATEandKVM_SET_NESTED_STATE, and makes a VM reset take the vCPU out of nested operation before its registers are reset.Rebased onto #5085, which added
Processor::reset_nested_state()and the empty-blob reset inset_nested_state(). This PR used to carry its own helper for that; it now uses #5085's and keeps the empty blob as the reset value.Save and restore.
get_nested_state()sizes its buffer fromKVM_CHECK_EXTENSION(KVM_CAP_NESTED_STATE), asked on the VM fd, and returns an empty blob when the host lacks the capability.set_nested_state()writes a saved blob back. Both bounds-check the size field against the buffer, so neither a short kernel result nor a malformed snapshot can cause an out-of-bounds read.NestedStateis already last in the per-VP state order, after the registers, MSRs and CPUID that KVM checks the blob against.Reset.
reset_allresets the nested state last, but KVM refuses the at-reset CR4 while the vCPU is in VMX operation, so a reset that arrives while the guest is running a hypervisor fails on the first register write. A guest-initiated reboot doesn't hit this, because the guest's hypervisor leaves VMX operation before it resets. A reset the guest didn't ask for does.reset()now callsreset_nested_state()first on a partition with nested virtualization.SVM host save area. KVM keeps the guest's
MSR_VM_HSAVE_PAoutside the SVM nested state blob, soNestedStatenow carries it next to the blob. On AMD the KVM backend reads it when saving, writes it back when restoring, and zeroes it on reset. Other backends leave it zero.Capability. Nested virtualization now requires
KVM_CAP_NESTED_STATE(Linux 4.19), so a reset can always leave nested operation and a save can always capture the state.PIC interrupts. On a partition with nested virtualization, every PIC interrupt used to be queued with
KVM_INTERRUPT, which KVM never reports in saved state. Now the interrupt goes through the injected-interrupt slot, as on other partitions, unless the VP is running a nested guest. Then only KVM can decide whether it causes an exit from that guest, so it is still queued, and saving the VP fails until KVM reports the VP ready for another interrupt. KVM has no way to report or restore a queued interrupt, so a snapshot attempted in that window fails and can be retried instead of losing the interrupt.This only matters on the Gen1 and x86 Linux direct-boot chipsets, the only ones with a PIC. A Gen2 UEFI VM has none, so the window never opens there and no KVM change is needed for it. On an Intel host I traced a full boot of a nested Windows Gen2 guest with VBS and HVCI and counted zero
KVM_INTERRUPTcalls.Testing.
xtask fmt,clippy --workspace --all-targets,build -p openvmm,doc --workspaceand thekvm/virt_kvmtests.vmxon_pa/vmcs12_paand no flags, SVM getsKVM_STATE_NESTED_GIF_SET./dev/kvm) cover a PIC interrupt delivered outside a nested guest on a nested partition, and tracking a queued interrupt across a stop.reset_nested_state(), has not been booted.MSR_VM_HSAVE_PAand restores it into a fresh VM. I ran that test on Intel only, where it just checks that the field stays zero.NestedStateencoded before this change still decodes, with the address zero (checked locally, no test added).