From d92d7978960746c004b647b806a4759739fbae01 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Wed, 24 Jul 2019 21:36:53 -0700 Subject: [PATCH] vfio: Update memory slot index to support multiple VFIO devices In order to correctly support multiple VFIO devices, we need to increment the memory slot index every time it is being used to set some user memory region through KVM. That's why the mem_slot parameter is made mutable. Signed-off-by: Sebastien Boeuf --- vfio/src/vfio_pci.rs | 19 ++++++++++++++----- vmm/src/vm.rs | 5 +++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/vfio/src/vfio_pci.rs b/vfio/src/vfio_pci.rs index f25be3b19..3fa671ab9 100644 --- a/vfio/src/vfio_pci.rs +++ b/vfio/src/vfio_pci.rs @@ -588,9 +588,18 @@ impl VfioPciDevice { /// Map MMIO regions into the guest, and avoid VM exits when the guest tries /// to reach those regions. - pub fn map_mmio_regions(&mut self, vm: &Arc, mem_slots: u32) -> Result<()> { - let mut slot = mem_slots; + /// + /// # Arguments + /// + /// * `vm` - The KVM VM file descriptor. It is used to set the VFIO MMIO regions + /// as KVM user memory regions. + /// * `mem_slot` - The KVM memory slot to set the user memopry regions. + /// # Return value + /// + /// This function returns the updated KVM memory slot id. + pub fn map_mmio_regions(&mut self, vm: &Arc, mem_slot: u32) -> Result { let fd = self.device.as_raw_fd(); + let mut new_mem_slot = mem_slot; for region in self.mmio_regions.iter() { // We want to skip the mapping of the BAR containing the MSI-X @@ -635,7 +644,7 @@ impl VfioPciDevice { } let mem_region = kvm_userspace_memory_region { - slot, + slot: new_mem_slot as u32, guest_phys_addr: region.start.raw_value() + mmap_offset, memory_size: mmap_size as u64, userspace_addr: host_addr as u64, @@ -647,11 +656,11 @@ impl VfioPciDevice { vm.set_user_memory_region(mem_region) .map_err(VfioPciError::MapRegionGuest)?; } - slot += 1; + new_mem_slot += 1; } } - Ok(()) + Ok(new_mem_slot) } } diff --git a/vmm/src/vm.rs b/vmm/src/vm.rs index 21ecd6c88..623f52c17 100755 --- a/vmm/src/vm.rs +++ b/vmm/src/vm.rs @@ -969,6 +969,7 @@ impl DeviceManager { buses: &mut BusInfo, mem_slots: u32, ) -> DeviceManagerResult<()> { + let mut mem_slot = mem_slots; if let Some(device_list_cfg) = &vm_cfg.devices { // Create the KVM VFIO device let device_fd = DeviceManager::create_kvm_device(vm_fd)?; @@ -986,8 +987,8 @@ impl DeviceManager { .allocate_bars(allocator) .map_err(DeviceManagerError::AllocateBars)?; - vfio_pci_device - .map_mmio_regions(vm_fd, mem_slots) + mem_slot = vfio_pci_device + .map_mmio_regions(vm_fd, mem_slot) .map_err(DeviceManagerError::VfioMapRegion)?; let vfio_pci_device = Arc::new(Mutex::new(vfio_pci_device));