Skip to content

vfio_assigned_device: dynamic IOMMU mapping for P2P DMA#3367

Open
jstarks wants to merge 9 commits intomicrosoft:mainfrom
jstarks:map_em_p2p
Open

vfio_assigned_device: dynamic IOMMU mapping for P2P DMA#3367
jstarks wants to merge 9 commits intomicrosoft:mainfrom
jstarks:map_em_p2p

Conversation

@jstarks
Copy link
Copy Markdown
Member

@jstarks jstarks commented Apr 23, 2026

VFIO device assignment needs peer-to-peer DMA between assigned devices — one device must be able to DMA into another device's BAR. The old VFIO container manager snapshot guest RAM at creation time via GuestMemory::full_mapping() and programmed a static set of IOMMU identity mappings, so device BARs (which are mapped dynamically as the guest configures them) were never visible to the IOMMU.

Introduce a DmaTarget trait in the region manager that receives incremental sub-mapping events (map_dma / unmap_dma) as regions are enabled, disabled, or modified. VFIO containers register themselves as DMA mapper consumers via a new DmaMapperClient, and the region manager replays all existing active sub-mappings on registration, then sends live updates as the memory map evolves. This means device BAR mappings are now programmed into every VFIO container's IOMMU as they appear, enabling P2P DMA between assigned devices.

The region manager maintains the VaMapper lifecycle for backends that need host VAs (VFIO type1's pin_user_pages), controlled by a needs_va flag at registration. This also sets up the abstraction for future iommufd support, which maps from fd+offset directly and won't need a VaMapper at all.

Copilot AI review requested due to automatic review settings April 23, 2026 22:19
@github-actions github-actions Bot added the unsafe Related to unsafe code label Apr 23, 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

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

Enables peer-to-peer DMA for VFIO-assigned devices by keeping VFIO Type1 IOMMU mappings synchronized with the evolving guest memory map (including dynamically-mapped device BARs) via a new DMA-mapping consumer interface in membacking.

Changes:

  • Add membacking::DmaTarget + DmaMapperClient to publish per-submapping map/unmap events (with optional host VA support via VaMapper).
  • Update vfio_assigned_device to register each VFIO container as a DMA-mapper consumer instead of snapshotting guest RAM mappings at container creation.
  • Make vfio_sys::Container::map_dma explicitly unsafe and take a typed pointer (*const u8) for the mapped host VA.

Reviewed changes

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

Show a summary per file
File Description
vm/devices/user_driver/vfio_sys/src/lib.rs Make DMA mapping API unsafe and pointer-typed to reflect memory safety constraints.
vm/devices/pci/vfio_assigned_device/src/resolver.rs Switch resolver construction to accept a DmaMapperClient rather than GuestMemory.
vm/devices/pci/vfio_assigned_device/src/manager.rs Register VFIO containers as DmaTarget consumers for dynamic IOMMU updates.
vm/devices/pci/vfio_assigned_device/src/lib.rs Allow unsafe usage at crate level (needed for DMA mapping integration).
vm/devices/pci/vfio_assigned_device/Cargo.toml Replace guestmem dependency with membacking.
openvmm/openvmm_core/src/worker/dispatch.rs Wire GuestMemoryManager::dma_mapper_client() into VFIO resolver creation.
openvmm/membacking/src/region_manager.rs Introduce DMA mapper registration + replay + live update plumbing in the region manager.
openvmm/membacking/src/memory_manager/mod.rs Expose dma_mapper_client() from GuestMemoryManager.
openvmm/membacking/src/memory_manager/device_memory.rs Clarify that dma_target=false affects sharing, not IOMMU DMA notifications.
openvmm/membacking/src/mapping_manager/va_mapper.rs Add small public accessors/doc comments used by DMA mapping logic.
openvmm/membacking/src/lib.rs Re-export DmaTarget/DmaMapperClient/DmaMapperHandle and Mappable for consumers.
Cargo.lock Update dependency graph for the new membacking usage.

Comment thread openvmm/membacking/src/region_manager.rs
Comment thread vm/devices/pci/vfio_assigned_device/src/manager.rs
Comment thread openvmm/membacking/src/region_manager.rs Outdated
Comment thread openvmm/membacking/src/region_manager.rs
Comment thread openvmm/membacking/src/region_manager.rs Outdated
VFIO device assignment needs peer-to-peer DMA between assigned devices —
one device must be able to DMA into another device's BAR. The old VFIO
container manager snapshot guest RAM at creation time via
GuestMemory::full_mapping() and programmed a static set of IOMMU
identity mappings, so device BARs (which are mapped dynamically as the
guest configures them) were never visible to the IOMMU.

Introduce a DmaTarget trait in the region manager that receives
incremental sub-mapping events (map_dma / unmap_dma) as regions are
enabled, disabled, or modified. VFIO containers register themselves as
DMA mapper consumers via a new DmaMapperClient, and the region manager
replays all existing active sub-mappings on registration, then sends
live updates as the memory map evolves. This means device BAR mappings
are now programmed into every VFIO container's IOMMU as they appear,
enabling P2P DMA between assigned devices.

The region manager maintains the VaMapper lifecycle for backends that
need host VAs (VFIO type1's pin_user_pages), controlled by a needs_va
flag at registration. This also sets up the abstraction for future
iommufd support, which maps from fd+offset directly and won't need a
VaMapper at all.

On the vfio_sys side, Container::map_dma now takes *const u8 instead of
a raw u64 and is marked unsafe, properly reflecting the memory safety
requirements that were previously hidden in the API.
Copilot AI review requested due to automatic review settings April 24, 2026 00:19
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

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

Comment thread vm/devices/pci/vfio_assigned_device/src/resolver.rs
Comment thread vm/devices/user_driver/vfio_sys/src/lib.rs Outdated
Comment thread openvmm/membacking/src/region_manager.rs Outdated
Comment thread openvmm/membacking/src/region_manager.rs Outdated
Comment thread openvmm/membacking/src/region_manager.rs
Comment thread vm/devices/pci/vfio_assigned_device/src/manager.rs
Comment thread openvmm/membacking/src/region_manager.rs
Copilot AI review requested due to automatic review settings April 25, 2026 00:19
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

Copilot reviewed 11 out of 12 changed files in this pull request and generated 4 comments.

Comment thread openvmm/membacking/src/region_manager.rs Outdated
Comment thread vm/devices/pci/vfio_assigned_device/src/manager.rs
Comment thread vm/devices/pci/vfio_assigned_device/src/resolver.rs
Comment thread openvmm/membacking/src/region_manager.rs
@jstarks jstarks marked this pull request as ready for review April 25, 2026 00:31
@jstarks jstarks requested a review from a team as a code owner April 25, 2026 00:31
Copilot AI review requested due to automatic review settings April 25, 2026 00:31
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

Copilot reviewed 11 out of 12 changed files in this pull request and generated 1 comment.

Comment thread openvmm/membacking/src/region_manager.rs Outdated
Copilot AI review requested due to automatic review settings April 26, 2026 21:41
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

Copilot reviewed 11 out of 12 changed files in this pull request and generated 4 comments.

Comment thread openvmm/membacking/src/region_manager.rs Outdated
Comment thread vm/devices/pci/vfio_assigned_device/src/manager.rs
Comment thread vm/devices/pci/vfio_assigned_device/src/resolver.rs
Comment thread openvmm/membacking/src/region_manager.rs
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