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 <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2019-07-24 21:36:53 -07:00 committed by Rob Bradford
parent b9f677c46c
commit d92d797896
2 changed files with 17 additions and 7 deletions

View File

@ -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<VmFd>, 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<VmFd>, mem_slot: u32) -> Result<u32> {
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)
}
}

View File

@ -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));