Skip to content

Harden vGPU release ordering in the instance lifecycle - #322

Open
yummybomb wants to merge 15 commits into
hypeship/vgpu-framework-abstractionfrom
hypeship/generalize-vgpu-device
Open

Harden vGPU release ordering in the instance lifecycle#322
yummybomb wants to merge 15 commits into
hypeship/vgpu-framework-abstractionfrom
hypeship/generalize-vgpu-device

Conversation

@yummybomb

@yummybomb yummybomb commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Summary

Layer 2 of the vendor VFIO vGPU stack (#366this#363#364#321). Where #366 is a behavior-preserving refactor, this layer changes lifecycle semantics — release ordering and retention — still on mdev-era machinery, before the vendor VFIO backend lands above:

  • Delete releases the vGPU before other teardown — a failed release retains instance metadata while nothing destructive has happened to its attachments yet, so a retried delete stays safe. A successful release persists the cleared assignment before later teardown, and stopped instances retry a previously failed release.
  • Stop keeps its no-op contract — a failed retained-release on stop logs and retains the assignment metadata instead of failing the stop; assignments are passed to DestroyVGPU as a struct.
  • Forks don't inherit retained assignments — a retained (failed-release) vGPU assignment is stripped from fork metadata so a fork can't release its parent's device.
  • Start persists a stale release immediately — releasing a stale assignment before start writes the cleared metadata at once, so a later start failure can't leave metadata pointing at a device that's already gone.

Testing

  • go build ./..., go vet clean
  • go test -race targeted lib/instances lifecycle suites pass

Note

Medium Risk
Changes delete/stop/start ordering and failure semantics for GPU host resources; incorrect ordering could leak vGPU slots or leave instances in a partially deleted state, but behavior is heavily tested and failures retain metadata for safe retries.

Overview
Hardens vGPU lifecycle semantics ahead of vendor VFIO work: release ordering, metadata retention when teardown fails, and fork/snapshot behavior so host GPU state cannot be double-freed or resurrected from stale snapshots.

Delete now blocks restart policy before teardown, releases vGPU before network/device/volume cleanup, fails the delete (keeping metadata) if release fails, and persists cleared assignment fields after a successful release. Stop no longer clears assignment metadata on a failed release—it logs and retains fields for retry; stop on an already-stopped instance retries a retained release via releaseRetainedVGPULocked. Start releases any stale retained assignment and saves metadata immediately before booting a new mdev.

Forks (forkInstance, forkSnapshot) strip live vGPU assignment from fork metadata (profile only); restore snapshot keeps the instance’s current assignment instead of the snapshot’s embedded copy. devices.DestroyVGPU takes a VGPUAssignment struct (framework, path, UUID) instead of separate parameters.

Lifecycle tests cover delete ordering, restart blocking on failed delete, fork/snapshot vGPU clearing, and stale-release persistence on start/stop.

Reviewed by Cursor Bugbot for commit cb55f3a. Bugbot is set up for automated code reviews on this repo. Configure here.

@yummybomb

Copy link
Copy Markdown
Contributor Author

Trimmed over-engineering after an architectural review against the parent task (host kernel 6.8 / Ubuntu 24.04 support):

  • 1c1f52e — dropped the HasMdev/IsAllocated() compatibility shim. VirtualFunction is internal and never serialized in an API response or persisted, so a single Allocated field is enough.

Kept as-is:

  • the delete/stop behavior change (fail and retain assignment metadata when release fails) — orphan reconciliation only runs at startup, so failing the delete is the correct fail-closed behavior;
  • the darwin DestroyVGPU unknown-framework error — it keeps the fail-closed rollback semantics identical on macOS, which the lib/instances tests exercise in CI.

Tests: go test -race ./lib/devices, ./lib/resources, ./lib/hypervisor/qemu, ./lib/hypervisor/cloudhypervisor, and the vGPU-targeted ./lib/instances tests all pass; the remaining lib/instances E2E failures require KVM/images and fail identically on an unmodified checkout.

@yummybomb
yummybomb marked this pull request as ready for review August 5, 2026 19:47
@yummybomb
yummybomb force-pushed the hypeship/generalize-vgpu-device branch from 8abb859 to d489510 Compare August 5, 2026 19:47
Comment thread lib/instances/stop.go Outdated
@yummybomb
yummybomb force-pushed the hypeship/generalize-vgpu-device branch from d489510 to 186cbf3 Compare August 5, 2026 20:24
Comment thread lib/instances/start.go
@yummybomb
yummybomb changed the base branch from main to hypeship/vgpu-framework-abstraction August 6, 2026 19:16
@yummybomb yummybomb changed the title Generalize vGPU device lifecycle Harden vGPU release ordering in the instance lifecycle Aug 6, 2026
@yummybomb
yummybomb force-pushed the hypeship/generalize-vgpu-device branch from e458102 to 7e121c6 Compare August 6, 2026 19:26
@yummybomb
yummybomb force-pushed the hypeship/generalize-vgpu-device branch 2 times, most recently from 7e10cf7 to cf815e2 Compare August 7, 2026 14:02
Comment thread lib/instances/snapshot.go
@yummybomb
yummybomb force-pushed the hypeship/generalize-vgpu-device branch from 8cf6df0 to ffbcfbb Compare August 7, 2026 20:52
Comment thread lib/instances/delete.go
Comment thread lib/instances/delete.go
@yummybomb
yummybomb force-pushed the hypeship/generalize-vgpu-device branch from 3ec8640 to e43b46f Compare August 10, 2026 18:58
…led release

Start now releases any stored assignment before acquiring a new one and
fails the start if that release fails. Stop and delete retain assignment
metadata when release fails instead of clearing it, so a failed release
can be retried later instead of leaking the device.
Fork cloned the source's StoredMetadata wholesale, so an assignment
retained by a failed release during stop was shared with the fork and
either instance's later release could invalidate the other's. Clear the
assignment fields on the fork while keeping GPUProfile; the fork
acquires its own vGPU on start.
Start released a retained assignment but only saved metadata on the
success path, so a failure later in start left on-disk metadata pointing
at a device that was already released. Save right after the release,
matching the retained-release retry on stop.
Restoring a snapshot rehydrated the vGPU assignment fields embedded in
the snapshot metadata. A snapshot taken while an assignment was retained
after a failed release could resurrect that claim after the release
later succeeded, pointing the instance at a device that is gone or
reused. Keep the instance's current assignment instead: device
assignments are host state, not snapshot payload, and a claim retained
at restore time must survive for the next release retry.
Comment thread lib/instances/snapshot.go
@yummybomb
yummybomb force-pushed the hypeship/generalize-vgpu-device branch from e43b46f to a4e00be Compare August 10, 2026 19:08
Comment thread lib/instances/delete.go

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 7b1f995. Configure here.

Comment thread lib/instances/delete.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant