Skip to content

feat(virtq): rebuild message queues on snapshot capture and restore - #2091

Open
andreiltd wants to merge 4 commits into
hyperlight-dev:mainfrom
andreiltd:restore/host-checkpoint
Open

andreiltd wants to merge 4 commits into
hyperlight-dev:mainfrom
andreiltd:restore/host-checkpoint

Conversation

@andreiltd

Copy link
Copy Markdown
Member

Currently on snapshot we are running the guest to reset its virtual queues and then we save those queues. This patch changes this to the host building fresh queues itself instead. Snapshot capture keeps the guest stopped.

This separates the lifetime of retained data from the queues used to deliver new messages and is important for guest-side zero-copy streams because it will allow us to refill pools with any scratch mapped memory not only memory bounded by initial pool layout. This also eliminates the need for snapshot preparation guest entry.

The new restore workflow looks roughly like this:

HOST
  Copy bootstrap images into scratch
                |
  Write request R into posted H2G buffers
                |
  Enter guest
                v
GUEST
  Detect generation change
                |
  from_bootstrap():
    rebuild private cursors, IDs, and buffer reservations
    leave shared rings and request R untouched
                |
  Receive and execute R

Initialize empty and prefilled rings through MemOps.

Signed-off-by: Tomasz Andrzejak <andreiltd@gmail.com>
Start fresh queues on capture, restore, and cloning. Keep retained
guest data separate, so it stays valid while the queues have their full
receive capacity. Preserve messages the host sends before the guest
next runs.

This provides the snapshot foundation for buffer replacement. It also
prepares for snapshot capture without running guest code to tidy the
queues.

Signed-off-by: Tomasz Andrzejak <andreiltd@gmail.com>
Signed-off-by: Tomasz Andrzejak <andreiltd@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 23, 2026 14:11
@andreiltd andreiltd added C-refactor For PRs that restructure or remove code without adding new functionality. S-needs-goldens Regenerate snapshot golden fixtures labels Nov 23, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Unsafe ownership reconstruction involving retained aliases, scratch reuse, and snapshot ABI changes requires human review and hypervisor-backed validation of capture, restore, and source continuation across supported architectures.

No open issues 🎉

What changed in this PR

Hyperlight snapshots use host-built queue images while the guest stays stopped. Guest entry reconstructs queue ownership, preserving retained data and host-written requests to support guest-side zero-copy streams.

Changes:

  • Deterministic queue initialization and producer retirement separate retained buffers from active queues.
  • Capture and restore provide full configured receive capacity before guest entry.
  • Snapshot ABI 12, documentation, and regression tests cover the transport lifecycle.
File Description
src/​tests/​rust_guests/​simpleguest/​src/​bin/​simpleguest-incomplete-init.rs Adds an early-halt fixture for incomplete initialization.
src/​hyperlight_host/​tests/​snapshot_goldens/​goldens_version.rs Sets snapshot goldens to v12.0.
src/​hyperlight_host/​tests/​integration_test.rs Tests retained buffers across transport generations with larger requests.
src/​hyperlight_host/​src/​sandbox/​uninitialized_evolve.rs Checks initialization readiness and tests early-halt rejection.
src/​hyperlight_host/​src/​sandbox/​snapshot/​tripwires.rs Pins the expected snapshot ABI to 12.
src/​hyperlight_host/​src/​sandbox/​snapshot/​mod.rs Clarifies scratch-size validation documentation.
src/​hyperlight_host/​src/​sandbox/​snapshot/​file/​transport.rs Uses bootstrap images in transport serialization tests.
src/​hyperlight_host/​src/​sandbox/​snapshot/​file/​media_types.rs Sets the snapshot ABI version to 12.
src/​hyperlight_host/​src/​sandbox/​snapshot/​file_tests.rs Tests retained buffers through OCI persistence and updates ABI assertions.
src/​hyperlight_host/​src/​sandbox/​outb.rs Removes checkpoint-specific host-call restrictions.
src/​hyperlight_host/​src/​sandbox/​initialized.rs Uses host-only capture and tests snapshot lifecycle behavior.
src/​hyperlight_host/​src/​mem/​virtq/​tests.rs Tests deterministic bootstrap validation and restoration.
src/​hyperlight_host/​src/​mem/​virtq/​mod.rs Builds, validates, and installs bootstrap ring images.
src/​hyperlight_host/​src/​mem/​virtq/​mem.rs Supports checked writes to private ring images.
src/​hyperlight_host/​src/​mem/​mgr.rs Installs fresh transport ownership and checks initialization readiness.
src/​hyperlight_host/​src/​mem/​layout.rs Updates mailbox documentation terminology.
src/​hyperlight_host/​src/​hypervisor/​hyperlight_vm/​test_support.rs Adds virtual CPU execution fault injection.
src/​hyperlight_guest/​src/​transport/​mod.rs Exposes initialization completion and removes checkpoint preparation.
src/​hyperlight_guest/​src/​transport/​context.rs Initializes queues and reconstructs producers on generation changes.
src/​hyperlight_guest/​src/​transport/​backing.rs Documents alias reservations across transport generations.
src/​hyperlight_guest_bin/​src/​lib.rs Publishes readiness after guest initialization.
src/​hyperlight_guest_bin/​src/​guest_function/​call.rs Removes checkpoint dispatch handling.
src/​hyperlight_common/​src/​virtq/​ring.rs Reconstructs private ring state from bootstrap geometry.
src/​hyperlight_common/​src/​virtq/​producer.rs Adds producer reconstruction, retirement, and ownership tests.
src/​hyperlight_common/​src/​virtq/​pool/​tests.rs Removes tests for the deleted pool-swap mechanism.
src/​hyperlight_common/​src/​virtq/​pool/​slot.rs Defines reusable pool geometry and deterministic allocation order.
src/​hyperlight_common/​src/​virtq/​pool.rs Exports PoolLayout.
src/​hyperlight_common/​src/​virtq/​mod.rs Adds ring initialization helpers and bootstrap exports.
src/​hyperlight_common/​src/​virtq/​buffer.rs Tests retained lease isolation with fresh pool instances.
src/​hyperlight_common/​src/​virtq/​bootstrap.rs Defines deterministic empty and prefilled queue images.
src/​hyperlight_common/​src/​transport.rs Removes checkpoint messages and defines initialization mailbox states.
src/​hyperlight_common/​src/​layout.rs Updates transport mailbox documentation terminology.
docs/​virtio-host-guest-communication.md Documents host-driven capture and guest ownership reconstruction.
docs/​snapshot-versioning.md Describes ABI 12 and snapshot regeneration requirements.
docs/​snapshot-oci-format.md Updates the protocol reference to snapshot bootstrap state.
CHANGELOG.md Records the ABI break, transport behavior, and removed APIs.

Review effort: Balanced

@hyperlight-gh-bot

Copy link
Copy Markdown

Benchmark Results

Measured commit: 4cd0c2642a64
Baseline commit: 7894cb20f748

