Skip to content

openvmm_entry: add VM operation gate to the ttrpc VmService - #4659

Open
Manish Ranjan Mahanta (marma-dev) wants to merge 24 commits into
microsoft:mainfrom
marma-dev:marma/vm-operation-gate
Open

Manish Ranjan Mahanta (marma-dev) wants to merge 24 commits into
microsoft:mainfrom
marma-dev:marma/vm-operation-gate

Conversation

@marma-dev

@marma-dev Manish Ranjan Mahanta (marma-dev) commented Oct 14, 2026 •

Copy link
Copy Markdown

What this does

The ttrpc VmService lets mutating RPCs run in any order. Device and resource changes run as background tasks, so a client (or two clients) can pause, resume, save or tear down the VM while a hot-add is still waiting on its device. This PR adds a small VmOperationGate that admits each mutating RPC only if it cannot conflict with what is already in flight.

Behavior

Operation Class While a device or resource change is in flight
ModifyResource, AddPcieDevice, RemovePcieDevice, AddVpciDevice, RemoveVpciDevice Shared Runs alongside it
PauseVM, ResumeVM, SaveVM Exclusive Rejected immediately with ABORTED
TeardownVM before Save, Quit Terminal Cancel the change first; its caller gets CANCELLED
  • Calls are never queued. A rejected call has no side effects, because the operation's work is built only after the gate is admitted.
  • PauseVM, ResumeVM, SaveVM and TeardownVM run inline in the request loop, so they cannot overlap each other. The gate arbitrates them against background changes.
  • The service holds a SaveVM gate guard rather than leaving it in the request future. If the Save client cancels, the service remains terminal because the controller may still publish the snapshot.
  • A completed SaveVM seals the gate. The VM reports Halted with reason snapshot saved; PropertiesVM, WaitVM, and Quit remain available. Pause, resume, save, shutdown, topology changes, teardown, create and restore return FAILED_PRECONDITION.
  • A cancelled SaveVM is conservatively terminal even before its eventual outcome is known. The client sends Quit; the VMM must not allow process reuse after a possibly committed save.
  • Quit remains available and terminates the process after releasing resources. A failed SaveVM, before publication, does not seal the gate and leaves the VM paused so the caller can resume or retry.
  • A client that cancels or disconnects does not end a background operation early. Only teardown and quit cancel in-flight topology changes.

Behavior changes

  • PauseVM/ResumeVM/SaveVM during an in-flight device or resource change now fail with ABORTED instead of running concurrently.
  • TeardownVM/Quit cancel in-flight changes before the saved VM terminal state. Previously teardown could hang: an in-flight change holds a device RPC channel, which keeps the worker from stopping.
  • After SaveVM starts, the service cannot resume, mutate, tear down, create, or restore a VM in the same process. This closes the reuse path where TeardownVM could clear snapshot_saved and allow a later VM to run with the old snapshot aliasing its memory file.
  • Consomme NIC UPDATE/REMOVE requires a running VM before the request is parsed further, because the backend services these requests only while its queue runs.

Why at the ttrpc layer

Device and resource changes are sent over per-device RPC channels, not the worker's request queue, so ordering has to be decided where requests are admitted. The CLI/REPL issues these sequentially from one caller and is unchanged here. A worker-level mutation fence remains separate Save/Restore work.

Why now

