Skip to content

Add opt-in transparent huge pages for scratch - #2111

Draft
ludfjig wants to merge 2 commits into
hyperlight-dev:mainfrom
ludfjig:ll/scratch-thp-opt-in
Draft

ludfjig wants to merge 2 commits into
hyperlight-dev:mainfrom
ludfjig:ll/scratch-thp-opt-in

Conversation

@ludfjig

@ludfjig ludfjig commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Depends on #2105, will mark ready when merged.

#2105 lets KVM and MSHV map scratch with 2 MiB pages, but only when the host backs scratch with transparent huge pages. Ubuntu and WSL set /sys/kernel/mm/transparent_hugepage/enabled to madvise, so scratch stays on 4 KiB pages there.

This change adds SandboxBuilder::scratch_transparent_huge_pages. When set, Hyperlight calls madvise(MADV_HUGEPAGE) on each new scratch mapping. Building the sandbox fails if the kernel lacks transparent huge page support. The default, false, leaves scratch to the host's policy.

The option is Linux-only. Windows and macOS have no equivalent AFAIK

Hosts with transparent huge pages set to always already get 2 MiB scratch pages from #2105, and the option made no measurable difference there. On madvise hosts, the option brings those gains. Results on madvise hosts, option off → on:

Benchmark KVM (WSL) MSHV 6.6 MSHV 6.18
heap_first_touch_new_sandbox/medium 71 → 14 ms 7.1 → 6.3 ms 15.2 → 9.2 ms
heap_random_access/large 19.1 → 18.7 ms 13.5 → 12.2 ms 14.0 → 12.9 ms
call_with_restore/small 363 → 489 µs 276 → 225 µs 264 → 275 µs

A KVM restore discards scratch, so the next call faults in a fresh 2 MiB page for each block it touches. MSHV zeroes scratch in place and has no such cost.

@ludfjig ludfjig added C-enhancement For PRs adding features, improving functionality, docs, tests, etc. A-perf Addresses performance A-api Related to the API or public interface O-linux Linux host support labels Nov 25, 2026
@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-thp-opt-in branch from a5de41d to 93ce469 Compare November 30, 2026 19:23
…parent huge pages for scratch on Linux, so KVM and MSHV can map it with 2 MiB pages on hosts that use them only on request.

Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
@ludfjig
ludfjig force-pushed the ll/scratch-thp-opt-in branch from 93ce469 to 78d23d8 Compare November 30, 2026 19:25
@hyperlight-gh-bot

Copy link
Copy Markdown

Benchmark Results

Measured commit: 78d23d882665
Baseline commit: 4c839511630d

