Repository navigation
perf(vm): stop repeating per-launch bookkeeping before the VM starts - #6056
balajinvda wants to merge 5 commits into
Conversation
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>
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>
|
The supervisor validation cache has an integrity-check bypass that should be addressed before merging the hash-cache change. In I reproduced this on macOS using the PR's actual
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>
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>
|
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. |
|
@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? |
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>
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>
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>
|
@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? |
Summary
Removes three pieces of per-launch bookkeeping the VM driver repeats on every sandbox create with an unchanged result: one
debugfsprocess 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(inrootfs.rs) stages the samemkdir/rm/write/set_inode_fieldcommands in the same order and runs them through the existingrun_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_filekeeps its signature (used byruntime.rs) and becomes a one-file batch;run_debugfsis now test-only. Thevm.prepare_guestspan moves to a newprepare_guest_overlayand so also covers the mount manifest and the write.perf(vm): hash the guest runtime identity at build time:build.rshashes exactly the bytes the crate embeds (fromOUT_DIR, plus the init script, which it now watches) andsandbox_guest_runtime_identity()returns that constant. Same value, so existing cached root disks stay valid. Addssha2as a build dependency (already a dependency;Cargo.lockunchanged).fix(vm): hash the cached host supervisor on every launch again: revertsperf(vm): hash the cached host supervisor once per file version(246766c) after review; the branch's net diff has no supervisor change.create_sandbox received→launcher spawned367 → 41–67 ms; the overlay writes take 13–27 ms of that. The supervisor cache affected only the time afterlauncher 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 onmain, 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_workerfirst askscached_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 diskimage_cache_rootfs_image(bootstrap_image_cache_identity(digest))exists;registry_authsucceeds; 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.vm.resolve_bootstrap_imagespan: theno local container engine availablewarning,ensuring cached root disk image (registry),root disk image cache hit (digest-pinned, no registry call), andpublish_registry_cache_hit(Pulling,CacheHit,Pulled).Nonebefore 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 stillCancelled, from the worker path), so error precedence does not change. Cancellation after the plan is still caught by the existingensure_provisioning_activecheck that follows image resolution. The overlay preparation keeps its worker.LocalEngineProbefunction so the tests can run on hosts where Docker answers (CI runners included); production passesconnect_local_container_engine.create_sandbox received→sandbox root disk plan resolved114 → 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 onmain(659b45f3b), which this branch is based on.Testing
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-featuresrootfs_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) onedebugfs -Rper 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_spansupdated forprepare_guest_overlay.The unit tests ran on Linux x86-64 with e2fsprogs installed; the build used stub embedded runtime artifacts (
OPENSHELL_VM_RUNTIME_COMPRESSED_DIRunset). 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 emitsPulling,CacheHit,Pulled. With the fast path disabled it fails withbootstrap-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 asCancelledand no worker starts.Checklist