kvm / amd (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 256.30 ns (➖ 1.08x faster)
vec_bytes 80.81 ns (➖ 1.01x faster)
374.16 µs (➖ 1.01x slower)

payload_allocation

slot_pool_segmented
65536 142.54 ns (➖ 1.01x slower)
262144 550.06 ns (➖ 1.06x slower)

sandboxes

create_initialized_and_drop
medium 13.71 ms (➖ 1.05x slower)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
7.68 ns (➖ 1.03x faster) 9.28 ns (➖ 1.21x slower) 7.68 ns (➖ 1.00x slower)

snapshot_files

load_snapshot_unverified
small 119.65 µs (➖ 1.04x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 1.90 µs (➖ 1.10x faster) 2.05 µs (➖ 1.08x slower)
262144 7.50 µs (➖ 1.03x slower) 7.08 µs (➖ 1.03x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 6.18 µs (➖ 1.03x faster) 6.20 µs (➖ 1.02x faster)
262144 28.20 µs (➖ 1.05x slower)
8192 1.02 µs (➖ 1.00x slower) 1.03 µs (➖ 1.02x slower)
kvm / intel (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 254.72 ns (➖ 1.03x faster)
vec_bytes 79.27 ns (➖ 1.05x faster)
663.60 µs (➖ 1.01x faster)

payload_allocation

slot_pool_segmented
65536 136.81 ns (➖ 1.26x faster)
262144 585.69 ns (➖ 1.17x slower)

sandboxes

create_initialized_and_drop
medium 15.04 ms (➖ 1.04x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
6.97 ns (➖ 1.02x slower) 7.00 ns (➖ 1.02x slower) 6.99 ns (➖ 1.02x slower)

snapshot_files

load_snapshot_unverified
small 59.02 µs (➖ 1.02x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.15 µs (➖ 1.00x faster) 2.13 µs (➖ 1.02x faster)
262144 7.58 µs (➖ 1.01x faster) 7.61 µs (➖ 1.01x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 7.14 µs (➖ 1.02x faster) 7.16 µs (➖ 1.01x faster)
262144 31.25 µs (➖ 1.05x slower)
8192 719.37 ns (➖ 1.02x slower) 724.85 ns (➖ 1.01x faster)
mshv3 / amd (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 417.92 ns (➖ 1.13x slower)
vec_bytes 103.31 ns (➖ 1.05x faster)
268.07 µs (➖ 1.31x faster)

payload_allocation

slot_pool_segmented
65536 193.26 ns (➖ 1.00x faster)
262144 750.24 ns (➖ 1.03x slower)

sandboxes

create_initialized_and_drop
medium 65.32 ms (➖ 1.01x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
9.87 ns (➖ 1.03x slower) 9.87 ns (➖ 1.01x slower) 9.87 ns (➖ 1.01x faster)

snapshot_files

load_snapshot_unverified
small 106.33 µs (➖ 1.04x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.24 µs (➖ 1.01x slower) 2.37 µs (➖ 1.03x faster)
262144 9.94 µs (➖ 1.09x faster) 10.27 µs (➖ 1.04x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 9.61 µs (➖ 1.01x faster) 10.21 µs (➖ 1.04x faster)
262144 36.31 µs (➖ 1.09x faster)
8192 1.23 µs (➖ 1.01x slower) 1.22 µs (➖ 1.04x faster)
mshv3 / intel (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 395.47 ns (➖ 1.09x slower)
vec_bytes 88.50 ns (➖ 1.10x faster)
692.20 µs (➖ 1.01x slower)

payload_allocation

slot_pool_segmented
65536 174.37 ns (➖ 1.42x faster)
262144 730.74 ns (➖ 1.00x faster)

sandboxes

create_initialized_and_drop
medium 82.28 ms (➖ 1.01x slower)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
8.69 ns (➖ 1.00x faster) 10.11 ns (➖ 1.16x slower) 8.69 ns (➖ 1.01x faster)

snapshot_files

load_snapshot_unverified
small 55.20 µs (➖ 1.01x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.29 µs (➖ 1.00x faster) 2.30 µs (➖ 1.00x slower)
262144 7.98 µs (➖ 1.00x faster) 7.96 µs (➖ 1.00x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 7.59 µs (➖ 1.02x slower) 7.57 µs (➖ 1.00x slower)
262144 38.43 µs (➖ 1.06x faster)
8192 893.80 ns (➖ 1.02x faster) 911.51 ns (➖ 1.01x faster)
hyperv-ws2025 / amd (Windows) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 575.79 ns (➖ 1.02x slower)
vec_bytes 162.51 ns (➖ 1.02x faster)
1.96 ms (➖ 1.01x faster)

payload_allocation

slot_pool_segmented
65536 228.85 ns (➖ 1.00x faster)
262144 796.99 ns (➖ 1.01x slower)

sandboxes

create_initialized_and_drop
medium 12.72 ms (➖ 1.09x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
10.51 ns (➖ 1.05x slower) 10.15 ns (➖ 1.02x slower) 10.36 ns (➖ 1.04x slower)

snapshot_files

load_snapshot_unverified
small 794.25 µs (➖ 1.02x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.30 µs (➖ 1.01x faster) 2.33 µs (➖ 1.01x faster)
262144 8.97 µs (➖ 1.04x faster) 9.32 µs (➖ 1.01x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 9.95 µs (➖ 1.04x slower) 8.97 µs (➖ 1.00x slower)
262144 38.17 µs (➖ 1.04x faster)
8192 1.30 µs (➖ 1.02x slower) 1.30 µs (➖ 1.03x slower)
hyperv-ws2025 / intel (Windows) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 585.99 ns (➖ 1.01x slower)
vec_bytes 169.47 ns (➖ 1.02x faster)
3.14 ms (➖ 1.03x slower)

payload_allocation

slot_pool_segmented
65536 212.88 ns (➖ 1.06x faster)
262144 763.06 ns (➖ 1.03x slower)

sandboxes

create_initialized_and_drop
medium 13.75 ms (➖ 1.04x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
10.56 ns (➖ 1.06x slower) 9.92 ns (➖ 1.12x faster) 10.10 ns (➖ 1.04x slower)

snapshot_files

load_snapshot_unverified
small 713.98 µs (➖ 1.05x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.29 µs (➖ 1.00x slower) 2.27 µs (➖ 1.01x faster)
262144 7.71 µs (➖ 1.00x slower) 7.49 µs (➖ 1.03x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 8.27 µs (➖ 1.01x faster) 8.28 µs (➖ 1.03x faster)
262144 40.05 µs (➖ 1.29x faster)
8192 1.04 µs (➖ 1.15x faster) 1.11 µs (➖ 1.04x slower)

Reported by cargo ci bench-report --candidate run:45191503406 --config-file bench_report.toml.

@ludfjig ludfjig left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am big fan of these changes! I also checked perf, snapshot is faster (yay!) but guest calls after restore is slower...

Benchmark MSHV 6.6 MSHV 6.18 KVM
snapshots/create/default −19% to −22% −18% to −20% −14% to −16%
snapshots/call_and_create/default −13% to −15% −11% to −14% −3% to −7%
guest_calls/call_with_restore/default +2% to +7% +3% to +6% +9% to +10%

I looked into the slow guest calls, and it's due to extra page faults in guest. After a restore, maybe_refresh builds both new producers before retiring the old ones. The new allocations land on fresh heap pages so more page faults.

Resetting the producers in place instead of having the two live at the same time before retiring the old one fixes most of it, and I think this shoudl be easy fix unless there's a reason we need both?

Separate: do we still need to save the transport blob in the snapshot on disk, or can they always be rebuilt from the layout? Maybe could be simplified?

Comment on lines 380 to 385
// SAFETY: The host discarded the old queues with no live chain handles.
// Old pools only release leases. New producers own the scratch slots.
unsafe {
self.g2h_producer
.reclaim_slots()
.expect("G2H pool recovery failed");
self.h2g_producer
.reclaim_slots()
.expect("H2G pool recovery failed");
old_g2h.retire().expect("G2H retirement failed");
old_h2g.retire().expect("H2G retirement failed");
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think we can replace the old ones inline or retire them before creating new one? It would save perf

Signed-off-by: Tomasz Andrzejak <andreiltd@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-refactor For PRs that restructure or remove code without adding new functionality. S-needs-goldens Regenerate snapshot golden fixtures

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants