Repository navigation
Expose how much scratch a sandbox has allocated - #2019
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the new guest scratch allocation accounting API.
No open issues 🎉
What changed in this PR
Exposes Sandbox::scratch_allocated_bytes() so hosts can track guest scratch allocation consistently across hypervisors.
Changes:
- Adds a public allocation counter with documented limits for guest-controlled values.
- Centralizes the bounded allocator read and reuses it during snapshot installation.
- Adds tests for allocation growth, deterministic counts across restores, and resets after restore and snapshot.
| File | Description |
|---|---|
src/hyperlight_host/src/sandbox/initialized.rs |
Adds the public API, usage documentation, and allocation-counter tests. |
src/hyperlight_host/src/mem/mgr.rs |
Reads and bounds the scratch allocator position and calculates allocated bytes. |
CHANGELOG.md |
Documents the allocation counter and its host accounting use case. |
Review effort: Balanced
Benchmark ResultsMeasured commit: kvm / amd (Linux) (➖ stable)No benchmark improved or regressed. Benchmark Resultsfunction_call_codec
payload_allocation
sandboxes
slot_pool
snapshot_files
virtq_readonly
virtq_readwrite
kvm / intel (Linux) (➖ stable)No benchmark improved or regressed. Benchmark Resultsfunction_call_codec
payload_allocation
sandboxes
slot_pool
snapshot_files
virtq_readonly
virtq_readwrite
mshv3 / amd (Linux) (➖ stable)No benchmark improved or regressed. Benchmark Resultsfunction_call_codec
payload_allocation
sandboxes
slot_pool
snapshot_files
virtq_readonly
virtq_readwrite
mshv3 / intel (Linux) (➖ stable)No benchmark improved or regressed. Benchmark Resultsfunction_call_codec
payload_allocation
sandboxes
slot_pool
snapshot_files
virtq_readonly
virtq_readwrite
hyperv-ws2025 / amd (Windows) (➖ stable)No benchmark improved or regressed. Benchmark Resultsfunction_call_codec
payload_allocation
sandboxes
slot_pool
snapshot_files
virtq_readonly
virtq_readwrite
hyperv-ws2025 / intel (Windows) (➖ stable)No benchmark improved or regressed. Benchmark Resultsfunction_call_codec
payload_allocation
sandboxes
slot_pool
snapshot_files
virtq_readonly
virtq_readwrite
Reported by |
ludfjig
left a comment
There was a problem hiding this comment.
Are there any alternatives we can consider where we can trust the value that comes out of here? Maybe related to dirty-bit tracking done by the hypervisor (I understand that it would be a bigger change though). But overall looks good to me!
Signed-off-by: danbugs <danilochiarlone@gmail.com>
Signed-off-by: danbugs <danilochiarlone@gmail.com>
c1e0cf6 to
e833eb4
Compare
#1968 removed SandboxBuilder::heap_size (and set_heap_size/get_heap_size), but the scratch_allocated_bytes_counts_written_pages test added by #2019 still called it, so main stopped compiling its tests once both landed — which is why clippy and miri fail on every PR. Drop the .heap_size(8 << 20) line; the test already sizes scratch with .scratch_size(16 << 20). Signed-off-by: danbugs <danilochiarlone@gmail.com>
Prepares the v0.19.0 release per `docs/how-to-make-releases.md` (the Cargo.toml version and CHANGELOG steps). The tag and GitHub release are cut after this merges. - Bump the workspace version `0.18.0` → `0.19.0`, and the internal `hyperlight-*` dependency version requirements with it. - Roll the CHANGELOG `[Prerelease]` section into `[v0.19.0]`, add a fresh empty `[Prerelease]`, and update the compare-link footer (`[v0.19.0]` added; `[Prerelease]` now compares from `v0.19.0`). - Sync the root `Cargo.lock` and `src/tests/rust_guests/Cargo.lock` to the new versions (workspace-only; no other dependencies touched). - Drop a stray `.heap_size(8 << 20)` call in the `scratch_allocated_bytes` test: #1968 removed `SandboxBuilder::heap_size`, but #2019's test (which landed separately) still called it, so main stopped compiling its tests once both merged. Folding the one-line fix in here so the release compiles. --------- Signed-off-by: danbugs <danilochiarlone@gmail.com>
hyperlight-host and hyperlight-common move to the 0.19.0 release, which has scratch resize (hyperlight-dev/hyperlight#1947) and scratch_allocated_bytes (hyperlight-dev/hyperlight#2019), so the [patch.crates-io] git pin goes from the crate, the clickhouse integration and the pptx-gen demo, and the crate can be published to crates.io again. The three lockfiles take 0.19.0 from crates.io; in re-resolving them cargo also moves some crates' windows-sys dependency to versions already in the locks (mostly 0.61.2). 0.19 drops the guest heap region (hyperlight-dev/hyperlight#1968), and with it SandboxConfiguration::set_heap_size, which the previous commit stopped calling. The CHANGELOG's 0.19 entry says what the re-exported hyperlight_host 0.19 changes, that a native kernel built against an earlier plat/hyperlight must be rebuilt, and, folding in the previous commit's note, what plat/hyperlight now requires and checks; the transport arena entry's own rebuild note goes. SNAPSHOT_CONTRACT stays 8: it was bumped for the transport arena after v0.20.1 and has not been released. Dev builds since then carry it, and their snapshots are refused all the same: the kernel change (the previous commit) rolls the key's other half, k<hash>, the embedded kernel's SHA-256, and Hyperlight's own snapshot ABI 9 refuses them besides. Closes hyperlight-dev#233 Signed-off-by: danbugs <danilochiarlone@gmail.com>
@danbugs If you're suggesting billing based on the allocator watermark to others, I will caution that it probably isn't a good idea, since it isn't necessarily trustworthy---it would be pretty easy to get undercharged. That said, it's difficult to efficiently calculate actually-used memory efficiently on all platforms.
@ludfjig We try hard to eliminate the expensive exit-on-first-write that's needed to allow the HV to track things. The tradeoff is that the host can't trust the tracking information, indeed. On host platform configurations where the physical memory of the scratch region is lazily populated, you could get some information at coarser granularity out of that, but it isn't (/can't be efficiently) the case everywhere. |
Adds
Sandbox::scratch_allocated_bytes(), which returns how far the guest's scratch bump allocator has moved since the sandbox was last restored or snapshotted.Every page a guest writes after a restore is taken from scratch through that allocator, whether it's a copy-on-write copy of a snapshot page, a fresh page, or a page table. So this is how much memory the sandbox has dirtied. The host already reads the allocator on restore. This makes it public, so a host such as fiberd can charge each sandbox for what it wrote, the same way on KVM, mshv, WHP and HVF.
SandboxMemoryManager::used_scratch_end_gpa()reads the allocator, held to the transport arena since the guest controls it.install_captured_snapshotnow uses it too.Sandbox::scratch_allocated_bytes()returns the bytes between the first free scratch page and that end. It is 0 after a restore or snapshot and capped at the scratch the guest can allocate.