Skip to content

Pick KVM scratch zeroing at runtime - #2084

Merged
jprendes merged 1 commit into
hyperlight-dev:mainfrom
ludfjig:kvm-runtime-scratch-zeroing
Nov 20, 2026
Merged

jprendes merged 1 commit into
hyperlight-dev:mainfrom
ludfjig:kvm-runtime-scratch-zeroing

Conversation

@ludfjig

@ludfjig ludfjig commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Restore zeroes scratch with fill(0) on KVM in default builds. zero_or_replace gated MADV_DONTNEED on not(feature = "mshv3"), and both kvm and mshv3 are default features.

This change checks get_available_hypervisor() at runtime and uses MADV_DONTNEED when the running hypervisor is KVM. MSHV and other backends keep fill(0).

snapshots/restore on a KVM host with default features:

Size Before After
default 21.7 µs 69.6 µs
small 224 µs 110 µs
medium 2.05 ms 112 µs
large 11.2 ms 134 µs

The default size matches kvm-only builds, which already use madvise. A size threshold could recover it later.

Closes #2083

Restore uses MADV_DONTNEED to zero scratch whenever the running
hypervisor is KVM. This makes KVM restores of medium and large
sandboxes about 18x to 86x faster in default builds.

The madvise path was gated on not(feature = "mshv3"). Both kvm and
mshv3 are default features, so KVM fell back to fill(0) over all of
scratch. MSHV pins guest pages and keeps fill(0).

Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
@ludfjig ludfjig added C-bug For PRs that fix bugs A-perf Addresses performance labels Nov 20, 2026
@ludfjig
ludfjig requested a review from danbugs as a code owner November 20, 2026 01:23
@ludfjig ludfjig added O-kvm Linux KVM backend O-linux Linux host support labels Nov 20, 2026
@ludfjig ludfjig added the C-bug For PRs that fix bugs label Nov 20, 2026
@ludfjig ludfjig added the A-perf Addresses performance label Nov 20, 2026
@ludfjig ludfjig added O-kvm Linux KVM backend O-linux Linux host support labels Nov 20, 2026
Copilot AI balanced review requested due to automatic review settings November 20, 2026 01:23

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.

🟢 Approval recommended

No active review issues were identified for the runtime-selected scratch-zeroing change.

No open issues 🎉

What changed in this PR

This PR addresses #2083 by selecting scratch-zeroing behavior at runtime, enabling lazy zeroing for KVM snapshot restores in default builds.

Changes:

  • Use MADV_DONTNEED when the selected hypervisor is KVM, with fill(0) as the fallback if it fails.
  • Preserve other backends’ zeroing behavior and document the KVM performance fix.
File Description
src/​hyperlight_host/​src/​mem/​shared_mem.rs Selects KVM lazy zeroing at runtime and updates safety comments and documentation.
CHANGELOG.md Records the scratch-zeroing performance fix for default KVM builds.

Review effort: Balanced

@hyperlight-gh-bot

Copy link
Copy Markdown

Benchmark Results

Measured commit: 675f96729a8d
Baseline commit: 3edcdc91bc76

