Skip to content

Align scratch memory with its guest address on Linux - #2105

Open
ludfjig wants to merge 1 commit into
hyperlight-dev:mainfrom
ludfjig:ll/scratch-gpa-alignment
Open

ludfjig wants to merge 1 commit into
hyperlight-dev:mainfrom
ludfjig:ll/scratch-gpa-alignment

Conversation

@ludfjig

@ludfjig ludfjig commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Guest scratch memory on Linux is mapped with 4 KiB pages on KVM and MSHV even when transparent huge pages are set to always. Hypervisors can use 2 MiB pages only when a region's host virtual address and guest physical address have the same offset within a 2 MiB boundary. Plain mmap places scratch at an arbitrary page address, so those offsets rarely match.

From the KVM API docs:

It is recommended that the lower 21 bits of guest_phys_addr and userspace_addr be identical. This allows large pages in the guest to be backed by large pages in the host.

This change aligns Linux scratch allocations of 2 MiB or more to match their guest physical address offset:

  • Reserve 2 MiB of extra virtual address space for scratch during sandbox creation and resized restore.
  • Place the usable scratch range inside that reservation so its host address matches the scratch GPA modulo 2 MiB.
  • Protect the unused ends of the reservation as PROT_NONE so they extend the guard pages and use no extra physical memory.
  • Leave other platforms and scratch sizes under 2 MiB unchanged.

This does not enable huge pages inside the guest. Guest page tables still use 4 KiB pages, and only the host hypervisor mappings use 2 MiB pages when transparent huge pages are enabled on the host.

Benchmark results with transparent huge pages set to always:

Benchmark Before After
KVM heap_first_touch_new_sandbox/large 272 ms 26.0 ms
KVM heap_first_touch_new_sandbox/medium 68.9 ms 7.0 ms
KVM call_with_restore/large 454 µs 231 µs
KVM heap_random_access/large 12.9 ms 11.5 ms
MSHV 6.18 heap_first_touch_new_sandbox/large 40.8 ms 38.0 ms
MSHV 6.18 heap_random_access/large 14.1 ms 13.1 ms
MSHV 6.6 heap_first_touch_new_sandbox/large 25.8 ms 24.1 ms
MSHV 6.6 heap_random_access/large 12.6 ms 11.6 ms

CI's KVM runners do not set transparent huge pages to always, so the CI benchmark comment shows no change.

@ludfjig ludfjig added C-enhancement For PRs adding features, improving functionality, docs, tests, etc. A-perf Addresses performance labels Nov 25, 2026
@hyperlight-gh-bot

This comment has been minimized.

@ludfjig
ludfjig force-pushed the ll/scratch-gpa-alignment branch from f3304c5 to ddae45a Compare November 25, 2026 04:12
@hyperlight-gh-bot

This comment has been minimized.

…can map it with 2 MiB pages.

Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
@ludfjig
ludfjig force-pushed the ll/scratch-gpa-alignment branch from ddae45a to 147734e Compare November 25, 2026 17:44
@ludfjig
ludfjig marked this pull request as ready for review November 25, 2026 17:44
Copilot AI balanced review requested due to automatic review settings November 25, 2026 17:44

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 remain for the Linux scratch-memory alignment changes.

No open issues 🎉

What changed in this PR

This PR improves Linux scratch-memory performance by matching host and guest address offsets. This lets Hyperlight’s KVM and MSHV backends use 2 MiB mappings when transparent huge pages are set to always.

Changes:

  • Aligns scratch allocations of at least 2 MiB while preserving usable bounds and guard pages.
  • Applies alignment during snapshot-based sandbox creation and scratch resizing.
  • Adds alignment checks and documents scratch-size guidance and performance benefits.
File Description
src/​hyperlight_host/​src/​sandbox/​initialized.rs Checks scratch alignment during sandbox creation and snapshot restore.
src/​hyperlight_host/​src/​sandbox/​config.rs Documents scratch-size guidance for MSHV performance.
src/​hyperlight_host/​src/​sandbox/​builder.rs Adds the same guidance to the builder API documentation.
src/​hyperlight_host/​src/​mem/​shared_mem.rs Implements aligned allocations, separates reservation ownership from logical bounds, and tests alignment and guards.
src/​hyperlight_host/​src/​mem/​mgr.rs Uses aligned allocations for snapshot creation and scratch resizing.
CHANGELOG.md Records Linux alignment behavior and measured KVM performance benefits.

Review effort: Balanced

@hyperlight-gh-bot

Copy link
Copy Markdown

Benchmark Results

Measured commit: 147734ed047f
Baseline commit: c46735cd076c

kvm / amd (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 286.07 ns (➖ 1.04x faster)
vec_bytes 82.26 ns (➖ 1.04x slower)
371.89 µs (➖ 1.00x faster)

payload_allocation

slot_pool_segmented
65536 153.11 ns (➖ 1.07x slower)
262144 525.64 ns (➖ 1.01x faster)

sandboxes

create_initialized_and_drop
medium 12.80 ms (➖ 1.04x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
7.67 ns (➖ 1.33x faster) 7.68 ns (➖ 1.00x faster) 7.68 ns (➖ 1.00x faster)

snapshot_files

load_snapshot_unverified
small 109.20 µs (➖ 1.01x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 1.91 µs (➖ 1.09x faster) 2.04 µs (➖ 1.07x slower)
262144 6.96 µs (➖ 1.05x faster) 6.99 µs (➖ 1.05x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 6.39 µs (➖ 1.02x faster) 6.20 µs (➖ 1.08x faster)
262144 28.10 µs (➖ 1.02x slower)
8192 1.02 µs (➖ 1.08x faster) 1.04 µ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 334.09 ns (➖ 1.08x slower)
vec_bytes 98.03 ns (➖ 1.02x slower)
776.92 µs (➖ 1.03x slower)

payload_allocation

slot_pool_segmented
65536 162.05 ns (➖ 1.00x slower)
262144 600.65 ns (➖ 1.03x faster)

sandboxes

create_initialized_and_drop
medium 15.01 ms (➖ 1.03x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
8.29 ns (➖ 1.01x faster) 8.31 ns (➖ 1.01x faster) 8.32 ns (➖ 1.01x faster)

snapshot_files

load_snapshot_unverified
small 71.49 µs (➖ 1.12x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.58 µs (➖ 1.01x slower) 2.57 µs (➖ 1.01x faster)
262144 9.19 µs (➖ 1.02x slower) 9.00 µs (➖ 1.00x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 8.44 µs (➖ 1.01x faster) 8.46 µs (➖ 1.00x faster)
262144 35.46 µs (➖ 1.01x faster)
8192 844.22 ns (➖ 1.01x faster) 859.20 ns (➖ 1.00x faster)
mshv3 / amd (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 396.34 ns (➖ 1.11x slower)
vec_bytes 102.10 ns (➖ 1.07x slower)
266.28 µs (➖ 1.15x faster)

payload_allocation

slot_pool_segmented
65536 191.26 ns (➖ 1.01x faster)
262144 727.34 ns (➖ 1.13x faster)

sandboxes

create_initialized_and_drop
medium 64.00 ms (➖ 1.02x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
9.86 ns (➖ 1.01x faster) 9.95 ns (➖ 1.01x slower) 9.86 ns (➖ 1.00x faster)

snapshot_files

load_snapshot_unverified
small 97.04 µs (➖ 1.01x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.42 µs (➖ 1.09x slower) 2.38 µs (➖ 1.03x faster)
262144 9.56 µs (➖ 1.02x faster) 11.54 µs (➖ 1.10x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 8.37 µs (➖ 1.16x faster) 8.29 µs (➖ 1.17x faster)
262144 36.39 µs (➖ 1.06x faster)
8192 1.24 µs (➖ 1.02x slower) 1.21 µs (➖ 1.03x faster)
mshv3 / intel (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 372.43 ns (➖ 1.05x faster)
vec_bytes 88.81 ns (➖ 1.04x slower)
740.74 µs (➖ 1.06x slower)

payload_allocation

slot_pool_segmented
65536 175.90 ns (➖ 1.02x slower)
262144 632.52 ns (➖ 1.05x faster)

sandboxes

create_initialized_and_drop
medium 65.28 ms (➖ 1.14x slower)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
8.72 ns (➖ 1.11x faster) 9.06 ns (➖ 1.02x faster) 9.15 ns (➖ 1.05x slower)

snapshot_files

load_snapshot_unverified
small 53.53 µs (➖ 1.00x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.24 µs (➖ 1.02x faster) 2.27 µs (➖ 1.00x slower)
262144 7.94 µs (➖ 1.01x slower) 7.96 µs (➖ 1.00x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 7.40 µs (➖ 1.02x faster) 7.52 µs (➖ 1.00x slower)
262144 38.64 µs (➖ 1.00x slower)
8192 902.32 ns (➖ 1.02x faster) 907.33 ns (➖ 1.02x faster)
hyperv-ws2025 / amd (Windows) (✅ 1.89x)

Top improvements

  • slot_pool/alloc_dealloc_4096 — ✅ 1.89x faster

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 561.08 ns (➖ 1.01x slower)
vec_bytes 156.53 ns (➖ 1.05x faster)
2.25 ms (➖ 1.01x slower)

payload_allocation

slot_pool_segmented
65536 228.36 ns (➖ 1.42x faster)
262144 800.34 ns (➖ 1.01x slower)

sandboxes

create_initialized_and_drop
medium 14.17 ms (➖ 1.03x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
10.37 ns (✅ 1.89x faster) 10.09 ns (➖ 1.00x slower) 10.35 ns (➖ 1.00x faster)

snapshot_files

load_snapshot_unverified
small 735.90 µs (➖ 1.02x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.31 µs (➖ 1.01x faster) 2.34 µs (➖ 1.01x slower)
262144 9.23 µs (➖ 1.01x slower) 9.14 µs (➖ 1.02x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 8.80 µs (➖ 1.03x faster) 8.91 µs (➖ 1.01x slower)
262144 38.91 µs (➖ 1.21x faster)
8192 1.27 µs (➖ 1.00x slower) 1.27 µs (➖ 1.02x slower)
hyperv-ws2025 / intel (Windows) (❌ 1.53x)

Top regressions

  • payload_allocation/slot_pool_segmented/65536 — ❌ 1.53x slower

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 533.24 ns (➖ 1.06x slower)
vec_bytes 145.43 ns (➖ 1.04x slower)
3.29 ms (➖ 1.03x faster)

payload_allocation

slot_pool_segmented
65536 296.23 ns (❌ 1.53x slower)
262144 675.95 ns (➖ 1.02x slower)

sandboxes

create_initialized_and_drop
medium 13.57 ms (➖ 1.01x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
8.82 ns (➖ 1.01x slower) 8.82 ns (➖ 1.01x slower) 8.82 ns (➖ 1.01x slower)

snapshot_files

load_snapshot_unverified
small 493.80 µs (➖ 1.04x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.56 µs (➖ 1.02x slower) 2.57 µs (➖ 1.02x slower)
262144 8.81 µs (➖ 1.02x slower) 8.82 µs (➖ 1.02x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 9.41 µs (➖ 1.00x slower) 9.30 µs (➖ 1.00x faster)
262144 35.48 µs (➖ 1.02x slower)
8192 1.07 µs (➖ 1.02x faster) 1.09 µs (➖ 1.04x slower)

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

@andreiltd andreiltd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice!

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

A-perf Addresses performance C-enhancement For PRs adding features, improving functionality, docs, tests, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants