Skip to content

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

Merged
ludfjig merged 1 commit into
hyperlight-dev:mainfrom
ludfjig:ll/scratch-gpa-alignment
Dec 1, 2026
Merged

ludfjig merged 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.

@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

This comment has been minimized.

andreiltd
andreiltd previously approved these changes Nov 26, 2026

@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!

…can map it with 2 MiB pages.

Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
@hyperlight-gh-bot

Copy link
Copy Markdown

Benchmark Results

Measured commit: f7c31df6393b
Baseline commit: 4c839511630d

kvm / amd (Linux) (✅ 1.68x)

Top improvements

  • payload_allocation/slot_pool_segmented/262144 — ✅ 1.68x faster

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 310.78 ns (➖ 1.05x slower)
vec_bytes 95.43 ns (➖ 1.04x slower)
372.22 µs (➖ 1.01x faster)

payload_allocation

slot_pool_segmented
65536 142.95 ns (➖ 1.01x slower)
262144 523.64 ns (✅ 1.68x faster)

sandboxes

create_initialized_and_drop
medium 12.84 ms (➖ 1.05x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
8.14 ns (➖ 1.05x slower) 7.70 ns (➖ 1.02x faster) 7.71 ns (➖ 1.01x faster)

snapshot_files

load_snapshot_unverified
small 105.20 µs (➖ 1.06x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 1.93 µs (➖ 1.10x faster) 2.03 µs (➖ 1.03x slower)
262144 7.31 µs (➖ 1.01x faster) 7.33 µs (➖ 1.00x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 6.15 µs (➖ 1.03x faster) 6.17 µs (➖ 1.03x faster)
262144 27.25 µs (➖ 1.01x faster)
8192 1.03 µs (➖ 1.00x slower) 1.02 µs (➖ 1.00x slower)
kvm / intel (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 265.45 ns (➖ 1.16x faster)
vec_bytes 90.55 ns (➖ 1.00x slower)
656.43 µs (➖ 1.00x slower)

payload_allocation

slot_pool_segmented
65536 141.79 ns (➖ 1.02x faster)
262144 531.54 ns (➖ 1.00x slower)

sandboxes

create_initialized_and_drop
medium 14.86 ms (➖ 1.05x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
7.15 ns (➖ 1.00x slower) 7.29 ns (➖ 1.00x faster) 7.18 ns (➖ 1.00x faster)

snapshot_files

load_snapshot_unverified
small 54.33 µs (➖ 1.01x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.10 µs (➖ 1.01x slower) 2.10 µs (➖ 1.01x faster)
262144 7.51 µs (➖ 1.01x slower) 7.46 µs (➖ 1.00x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 6.94 µs (➖ 1.01x faster) 6.98 µs (➖ 1.01x faster)
262144 29.30 µs (➖ 1.06x faster)
8192 708.42 ns (➖ 1.00x faster) 712.02 ns (➖ 1.02x faster)
mshv3 / amd (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 360.37 ns (➖ 1.16x faster)
vec_bytes 116.81 ns (➖ 1.06x faster)
326.41 µs (➖ 1.00x faster)

payload_allocation

slot_pool_segmented
65536 193.53 ns (➖ 1.00x faster)
262144 811.47 ns (➖ 1.08x slower)

sandboxes

create_initialized_and_drop
medium 65.56 ms (➖ 1.26x slower)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
9.93 ns (➖ 1.01x faster) 9.87 ns (➖ 1.00x faster) 9.89 ns (➖ 1.01x faster)

snapshot_files

load_snapshot_unverified
small 105.53 µs (➖ 1.08x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.22 µs (➖ 1.02x faster) 2.37 µs (➖ 1.09x faster)
262144 9.88 µs (➖ 1.01x slower) 10.56 µs (➖ 1.02x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 8.33 µs (➖ 1.17x faster) 8.23 µs (➖ 1.19x faster)
262144 38.69 µs (➖ 1.01x slower)
8192 1.23 µs (➖ 1.01x slower) 1.31 µs (➖ 1.07x faster)
mshv3 / intel (Linux) (✅ 1.59x)

Top improvements

  • sandboxes/create_initialized_and_drop/medium — ✅ 1.59x faster

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 353.93 ns (➖ 1.22x faster)
vec_bytes 94.31 ns (➖ 1.02x slower)
677.05 µs (➖ 1.05x slower)

payload_allocation

slot_pool_segmented
65536 177.63 ns (➖ 1.04x slower)
262144 719.64 ns (➖ 1.13x slower)

sandboxes

create_initialized_and_drop
medium 54.04 ms (✅ 1.59x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
8.94 ns (➖ 1.03x slower) 9.24 ns (➖ 1.02x slower) 8.92 ns (➖ 1.02x slower)

snapshot_files

load_snapshot_unverified
small 57.02 µs (➖ 1.04x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.29 µs (➖ 1.02x slower) 2.28 µs (➖ 1.02x slower)
262144 7.99 µs (➖ 1.00x slower) 7.88 µs (➖ 1.00x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 7.45 µs (➖ 1.01x slower) 7.44 µs (➖ 1.01x faster)
262144 39.62 µs (➖ 1.02x slower)
8192 894.33 ns (➖ 1.02x faster) 905.27 ns (➖ 1.02x faster)
hyperv-ws2025 / amd (Windows) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 560.42 ns (➖ 1.04x faster)
vec_bytes 174.17 ns (➖ 1.04x slower)
4.13 ms (➖ 1.03x slower)

payload_allocation

slot_pool_segmented
65536 240.07 ns (➖ 1.05x slower)
262144 795.11 ns (➖ 1.00x slower)

sandboxes

create_initialized_and_drop
medium 12.71 ms (➖ 1.03x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
10.47 ns (➖ 1.01x slower) 10.10 ns (➖ 1.00x slower) 10.42 ns (➖ 1.00x slower)

snapshot_files

load_snapshot_unverified
small 715.28 µs (➖ 1.03x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.59 µs (➖ 1.08x slower) 2.35 µs (➖ 1.05x faster)
262144 8.91 µs (➖ 1.05x faster) 9.45 µs (➖ 1.04x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 8.84 µs (➖ 1.01x slower) 9.10 µs (➖ 1.02x slower)
262144 40.19 µs (➖ 1.03x slower)
8192 1.26 µs (➖ 1.02x faster) 1.27 µs (➖ 1.00x slower)
hyperv-ws2025 / intel (Windows) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 561.34 ns (➖ 1.06x faster)
vec_bytes 162.03 ns (➖ 1.08x faster)
3.70 ms (➖ 1.01x faster)

payload_allocation

slot_pool_segmented
65536 212.70 ns (➖ 1.02x faster)
262144 755.68 ns (➖ 1.02x faster)

sandboxes

create_initialized_and_drop
medium 14.34 ms (➖ 1.14x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
10.14 ns (➖ 1.01x faster) 10.94 ns (➖ 1.10x slower) 9.90 ns (➖ 1.01x faster)

snapshot_files

load_snapshot_unverified
small 579.58 µs (➖ 1.01x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.28 µs (➖ 1.04x faster) 2.28 µs (➖ 1.01x slower)
262144 7.90 µs (➖ 1.02x faster) 7.62 µs (➖ 1.01x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 9.70 µs (➖ 1.04x faster) 8.18 µs (➖ 1.04x faster)
262144 42.67 µs (➖ 1.01x faster)
8192 1.06 µs (➖ 1.01x slower) 1.05 µs (➖ 1.00x slower)

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

@ludfjig
ludfjig merged commit 54ae6d8 into hyperlight-dev:main Dec 1, 2026
76 of 82 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-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