Skip to content

Integrate vendor VFIO vGPUs into the instance lifecycle - #321

Open
yummybomb wants to merge 76 commits into
hypeship/vendor-vfio-backendfrom
hypeship/vendor-vfio-vgpu
Open

Integrate vendor VFIO vGPUs into the instance lifecycle#321
yummybomb wants to merge 76 commits into
hypeship/vendor-vfio-backendfrom
hypeship/vendor-vfio-vgpu

Conversation

@yummybomb

@yummybomb yummybomb commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Summary

Top layer of the vendor VFIO vGPU stack (generalize-vgpu-device #322hypervisor-liveness #363vendor-vfio-backend #364this). The backend itself is in #364; this layer wires it into the instance lifecycle and adds the release guards that make reusable-VF assignments safe:

  • Live-instance claim guard on release — an assignment goes stale when its release succeeds but the metadata save does not (or start fails between the release and its first save). Since vendor VFIO reuses the same VF path across assignments, honoring a stale claim could clear a VF out from under the live instance that owns it now. Every release consults live instance metadata (liveness verified via Harden hypervisor process liveness checks #363's socket-ownership check) and drops stale metadata without touching the device.
  • Assignment durability — assignments are tagged with the owning instance ID, persisted before booting a started instance without carrying over the previous VMM identity, and retained in metadata when rollback release fails in create/start so later release paths can still find the device. Retention persistence is a single attempt: when it fails, the response reports the assignment as unretained and the periodic reconciler releases the VF once the grace period and open-VFIO-handle checks prove it free.
  • Fail-closed startup reconciliation — the protected set of live VFs is built from ListInstancesForReconcile, which fails on any unreadable metadata instead of silently skipping it. Recent assignments receive bounded startup protection when their PID is absent or stale; when the inventory is unavailable, vendor VFIO reconciliation is skipped entirely while mdev reconciliation still runs.
  • Hypervisor scope — hypervisor selection remains a caller policy, preserving the framework layer's existing mdev behavior. The production rollout continues to place vGPU instances on QEMU; the Cloud Hypervisor vendor-VFIO limitation (upstream cloud-hypervisor#7572) is documented in lib/devices/GPU.md (in the base PR Add the vendor VFIO vGPU device backend #364).
  • Failure reporting — a failed create or start whose vGPU release also failed surfaces as vgpu_cleanup_pending with retained/unretained guidance; the retention record keeps identity fields (name, image, hypervisor) so it lists as a recognizable, deletable instance. The instance API exposes gpu.device_path, the assignment identity on vendor VFIO hosts where mdev_uuid is empty.

Testing

  • go build ./..., go vet ./... clean
  • go test -race passes for lib/instances targeted suites, lib/devices, lib/resources, lib/builds (TestCreateInstanceWithNetwork needs image pulls + iptables and fails in this environment on the unmodified base as well)

Note

High Risk
Changes core GPU assignment, release, and hypervisor teardown paths on production hosts; incorrect reconciliation or release ordering could leak VFs or disrupt live VMs.

Overview
Vendor VFIO vGPUs are wired into create/start/stop/delete: assignments are persisted with GPUAssignedAt, released through the instance manager, and protected on release by scanning live instance metadata so a stale claim cannot clear a VF owned by another instance.

When create or start rollback cannot release a vGPU, the manager retains a delete-only metadata stub (GPURetainedForCleanup); start, fork, snapshot, and restore refuse that record until delete succeeds. API create/start map VGPUCleanupPendingError to 500 vgpu_cleanup_pending with retained vs unretained guidance; instance responses can include gpu.device_path.

Startup mdev-only reconcile is replaced by StartVGPUReconciler: an initial pass plus minute periodic cleanup that releases stale metadata claims, then sweeps orphaned mdev/vendor assignments (5-minute grace, live VFIO/socket protection, mdevs claimed in metadata). Listing metadata for reconcile fails closed—unreadable inventory skips the device sweep.

QEMU/hypervisor kill path for vGPU and passthrough devices SIGTERMs first (VFIOTermGrace) before SIGKILL to reduce VF wedge risk; docs and tests reflect that behavior. Metrics cover reconcile failures, stale releases, and retained assignments.

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

@yummybomb
yummybomb force-pushed the hypeship/vendor-vfio-vgpu branch from 199b1d1 to a93d009 Compare July 28, 2026 21:32
@yummybomb
yummybomb changed the base branch from main to hypeship/generalize-vgpu-device July 28, 2026 21:32
@yummybomb
yummybomb force-pushed the hypeship/vendor-vfio-vgpu branch 4 times, most recently from d893fce to de4742a Compare July 29, 2026 15:15
@yummybomb
yummybomb force-pushed the hypeship/vendor-vfio-vgpu branch from de4742a to 18e047f Compare July 29, 2026 16:09
@yummybomb

yummybomb commented Aug 4, 2026

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). Most of the removed weight came from earlier review-round additions rather than the original design:

@yummybomb
yummybomb marked this pull request as ready for review August 5, 2026 19:47
@yummybomb
yummybomb force-pushed the hypeship/vendor-vfio-vgpu branch from 7ef1f8d to bf21162 Compare August 5, 2026 19:57
Comment thread lib/instances/start.go Outdated
Comment thread integration/vgpu_test.go
@yummybomb
yummybomb force-pushed the hypeship/vendor-vfio-vgpu branch from bf21162 to 83c53ea Compare August 5, 2026 20:24
@yummybomb
yummybomb force-pushed the hypeship/vendor-vfio-vgpu branch from 83c53ea to 79eef07 Compare August 5, 2026 20:39
Comment thread cmd/api/main.go Outdated
Comment thread lib/devices/vendor_vfio_linux.go Outdated
Comment thread lib/instances/query.go Outdated
Comment thread lib/instances/create.go Outdated
@yummybomb
yummybomb force-pushed the hypeship/vendor-vfio-vgpu branch from ab2b0e8 to 19b9602 Compare August 6, 2026 17:08
Comment thread lib/instances/start.go
@yummybomb
yummybomb force-pushed the hypeship/vendor-vfio-vgpu branch from d92201d to f177335 Compare August 6, 2026 19:06
@yummybomb
yummybomb changed the base branch from hypeship/generalize-vgpu-device to hypeship/vendor-vfio-backend August 6, 2026 19:08
@yummybomb yummybomb changed the title Support vendor VFIO vGPU devices Integrate vendor VFIO vGPUs into the instance lifecycle Aug 6, 2026
@yummybomb
yummybomb force-pushed the hypeship/vendor-vfio-vgpu branch from f177335 to 1d875f2 Compare August 6, 2026 19:26
@yummybomb
yummybomb force-pushed the hypeship/vendor-vfio-vgpu branch from 1d875f2 to 23fb2f5 Compare August 6, 2026 19:40
A concurrent instance deletion between the claim scan's metadata listing
and load turned ErrNotFound into a host-wide fail-closed release error,
even though a vanished record cannot be a live claimant; skip it.

Stop on a delete-only retention stub released its VF while leaving
GPURetainedForCleanup set, so the stub's start/fork/snapshot errors kept
claiming an assignment that no longer existed. Retention stubs now
release only through delete, as documented.
The two rollback paths carried near-identical retention-stub literals
that would drift as fields are added. Build both from one helper, use
nowUTC like the rest of the file, and drop the starter guard that is
dead since create fails on a nil starter long before the vGPU block.
Replace the listMetadataFilesWithStatErrors(bool) mode flag with
listMetadataFiles / listMetadataFilesStrict so call sites say which
failure semantics they rely on.
ListInstancesForReconcile failed hard when an instance was deleted
between the metadata listing and its load. The startup call runs before
the API serves, but the grace-period retry fires while deletes are in
flight; one racing delete errored the whole list, which zeroed the
retry and left vendor VFIO reconciliation disabled until the next
restart. Skip ErrNotFound like the release claim scan does: a vanished
record cannot claim a VF.
Giving up on an orphaned release leaves the VF allocated while
/resources still advertises it, until startup reconciliation or manual
remediation. That was visible only as a log line; count it so capacity
leaks can alert.
Start, fork, and snapshot each carried a verbatim copy of the rejection
error and its rationale; the stub fill in create's cleanup closure
duplicated retainedVGPUFromCreateError field-for-field. One error value
and one device-to-stub helper replace the copies.
The create and start handlers carried near-identical 20-line blocks
deriving the vgpu_cleanup_pending message and inner error detail,
differing only in the verb and release guidance.
- Retry the vendor VFIO reconcile sweep with a bounded delay when the
  startup instance listing fails, instead of disabling orphan recovery
  until the next process restart. One pending retry at a time.
- Schedule the in-process orphaned-release retry when a rollback's
  retention record cannot be saved (create and start), instead of
  leaking the VF until restart. The retry scans claims without a
  self-exclusion because a restarted instance may hold the same VF.
- Reject snapshot restore into a vGPU retention stub, matching start,
  fork, and snapshot.
- Give passthrough PCI instances the same SIGTERM grace as vGPU
  instances on stop/delete, matching the QEMU-side vfioTermGraceFor.
- Render the vgpu_cleanup_pending API detail from the error itself
  instead of duplicating its prose; use the manager clock in the claim
  scan; collapse the create-rollback retention branch.
- Move ReconcileVGPUs off the Manager interface to a startup type
  assertion and unexport listInstancesForReconcile and
  hypervisorMayBeAlive.
Record the owning instance and assignment time for each vendor VFIO VF so
a reconcile sweep can run while instances are being created: recently
assigned VFs get a grace period before they are eligible (mirroring
orphanedMdevGracePeriod), and owned VFs are destroyed with their recorded
owner ID instead of failing the ownership check.
Run the fail-closed vGPU reconcile once at startup and every minute after,
skipping hosts without a vGPU framework. Each pass retries releases for
assignments whose owner is no longer live (re-verified under the instance
lock) and then sweeps device-level leftovers with no live metadata claim.

This deletes the per-path orphan retry goroutines - whose path-keyed
dedup could drop cleanup for a newer assignment reusing the same VF -
the CAS/timer retry in ReconcileVGPUs, the stopped-instance release
special case in StopInstance, the retention fallbacks that scheduled
background retries, and the orphan-abandoned metric.
An assignment could lose its persisted hypervisor PID while its VM stays
alive - a post-boot metadata save failure or a hypeman crash before the
save. The liveness checks gated the socket-ownership scan on a non-nil
PID, so once the startup grace expired the reconciler considered such an
assignment stale and could remove an mdev out from under the live VM
(DestroyMdev has no in-use guard).

Run the socket-ownership scan unconditionally: a live VMM always holds
its control-socket listener, and a missing listener still resolves to
not-alive, so genuinely stopped instances are released as before. The
claim scan in releaseStoredVGPUExcluding gets the same treatment.
The five-minute fresh-assignment protection existed three times: the
instances startup grace, the mdev orphan grace, and the vendor VFIO
sweep grace. Define it once in lib/devices and alias the instances
constant to it.
A wedged or leaked VF presents as reduced GPU capacity while /resources
still reports full capacity, and reconcile, stale-release, and retention
failures were visible only as log lines. Count failed reconcile stages,
failed stale releases, and retained assignments (by operation and
whether the retention record persisted) so sustained failure can alert.
When rollback release fails and the subsequent retention save also fails,
the assignment save from earlier in start may still hold the claim on
disk. Check for that surviving claim before reporting the retention as
unpersisted, so the API does not emit vgpu_unretained_instance for an
assignment that is still durably attributed.
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.

2 participants