Save/Restore over the API (#4553) is in main. A snapshot must capture a stable device set, and a published save permanently aliases the VM's memory backing file. Operation ordering and post-save process lifetime therefore need one enforceable service contract.

Not in this PR

  • The worker-level mutation fence, also covering the REPL.
  • The ttrpc shutdown reply-flush barrier.
  • Servicing Consomme requests outside queue processing.
  • Capture rollback/staged publication semantics.

Testing

  • Gate tests: exclusive rejection, shared topology operations, drop-safety and permanent sealing.
  • VmService tests: hot-add ordering and cancellation, committed SaveVM terminal state, WaitVM, rejection of resume/pause/teardown/create/restore, cancelled-Save terminal behavior, and worker-stop sealing.
  • cargo test -p openvmm_entry ttrpc:: --lib: 50 passed against current main.
  • cargo clippy -p openvmm_entry --all-targets: passed.
  • cargo doc -p openvmm_entry --no-deps: passed.
  • cargo xtask fmt --fix: passed.

Docs: the gRPC management reference now describes the terminal SaveVM/explicit-Quit lifecycle and gate sealing.

  • Live trpc::test_ttrpc_interface on KVM and MSHV at head 78df413b: passed, including Save -> terminal state -> Quit -> process exit -> cross-process Restore.

  • Latest review fix: pending WaitVM completes on terminal Save; proto/Guide require explicit Quit and process exit.

Copilot AI balanced review requested due to automatic review settings October 14, 2026 09:43
@github-actions github-actions Bot added the Guide label Oct 14, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Exclusive RPCs block request admission, and ungated Quit can hang during active topology work.

3 open findings
What changed in this PR

Adds a concurrency gate to prevent conflicting ttrpc VM mutations.

Changes:

  • Introduces shared and exclusive operation gating.
  • Applies gating to lifecycle, teardown, and topology RPCs.
  • Documents concurrency behavior.
File Description
operation_gate.rs Implements gate state and tests.
mod.rs Integrates gating into VmService.
grpc.md Documents operation concurrency.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread openvmm/openvmm_entry/src/ttrpc/mod.rs Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Consomme port modifications on a paused VM can hold the gate indefinitely, blocking resume and shutdown.

2 open findings
2 resolved since last review

🧠 Review effort: Balanced

Comment thread openvmm/openvmm_entry/src/ttrpc/mod.rs Resolved
@github-actions

Copy link
Copy Markdown

Copilot AI balanced review requested due to automatic review settings October 16, 2026 11:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Shutdown now drops in-flight mutation futures, requiring human confirmation of cancellation safety across supported backends.

0 open findings

2 resolved since last review

🧠 Review effort: Balanced

@github-actions

Copy link
Copy Markdown

Manish Ranjan Mahanta added 4 commits October 22, 2026 19:54
Topology operations (device and resource hot add/remove) share the gate
with each other; pause, resume and teardown are exclusive. A conflicting
operation is rejected immediately with ABORTED. Topology RPCs are admitted
before their request is processed, so a rejected call has no side effects,
and the guard is released when the operation completes, fails, or is
cancelled.
Describe which ttrpc operations are exclusive or shared, the ABORTED response for a conflicting request, and that a rejected request has no side effects.
Split the pause/resume gate kind into separate Pause and Resume kinds so a
conflict names the operation that is actually running.

Make Quit wait for in-flight device and resource changes before dropping
the VM. Such a change can hold a clone of a device RPC channel, and a live
channel keeps the worker from stopping, so Quit could otherwise hang.

Correct the Guide: pause, resume, and teardown run to completion before the
server reads its next request, so requests arriving meanwhile are processed
afterwards rather than rejected; ABORTED applies when one of them arrives
while a device or resource change is still in flight.
A device or resource change can stay in flight indefinitely. For example, a
Consomme port change waits for the NIC's queue processing, which is stopped
while the VM is paused. Such a change held the gate, so resume and teardown
were rejected with ABORTED and Quit, which waited for it, hung.

Teardown and Quit are now terminal: they cancel in-flight gated changes
before proceeding, and the cancelled calls return CANCELLED. Only the
service-side wait is cancelled; a request already delivered to the VM is
completed or discarded when the worker stops. The server's own shutdown path
does the same.

Consomme port changes are also rejected with FAILED_PRECONDITION unless the
VM is running, since they cannot complete otherwise.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Shutdown cancellation across backends warrants human safety review, and existing Consomme test assertions need updating.

1 open issue

Review effort: Balanced

Comment thread openvmm/openvmm_entry/src/ttrpc/mod.rs Resolved
@marma-dev

Copy link
Copy Markdown
Author

Drop safety of gated operations when Teardown/Quit cancels them (re: the "needs a closer look" note)

I checked every operation cancel_gated_tasks can cancel at 0ced5c4: ModifyResource (SCSI add/remove, NIC add, Consomme port bind/unbind), AddPcieDevice, RemovePcieDevice, AddVpciDevice and RemoveVpciDevice.

There are only two kinds of await point:

  1. A mesh RPC reply (worker_rpc / scsi_rpc / consomme_rpc call_failable). The request is already queued when the future is dropped, so dropping only discards the reply. The VM side completes or discards the request as the worker stops. That is exactly what happens on main today when Teardown races an in-flight hot-plug.
  2. openvmm_helpers::disk::open_disk_type (SCSI add, virtio-blk, NVMe). Raw, VHD1 and VMGS files are opened synchronously with std::fs, and Windows vhdmp opens synchronously. The only real await is the Linux VHDX chain, which reads metadata read-only through disklayer_vhdx::io::BlockingFile (blocking::unblock + pread on owned buffers). Dropping it closes the file handles; any in-flight pool read finishes on its own buffer and is discarded.

What this means:

  • No io_uring. Nothing issues io_uring I/O on the service driver, so the IoFuture drop abort can't happen here. The cancel_gated_tasks doc comment records that constraint for future gated operations.
  • No service state to tear. Each future is 'static and captures only clones (RPC senders, Arc registries). There's no &mut self access after admission, so dropping one can't leave the service half-updated. The gate guard is released in the task after cancellation, and the caller gets CANCELLED.
  • Partial effects are moot. A Consomme request with several ports, or a hot-add the worker has already received, can be partly applied when cancelled. Cancellation only happens on Teardown/Quit, which destroy the VM, so that state is discarded with it.
  • Backend-independent. The gated futures run in the service process and never touch the hypervisor. KVM, MSHV and WHP differ only inside the worker, which this change doesn't affect.

Pre-existing and separate: if the worker is mid-hot-add when it stops, it can drop a running device. On Linux block devices, that triggers the io_uring abort on teardown. It reproduces on main without this PR, with or without the gate, and is fixed separately: the worker pauses before teardown, and removed device units stop running devices. That fix is on a branch, with a PR to follow.

@github-actions

Copy link
Copy Markdown

Copilot AI balanced review requested due to automatic review settings October 28, 2026 12:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The new running-VM precondition deterministically breaks existing paused-VM port-validation tests, requiring those cases to move to the running phase and a separate paused-state FAILED_PRECONDITION assertion.

1 open issue

Review effort: Balanced

@github-actions

Copy link
Copy Markdown

Consomme port updates and removals now return FAILED_PRECONDITION unless
the VM is running, since their requests are serviced by the NIC's queue
processing, which is stopped while paused. test_ttrpc_interface sent
malformed port requests while the VM was still paused after CreateVm and
expected parse errors, so it now fails with FAILED_PRECONDITION instead.

Assert FAILED_PRECONDITION for both update and removal while paused, run
the malformed-port cases after ResumeVm, and check that a port change
after PauseVm fails fast and doesn't block the teardown that follows.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The review identifies no active issues with the VM operation gate changes.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@github-actions

Copy link
Copy Markdown

Copilot AI balanced review requested due to automatic review settings November 20, 2026 17:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Terminal save outcomes leave pending WaitVM calls hanging, while save/restore tests still expect a paused state and same-process restoration instead of the new terminal-save behavior.

2 open issues

Review effort: Balanced

Comment thread openvmm/openvmm_entry/src/ttrpc/mod.rs Resolved
# Conflicts:
#	openvmm/openvmm_entry/src/ttrpc/mod.rs
Copilot AI balanced review requested due to automatic review settings November 20, 2026 17:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Integration tests still assume a reusable post-save process and fail under the new terminal lifecycle, while pending WaitVM requests remain blocked after saves that leave the service terminal.

3 open issues

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 22, 2026 06:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Pending WaitVM requests remain unresolved when saving halts the VM, and requiring successful Quit replies makes the save/restore tests flaky.

4 open issues

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 22, 2026 06:31
# Conflicts:
#	openvmm/openvmm_entry/src/ttrpc/mod.rs

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Saving transitions the VM to Halted without completing or clearing a pending WaitVM response, leaving the original caller blocked until Quit and rejecting subsequent waiters.

3 open issues
1 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 22, 2026 06:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Successful SaveVM transitions leave a pending WaitVM unresolved despite entering Halted, violating the API contract and causing subsequent waits to fail.

4 open issues

Review effort: Balanced

Comment thread openvmm/openvmm_entry/src/ttrpc/mod.rs Resolved
Copilot AI balanced review requested due to automatic review settings November 23, 2026 18:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

After a cancelled SaveVM and worker stop, a fresh WaitVM incorrectly reports “VM not created yet” rather than succeeding because the missing-VM check precedes the halted snapshot-saved check.

1 open issue
4 resolved since last review

Review effort: Balanced

Comment thread openvmm/openvmm_entry/src/ttrpc/mod.rs Resolved
Copilot AI balanced review requested due to automatic review settings November 23, 2026 18:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the lifecycle operation-coordination changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants