From b0dd4e72c55769e2b06e5632068709aaaf966bd3 Mon Sep 17 00:00:00 2001 From: Thomas Barrett Date: Wed, 14 Feb 2024 20:26:07 +0000 Subject: [PATCH] pci: vfio: naturally align bar According to PCIe specification, a 64-bit MMIO BAR should be naturally aligned. In addition to being more compliant with the specification, natural aligned BARs are mapped with the largest possible page size by the host iommu driver, which should speed up boot time and reduce IOTLB thrashing for virtual machines with VFIO devices. Signed-off-by: Thomas Barrett (cherry picked from commit c9f94be7ab996e63400c649f47a7809655be7309) --- pci/src/vfio.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pci/src/vfio.rs b/pci/src/vfio.rs index a685ceedf..cf66f06b1 100644 --- a/pci/src/vfio.rs +++ b/pci/src/vfio.rs @@ -696,8 +696,11 @@ impl VfioCommon { .allocate( restored_bar_addr, region_size, - // SAFETY: FFI call. Trivially safe. - Some(unsafe { sysconf(_SC_PAGESIZE) as GuestUsize }), + Some(std::cmp::max( + // SAFETY: FFI call. Trivially safe. + unsafe { sysconf(_SC_PAGESIZE) as GuestUsize }, + region_size, + )), ) .ok_or(PciDeviceError::IoAllocationFailed(region_size))? }