kvm / amd (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 260.28 ns (➖ 1.03x faster)
vec_bytes 77.98 ns (➖ 1.05x faster)
371.73 µs (➖ 1.00x faster)

payload_allocation

slot_pool_segmented
262144 579.23 ns (➖ 1.05x faster)
65536 141.15 ns (➖ 1.05x faster)

sandboxes

create_initialized_and_drop
medium 13.29 ms (➖ 1.03x slower)

slot_pool

alloc_dealloc_128 alloc_dealloc_4096 alloc_dealloc_1500
7.70 ns (➖ 1.02x faster) 7.71 ns (➖ 1.02x faster) 7.69 ns (➖ 1.00x faster)

snapshot_files

load_snapshot_unverified
small 99.41 µs (➖ 1.01x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
262144 7.80 µs (➖ 1.07x slower) 8.53 µs (➖ 1.20x slower)
65536 1.92 µs (➖ 1.09x faster) 2.03 µs (➖ 1.05x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
262144 28.41 µs (➖ 1.03x slower)
65536 6.11 µs (➖ 1.03x faster) 6.23 µs (➖ 1.01x faster)
8192 1.02 µs (➖ 1.00x slower) 1.02 µs (➖ 1.07x faster)
kvm / intel (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 290.24 ns (➖ 1.07x faster)
vec_bytes 90.94 ns (➖ 1.08x faster)
741.57 µs (➖ 1.03x faster)

payload_allocation

slot_pool_segmented
262144 594.13 ns (➖ 1.04x faster)
65536 207.49 ns (➖ 1.01x slower)

sandboxes

create_initialized_and_drop
medium 14.73 ms (➖ 1.02x faster)

slot_pool

alloc_dealloc_128 alloc_dealloc_4096 alloc_dealloc_1500
8.18 ns (➖ 1.00x slower) 8.18 ns (➖ 1.01x slower) 8.17 ns (➖ 1.00x slower)

snapshot_files

load_snapshot_unverified
small 57.04 µs (➖ 1.03x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
262144 9.05 µs (➖ 1.01x slower) 8.98 µs (➖ 1.00x slower)
65536 2.55 µs (➖ 1.01x slower) 2.53 µs (➖ 1.01x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
262144 34.55 µs (➖ 1.04x faster)
65536 8.36 µs (➖ 1.02x faster) 8.44 µs (➖ 1.01x faster)
8192 850.50 ns (➖ 1.02x slower) 855.42 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 381.42 ns (➖ 1.02x slower)
vec_bytes 100.60 ns (➖ 1.06x faster)
265.41 µs (➖ 1.05x faster)

payload_allocation

slot_pool_segmented
262144 716.43 ns (➖ 1.00x faster)
65536 188.21 ns (➖ 1.00x faster)

sandboxes

create_initialized_and_drop
medium 64.68 ms (➖ 1.00x faster)

slot_pool

alloc_dealloc_128 alloc_dealloc_4096 alloc_dealloc_1500
9.69 ns (➖ 1.01x faster) 9.85 ns (➖ 1.01x slower) 10.18 ns (➖ 1.05x slower)

snapshot_files

load_snapshot_unverified
small 104.23 µs (➖ 1.01x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
262144 9.93 µs (➖ 1.01x faster) 10.53 µs (➖ 1.01x slower)
65536 2.25 µs (➖ 1.01x slower) 2.43 µs (➖ 1.01x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
262144 36.05 µs (➖ 1.07x faster)
65536 8.38 µs (➖ 1.20x faster) 8.47 µs (➖ 1.13x faster)
8192 1.25 µs (➖ 1.05x slower) 1.24 µs (➖ 1.00x faster)
mshv3 / intel (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 371.28 ns (➖ 1.08x slower)
vec_bytes 87.04 ns (➖ 1.05x faster)
771.45 µs (➖ 1.00x slower)

payload_allocation

slot_pool_segmented
262144 656.68 ns (➖ 1.00x slower)
65536 233.26 ns (➖ 1.06x faster)

sandboxes

create_initialized_and_drop
medium 54.08 ms (➖ 1.17x faster)

slot_pool

alloc_dealloc_128 alloc_dealloc_4096 alloc_dealloc_1500
9.14 ns (➖ 1.00x slower) 10.01 ns (➖ 1.11x slower) 9.03 ns (➖ 1.01x faster)

snapshot_files

load_snapshot_unverified
small 53.55 µs (➖ 1.01x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
262144 7.91 µs (➖ 1.00x faster) 7.96 µs (➖ 1.00x slower)
65536 2.25 µs (➖ 1.01x faster) 2.26 µs (➖ 1.00x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
262144 41.24 µs (➖ 1.05x slower)
65536 7.36 µs (➖ 1.02x faster) 7.45 µs (➖ 1.01x slower)
8192 884.64 ns (➖ 1.01x faster) 897.85 ns (➖ 1.03x faster)
hyperv-ws2025 / amd (Windows) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 544.46 ns (➖ 1.03x faster)
vec_bytes 163.73 ns (➖ 1.04x slower)
1.85 ms (➖ 1.05x faster)

payload_allocation

slot_pool_segmented
262144 785.10 ns (➖ 1.04x faster)
65536 230.62 ns (➖ 1.00x faster)

sandboxes

create_initialized_and_drop
medium 12.43 ms (➖ 1.04x faster)

slot_pool

alloc_dealloc_128 alloc_dealloc_4096 alloc_dealloc_1500
10.00 ns (➖ 1.00x slower) 9.98 ns (➖ 1.09x faster) 10.02 ns (➖ 1.01x slower)

snapshot_files

load_snapshot_unverified
small 987.15 µs (➖ 1.25x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
262144 9.24 µs (➖ 1.02x faster) 9.30 µs (➖ 1.00x slower)
65536 2.30 µs (➖ 1.01x faster) 2.31 µs (➖ 1.01x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
262144 38.70 µs (➖ 1.01x faster)
65536 9.25 µs (➖ 1.06x slower) 8.88 µs (➖ 1.02x faster)
8192 1.28 µs (➖ 1.02x slower) 1.29 µ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 574.88 ns (➖ 1.02x slower)
vec_bytes 169.45 ns (➖ 1.03x faster)
3.33 ms (➖ 1.06x slower)

payload_allocation

slot_pool_segmented
262144 740.87 ns (➖ 1.01x faster)
65536 236.47 ns (➖ 1.10x slower)

sandboxes

create_initialized_and_drop
medium 14.65 ms (➖ 1.03x slower)

slot_pool

alloc_dealloc_128 alloc_dealloc_4096 alloc_dealloc_1500
9.76 ns (➖ 1.00x faster) 10.05 ns (➖ 1.00x faster) 9.63 ns (➖ 1.00x faster)

snapshot_files

load_snapshot_unverified
small 690.31 µs (➖ 1.01x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
262144 7.69 µs (➖ 1.01x slower) 7.48 µs (➖ 1.04x faster)
65536 2.28 µs (➖ 1.00x slower) 2.24 µs (➖ 1.05x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
262144 47.62 µs (➖ 1.05x faster)
65536 8.32 µs (➖ 1.01x faster) 8.34 µs (➖ 1.00x faster)
8192 1.04 µs (➖ 1.13x faster) 1.10 µs (➖ 1.03x slower)

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

@jprendes
jprendes merged commit 3011e55 into hyperlight-dev:main Nov 20, 2026
71 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-perf Addresses performance C-bug For PRs that fix bugs O-kvm Linux KVM backend O-linux Linux host support

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Restore zeroes scratch eagerly on KVM in default builds

3 participants