Repository navigation
Restore the guest TSC exactly on KVM - #2015
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The regression test can falsely fail due to scheduling delays and Hyper-V TSC_ADJUST behavior, while the documentation incorrectly guarantees rewind behavior across all backends.
3 open issues
What changed in this PR
This PR addresses #2014 by making KVM restore the guest TSC reliably during closely spaced sandbox restores, without an extra ioctl.
Changes:
- Write a decoy TSC before the snapshot value in the same
KVM_SET_MSRScall, with special handling for zero. - Add guest counter reads and regression tests for repeated restores and zero values.
- Document guest-time behavior and record the fix in the changelog.
| File | Description |
|---|---|
src/tests/rust_guests/simpleguest/src/main.rs |
Exposes ReadTsc for host tests. |
src/hyperlight_host/src/sandbox/initialized.rs |
Tests TSC rewind and zero-value restore. |
src/hyperlight_host/src/hypervisor/virtual_machine/kvm/x86_64.rs |
Adds paired TSC writes to bypass KVM synchronization. |
docs/msr.md |
Describes guest timing and the KVM restore mechanism. |
CHANGELOG.md |
Records the KVM TSC restore fix. |
Review effort: Balanced
e02c90e to
9d8531c
Compare
Restore did not rewind the guest TSC on KVM when restores were less than a second apart. KVM ignores a host TSC write within one second of the value it expects and treats it as a vCPU sync. Write a decoy TSC value half the counter range away immediately before the restored value, in the same KVM_SET_MSRS call. KVM then applies the restored value exactly, with no extra ioctl. KVM also syncs a zero write, so write a restored value of 0 as 1. Add tests that restore repeatedly and that restore a zero TSC. Fixes hyperlight-dev#2014 Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
c334b69 to
7591df8
Compare
Benchmark ResultsMeasured commit: kvm / amd (Linux) (➖ stable)No benchmark improved or regressed. Benchmark Resultsfunction_call_codec
payload_allocation
sandboxes
slot_pool
snapshot_files
virtq_readonly
virtq_readwrite
kvm / intel (Linux) (➖ stable)No benchmark improved or regressed. Benchmark Resultsfunction_call_codec
payload_allocation
sandboxes
slot_pool
snapshot_files
virtq_readonly
virtq_readwrite
mshv3 / amd (Linux) (➖ stable)No benchmark improved or regressed. Benchmark Resultsfunction_call_codec
payload_allocation
sandboxes
slot_pool
snapshot_files
virtq_readonly
virtq_readwrite
mshv3 / intel (Linux) (➖ stable)No benchmark improved or regressed. Benchmark Resultsfunction_call_codec
payload_allocation
sandboxes
slot_pool
snapshot_files
virtq_readonly
virtq_readwrite
hyperv-ws2025 / amd (Windows) (➖ stable)No benchmark improved or regressed. Benchmark Resultsfunction_call_codec
payload_allocation
sandboxes
slot_pool
snapshot_files
virtq_readonly
virtq_readwrite
hyperv-ws2025 / intel (Windows) (➖ stable)No benchmark improved or regressed. Benchmark Resultsfunction_call_codec
payload_allocation
sandboxes
slot_pool
snapshot_files
virtq_readonly
virtq_readwrite
Reported by |
danbugs
left a comment
There was a problem hiding this comment.
Small question. Otherwise, LGTM


Restore does not rewind the guest TSC on KVM when it comes less than about a second after the previous restore. Restore now writes a decoy TSC value half the counter range away just before the snapshot value, in the same
KVM_SET_MSRScall. KVM then applies the snapshot value exactly, with no extra ioctl.Fixes #2014