kvm / amd (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 349.98 ns (➖ 1.09x slower)
vec_bytes 92.71 ns (➖ 1.00x faster)
371.64 µs (➖ 1.00x faster)

payload_allocation

slot_pool_segmented
65536 141.24 ns (➖ 1.02x faster)
262144 521.46 ns (➖ 1.00x faster)

sandboxes

create_initialized_and_drop
medium 13.19 ms (➖ 1.02x faster)

slot_pool

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

snapshot_files

load_snapshot_unverified
small 107.94 µs (➖ 1.00x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 1.96 µs (➖ 1.06x faster) 2.03 µs (➖ 1.07x slower)
262144 7.00 µs (➖ 1.00x faster) 7.52 µs (➖ 1.04x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 6.17 µs (➖ 1.04x faster) 6.20 µs (➖ 1.04x faster)
262144 26.98 µs (➖ 1.01x faster)
8192 1.02 µs (➖ 1.05x faster) 1.03 µs (➖ 1.01x slower)
kvm / intel (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 334.33 ns (➖ 1.09x faster)
vec_bytes 101.24 ns (➖ 1.05x faster)
710.37 µs (➖ 1.05x faster)

payload_allocation

slot_pool_segmented
65536 163.01 ns (➖ 1.01x faster)
262144 616.47 ns (➖ 1.02x faster)

sandboxes

create_initialized_and_drop
medium 15.40 ms (➖ 1.01x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
7.98 ns (➖ 1.06x faster) 8.09 ns (➖ 1.07x faster) 7.99 ns (➖ 1.06x faster)

snapshot_files

load_snapshot_unverified
small 58.98 µs (➖ 1.08x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.30 µs (➖ 1.06x faster) 2.27 µs (➖ 1.09x faster)
262144 8.15 µs (➖ 1.08x faster) 8.23 µs (➖ 1.07x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 7.75 µs (➖ 1.08x faster) 7.59 µs (➖ 1.09x faster)
262144 33.51 µs (➖ 1.02x faster)
8192 777.81 ns (➖ 1.06x faster) 787.56 ns (➖ 1.07x faster)
mshv3 / amd (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 370.59 ns (➖ 1.19x faster)
vec_bytes 123.03 ns (➖ 1.10x slower)
344.30 µs (➖ 1.30x slower)

payload_allocation

slot_pool_segmented
65536 192.81 ns (➖ 1.01x faster)
262144 790.97 ns (➖ 1.05x slower)

sandboxes

create_initialized_and_drop
medium 63.44 ms (➖ 1.00x faster)

slot_pool

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

snapshot_files

load_snapshot_unverified
small 99.11 µs (➖ 1.01x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.20 µs (➖ 1.00x faster) 2.38 µs (➖ 1.03x faster)
262144 9.73 µs (➖ 1.01x faster) 10.40 µs (➖ 1.04x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 8.46 µs (➖ 1.12x faster) 8.57 µs (➖ 1.13x faster)
262144 36.30 µs (➖ 1.04x faster)
8192 1.23 µs (➖ 1.04x slower) 1.35 µs (➖ 1.08x slower)
mshv3 / intel (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 371.68 ns (➖ 1.04x faster)
vec_bytes 93.71 ns (➖ 1.01x slower)
685.83 µs (➖ 1.00x faster)

payload_allocation

slot_pool_segmented
65536 171.06 ns (➖ 1.01x faster)
262144 631.80 ns (➖ 1.01x faster)

sandboxes

create_initialized_and_drop
medium 73.32 ms (➖ 1.00x slower)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
8.68 ns (➖ 1.00x faster) 8.99 ns (➖ 1.02x faster) 8.70 ns (➖ 1.00x faster)

snapshot_files

load_snapshot_unverified
small 51.73 µs (➖ 1.01x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.33 µs (➖ 1.03x slower) 2.27 µs (➖ 1.00x slower)
262144 7.92 µs (➖ 1.01x slower) 7.96 µs (➖ 1.01x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 7.40 µs (➖ 1.00x faster) 7.45 µs (➖ 1.01x faster)
262144 41.34 µs (➖ 1.00x faster)
8192 902.43 ns (➖ 1.03x faster) 900.46 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 579.60 ns (➖ 1.00x slower)
vec_bytes 171.45 ns (➖ 1.02x slower)
4.01 ms (➖ 1.05x faster)

payload_allocation

slot_pool_segmented
65536 240.12 ns (➖ 1.03x slower)
262144 793.54 ns (➖ 1.01x faster)

sandboxes

create_initialized_and_drop
medium 12.56 ms (➖ 1.06x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
10.41 ns (➖ 1.03x faster) 10.11 ns (➖ 1.03x faster) 10.42 ns (➖ 1.09x faster)

snapshot_files

load_snapshot_unverified
small 739.16 µs (➖ 1.02x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.57 µs (➖ 1.10x slower) 2.40 µs (➖ 1.02x slower)
262144 8.86 µs (➖ 1.02x faster) 9.31 µs (➖ 1.03x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 9.34 µs (➖ 1.04x slower) 8.87 µs (➖ 1.02x faster)
262144 38.82 µs (➖ 1.15x faster)
8192 1.28 µs (➖ 1.05x faster) 1.33 µ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 536.02 ns (➖ 1.08x slower)
vec_bytes 144.62 ns (➖ 1.13x faster)
3.22 ms (➖ 1.01x faster)

payload_allocation

slot_pool_segmented
65536 196.59 ns (➖ 1.00x slower)
262144 688.20 ns (➖ 1.03x slower)

sandboxes

create_initialized_and_drop
medium 13.83 ms (➖ 1.01x slower)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
8.83 ns (➖ 1.00x slower) 8.83 ns (➖ 1.00x slower) 8.83 ns (➖ 1.02x faster)

snapshot_files

load_snapshot_unverified
small 470.38 µs (➖ 1.04x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.62 µs (➖ 1.02x slower) 2.55 µs (➖ 1.00x faster)
262144 8.82 µs (➖ 1.00x slower) 8.80 µs (➖ 1.01x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 9.31 µs (➖ 1.02x faster) 9.35 µs (➖ 1.00x faster)
262144 35.51 µs (➖ 1.02x faster)
8192 1.11 µs (➖ 1.02x slower) 1.08 µs (➖ 1.01x faster)

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

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-api Related to the API or public interface A-perf Addresses performance C-enhancement For PRs adding features, improving functionality, docs, tests, etc. O-linux Linux host support

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant