Skip to content

virt: add irqfd trait and implement for mshv and KVM#3194

Open
will-j-wright wants to merge 1 commit intomicrosoft:mainfrom
will-j-wright:user/wwright/mshv-irqfd
Open

virt: add irqfd trait and implement for mshv and KVM#3194
will-j-wright wants to merge 1 commit intomicrosoft:mainfrom
will-j-wright:user/wwright/mshv-irqfd

Conversation

@will-j-wright
Copy link
Copy Markdown
Contributor

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

Tested: End-to-end with VFIO NVMe passthrough on mshv (L1 Azure Linux VM → L2 guest). NVMe driver probes, reads 100MB at 425MB/s with MSI-X interrupts delivered via irqfd.

@will-j-wright will-j-wright requested a review from a team as a code owner April 4, 2026 06:38
Copilot AI review requested due to automatic review settings April 4, 2026 06:38
@github-actions github-actions bot added the unsafe Related to unsafe code label Apr 4, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 4, 2026

⚠️ 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

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces a cross-backend irqfd abstraction in virt and wires up backend implementations for mshv and KVM so VFIO-style MSI/MSI-X interrupts can be injected by the kernel on eventfd signal (avoiding userspace transitions).

Changes:

  • Added virt::irqfd traits and a Partition::irqfd() entry point to access irqfd routing when supported.
  • Implemented mshv irqfd + MSI routing support (GSI allocation + MSHV_IRQFD / MSHV_SET_MSI_ROUTING ioctls), including shared VmFd ownership via Arc.
  • Implemented KVM irqfd support by wrapping existing GsiRoute infrastructure and exposing it through the new traits.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
vmm_core/virt/src/lib.rs Exposes the new irqfd module from the virt crate.
vmm_core/virt/src/irqfd.rs Defines the new IrqFd / IrqFdRoute traits and access patterns.
vmm_core/virt/src/generic.rs Adds Partition::irqfd() default method returning None for unsupported backends.
vmm_core/virt_mshv/src/lib.rs Wires mshv partition to provide irqfd support; switches VmFd storage to Arc<VmFd>.
vmm_core/virt_mshv/src/irqfd.rs Implements irqfd + MSI routing table management for mshv via ioctls.
vmm_core/virt_kvm/src/gsi.rs Adds KvmIrqFdState/KvmIrqFdRoute backed by existing GsiRoute.
vmm_core/virt_kvm/src/arch/x86_64/mod.rs Exposes KVM irqfd support through Partition::irqfd() on x86_64.
Comments suppressed due to low confidence (1)

vmm_core/virt_kvm/src/gsi.rs:152

  • KvmIrqFdRoute::set_msi/clear_msi always return Ok(()), but the underlying GsiRoute::enable/disable paths can panic on KVM ioctl failures (set_gsi_routes(...).expect("should not fail"), kvm.irqfd(...).expect("should not fail")). Since the new IrqFdRoute API is fallible, it would be better to plumb errors through instead of panicking (change enable/disable/update_routes/irqfd calls to return Result and propagate up).
    pub fn enable(&self, entry: kvm::RoutingEntry) {
        let partition = self.set_entry(Some(entry));
        let _lock = self.enable_mutex.lock();
        if !self.enabled.load(Ordering::Relaxed) {
            if let (Some(partition), Some(event)) = (&partition, &self.irqfd_event) {
                partition
                    .kvm
                    .irqfd(self.gsi, event.as_fd().as_raw_fd(), true)
                    .expect("should not fail");

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>
@will-j-wright will-j-wright force-pushed the user/wwright/mshv-irqfd branch from e0cc2a5 to 9ee6044 Compare April 4, 2026 08:00
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.

2 participants