VIRTIO-IOMMU/VFIO: Fix host iommu geometry handling for hotplugged devices
v4 cover VIRTIO-IOMMU/VFIO: Fix host iommu geometry handling for hotplugged devices
This series is based on Zhenzhong HostIOMMUDevice:
[PATCH v7 00/17] Add a host IOMMU device abstraction to check with vIOMMU
https://lore.kernel.org/all/[email protected]/
It allows to convey host IOVA reserved regions to the virtio-iommu and uses the HostIOMMUDevice infrastructure. This replaces the usage of IOMMU MR ops which fail to satisfy this need for hotplugged devices.
See below for additional background.
In [1] we attempted to fix a case where a VFIO-PCI device protected with a virtio-iommu was assigned to an x86 guest. On x86 the physical IOMMU may have an address width (gaw) of 39 or 48 bits whereas the virtio-iommu used to expose a 64b address space by default. Hence the guest was trying to use the full 64b space and we hit DMA MAP failures. To work around this issue we managed to pass usable IOVA regions (excluding the out of range space) from VFIO to the virtio-iommu device. This was made feasible by introducing a new IOMMU Memory Region callback dubbed iommu_set_iova_regions(). This latter gets called when the IOMMU MR is enabled which causes the vfio_listener_region_add() to be called.
For coldplugged devices the technique works because we make sure all the IOMMU MR are enabled once on the machine init done: 94df5b2180 ("virtio-iommu: Fix 64kB host page size VFIO device assignment") for granule freeze. But I would be keen to get rid of this trick.
However with VFIO-PCI hotplug, this technique fails due to the race between the call to the callback in the add memory listener and the virtio-iommu probe request. Indeed the probe request gets called before the attach to the domain. So in that case the usable regions are communicated after the probe request and fail to be conveyed to the guest.
Using an IOMMU MR Ops is unpractical because this relies on the IOMMU MR to have been enabled and the corresponding vfio_listener_region_add() to be executed. Instead this series proposes to replace the usage of this API by the recently introduced PCIIOMMUOps: ba7d12eb8c ("hw/pci: modify pci_setup_iommu() to set PCIIOMMUOps"). That way, the callback can be called earlier, once the usable IOVA regions have been collected by VFIO, without the need for the IOMMU MR to be enabled.
This series also removes the spurious message:
qemu-system-aarch64: warning: virtio-iommu-memory-region-7-0: Notified about new host reserved regions after probe
In the short term this may also be used for passing the page size mask, which would allow to get rid of the hacky transient IOMMU MR enablement mentionned above.
[1] [PATCH v4 00/12] VIRTIO-IOMMU/VFIO: Don't assume 64b IOVA space
https://lore.kernel.org/all/[email protected]/
Extra Notes:
With that series, the reserved memory regions are communicated on time so that the virtio-iommu probe request grabs them. However this is not sufficient. In some cases (my case), I still see some DMA MAP failures and the guest keeps on using IOVA ranges outside the geometry of the physical IOMMU. This is due to the fact the VFIO-PCI device is in the same iommu group as the pcie root port. Normally the kernel iova_reserve_iommu_regions (dma-iommu.c) is supposed to call reserve_iova() for each reserved IOVA, which carves them out of the allocator. When iommu_dma_init_domain() gets called for the hotplugged vfio-pci device the iova domain is already allocated and set and we don't call iova_reserve_iommu_regions() again for the vfio-pci device. So its corresponding reserved regions are not properly taken into account.
This is not trivial to fix because theoretically the 1st attached devices could already have allocated IOVAs within the reserved regions of the second device. Also we are somehow hijacking the reserved memory regions to model the geometry of the physical IOMMU so not sure any attempt to fix that upstream will be accepted. At the moment one solution is to make sure assigned devices end up in singleton group. Another solution is to work on a different approach where the gaw can be passed as an option to the virtio-iommu device, similarly at what is done with intel iommu.
This series can be found at:
https://github.com/eauger/qemu/tree/iommufd_nesting_preq_v7_resv_regions_v4
History:
v3 -> v4:
- add one patch to add aliased pci bus and devfn in the HostIOMMUDevice
- Use those for resv regions computation
- Remove VirtioHostIOMMUDevice and simply use the base object
v2 -> v3:
- moved the series from RFC to patch
- collected Zhenzhong's R-bs and took into account most of his comments (see replies on v2)
Eric Auger (8):
HostIOMMUDevice: Store the VFIO/VDPA agent
virtio-iommu: Implement set|unset]_iommu_device() callbacks
HostIOMMUDevice: Introduce get_iova_ranges callback
HostIOMMUDevice: Store the aliased bus and devfn
virtio-iommu: Compute host reserved regions
virtio-iommu: Remove the implementation of iommu_set_iova_range hw/vfio: Remove memory_region_iommu_set_iova_ranges() call
memory: Remove IOMMU MR iommu_set_iova_range API
include/exec/memory.h | 32 ---- include/hw/virtio/virtio-iommu.h | 2 + include/sysemu/host_iommu_device.h | 11 ++ hw/pci/pci.c | 8 +- hw/vfio/common.c | 10 - hw/vfio/container.c | 17 ++ hw/vfio/iommufd.c | 18 ++ hw/virtio/virtio-iommu.c | 296 +++++++++++++++++++---------- system/memory.c | 13 -- 9 files changed, 249 insertions(+), 158 deletions(-)
Signature · 1 line
2.41.0
PATCH v4 1/8 HostIOMMUDevice: Store the VFIO/VDPA agent
Store the agent device (VFIO or VDPA) in the host IOMMU device. This will allow easy access to some of its resources.
--- include/sysemu/host_iommu_device.h | 1 + hw/vfio/container.c | 1 + hw/vfio/iommufd.c | 2 ++ 3 files changed, 4 insertions(+)
include/sysemu/host_iommu_device.h +1 -0
@@ -34,6 +34,7 @@ struct HostIOMMUDevice {Object parent_obj;char *name; +void *agent;/* pointer to agent device, ie. VFIO or VDPA device */ HostIOMMUDeviceCaps caps; };
hw/vfio/container.c +1 -0
@@ -1145,6 +1145,7 @@ static bool hiod_legacy_vfio_realize(HostIOMMUDevice *hiod, void *opaque, hiod->name =g_strdup (vdev->name); hiod->caps.aw_bits =vfio_device_get_aw_bits (vdev); + hiod->agent = opaque;return true; }
hw/vfio/iommufd.c +2 -0
@@ -631,6 +631,8 @@ static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,struct iommu_hw_info_vtd vtd; } data; + hiod->agent = opaque; +if (!iommufd_backend_get_device_info (vdev->iommufd, vdev->devid, &type, &data,sizeof (data), errp)) {return false;
Signature · 1 line
2.41.0
PATCH v4 2/8 virtio-iommu: Implement set|unset]_iommu_device() callbacks
In reply to Eric AugerImplement PCIIOMMUOPs [set|unset]_iommu_device() callbacks. In set(), the HostIOMMUDevice handle is stored in a hash table indexed by PCI BDF. The object will allow to retrieve information related to the physical IOMMU.
---
v3 -> v4:
- Simply use native HostIOMMUDevice instead of VirtioHostIOMMUDevice
v2 -> v3:
- include host_iommu_device.h in virtio-iommu.h header
- introduce hiod_destroy() and fix UAF in virtio_iommu_unset_iommu_device()
--- include/hw/virtio/virtio-iommu.h | 2 + hw/virtio/virtio-iommu.c | 82 ++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+)
include/hw/virtio/virtio-iommu.h +2 -0
@@ -25,6 +25,7 @@#include "hw/pci/pci.h" #include "qom/object.h" #include "qapi/qapi-types-virtio.h" +#include "sysemu/host_iommu_device.h" #define TYPE_VIRTIO_IOMMU "virtio-iommu-device"#define TYPE_VIRTIO_IOMMU_PCI "virtio-iommu-pci"
@@ -57,6 +58,7 @@ struct VirtIOIOMMU {struct virtio_iommu_config config;uint64_t features;GHashTable *as_by_busptr; +GHashTable *host_iommu_devices;IOMMUPciBus *iommu_pcibus_by_bus_num[PCI_BUS_MAX ];PCIBus *primary_bus;ReservedRegion *prop_resv_regions;
hw/virtio/virtio-iommu.c +82 -0
102 lines not on this page · raw message
Signature · 1 line
2.41.0
PATCH v4 3/8 HostIOMMUDevice: Introduce get_iova_ranges callback
In reply to Eric AugerIntroduce a new HostIOMMUDevice callback that allows to retrieve the usable IOVA ranges.
Implement this callback in the legacy VFIO and IOMMUFD VFIO host iommu devices. This relies on the VFIODevice agent's base container iova_ranges resource.
---
v2 -> v3:
- add g_assert(vdev)
--- include/sysemu/host_iommu_device.h | 8 ++++++++ hw/vfio/container.c | 16 ++++++++++++++++ hw/vfio/iommufd.c | 16 ++++++++++++++++ 3 files changed, 40 insertions(+)
include/sysemu/host_iommu_device.h +8 -0
@@ -80,6 +80,14 @@ struct HostIOMMUDeviceClass {* i.e., HOST_IOMMU_DEVICE_CAP_AW_BITS. */ int (*get_cap)(HostIOMMUDevice *hiod,int cap,Error **errp); +/** +* @get_iova_ranges: Return the list of usable iova_ranges along with +* @hiod Host IOMMU device +* +* @hiod: handle to the host IOMMU device +* @errp: error handle +*/ + GList* (*get_iova_ranges)(HostIOMMUDevice *hiod, Error **errp); };/*
hw/vfio/container.c +16 -0
@@ -1164,12 +1164,28 @@ static int hiod_legacy_vfio_get_cap(HostIOMMUDevice *hiod, int cap, } } +static GList * +hiod_legacy_vfio_get_iova_ranges (HostIOMMUDevice *hiod,Error **errp) +{ +VFIODevice *vdev = hiod->agent; +GList *l =NULL ; + +g_assert (vdev); + +if (vdev->bcontainer) { + l =g_list_copy (vdev->bcontainer->iova_ranges); + } + +return l; +} +static void hiod_legacy_vfio_class_init (ObjectClass *oc,void *data) {HostIOMMUDeviceClass *hioc =HOST_IOMMU_DEVICE_CLASS (oc); hioc->realize = hiod_legacy_vfio_realize; hioc->get_cap = hiod_legacy_vfio_get_cap; + hioc->get_iova_ranges = hiod_legacy_vfio_get_iova_ranges; };static const TypeInfo types[] = {
hw/vfio/iommufd.c +16 -0
@@ -645,11 +645,27 @@ static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,return true; } +static GList * +hiod_iommufd_vfio_get_iova_ranges (HostIOMMUDevice *hiod,Error **errp) +{ +VFIODevice *vdev = hiod->agent; +GList *l =NULL ; + +g_assert (vdev); + +if (vdev->bcontainer) { + l =g_list_copy (vdev->bcontainer->iova_ranges); + } + +return l; +} +static void hiod_iommufd_vfio_class_init (ObjectClass *oc,void *data) {HostIOMMUDeviceClass *hiodc =HOST_IOMMU_DEVICE_CLASS (oc); hiodc->realize = hiod_iommufd_vfio_realize; + hiodc->get_iova_ranges = hiod_iommufd_vfio_get_iova_ranges; };static const TypeInfo types[] = {
Signature · 1 line
2.41.0
Re: [PATCH v4 3/8] HostIOMMUDevice: Introduce get_iova_ranges callback
Quoted 84 lines · Eric Auger
Introduce a new HostIOMMUDevice callback that allows to retrieve the usable IOVA ranges.
Implement this callback in the legacy VFIO and IOMMUFD VFIO host iommu devices. This relies on the VFIODevice agent's base container
iova_rangesresource.Signed-off-by Eric Auger <[email protected]>Reviewed-by Zhenzhong Duan <[email protected]>---
v2 -> v3:
- addg_assert(vdev)--- include/sysemu/host_iommu_device.h | 8 ++++++++ hw/vfio/container.c | 16 ++++++++++++++++ hw/vfio/iommufd.c | 16 ++++++++++++++++ 3 files changed, 40 insertions(+)include/sysemu/host_iommu_device.h +8
-0@@ -80,6 +80,14 @@ struct HostIOMMUDeviceClass {* i.e., HOST_IOMMU_DEVICE_CAP_AW_BITS. */ int (*get_cap)(HostIOMMUDevice *hiod,int cap,Error **errp); +/** +* @get_iova_ranges: Return the list of usable iova_ranges along with +* @hiod Host IOMMU device +* +* @hiod: handle to the host IOMMU device +* @errp: error handle +*/ + GList* (*get_iova_ranges)(HostIOMMUDevice *hiod, Error **errp); };/* hw/vfio/container.c +16
-0@@ -1164,12 +1164,28 @@ static int hiod_legacy_vfio_get_cap(HostIOMMUDevice *hiod, int cap, } } +static GList * +hiod_legacy_vfio_get_iova_ranges (HostIOMMUDevice *hiod,Error **errp) +{ +VFIODevice *vdev = hiod->agent; +GList *l =NULL ; + +g_assert (vdev); + +if (vdev->bcontainer) { + l =g_list_copy (vdev->bcontainer->iova_ranges); + } + +return l; +} +static void hiod_legacy_vfio_class_init (ObjectClass *oc,void *data) {HostIOMMUDeviceClass *hioc =HOST_IOMMU_DEVICE_CLASS (oc); hioc->realize = hiod_legacy_vfio_realize; hioc->get_cap = hiod_legacy_vfio_get_cap; + hioc->get_iova_ranges = hiod_legacy_vfio_get_iova_ranges; };static const TypeInfo types[] = {hw/vfio/iommufd.c +4
-0@@ -645,11 +645,27 @@ static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,return true; } +static GList * +hiod_iommufd_vfio_get_iova_ranges (HostIOMMUDevice *hiod,Error **errp) +{ +VFIODevice *vdev = hiod->agent;
hw/vfio/iommufd.c static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque, · line 661
Quoted 10 lines
hw/vfio/iommufd.c +10
-0+GList *l =NULL ; + +g_assert (vdev); + +if (vdev->bcontainer) { + l =g_list_copy (vdev->bcontainer->iova_ranges); + } + +return l; +}
May be introduce a common vfio_container_get_iova_ranges() to be called from the get_iova_ranges() handlers ?
Thanks,
C.
Quoted 9 lines
hw/vfio/iommufd.c +1
-0static void hiod_iommufd_vfio_class_init (ObjectClass *oc,void *data) {HostIOMMUDeviceClass *hiodc =HOST_IOMMU_DEVICE_CLASS (oc); hiodc->realize = hiod_iommufd_vfio_realize; + hiodc->get_iova_ranges = hiod_iommufd_vfio_get_iova_ranges; };static const TypeInfo types[] = {
PATCH v4 4/8 HostIOMMUDevice: Store the aliased bus and devfn
In reply to Eric AugerStore the aliased bus and devfn in the HostIOMMUDevice. This will be useful to handle info that are iommu group specific and not device specific (such as reserved iova ranges).
--- include/sysemu/host_iommu_device.h | 2 ++ hw/pci/pci.c | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-)
include/sysemu/host_iommu_device.h +2 -0
@@ -35,6 +35,8 @@ struct HostIOMMUDevice {char *name;void *agent;/* pointer to agent device, ie. VFIO or VDPA device */ +PCIBus *aliased_bus; +int aliased_devfn;HostIOMMUDeviceCaps caps; };
hw/pci/pci.c +6 -2
@@ -2745,11 +2745,15 @@ AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)bool pci_device_set_iommu_device (PCIDevice *dev,HostIOMMUDevice *hiod,Error **errp) { -PCIBus *iommu_bus; +PCIBus *iommu_bus, *aliased_bus; +int aliased_devfn;/* set_iommu_device requires device's direct BDF instead of aliased BDF */ -pci_device_get_iommu_bus_devfn (dev, &iommu_bus,NULL ,NULL ); +pci_device_get_iommu_bus_devfn (dev, &iommu_bus, + &aliased_bus, &aliased_devfn);if (iommu_bus && iommu_bus->iommu_ops->set_iommu_device) { + hiod->aliased_bus = aliased_bus; + hiod->aliased_devfn = aliased_devfn;return iommu_bus->iommu_ops->set_iommu_device (pci_get_bus (dev), iommu_bus->iommu_opaque, dev->devfn, hiod, errp);
Signature · 1 line
2.41.0
PATCH v4 5/8 virtio-iommu: Compute host reserved regions
In reply to Eric AugerCompute the host reserved regions in virtio_iommu_set_iommu_device(). The usable IOVA regions are retrieved from the HostIOMMUDevice. The virtio_iommu_set_host_iova_ranges() helper turns usable regions into complementary reserved regions while testing the inclusion into existing ones. virtio_iommu_set_host_iova_ranges() reuse the implementation of virtio_iommu_set_iova_ranges() which will be removed in subsequent patches. rebuild_resv_regions() is just moved.
---
v3 -> v4:
- use aliased pci bus and devfn
v2 -> v3:
- added g_assert(!sdev->probe_done)
--- hw/virtio/virtio-iommu.c | 147 ++++++++++++++++++++++++++++++--------- 1 file changed, 113 insertions(+), 34 deletions(-)
hw/virtio/virtio-iommu.c +113 -34
@@ -498,11 +498,108 @@ get_host_iommu_device(VirtIOIOMMU *viommu, PCIBus *bus, int devfn) {return g_hash_table_lookup (viommu->host_iommu_devices, &key); } +/** +* rebuild_resv_regions: rebuild resv regions with both the +* info of host resv ranges and property set resv ranges +*/ +static int rebuild_resv_regions (IOMMUDevice *sdev) +{ +GList *l; +int i =0 ; + +/* free the existing list and rebuild it from scratch */ +g_list_free_full (sdev->resv_regions, g_free); + sdev->resv_regions =NULL ; + +/* First add host reserved regions if any, all tagged as RESERVED */ +for (l = sdev->host_resv_ranges; l; l = l->next) { +ReservedRegion *reg =g_new0 (ReservedRegion,1 ); +Range *r = (Range *)l->data; + + reg->type =VIRTIO_IOMMU_RESV_MEM_T_RESERVED ; +range_set_bounds (®->range,range_lob (r),range_upb (r)); + sdev->resv_regions =resv_region_list_insert (sdev->resv_regions, reg); +trace_virtio_iommu_host_resv_regions (sdev->iommu_mr.parent_obj.name, i, +range_lob (®->range), +range_upb (®->range)); + i++; + } +/* +* then add higher priority reserved regions set by the machine +* through properties +*/ +add_prop_resv_regions (sdev); +return 0 ; +} + +static int virtio_iommu_set_host_iova_ranges (VirtIOIOMMU *s,PCIBus *bus, +int devfn,GList *iova_ranges, +Error **errp) +{ +IOMMUPciBus *sbus =g_hash_table_lookup (s->as_by_busptr, bus); +IOMMUDevice *sdev; +GList *current_ranges; +GList *l, *tmp, *new_ranges =NULL ; +int ret = -EINVAL ; + +if (!sbus) { +error_report ("%s no sbus" , __func__); + } + + sdev = sbus->pbdev[devfn]; + + current_ranges = sdev->host_resv_ranges; + +g_assert (!sdev->probe_done); + +/* check that each new resv region is included in an existing one */ +if (sdev->host_resv_ranges) { +range_inverse_array (iova_ranges, + &new_ranges, +0 ,UINT64_MAX ); + +for (tmp = new_ranges; tmp; tmp = tmp->next) { +Range *newr = (Range *)tmp->data; +bool included = false; + +for (l = current_ranges; l; l = l->next) { +Range * r = (Range *)l->data; + +if (range_contains_range (r, newr)) { + included = true; +break ; + } + } +if (!included) { + goto error; + } + } +/* all new reserved ranges are included in existing ones */ + ret =0 ; + goto out; + } + +range_inverse_array (iova_ranges, + &sdev->host_resv_ranges, +0 ,UINT64_MAX ); +rebuild_resv_regions (sdev); + +return 0 ; +error: +error_setg (errp,"%s Conflicting host reserved ranges set!" , + __func__); +out: +g_list_free_full (new_ranges, g_free); +return ret; +} +static bool virtio_iommu_set_iommu_device (PCIBus *bus,void *opaque,int devfn,HostIOMMUDevice *hiod,Error **errp) {VirtIOIOMMU *viommu = opaque; +HostIOMMUDeviceClass *hiodc =HOST_IOMMU_DEVICE_GET_CLASS (hiod);struct hiod_key *new_key; +GList *host_iova_ranges =NULL ;assert (hiod);
@@ -511,12 +608,28 @@ static bool virtio_iommu_set_iommu_device(PCIBus *bus, void *opaque, int devfn,return false; } +if (hiodc->get_iova_ranges) { +int ret; + host_iova_ranges = hiodc->get_iova_ranges (hiod, errp); +if (!host_iova_ranges) { +return true;/* some old kernels may not support that capability */ + } + ret =virtio_iommu_set_host_iova_ranges (viommu, hiod->aliased_bus, + hiod->aliased_devfn, + host_iova_ranges, errp); +if (ret) { +g_list_free_full (host_iova_ranges, g_free); +return false; + } + } + new_key =g_malloc (sizeof (*new_key)); new_key->bus = bus; new_key->devfn = devfn;object_ref (hiod);g_hash_table_insert (viommu->host_iommu_devices, new_key, hiod); +g_list_free_full (host_iova_ranges, g_free);return true; }
@@ -1238,40 +1351,6 @@ static int virtio_iommu_set_page_size_mask(IOMMUMemoryRegion *mr,return 0 ; } -/** -* rebuild_resv_regions: rebuild resv regions with both the -* info of host resv ranges and property set resv ranges -*/ -static int rebuild_resv_regions (IOMMUDevice *sdev) -{ -GList *l; -int i =0 ; - -/* free the existing list and rebuild it from scratch */ -g_list_free_full (sdev->resv_regions, g_free); - sdev->resv_regions =NULL ; - -/* First add host reserved regions if any, all tagged as RESERVED */ -for (l = sdev->host_resv_ranges; l; l = l->next) { -ReservedRegion *reg =g_new0 (ReservedRegion,1 ); -Range *r = (Range *)l->data; - - reg->type =VIRTIO_IOMMU_RESV_MEM_T_RESERVED ; -range_set_bounds (®->range,range_lob (r),range_upb (r)); - sdev->resv_regions =resv_region_list_insert (sdev->resv_regions, reg); -trace_virtio_iommu_host_resv_regions (sdev->iommu_mr.parent_obj.name, i, -range_lob (®->range), -range_upb (®->range)); - i++; - } -/* -* then add higher priority reserved regions set by the machine -* through properties -*/ -add_prop_resv_regions (sdev); -return 0 ; -} -/** * virtio_iommu_set_iova_ranges: Conveys the usable IOVA ranges *
Signature · 1 line
2.41.0
PATCH v4 6/8 virtio-iommu: Remove the implementation of iommu_set_iova_range
In reply to Eric AugerNow that we use PCIIOMMUOps to convey information about usable IOVA ranges we do not to implement the iommu_set_iova_ranges IOMMU MR callback.
--- hw/virtio/virtio-iommu.c | 67 ---------------------------------------- 1 file changed, 67 deletions(-)
hw/virtio/virtio-iommu.c +0 -67
@@ -1351,72 +1351,6 @@ static int virtio_iommu_set_page_size_mask(IOMMUMemoryRegion *mr,return 0 ; } -/** -* virtio_iommu_set_iova_ranges: Conveys the usable IOVA ranges -* -* The function turns those into reserved ranges. Once some -* reserved ranges have been set, new reserved regions cannot be -* added outside of the original ones. -* -* @mr: IOMMU MR -* @iova_ranges: list of usable IOVA ranges -* @errp: error handle -*/ -static int virtio_iommu_set_iova_ranges (IOMMUMemoryRegion *mr, -GList *iova_ranges, -Error **errp) -{ -IOMMUDevice *sdev =container_of (mr, IOMMUDevice, iommu_mr); -GList *current_ranges = sdev->host_resv_ranges; -GList *l, *tmp, *new_ranges =NULL ; -int ret = -EINVAL ; - -/* check that each new resv region is included in an existing one */ -if (sdev->host_resv_ranges) { -range_inverse_array (iova_ranges, - &new_ranges, -0 ,UINT64_MAX ); - -for (tmp = new_ranges; tmp; tmp = tmp->next) { -Range *newr = (Range *)tmp->data; -bool included = false; - -for (l = current_ranges; l; l = l->next) { -Range * r = (Range *)l->data; - -if (range_contains_range (r, newr)) { - included = true; -break ; - } - } -if (!included) { - goto error; - } - } -/* all new reserved ranges are included in existing ones */ - ret =0 ; - goto out; - } - -if (sdev->probe_done) { -warn_report ("%s: Notified about new host reserved regions after probe" , - mr->parent_obj.name); - } - -range_inverse_array (iova_ranges, - &sdev->host_resv_ranges, -0 ,UINT64_MAX ); -rebuild_resv_regions (sdev); - -return 0 ; -error: -error_setg (errp,"IOMMU mr=%s Conflicting host reserved ranges set!" , - mr->parent_obj.name); -out: -g_list_free_full (new_ranges, g_free); -return ret; -} -static void virtio_iommu_system_reset (void *opaque) {VirtIOIOMMU *s = opaque;
@@ -1742,7 +1676,6 @@ static void virtio_iommu_memory_region_class_init(ObjectClass *klass, imrc->replay = virtio_iommu_replay; imrc->notify_flag_changed = virtio_iommu_notify_flag_changed; imrc->iommu_set_page_size_mask = virtio_iommu_set_page_size_mask; - imrc->iommu_set_iova_ranges = virtio_iommu_set_iova_ranges; }static const TypeInfo virtio_iommu_info = {
Signature · 1 line
2.41.0
PATCH v4 7/8 hw/vfio: Remove memory_region_iommu_set_iova_ranges() call
In reply to Eric AugerAs we have just removed the only implementation of iommu_set_iova_ranges IOMMU MR callback in the virtio-iommu, let's remove the call to the memory wrapper. Usable IOVA ranges are now conveyed through the PCIIOMMUOps in VFIO-PCI.
--- hw/vfio/common.c | 10 ---------- 1 file changed, 10 deletions(-)
hw/vfio/common.c +0 -10
@@ -630,16 +630,6 @@ static void vfio_listener_region_add(MemoryListener *listener, goto fail; } -if (bcontainer->iova_ranges) { - ret =memory_region_iommu_set_iova_ranges (giommu->iommu_mr, - bcontainer->iova_ranges, - &err); -if (ret) { -g_free (giommu); - goto fail; - } - } - ret =memory_region_register_iommu_notifier (section->mr, &giommu->n, &err);if (ret) {
Signature · 1 line
2.41.0
PATCH v4 8/8 memory: Remove IOMMU MR iommu_set_iova_range API
In reply to Eric AugerSince the host IOVA ranges are now passed through the PCIIOMMUOps set_host_resv_regions and we have removed the only implementation of iommu_set_iova_range() in the virtio-iommu and the only call site in vfio/common, let's retire the IOMMU MR API and its memory wrapper.
--- include/exec/memory.h | 32 -------------------------------- system/memory.c | 13 ------------- 2 files changed, 45 deletions(-)
include/exec/memory.h +0 -32
@@ -530,26 +530,6 @@ struct IOMMUMemoryRegionClass {int (*iommu_set_page_size_mask)(IOMMUMemoryRegion *iommu,uint64_t page_size_mask,Error **errp); -/** -* @iommu_set_iova_ranges: -* -* Propagate information about the usable IOVA ranges for a given IOMMU -* memory region. Used for example to propagate host physical device -* reserved memory region constraints to the virtual IOMMU. -* -* Optional method: if this method is not provided, then the default IOVA -* aperture is used. -* -* @iommu: the IOMMUMemoryRegion -* -* @iova_ranges: list of ordered IOVA ranges (at least one range) -* -* Returns 0 on success, or a negative error. In case of failure, the error -* object must be created. -*/ -int (*iommu_set_iova_ranges)(IOMMUMemoryRegion *iommu, -GList *iova_ranges, -Error **errp); };typedef struct RamDiscardListener RamDiscardListener ;
@@ -1951,18 +1931,6 @@ int memory_region_iommu_set_page_size_mask(IOMMUMemoryRegion *iommu_mr,uint64_t page_size_mask, Error **errp); -/** -* memory_region_iommu_set_iova_ranges - Set the usable IOVA ranges -* for a given IOMMU MR region -* -* @iommu: IOMMU memory region -* @iova_ranges: list of ordered IOVA ranges (at least one range) -* @errp: pointer to Error*, to store an error if it happens. -*/ -int memory_region_iommu_set_iova_ranges (IOMMUMemoryRegion *iommu, -GList *iova_ranges, -Error **errp); -/** * memory_region_name: get a memory region's name *
system/memory.c +0 -13
@@ -1914,19 +1914,6 @@ int memory_region_iommu_set_page_size_mask(IOMMUMemoryRegion *iommu_mr,return ret; } -int memory_region_iommu_set_iova_ranges (IOMMUMemoryRegion *iommu_mr, -GList *iova_ranges, -Error **errp) -{ -IOMMUMemoryRegionClass *imrc =IOMMU_MEMORY_REGION_GET_CLASS (iommu_mr); -int ret =0 ; - -if (imrc->iommu_set_iova_ranges) { - ret = imrc->iommu_set_iova_ranges (iommu_mr, iova_ranges, errp); - } -return ret; -} -int memory_region_register_iommu_notifier (MemoryRegion *mr,IOMMUNotifier *n,Error **errp) {
Signature · 1 line
2.41.0
Quoted 90 lines
-----Original Message-----
From: Eric Auger <[email protected]>
Subject: [PATCH v4 0/8] VIRTIO-IOMMU/VFIO: Fix host iommu geometry handling for hotplugged devicesThis series is based on Zhenzhong HostIOMMUDevice:
[PATCH v7 00/17] Add a host IOMMU device abstraction to check with vIOMMU
https://lore.kernel.org/all/20240605083043.317831-1-
[email protected]/It allows to convey host IOVA reserved regions to the virtio-iommu and uses the HostIOMMUDevice infrastructure. This replaces the usage of IOMMU MR ops which fail to satisfy this need for hotplugged devices.
See below for additional background.
In [1] we attempted to fix a case where a VFIO-PCI device protected with a virtio-iommu was assigned to an x86 guest. On x86 the physical IOMMU may have an address width (gaw) of 39 or 48 bits whereas the virtio-iommu used to expose a 64b address space by default. Hence the guest was trying to use the full 64b space and we hit DMA MAP failures. To work around this issue we managed to pass usable IOVA regions (excluding the out of range space) from VFIO to the virtio-iommu device. This was made feasible by introducing a new IOMMU Memory Region callback dubbed
iommu_set_iova_regions(). This latter gets called when the IOMMU MR is enabled which causes thevfio_listener_region_add()to be called.For coldplugged devices the technique works because we make sure all the IOMMU MR are enabled once on the machine init done:
94df5b2180("virtio-iommu: Fix 64kB host page size VFIO device assignment") for granule freeze. But I would be keen to get rid of this trick.However with VFIO-PCI hotplug, this technique fails due to the race between the call to the callback in the add memory listener and the virtio-iommu probe request. Indeed the probe request gets called before the attach to the domain. So in that case the usable regions are communicated after the probe request and fail to be conveyed to the guest.
Using an IOMMU MR Ops is unpractical because this relies on the IOMMU MR to have been enabled and the corresponding
vfio_listener_region_add()to be executed. Instead this series proposes to replace the usage of this API by the recently introduced PCIIOMMUOps:ba7d12eb8c("hw/pci:
modifypci_setup_iommu()to set PCIIOMMUOps"). That way, the callback can be called earlier, once the usable IOVA regions have been collected by VFIO, without the need for the IOMMU MR to be enabled.This series also removes the spurious message:
qemu-system-aarch64: warning: virtio-iommu-memory-region-7-0: Notified about new host reserved regions after probeIn the short term this may also be used for passing the page size mask, which would allow to get rid of the hacky transient IOMMU MR enablement mentionned above.
[1] [PATCH v4 00/12] VIRTIO-IOMMU/VFIO: Don't assume 64b IOVA space
https://lore.kernel.org/all/20231019134651.842175-1-
[email protected]/Extra Notes:
With that series, the reserved memory regions are communicated on time so that the virtio-iommu probe request grabs them. However this is not sufficient. In some cases (my case), I still see some DMA MAP failures and the guest keeps on using IOVA ranges outside the geometry of the physical IOMMU. This is due to the fact the VFIO-PCI device is in the same iommu group as the pcie root port. Normally the kerneliova_reserve_iommu_regions(dma-iommu.c) is supposed to callreserve_iova()
for each reserved IOVA, which carves them out of the allocator. Wheniommu_dma_init_domain()gets called for the hotplugged vfio-pci device the iova domain is already allocated and set and we don't calliova_reserve_iommu_regions()again for the vfio-pci device. So its corresponding reserved regions are not properly taken into account.This is not trivial to fix because theoretically the 1st attached devices could already have allocated IOVAs within the reserved regions of the second device. Also we are somehow hijacking the reserved memory regions to model the geometry of the physical IOMMU so not sure any attempt to fix that upstream will be accepted. At the moment one solution is to make sure assigned devices end up in singleton group. Another solution is to work on a different approach where the gaw can be passed as an option to the virtio-iommu device, similarly at what is done with intel iommu.
Quoted 3 lines
This series can be found at:
https://github.com/eauger/qemu/tree/iommufd_nesting_preq_v7_resv_regions_v4
For the whole series,
Thanks
Zhenzhong
Quoted 36 lines
History:
v3 -> v4:
- add one patch to add aliased pci bus and devfn in the HostIOMMUDevice
- Use those for resv regions computation
- Remove VirtioHostIOMMUDevice and simply use the base objectv2 -> v3:
- moved the series from RFC to patch
- collected Zhenzhong's R-bs and took into account most of his comments (see replies on v2)Eric Auger (8):
HostIOMMUDevice: Store the VFIO/VDPA agent
virtio-iommu: Implement set|unset]_iommu_device() callbacks
HostIOMMUDevice: Introduceget_iova_rangescallback
HostIOMMUDevice: Store the aliased bus and devfn
virtio-iommu: Compute host reserved regions
virtio-iommu: Remove the implementation ofiommu_set_iova_rangehw/vfio: Removememory_region_iommu_set_iova_ranges()call
memory: Remove IOMMU MRiommu_set_iova_rangeAPIinclude/exec/memory.h | 32 ---- include/hw/virtio/virtio-iommu.h | 2 + include/sysemu/host_iommu_device.h | 11 ++ hw/pci/pci.c | 8 +- hw/vfio/common.c | 10 - hw/vfio/container.c | 17 ++ hw/vfio/iommufd.c | 18 ++ hw/virtio/virtio-iommu.c | 296 +++++++++++++++++++---------- system/memory.c | 13 -- 9 files changed, 249 insertions(+), 158 deletions(-)--
2.41.0
Quoted 10 lines · Eric Auger
This series is based on Zhenzhong HostIOMMUDevice:
[PATCH v7 00/17] Add a host IOMMU device abstraction to check with vIOMMU
https://lore.kernel.org/all/[email protected]/It allows to convey host IOVA reserved regions to the virtio-iommu and uses the HostIOMMUDevice infrastructure. This replaces the usage of IOMMU MR ops which fail to satisfy this need for hotplugged devices.
See below for additional background.
Should likely be merged together with the dependency. I can either merge both this one and the dependency, or Alex can do that because of the vfio changes.
Quoted 104 lines
In [1] we attempted to fix a case where a VFIO-PCI device protected with a virtio-iommu was assigned to an x86 guest. On x86 the physical IOMMU may have an address width (gaw) of 39 or 48 bits whereas the virtio-iommu used to expose a 64b address space by default. Hence the guest was trying to use the full 64b space and we hit DMA MAP failures. To work around this issue we managed to pass usable IOVA regions (excluding the out of range space) from VFIO to the virtio-iommu device. This was made feasible by introducing a new IOMMU Memory Region callback dubbed
iommu_set_iova_regions(). This latter gets called when the IOMMU MR is enabled which causes thevfio_listener_region_add()to be called.For coldplugged devices the technique works because we make sure all the IOMMU MR are enabled once on the machine init done:
94df5b2180("virtio-iommu: Fix 64kB host page size VFIO device assignment") for granule freeze. But I would be keen to get rid of this trick.However with VFIO-PCI hotplug, this technique fails due to the race between the call to the callback in the add memory listener and the virtio-iommu probe request. Indeed the probe request gets called before the attach to the domain. So in that case the usable regions are communicated after the probe request and fail to be conveyed to the guest.
Using an IOMMU MR Ops is unpractical because this relies on the IOMMU MR to have been enabled and the corresponding
vfio_listener_region_add()to be executed. Instead this series proposes to replace the usage of this API by the recently introduced PCIIOMMUOps:ba7d12eb8c("hw/pci: modifypci_setup_iommu()to set PCIIOMMUOps"). That way, the callback can be called earlier, once the usable IOVA regions have been collected by VFIO, without the need for the IOMMU MR to be enabled.This series also removes the spurious message:
qemu-system-aarch64: warning: virtio-iommu-memory-region-7-0: Notified about new host reserved regions after probeIn the short term this may also be used for passing the page size mask, which would allow to get rid of the hacky transient IOMMU MR enablement mentionned above.
[1] [PATCH v4 00/12] VIRTIO-IOMMU/VFIO: Don't assume 64b IOVA space
https://lore.kernel.org/all/[email protected]/Extra Notes:
With that series, the reserved memory regions are communicated on time so that the virtio-iommu probe request grabs them. However this is not sufficient. In some cases (my case), I still see some DMA MAP failures and the guest keeps on using IOVA ranges outside the geometry of the physical IOMMU. This is due to the fact the VFIO-PCI device is in the same iommu group as the pcie root port. Normally the kerneliova_reserve_iommu_regions(dma-iommu.c) is supposed to callreserve_iova()for each reserved IOVA, which carves them out of the allocator. Wheniommu_dma_init_domain()gets called for the hotplugged vfio-pci device the iova domain is already allocated and set and we don't calliova_reserve_iommu_regions()again for the vfio-pci device. So its corresponding reserved regions are not properly taken into account.This is not trivial to fix because theoretically the 1st attached devices could already have allocated IOVAs within the reserved regions of the second device. Also we are somehow hijacking the reserved memory regions to model the geometry of the physical IOMMU so not sure any attempt to fix that upstream will be accepted. At the moment one solution is to make sure assigned devices end up in singleton group. Another solution is to work on a different approach where the gaw can be passed as an option to the virtio-iommu device, similarly at what is done with intel iommu.
This series can be found at:
https://github.com/eauger/qemu/tree/iommufd_nesting_preq_v7_resv_regions_v4History:
v3 -> v4:
- add one patch to add aliased pci bus and devfn in the HostIOMMUDevice
- Use those for resv regions computation
- Remove VirtioHostIOMMUDevice and simply use the base objectv2 -> v3:
- moved the series from RFC to patch
- collected Zhenzhong's R-bs and took into account most of his comments (see replies on v2)Eric Auger (8):
HostIOMMUDevice: Store the VFIO/VDPA agent
virtio-iommu: Implement set|unset]_iommu_device() callbacks
HostIOMMUDevice: Introduceget_iova_rangescallback
HostIOMMUDevice: Store the aliased bus and devfn
virtio-iommu: Compute host reserved regions
virtio-iommu: Remove the implementation ofiommu_set_iova_rangehw/vfio: Removememory_region_iommu_set_iova_ranges()call
memory: Remove IOMMU MRiommu_set_iova_rangeAPIinclude/exec/memory.h | 32 ---- include/hw/virtio/virtio-iommu.h | 2 + include/sysemu/host_iommu_device.h | 11 ++ hw/pci/pci.c | 8 +- hw/vfio/common.c | 10 - hw/vfio/container.c | 17 ++ hw/vfio/iommufd.c | 18 ++ hw/virtio/virtio-iommu.c | 296 +++++++++++++++++++---------- system/memory.c | 13 -- 9 files changed, 249 insertions(+), 158 deletions(-)Signature · 1 line
2.41.0
Cédric Le Goater
AppliedQuoted 102 lines · Eric Auger
This series is based on Zhenzhong HostIOMMUDevice:
[PATCH v7 00/17] Add a host IOMMU device abstraction to check with vIOMMU
https://lore.kernel.org/all/[email protected]/It allows to convey host IOVA reserved regions to the virtio-iommu and uses the HostIOMMUDevice infrastructure. This replaces the usage of IOMMU MR ops which fail to satisfy this need for hotplugged devices.
See below for additional background.
In [1] we attempted to fix a case where a VFIO-PCI device protected with a virtio-iommu was assigned to an x86 guest. On x86 the physical IOMMU may have an address width (gaw) of 39 or 48 bits whereas the virtio-iommu used to expose a 64b address space by default. Hence the guest was trying to use the full 64b space and we hit DMA MAP failures. To work around this issue we managed to pass usable IOVA regions (excluding the out of range space) from VFIO to the virtio-iommu device. This was made feasible by introducing a new IOMMU Memory Region callback dubbed
iommu_set_iova_regions(). This latter gets called when the IOMMU MR is enabled which causes thevfio_listener_region_add()to be called.For coldplugged devices the technique works because we make sure all the IOMMU MR are enabled once on the machine init done:
94df5b2180("virtio-iommu: Fix 64kB host page size VFIO device assignment") for granule freeze. But I would be keen to get rid of this trick.However with VFIO-PCI hotplug, this technique fails due to the race between the call to the callback in the add memory listener and the virtio-iommu probe request. Indeed the probe request gets called before the attach to the domain. So in that case the usable regions are communicated after the probe request and fail to be conveyed to the guest.
Using an IOMMU MR Ops is unpractical because this relies on the IOMMU MR to have been enabled and the corresponding
vfio_listener_region_add()to be executed. Instead this series proposes to replace the usage of this API by the recently introduced PCIIOMMUOps:ba7d12eb8c("hw/pci: modifypci_setup_iommu()to set PCIIOMMUOps"). That way, the callback can be called earlier, once the usable IOVA regions have been collected by VFIO, without the need for the IOMMU MR to be enabled.This series also removes the spurious message:
qemu-system-aarch64: warning: virtio-iommu-memory-region-7-0: Notified about new host reserved regions after probeIn the short term this may also be used for passing the page size mask, which would allow to get rid of the hacky transient IOMMU MR enablement mentionned above.
[1] [PATCH v4 00/12] VIRTIO-IOMMU/VFIO: Don't assume 64b IOVA space
https://lore.kernel.org/all/[email protected]/Extra Notes:
With that series, the reserved memory regions are communicated on time so that the virtio-iommu probe request grabs them. However this is not sufficient. In some cases (my case), I still see some DMA MAP failures and the guest keeps on using IOVA ranges outside the geometry of the physical IOMMU. This is due to the fact the VFIO-PCI device is in the same iommu group as the pcie root port. Normally the kerneliova_reserve_iommu_regions(dma-iommu.c) is supposed to callreserve_iova()for each reserved IOVA, which carves them out of the allocator. Wheniommu_dma_init_domain()gets called for the hotplugged vfio-pci device the iova domain is already allocated and set and we don't calliova_reserve_iommu_regions()again for the vfio-pci device. So its corresponding reserved regions are not properly taken into account.This is not trivial to fix because theoretically the 1st attached devices could already have allocated IOVAs within the reserved regions of the second device. Also we are somehow hijacking the reserved memory regions to model the geometry of the physical IOMMU so not sure any attempt to fix that upstream will be accepted. At the moment one solution is to make sure assigned devices end up in singleton group. Another solution is to work on a different approach where the gaw can be passed as an option to the virtio-iommu device, similarly at what is done with intel iommu.
This series can be found at:
https://github.com/eauger/qemu/tree/iommufd_nesting_preq_v7_resv_regions_v4History:
v3 -> v4:
- add one patch to add aliased pci bus and devfn in the HostIOMMUDevice
- Use those for resv regions computation
- Remove VirtioHostIOMMUDevice and simply use the base objectv2 -> v3:
- moved the series from RFC to patch
- collected Zhenzhong's R-bs and took into account most of his comments (see replies on v2)Eric Auger (8):
HostIOMMUDevice: Store the VFIO/VDPA agent
virtio-iommu: Implement set|unset]_iommu_device() callbacks
HostIOMMUDevice: Introduceget_iova_rangescallback
HostIOMMUDevice: Store the aliased bus and devfn
virtio-iommu: Compute host reserved regions
virtio-iommu: Remove the implementation ofiommu_set_iova_rangehw/vfio: Removememory_region_iommu_set_iova_ranges()call
memory: Remove IOMMU MRiommu_set_iova_rangeAPI
Quoted 11 lines
include/exec/memory.h | 32 ---- include/hw/virtio/virtio-iommu.h | 2 + include/sysemu/host_iommu_device.h | 11 ++ hw/pci/pci.c | 8 +- hw/vfio/common.c | 10 - hw/vfio/container.c | 17 ++ hw/vfio/iommufd.c | 18 ++ hw/virtio/virtio-iommu.c | 296 +++++++++++++++++++---------- system/memory.c | 13 -- 9 files changed, 249 insertions(+), 158 deletions(-)
Applied to vfio-next.
Thanks,
C.