vfio: Improve error reporting when MMIO region mapping fails
cover vfio: Improve error reporting when MMIO region mapping fails
Hello,
Under certain circumstances, a MMIO region of a device fails to map because the region is outside the supported IOVA ranges of the VM. In this case, PCI peer-to-peer transactions on BARs are not supported. This typically occurs when the IOMMU address space width is less than the physical address width, as can be the case on consumer processors or when using a vIOMMU device with default settings.
This series tries to clarify the error message reported to the user and provide advice on how to possibly resolve this issue.
Thanks,
C.
Cédric Le Goater (5):
vfio/pci: Replace "iommu_device" by "vIOMMU"
vfio: Modify vfio_viommu_preset() parameter
vfio: Improve error reporting when MMIO region mapping fails
cpu: Introduce cpu_get_phys_bits()
vfio: Check compatibility of CPU and IOMMU address space width
include/hw/core/cpu.h | 9 ++++++ include/hw/core/sysemu-cpu-ops.h | 6 ++++ include/hw/vfio/vfio-common.h | 2 +- cpu-target.c | 5 ++++ hw/core/cpu-system.c | 11 ++++++++ hw/vfio/common.c | 47 +++++++++++++++++++++++++++++--- hw/vfio/migration.c | 2 +- hw/vfio/pci.c | 2 +- target/i386/cpu.c | 6 ++++ 9 files changed, 83 insertions(+), 7 deletions(-)
Signature · 1 line
2.48.1
PATCH 1/5 vfio/pci: Replace "iommu_device" by "vIOMMU"
This is to be consistent with other reported errors related to vIOMMU devices.
--- hw/vfio/pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/vfio/pci.c +1 -1
@@ -3131,7 +3131,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)if (!vbasedev->mdev && !pci_device_set_iommu_device (pdev, vbasedev->hiod, errp)) { -error_prepend (errp,"Failed to set iommu_device: " ); +error_prepend (errp,"Failed to set vIOMMU: " ); goto out_teardown; }
Signature · 1 line
2.48.1
PATCH 2/5 vfio: Modify vfio_viommu_preset() parameter
In reply to Cédric Le GoaterWe plan to use vfio_viommu_preset() in MemoryListener handlers which operate at the container level. Change the parameter to VFIOContainerBase to ease future changes.
--- include/hw/vfio/vfio-common.h | 2 +- hw/vfio/common.c | 4 ++-- hw/vfio/migration.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-)
include/hw/vfio/vfio-common.h +1 -1
@@ -274,7 +274,7 @@ extern int vfio_kvm_device_fd;bool vfio_mig_active (void );int vfio_block_multiple_devices_migration (VFIODevice *vbasedev,Error **errp);void vfio_unblock_multiple_devices_migration (void ); -bool vfio_viommu_preset (VFIODevice *vbasedev); +bool vfio_viommu_preset (VFIOContainerBase *bcontainer);int64_t vfio_mig_bytes_transferred (void );void vfio_reset_bytes_transferred (void );bool vfio_device_state_is_running (VFIODevice *vbasedev);
hw/vfio/common.c +2 -2
@@ -142,9 +142,9 @@ void vfio_unblock_multiple_devices_migration(void)migrate_del_blocker (&multiple_devices_migration_blocker); } -bool vfio_viommu_preset (VFIODevice *vbasedev) +bool vfio_viommu_preset (VFIOContainerBase *bcontainer) { -return vbasedev->bcontainer->space->as != &address_space_memory; +return bcontainer->space->as != &address_space_memory; }static void vfio_set_migration_error (int ret)
hw/vfio/migration.c +1 -1
@@ -1069,7 +1069,7 @@ bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp) goto out_deinit; } -if (vfio_viommu_preset (vbasedev)) { +if (vfio_viommu_preset (vbasedev->bcontainer)) {error_setg (&err,"%s: Migration is currently not supported " "with vIOMMU enabled" , vbasedev->name); goto add_blocker;
Signature · 1 line
2.48.1
PATCH 3/5 vfio: Improve error reporting when MMIO region mapping fails
In reply to Cédric Le GoaterWhen the IOMMU address space width is smaller than the physical address width, a MMIO region of a device can fail to map because the region is outside the supported IOVA ranges of the VM. In this case, PCI peer-to-peer transactions on BARs are not supported.
This can occur with the 39-bit IOMMU address space width, as can be the case on consumer processors or when using a vIOMMU device with default settings.
The current error message is unclear. Change the error report to a warning because it is a non fatal condition for the VM, clarify the induced limitations for the user and provide advice on how to possibly resolve this issue: setting the CPU address space width of the IOMMU address space width accordingly.
--- hw/vfio/common.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-)
hw/vfio/common.c +20 -2
@@ -569,6 +569,20 @@ static bool vfio_get_section_iova_range(VFIOContainerBase *bcontainer,return true; } +static void vfio_container_p2p_error_append (VFIOContainerBase *bcontainer, +Error **errp) +{ +error_append_hint (errp,"PCI peer-to-peer transactions on BARs " +"are not supported. " ); +if (vfio_viommu_preset (bcontainer)) { +error_append_hint (errp,"Try setting the vIOMMU \"aw-bits\" " +"property to match CPU address space width\n" ); + }else { +error_append_hint (errp,"Try setting the CPU \"phys-bits\" " +"property to match IOMMU address space width\n" ); + } +} +static void vfio_listener_region_add (MemoryListener *listener,MemoryRegionSection *section) {
@@ -685,9 +699,13 @@ static void vfio_listener_region_add(MemoryListener *listener,"0x%" HWADDR_PRIx", %p) = %d (%s)" , bcontainer, iova,int128_get64 (llsize), vaddr, ret, strerror(-ret)); +/* +* MMIO region mapping failures are not fatal but in this case +* PCI peer-to-peer transactions are broken. +*/ if (memory_region_is_ram_device (section->mr)) { -/* Allow unexpected mappings not to be fatal for RAM devices */ -error_report_err (err); +vfio_container_p2p_error_append (bcontainer, &err); +warn_report_err (err);return ; } goto fail;
Signature · 1 line
2.48.1
PATCH 4/5 cpu: Introduce cpu_get_phys_bits()
In reply to Cédric Le GoaterThe Intel CPU has a complex history regarding setting of the physical address space width on KVM. A 'phys_bits' field and a "phys-bits" property were added by commit af45907a1328 ("target-i386: Allow physical address bits to be set") to tune this value.
In certain circumstances, it is interesting to know this value to check that all the conditions are met for optimal operation. For instance, when the system has a 39-bit IOMMU address space width and a larger CPU physical address space, we expect issues when mapping the MMIO regions of passthrough devices and it would good to report to the user. These hybrid HW configs can be found on some consumer grade processors or when using a vIOMMU device with default settings.
For this purpose, add an helper routine and a CPUClass callback to return the physical address space width of a CPU.
--- include/hw/core/cpu.h | 9 +++++++++ include/hw/core/sysemu-cpu-ops.h | 6 ++++++ cpu-target.c | 5 +++++ hw/core/cpu-system.c | 11 +++++++++++ target/i386/cpu.c | 6 ++++++ 5 files changed, 37 insertions(+)
include/hw/core/cpu.h +9 -0
@@ -748,6 +748,14 @@ int cpu_asidx_from_attrs(CPUState *cpu, MemTxAttrs attrs);*/ bool cpu_virtio_is_big_endian (CPUState *cpu); +/** +* cpu_get_phys_bits: +* @cpu: CPU +* +* Return the physical address space width of the CPU @cpu. +*/ +uint32_t cpu_get_phys_bits (const CPUState *cpu); +#endif /* CONFIG_USER_ONLY */ /**
@@ -1168,6 +1176,7 @@ void cpu_exec_unrealizefn(CPUState *cpu);void cpu_exec_reset_hold (CPUState *cpu);const char *target_name (void ); +uint32_t target_phys_bits (void );#ifdef COMPILING_PER_TARGET
include/hw/core/sysemu-cpu-ops.h +6 -0
@@ -81,6 +81,12 @@ typedef struct SysemuCPUOps {*/ bool (*virtio_is_big_endian)(CPUState *cpu); +/** +* @get_phys_bits: Callback to return the physical address space +* width of a CPU. +*/ +uint32_t (*get_phys_bits)(const CPUState *cpu); +/** * @legacy_vmsd: Legacy state for migration. * Do not use in new targets, use #DeviceClass::vmsd instead.
cpu-target.c +5 -0
@@ -472,3 +472,8 @@ const char *target_name(void) {return TARGET_NAME ; } + +uint32_t target_phys_bits (void ) +{ +return TARGET_PHYS_ADDR_SPACE_BITS ; +}
hw/core/cpu-system.c +11 -0
@@ -60,6 +60,17 @@ hwaddr cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr,return cc->sysemu_ops->get_phys_page_debug (cpu, addr); } +uint32_t cpu_get_phys_bits (const CPUState *cpu) +{ +CPUClass *cc =CPU_GET_CLASS (cpu); + +if (cc->sysemu_ops->get_phys_bits) { +return cc->sysemu_ops->get_phys_bits (cpu); + } + +return target_phys_bits (); +} +hwaddr cpu_get_phys_page_debug (CPUState *cpu,vaddr addr) {MemTxAttrs attrs = {};
target/i386/cpu.c +6 -0
@@ -8239,6 +8239,11 @@ static bool x86_cpu_get_paging_enabled(const CPUState *cs) return cpu->env.cr[0] & CR0_PG_MASK; } + +static uint32_t x86_cpu_get_phys_bits (const CPUState *cs) +{ +return X86_CPU (cs)->phys_bits; +}#endif /* !CONFIG_USER_ONLY */ static void x86_cpu_set_pc (CPUState *cs,vaddr value)
@@ -8547,6 +8552,7 @@ static const struct SysemuCPUOps i386_sysemu_ops = { .get_memory_mapping = x86_cpu_get_memory_mapping, .get_paging_enabled = x86_cpu_get_paging_enabled, .get_phys_page_attrs_debug = x86_cpu_get_phys_page_attrs_debug, + .get_phys_bits = x86_cpu_get_phys_bits, .asidx_from_attrs = x86_asidx_from_attrs, .get_crash_info = x86_cpu_get_crash_info, .write_elf32_note = x86_cpu_write_elf32_note,
Signature · 1 line
2.48.1
PATCH 5/5 vfio: Check compatibility of CPU and IOMMU address space width
In reply to Cédric Le GoaterPrint a warning if IOMMU address space width is smaller than the physical address width. In this case, PCI peer-to-peer transactions on BARs are not supported and failures of device MMIO regions are to be expected.
This can occur with the 39-bit IOMMU address space width as found on consumer grade processors or when using a vIOMMU device with default settings.
--- hw/vfio/common.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
hw/vfio/common.c +21 -0
@@ -44,6 +44,8 @@#include "migration/qemu-file.h" #include "system/tpm.h" +#include "hw/core/cpu.h" +VFIODeviceList vfio_device_list =QLIST_HEAD_INITIALIZER (vfio_device_list); static QLIST_HEAD(, VFIOAddressSpace) vfio_address_spaces =
@@ -1561,12 +1563,28 @@ retry:return info; } +static bool vfio_device_check_address_space (VFIODevice *vbasedev,Error **errp) +{ +uint32_t cpu_aw_bits =cpu_get_phys_bits (first_cpu); +uint32_t iommu_aw_bits =vfio_device_get_aw_bits (vbasedev); + +if (cpu_aw_bits && cpu_aw_bits > iommu_aw_bits) { +error_setg (errp,"Host physical address space (%u) is larger than " +"the host IOMMU address space (%u)." , cpu_aw_bits, + iommu_aw_bits); +vfio_container_p2p_error_append (vbasedev->bcontainer, errp); +return false; + } +return true; +} +bool vfio_attach_device (char *name,VFIODevice *vbasedev,AddressSpace *as,Error **errp) {const VFIOIOMMUClass *ops =VFIO_IOMMU_CLASS (object_class_by_name (TYPE_VFIO_IOMMU_LEGACY ));HostIOMMUDevice *hiod =NULL ; +Error *local_err =NULL ;if (vbasedev->iommufd) { ops =VFIO_IOMMU_CLASS (object_class_by_name (TYPE_VFIO_IOMMU_IOMMUFD ));
@@ -1586,6 +1604,9 @@ bool vfio_attach_device(char *name, VFIODevice *vbasedev,return false; } +if (!vfio_device_check_address_space (vbasedev, &local_err)) { +warn_report_err (local_err); + }return true; }
Signature · 1 line
2.48.1