virt: add irqfd trait and implement for mshv and KVM#3206
virt: add irqfd trait and implement for mshv and KVM#3206will-j-wright wants to merge 1 commit intomicrosoft:mainfrom
Conversation
|
This PR modifies files containing For more on why we check whole files, instead of just diffs, check out the Rustonomicon |
There was a problem hiding this comment.
Pull request overview
This PR introduces an irqfd abstraction to the virt layer and wires up backend implementations for mshv and KVM, enabling kernel-assisted MSI injection (needed for VFIO-style interrupt delivery without userspace transitions).
Changes:
- Add new
virt::irqfdmodule definingIrqFd/IrqFdRoutetraits andPartition::irqfd()capability hook. - Implement irqfd + MSI routing for mshv (GSI allocation,
MSHV_IRQFD, atomicMSHV_SET_MSI_ROUTINGupdates). - Implement irqfd support for KVM by wrapping existing
GsiRouteinfrastructure and exposing it viaPartition::irqfd()on x86_64.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| vmm_core/virt/src/lib.rs | Exposes the new irqfd module. |
| vmm_core/virt/src/irqfd.rs | Adds IrqFd / IrqFdRoute traits and semantics for MSI routing + mask/unmask behavior. |
| vmm_core/virt/src/generic.rs | Adds Partition::irqfd() optional capability entrypoint. |
| vmm_core/virt_mshv/src/lib.rs | Stores shared MshvIrqFdState in the partition and exposes it via Partition::irqfd(). |
| vmm_core/virt_mshv/src/irqfd.rs | New mshv irqfd implementation: GSI table + irqfd registration + full routing table push. |
| vmm_core/virt_mshv/Cargo.toml | Adds headervec dependency for variable-length ioctl buffer building. |
| vmm_core/virt_kvm/src/gsi.rs | Adds KvmIrqFdState/KvmIrqFdRoute wrapper implementing the new traits. |
| vmm_core/virt_kvm/src/arch/x86_64/mod.rs | Wires Partition::irqfd() for KVM x86_64 partitions. |
vmm_core/virt_mshv/src/irqfd.rs
Outdated
| *self.last_route.lock() = Some(route); | ||
| self.shared.set_gsi_route(self.gsi, Some(route)) | ||
| } | ||
|
|
||
| fn clear_msi(&self) -> anyhow::Result<()> { | ||
| *self.last_route.lock() = None; | ||
| self.shared.set_gsi_route(self.gsi, None) |
There was a problem hiding this comment.
last_route is updated before/without confirming the kernel routing update succeeded. If set_gsi_route fails (and rolls back the shared in-memory routing state), last_route can become inconsistent with the actual configured route, which can break unmask() semantics and error recovery. Update last_route only after set_gsi_route(...) succeeds (or roll it back on error); similarly, only clear last_route after successfully clearing the route.
| *self.last_route.lock() = Some(route); | |
| self.shared.set_gsi_route(self.gsi, Some(route)) | |
| } | |
| fn clear_msi(&self) -> anyhow::Result<()> { | |
| *self.last_route.lock() = None; | |
| self.shared.set_gsi_route(self.gsi, None) | |
| self.shared.set_gsi_route(self.gsi, Some(route))?; | |
| *self.last_route.lock() = Some(route); | |
| Ok(()) | |
| } | |
| fn clear_msi(&self) -> anyhow::Result<()> { | |
| self.shared.set_gsi_route(self.gsi, None)?; | |
| *self.last_route.lock() = None; | |
| Ok(()) |
vmm_core/virt_mshv/src/irqfd.rs
Outdated
| // queried via is_pending() or consumed via consume_pending(). The | ||
| // last route is preserved so it can be restored on unmask. |
There was a problem hiding this comment.
The comment references is_pending(), but IrqFdRoute does not define such a method (only consume_pending). This is misleading for callers; either remove the is_pending() mention or align it with the actual API (e.g., only refer to consume_pending).
| // queried via is_pending() or consumed via consume_pending(). The | |
| // last route is preserved so it can be restored on unmask. | |
| // consumed via consume_pending(). The last route is preserved so it | |
| // can be restored on unmask. |
Add irqfd support enabling the kernel to inject MSIs directly into the guest when an eventfd is signaled, without a userspace transition. This is required for VFIO device passthrough where physical device interrupts must be delivered to the guest via the hypervisor's irqfd mechanism. New traits in virt crate: - IrqFd: allocates GSIs and registers eventfds as irqfd routes - IrqFdRoute: updates/clears MSI routing (address/data) per GSI - Partition::irqfd(): returns the irqfd interface if supported mshv implementation (virt_mshv/src/irqfd.rs): - GSI allocation table (2048 slots) - MSHV_IRQFD ioctl for register/unregister eventfds - MSHV_SET_MSI_ROUTING ioctl to push full routing table - Automatic cleanup on route drop (unregister + free GSI) - VmFd changed to Arc<VmFd> for shared ownership KVM implementation (virt_kvm/src/gsi.rs): - KvmIrqFdState wraps existing GsiRoute infrastructure - KvmIrqFdRoute delegates to GsiRoute::enable/disable - Partition::irqfd() wired in x86_64 arch module Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add irqfd support enabling the kernel to inject MSIs directly into the guest when an event is signaled, without a userspace transition. This is required for VFIO device passthrough where physical device interrupts must be delivered to the guest via the hypervisor's irqfd mechanism.
New traits
IrqFd— creates irqfd routes (GSI allocation + event registration)IrqFdRoute— manages per-vector MSI routing, masking, and pending state for PBA supportPartition::irqfd()— returns the irqfd interface if the backend supports itThe event is created by the implementation rather than provided by the caller. This accommodates backends like WHP where the hypervisor owns the event handle (
WHvCreateTrigger).mshv implementation
MSHV_IRQFDioctl for register/unregister eventfdsMSHV_SET_MSI_ROUTINGioctl to push full routing table atomicallyKVM implementation
KvmIrqFdStatewraps existingGsiRouteinfrastructureKvmIrqFdRoutedelegates toGsiRoute::enable/disableTesting
End-to-end with VFIO NVMe passthrough on mshv.