Skip to content

perf(vm): stop repeating per-launch bookkeeping before the VM starts - #6056

Open
balajinvda wants to merge 5 commits into
ai-openshell:mainfrom
balajinvda:perf/6054-vm-launch-bookkeeping/balajinvda
Open

balajinvda wants to merge 5 commits into
ai-openshell:mainfrom
balajinvda:perf/6054-vm-launch-bookkeeping/balajinvda

Conversation

@balajinvda

@balajinvda balajinvda commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Removes three pieces of per-launch bookkeeping the VM driver repeats on every sandbox create with an unchanged result: one debugfs process per overlay command, hashing the embedded guest binaries for the image cache key, and an image preparation worker for a cached digest-pinned bootstrap image. This PR now also carries #6057's change (closed in favour of this one), since that change depends on the build-time identity commit here. One commit per change; behaviour is unchanged apart from timing.

The third change first proposed here (caching the host supervisor's hash) was reverted after review: file metadata cannot vouch for contents that a write through an open shared mapping can still change, so the supervisor is hashed on every launch as before. That item now has its own issue, #6131.

Related Issue

Closes #6054 (items 1 and 2; the host supervisor hash moved to #6131)
Closes #6055

Changes

  • perf(vm): write a launch's overlay files in one debugfs run: RootfsImageBatch (in rootfs.rs) stages the same mkdir / rm / write / set_inode_field commands in the same order and runs them through the existing run_debugfs_batch (debugfs -w -f): one run for the guest overlay files, one for the boundary bundle, one for the generation cleanup. debugfs reports a failing command on stderr and exits 0 in both modes, so the disk contents and the success or failure are the same as with separate runs. A path debugfs would read as two lines is refused when staged, before anything is written; staged temp files are removed when the batch runs or is dropped. write_rootfs_image_file keeps its signature (used by runtime.rs) and becomes a one-file batch; run_debugfs is now test-only. The vm.prepare_guest span moves to a new prepare_guest_overlay and so also covers the mount manifest and the write.
  • perf(vm): hash the guest runtime identity at build time: build.rs hashes exactly the bytes the crate embeds (from OUT_DIR, plus the init script, which it now watches) and sandbox_guest_runtime_identity() returns that constant. Same value, so existing cached root disks stay valid. Adds sha2 as a build dependency (already a dependency; Cargo.lock unchanged).
  • fix(vm): hash the cached host supervisor on every launch again: reverts perf(vm): hash the cached host supervisor once per file version (246766c) after review; the branch's net diff has no supervisor change.
  • Measured on an x86-64 KVM host without SHA instructions with the overlay batching, the build-time guest identity, the since-reverted supervisor hash cache and VM driver starts an image preparation worker on every create, even for a cached digest-pinned image #6055's change (p50, ten re-creates of a cached digest-pinned image): image resolution 114 → 1–2 ms, create_sandbox received → launcher spawned 367 → 41–67 ms; the overlay writes take 13–27 ms of that. The supervisor cache affected only the time after launcher spawned, so the image resolution and create-to-launcher figures do not depend on it (per the timings in VM driver repeats per-launch bookkeeping before the VM starts: a debugfs process per overlay command, guest identity re-hashed #6054; not re-measured without it). These measurements were taken on the v0.1.7 driver; the code paths are unchanged on main, which this branch is based on (659b45f3b). Per-item numbers and which are inferred are in VM driver repeats per-launch bookkeeping before the VM starts: a debugfs process per overlay command, guest identity re-hashed #6054.
  • perf(vm): resolve a cached digest-pinned bootstrap image without a worker (from perf(vm): resolve a cached digest-pinned bootstrap image without a preparation worker #6057):
    • prepare_images_in_worker first asks cached_digest_pinned_image_plan. It returns a plan only when: no rootfs tar; a bootstrap image is configured; the requested image is the bootstrap image (or the request is bootstrap-only); the sandbox is registered, not deleting and not already preparing; the reference is not a local build image and carries a digest; the root disk image_cache_rootfs_image(bootstrap_image_cache_identity(digest)) exists; registry_auth succeeds; and no local container engine answers. These are the conditions under which the worker answers from the cache without a registry call since fix(vm): boot a digest-pinned image from the cache without a registry call #4582.
    • On that path it emits what the worker emitted, in order, under the same vm.resolve_bootstrap_image span: the no local container engine available warning, ensuring cached root disk image (registry), root disk image cache hit (digest-pinned, no registry call), and publish_registry_cache_hit (Pulling, CacheHit, Pulled).
    • Every other case returns None before any log or event and goes to the worker, which prepares, reports and fails exactly as before. Errors are left to the worker on purpose (for example a missing sandbox is still Cancelled, from the worker path), so error precedence does not change. Cancellation after the plan is still caught by the existing ensure_provisioning_active check that follows image resolution. The overlay preparation keeps its worker.
    • The engine probe is passed as a LocalEngineProbe function so the tests can run on hosts where Docker answers (CI runners included); production passes connect_local_container_engine.
    • Measured on an x86-64 KVM host with both changes, the worker bypass and the build-time guest identity (together with the rest of VM driver repeats per-launch bookkeeping before the VM starts: a debugfs process per overlay command, guest identity re-hashed #6054's changes, p50, ten re-creates of a cached digest-pinned image): create_sandbox received → sandbox root disk plan resolved 114 → 1–2 ms. This figure is not a measurement of the worker bypass alone. Measured on the v0.1.7 driver with the same changes; the code path is unchanged on main (659b45f3b), which this branch is based on.

Testing

  • Checks appropriate to the affected code and behavior pass: cargo fmt -p openshell-driver-vm -- --check, cargo clippy -p openshell-driver-vm --all-targets -- -D warnings, cargo test -p openshell-driver-vm (lib: 306 passed with all four commits), cargo check -p openshell-driver-vm --no-default-features
  • Unit tests added/updated (if applicable):
    • rootfs_image_batch_writes_what_one_debugfs_per_command_wrote: applies the launch's shape of writes, modes and removals (a replaced file, a new nested directory, a path with a space, harmless failures on a missing file) one debugfs -R per command to one copy of an image and as one batch to another, then compares every entry's mode, owner, size and contents; checks the staged files are gone.
    • rootfs_image_batch_refuses_a_path_debugfs_reads_as_two_lines: newline/CR paths are refused at staging, only valid commands are staged, a dropped batch removes its staged files.
    • guest_runtime_identity_built_once_matches_the_embedded_bytes: the build-time value equals the hash of the embedded bytes computed at run time.
    • post_overlay_provisioning_stages_record_child_spans updated for prepare_guest_overlay.
  • E2E tests added/updated (if applicable)

The unit tests ran on Linux x86-64 with e2fsprogs installed; the build used stub embedded runtime artifacts (OPENSHELL_VM_RUNTIME_COMPRESSED_DIR unset). The overlay batching and build-time identity changes (with the since-reverted supervisor cache), on the v0.1.7 driver, booted VM sandboxes from a cached digest-pinned image on an x86-64 KVM host, and the unit tests above (in their v0.1.7 form) also passed on arm64.

Also, for the worker bypass:

  • cached_digest_pinned_image_plan_skips_the_worker_only_on_a_digest_cache_hit: eight cases through a stub worker that records each run (bootstrap-only hit, workload equal to the bootstrap image, cache miss, tag, other workload image, rootfs tar, reachable engine, locally built image); checks which ones ran the worker, the plan, and that the fast path emits Pulling, CacheHit, Pulled. With the fast path disabled it fails with bootstrap-only hit: worker ran.
  • cached_digest_pinned_image_plan_keeps_the_worker_path_errors: with the root disk cached, a missing and a deleting sandbox still fail as Cancelled and no worker starts.

Checklist

  • Follows Conventional Commits
  • Commits are signed off (DCO)
  • Architecture docs updated (if applicable)

A sandbox launch writes its per-sandbox files into the overlay ext4 (the
init drop-ins and their manifest, the mount manifest, then the boundary
configuration, certificate and key) with one `debugfs -w -R` process per
command: a mkdir per parent directory, rm and write per file,
set_inode_field per mode. That is about thirty debugfs processes per
launch. Measured with the same debugfs, overlay template and commands on
an x86-64 workstation: 152 to 183 ms as separate processes, 7 to 9 ms as
one `debugfs -f` run.

RootfsImageBatch stages the same commands in the same order and runs
them as one `debugfs -w -f` process (the existing run_debugfs_batch): one
run for the guest overlay files, one for the boundary bundle (the
boundary provisioning sits between the two), and one for the three
removals of a runtime generation's material. debugfs reports a failing
command (an existing directory, a missing file) on stderr and exits 0 in
both modes, so a batch leaves the same disk contents and the same
success or failure as the separate runs. A path debugfs would read as
two lines is refused when it is staged, before anything is written, and
staged contents are removed when the batch has run or is dropped.

The vm.prepare_guest span moves to prepare_guest_overlay, which stages
the drop-ins and the mount manifest and writes them, so it now covers
the mount manifest and the write itself.

Test: rootfs_image_batch_writes_what_one_debugfs_per_command_wrote applies
the launch's shape of writes, modes and removals (including the harmless
failures) both ways to copies of one image and compares every entry's
mode, owner, size and contents; rootfs_image_batch_refuses_a_path_debugfs_reads_as_two_lines
covers the refusal and the cleanup of a dropped batch.

Closes ai-openshell#6054

Signed-off-by: Balaji Ganesan <bganesan@nvidia.com>
Every bootstrap root disk's image cache key includes
sandbox_guest_runtime_identity(), a SHA-256 over the embedded sandbox,
vm-init and umoci blobs and the guest init script. It was computed on
every call, and every launch resolves its root disk in a fresh image
preparation worker process, so every launch hashed the embedded blobs
again, even on a cache hit. On an x86-64 host without SHA instructions
most of the 90 ms between the worker's "ensuring cached root disk image"
and its cache hit is this hash (inferred from the driver log gap, not
profiled).

build.rs now hashes exactly the files the crate embeds (from OUT_DIR,
plus the init script, which build.rs now watches) and the function
returns that constant. The value is unchanged, so cached root disks stay
valid.

Test: guest_runtime_identity_built_once_matches_the_embedded_bytes holds
the build-time value equal to the hash of the embedded bytes computed at
run time, as before.

Closes ai-openshell#6054

Signed-off-by: Balaji Ganesan <bganesan@nvidia.com>
Every launch validates the cached host supervisor in host_supervisor_binary
by hashing all of it (about 50 MB) on an async runtime thread. On an
x86-64 host without SHA instructions the driver log shows about 280 ms
between "launcher spawned" and the host supervisor's start, consistent
with a software SHA-256 of that file (inferred, not profiled). It runs
while the guest boots, so it is mostly hidden today, but it becomes the
floor of a launch once the guest boots faster, and it holds a runtime
thread meanwhile.

A file this driver process hashed and found good is not hashed again
while its device, inode, size, mode, mtime and ctime are unchanged. Any
write, chmod, chown, rename or replacement changes the ctime (set by the
kernel; no caller can set it back) or the inode, and the file is hashed
again. The kernel stamps ctime from a coarse clock, so a rewrite in the
same tick as the file's last change can keep its ctime: only a file
whose ctime is at least 1 s old is remembered (git's "racily clean"
rule), so any later change gets a later ctime. Only a file unchanged
across its own hash is remembered, a refused file never is, the memory
is per embedded supervisor digest, and a new driver process hashes again
on its first launch.

Test: host_supervisor_validated_once_per_file_version counts the hashes
across a just-written file (not remembered), a settled file (hashed
once), a same-size rewrite in place, a replacement by rename, a chmod
that drops the execute bit, another embedded digest and a fresh process.

Closes ai-openshell#6054

Signed-off-by: Balaji Ganesan <bganesan@nvidia.com>
mr-miles added a commit to mr-miles/openshell-firecracker that referenced this pull request Nov 30, 2026
The trial built its runtime from the old `patched` branch (e71f2d2c, now
`patched-v0.1.9`) through OPENSHELL_RUNTIME_*. The pin itself is now the
fork's `patched` branch at 36761e1 (upstream f16795b, which carries #5361,
plus the L7 SigV4 fix and ai-openshell/openshell#6056/#6057), so the
driver crate, its git dependencies and the embedded runtime all come from
one commit and the runtime override is dropped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TaylorMutch

Copy link
Copy Markdown
Contributor

The supervisor validation cache has an integrity-check bypass that should be addressed before merging the hash-cache change.

In crates/openshell-driver-vm/src/rootfs.rs, validate_host_supervisor_once returns success without reading the file when its path, device, inode, size, mode, mtime, ctime, and expected digest match the remembered entry. Writes through an existing writable shared memory mapping can change the contents without changing those metadata fields. Waiting for ctime to settle does not prevent subsequent writes through that mapping.

I reproduced this on macOS using the PR's actual FileStamp and validate_host_supervisor_once implementation, with a byte-comparison validator substituted for the SHA-256 validator:

  1. Create an executable file containing known bytes and open a writable shared mapping.
  2. Touch and flush the mapping, then wait more than one second.
  3. Validate the file, populating the cache.
  4. Change a byte through the still-open mapping.
  5. Validate again. The cache returns success without calling the validator, although reading the file confirms that its contents changed.
Before mutation: accepted=true validations=1
After mutation:  accepted=true validations=1
Content actually changed: True

Before this PR, the next validation hashes the changed contents and rejects them. With the cache, modified contents can continue to pass validation. This violates #6054's requirement that any rewrite triggers validation.

Exploitation requires host-side write access to the cached supervisor; this reproduction does not establish a sandbox escape or demonstrate execution of a modified binary. Linux behavior has not been tested here.

Please retain per-launch hashing until immutability of the executable being validated and launched can be guaranteed for the lifetime of the cached result. The overlay batching and build-time guest identity changes can proceed independently. Add a regression test covering a write through an already-open shared mapping.

This reverts commit 246766c.

The cache skipped the SHA-256 of the cached host supervisor when the
file's path, device, inode, size, mode, mtime, ctime and the expected
digest matched the last file found good. That metadata cannot vouch for
the contents: a write through an already-open writable shared mapping
changes the bytes without changing those fields (review on ai-openshell#6056
reproduced this on macOS; on Linux mtime and ctime move only on the
first write fault after writeback, not on every write), so modified
contents could keep passing validation.

validate_host_supervisor therefore hashes the file on every launch
again, as before. The per-launch check stays until the executable that
is validated is also the one launched and cannot change afterwards, for
example a sealed copy (a memfd with F_SEAL_WRITE).

Refs ai-openshell#6054

Signed-off-by: Balaji Ganesan <bganesan@nvidia.com>
balajinvda added a commit to balajinvda/OpenShell that referenced this pull request Nov 30, 2026
Every bootstrap root disk's image cache key includes
sandbox_guest_runtime_identity(), a SHA-256 over the embedded sandbox,
vm-init and umoci blobs and the guest init script. It was computed on
every call, and every launch resolves its root disk in a fresh image
preparation worker process, so every launch hashed the embedded blobs
again, even on a cache hit. On an x86-64 host without SHA instructions
most of the 90 ms between the worker's "ensuring cached root disk image"
and its cache hit is this hash (inferred from the driver log gap, not
profiled).

build.rs now hashes exactly the files the crate embeds (from OUT_DIR,
plus the init script, which build.rs now watches) and the function
returns that constant. The value is unchanged, so cached root disks stay
valid.

Test: guest_runtime_identity_built_once_matches_the_embedded_bytes holds
the build-time value equal to the hash of the embedded bytes computed at
run time, as before.

Cherry-picked from ai-openshell#6056 (ea6c298) so
that this PR stands alone: cached_digest_pinned_image_plan calls
bootstrap_image_cache_identity on the async runtime, which must not hash
the embedded blobs there.

Refs ai-openshell#6054

Signed-off-by: Balaji Ganesan <bganesan@nvidia.com>
@balajinvda balajinvda changed the title perf(vm): stop repeating per-launch overlay writes and hashes before the VM starts perf(vm): stop repeating per-launch overlay writes and guest identity hashing before the VM starts Nov 30, 2026
@balajinvda

Copy link
Copy Markdown
Contributor Author

Thanks, agreed: metadata can't vouch for contents that a live shared mapping can still change, and Linux has the same gap (mtime/ctime update on the first write fault after writeback, not on every write). I've reverted the supervisor-cache commit, so this PR now carries only the overlay batching and the build-time guest identity. If we revisit caching, it would validate and launch from a sealed copy (a memfd with F_SEAL_WRITE), with your mapped-write case as a regression test.

@balajinvda

Copy link
Copy Markdown
Contributor Author

@TaylorMutch both points are addressed (supervisor cache reverted; this PR is items 1 and 2 only). I moved the supervisor hash to its own issue, #6131, so this PR now closes #6054. Could you take another look, and approve the pending Branch Checks / Helm Lint runs?

mr-miles added a commit to mr-miles/openshell-firecracker that referenced this pull request Dec 1, 2026
Move the OpenShell pin from the fork's `patched` branch at bb42c30
(f16795b plus the fork's stacks) to `patched` at 4a1528b1, the same
stacks rebuilt on the v0.1.11 release: the L7 percent-encoding and S3
signing fix, ai-openshell/openshell#6056 and #6057, and the shared
supervisor host over Unix sockets.

Three-way merge the upstream driver changes since f16795b:
- #5117: driver_config is decoded with proto_struct::from_struct, which
  keeps protobuf integers as integers.
- #6023: the counting test allocator moved to openshell-supervisor-network,
  so allocation_tracking and its feature are gone.

Not taken: #5914's launched mode (`serve` and `preflight`, the table on
stdin and the gateway's Configure call; src/serve.rs, settings.rs and
listener.rs). This driver runs as an external service the gateway reaches
by socket_path, which v0.1.11 still supports, and keeps its flag and
environment mode, which upstream also keeps. Its Podman socket detection
move does not apply: the fork has its own local-image sources.

Re-lock: v0.1.11 requires libc 0.2.191, smallvec and kube 5.2.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mr-miles added a commit to mr-miles/openshell-firecracker that referenced this pull request Dec 1, 2026
Pin the vendored driver's OpenShell git dependencies and the embedded
runtime to the `patched` branch of mr-miles/OpenShell at 4a1528b1, so
the Release workflow builds them from one commit. That branch is the
v0.1.11 release plus stacks not yet upstream:
- the L7 fix: percent-encoded path bytes are forwarded as sent, and S3
  paths are signed single-encoded;
- the shared supervisor host over Unix sockets, which the driver's
  OPENSHELL_VM_SHARED_SUPERVISOR needs;
- ai-openshell/openshell#6056 and #6057 (driver-only; the vendored
  driver carries its own merge of them).

Three-way merge the upstream driver changes since f16795b:
- #5117: driver_config is decoded with proto_struct::from_struct, which
  keeps protobuf integers as integers.
- #6023: the counting test allocator moved to openshell-supervisor-network,
  so allocation_tracking and its feature are gone.

Not taken: #5914's launched mode (`serve` and `preflight`, the table on
stdin and the gateway's Configure call). This driver runs as an external
service the gateway reaches by socket_path, which v0.1.11 still supports.

Re-lock: v0.1.11 requires libc 0.2.191, smallvec and kube 5.2.0.

Not for merging.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mr-miles added a commit to mr-miles/openshell-firecracker that referenced this pull request Dec 1, 2026
Three-way merge the driver half of #6056/#6057, which cut per-launch VM
bookkeeping and are not yet upstream, into the vendored crate:
- write a launch's overlay files in one debugfs run (RootfsImageBatch);
- hash the guest runtime identity at build time (build.rs);
- re-hash the cached host supervisor only when the file changes;
- resolve a cached digest-pinned bootstrap image without a preparation
  worker, which here also spares the launcher's exec of the driver.

Adapted to the fork: no mount manifest is written (bind mounts are
refused and the guest init reads none), and the worker is skipped only
for a name with a registry host. Those never consult a local engine
here, so the fast path probes none. write_rootfs_image_file is dropped:
its only upstream caller is the libkrun runtime, which is not vendored.

Kept off main: it arrives with a pin once the PRs land upstream.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rker

Every launch starts an image preparation worker (an exec of the driver
binary, which then probes for a local container engine, plus the
parent's 10 ms exit poll) to resolve its root disk. When the workload
image is the bootstrap image, pinned by digest, its root disk is cached
and no local container engine answers, the worker writes nothing: it
finds no engine and answers from the cache without a registry call
(since ai-openshell#4582).

The driver now answers that case in its own process, with the same logs
and the same Pulling, CacheHit and Pulled events under the same
vm.resolve_bootstrap_image span. Everything else keeps the worker: a
rootfs tar, another workload image, a tag, a locally built image, a
reachable container engine, a cache miss, a registry credential error,
a missing bootstrap image, a sandbox that is gone or already preparing.
Those checks run before any log or event, so the worker then prepares,
reports and fails exactly as before. The overlay preparation keeps its
worker.

The engine probe is passed in (LocalEngineProbe) so the tests can run on
a host where Docker answers.

Test: cached_digest_pinned_image_plan_skips_the_worker_only_on_a_digest_cache_hit
runs eight cases through a recording worker and checks which ones the
worker answered, the plan and the events; it fails when the fast path is
disabled. cached_digest_pinned_image_plan_keeps_the_worker_path_errors
checks a missing and a deleting sandbox still fail as Cancelled with a
cached root disk present.

Closes ai-openshell#6055

Signed-off-by: Balaji Ganesan <bganesan@nvidia.com>
@balajinvda balajinvda changed the title perf(vm): stop repeating per-launch overlay writes and guest identity hashing before the VM starts perf(vm): stop repeating per-launch bookkeeping before the VM starts Dec 2, 2026
@balajinvda

Copy link
Copy Markdown
Contributor Author

@TaylorMutch I folded #6057 into this PR as one more commit (286504a), so you review the shared build-time identity commit once. It now closes #6054 and #6055; the supervisor hash stays out (#6131). fmt, clippy -D warnings and the crate's 306 lib tests pass on the combined head. Could you take another look, and approve the pending Branch Checks / Helm Lint runs?

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

None yet

Projects

None yet

2 participants