Skip to content

virtio: Notify guests of runtime config changes - #5150

Open
Anatol Belski (weltling) wants to merge 4 commits into
microsoft:mainfrom
weltling:virtio-config-generation
Open

Anatol Belski (weltling) wants to merge 4 commits into
microsoft:mainfrom
weltling:virtio-config-generation

Conversation

@weltling

Copy link
Copy Markdown
Member

The VirtIO config generation counter previously advanced only during feature negotiation. This change lets devices notify guests when their config changes at runtime.

  • Use an 8-bit counter
  • Deliver config change interrupts over PCI and MMIO
  • Notify guests when virtio-blk capacity or virtio-net link status changes

@weltling
Anatol Belski (weltling) requested a review from a team as a code owner November 18, 2026 22:54
Copilot AI balanced review requested due to automatic review settings November 18, 2026 22:54
@github-actions github-actions Bot added the unsafe Related to unsafe code label Nov 18, 2026
@github-actions

Copy link
Copy Markdown

⚠️ Unsafe Code Detected

This PR modifies files containing unsafe Rust code. Extra scrutiny is required during review.

For more on why we check whole files, instead of just diffs, check out the Rustonomicon

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

The tests can miss regressions because they do not verify MSI-X interrupt delivery, and the resize assertion can be satisfied by the worker’s initial capacity snapshot rather than runtime resize handling.

2 open issues
What changed in this PR

This PR lets VirtIO devices notify guests of runtime configuration changes through the shared transport layer, including block capacity and network link-status changes.

Changes:

  • Introduces a shared 8-bit generation counter with pending-change notifications.
  • Connects runtime updates to PCI and MMIO interrupts and prompts guests to reread configuration after restore.
  • Adds transport, disk-resize, and network link-status tests.
File Description
vm/​devices/​virtio/​virtio/​src/​transport/​saved_state.rs Stores the generation counter as u8.
vm/​devices/​virtio/​virtio/​src/​transport/​pci.rs Reads the shared generation counter for PCI.
vm/​devices/​virtio/​virtio/​src/​transport/​mmio.rs Reads the shared generation counter for MMIO.
vm/​devices/​virtio/​virtio/​src/​transport/​core.rs Handles pending notifications, reset, restore, and teardown.
vm/​devices/​virtio/​virtio/​src/​tests.rs Tests runtime updates, counter wrapping, and restore notifications.
vm/​devices/​virtio/​virtio/​src/​lib.rs Exports ConfigGeneration.
vm/​devices/​virtio/​virtio/​src/​device.rs Adds the optional configuration-generation handle to device traits.
vm/​devices/​virtio/​virtio/​src/​config.rs Implements shared generation tracking and notification state.
vm/​devices/​virtio/​virtio_net/​src/​tests.rs Tests link-status updates and adapts register access.
vm/​devices/​virtio/​virtio_net/​src/​lib.rs Publishes endpoint link-status changes.
vm/​devices/​virtio/​virtio_net/​Cargo.toml Makes parking_lot a runtime dependency.
vm/​devices/​virtio/​virtio_blk/​src/​lib.rs Watches disk resizing and publishes updated capacity.
vm/​devices/​virtio/​virtio_blk/​src/​integration_tests.rs Adds a guest-visible capacity update test.
vm/​devices/​virtio/​virtio_blk/​Cargo.toml Adds synchronization and resize-test dependencies.
Cargo.lock Records updated block-device dependencies.

Review effort: Balanced

Comment thread vm/devices/virtio/virtio/src/tests.rs Resolved
Copilot AI balanced review requested due to automatic review settings November 18, 2026 23:03

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 remain for the VirtIO runtime configuration-change notification updates.

2 open issues

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 18, 2026 23: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.

🟢 Approval recommended

No active review issues block the VirtIO runtime configuration-change notification changes.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

Comment thread vm/devices/virtio/virtio/src/config.rs Outdated
Comment on lines +17 to +19
pending: bool,
#[inspect(skip)]
waker: Option<Waker>,

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.

Could we reuse vmcore::slim_event::SlimEvent for the notification side here? The pending flag and waker handling substantially duplicate it. The generation/update synchronization belongs in this wrapper, but it seems worth separating that from the existing event primitive.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Switched to the SlimEvent API. The config update and the generation bump are still under the mutex.

Thanks

Comment thread vm/devices/virtio/virtio/src/config.rs Outdated
Comment on lines +57 to +68
pub(crate) fn poll_change(&self, waker: &Waker, ready: bool) -> bool {
let mut state = self.0.lock();
match &mut state.waker {
Some(registered) => registered.clone_from(waker),
None => state.waker = Some(waker.clone()),
}
ready && std::mem::take(&mut state.pending)
}

pub(crate) fn clear_waker(&self) {
self.0.lock().waker = None;
}

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.

The following comment goes away if we adopt SlimEvent, but registere.clone_from(waker) can drop the previous waker while holding the generation mutex. clear_waker() also drops it under that mutex. We should move the old waker out and drop it after releasing the lock to prevent possible deadlock.

SlimEvent's poll_wait defers destruction after unlocking.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Rewrote as suggested, so the event is signalled outside the generation mutex. The old event is also dropped after unlocking.

Thanks

Copilot AI balanced review requested due to automatic review settings November 19, 2026 19:53

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 remain for the VirtIO runtime configuration-change support.

No open issues 🎉

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 20, 2026 22:03

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

A critical lock-order inversion between the config and generation mutexes can deadlock inspection when one queue is running and another is inactive.

1 open issue

Review effort: Balanced

Comment thread vm/devices/virtio/virtio_blk/src/lib.rs Resolved
Keep the generation in the width of the PCI common config field. Widen
it only at the register boundary so it cannot overlap the queue
selector.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
Copilot AI balanced review requested due to automatic review settings November 21, 2026 12:13

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

The resize and link-status tests verify register updates but lack transport-backed checks that generation advances and config-change interrupts reach the guest with DRIVER_OK set.

2 open issues
1 resolved since last review

Review effort: Balanced

Comment thread vm/devices/virtio/virtio_blk/src/lib.rs Resolved
Comment thread vm/devices/virtio/virtio_net/src/lib.rs Resolved
Let devices advance their config generation when publishing new values.
Deliver pending notifications through both transports and refresh guest
config after restore.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
Watch backend resize events and publish the new capacity with a config
generation update. Exercise a RAM disk resize in the block device test.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
Copilot AI balanced review requested due to automatic review settings November 21, 2026 13:54

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

The capacity calculation can overflow after a runtime disk resize, causing a panic or reporting zero capacity unless unrepresentable sizes are explicitly handled.

1 open issue
2 resolved since last review

Review effort: Balanced

Comment thread vm/devices/virtio/virtio_blk/src/lib.rs Resolved
Publish endpoint link status changes through the shared config
generation handle.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

unsafe Related to unsafe code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants