From f0c1f8d079d90d2390b2c01a88d81cd00dd08209 Mon Sep 17 00:00:00 2001 From: Thomas Barrett Date: Tue, 23 Jan 2024 03:21:45 +0000 Subject: [PATCH] pci: vfio: VFIO_IOMMU_MAP_DMA mmio regions For all VFIO devices, map all non-emulated MMIO regions to the vfio container to allow PCIe P2P between all VFIO devices on the virtual machine. This is required for a wide variety of advanced GPU workloads such as GPUDirect P2P (DMA between two GPUs), GPUDirect RDMA (DMA between a GPU and an IB device). Signed-off-by: Thomas Barrett --- pci/src/vfio.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pci/src/vfio.rs b/pci/src/vfio.rs index a685ceedf..1641b619f 100644 --- a/pci/src/vfio.rs +++ b/pci/src/vfio.rs @@ -1568,6 +1568,16 @@ impl VfioPciDevice { self.vm .create_user_memory_region(mem_region) .map_err(VfioPciError::CreateUserMemoryRegion)?; + + if !self.iommu_attached { + self.container + .vfio_dma_map( + user_memory_region.start, + user_memory_region.size, + user_memory_region.host_addr, + ) + .map_err(VfioPciError::DmaMap)?; + } } } } @@ -1578,6 +1588,16 @@ impl VfioPciDevice { pub fn unmap_mmio_regions(&mut self) { for region in self.common.mmio_regions.iter() { for user_memory_region in region.user_memory_regions.iter() { + // Unmap from vfio container + if !self.iommu_attached { + if let Err(e) = self + .container + .vfio_dma_unmap(user_memory_region.start, user_memory_region.size) + { + error!("Could not unmap mmio region from vfio container: {}", e); + } + } + // Remove region let r = self.vm.make_user_memory_region( user_memory_region.slot,