VIRTIO-IOMMU/VFIO: Don't assume 64b IOVA space
cover VIRTIO-IOMMU/VFIO: Don't assume 64b IOVA space
On x86, when assigning VFIO-PCI devices protected with virtio-iommu we encounter the case where the guest tries to map IOVAs beyond 48b whereas the physical VTD IOMMU only supports 48b. This ends up with VFIO_MAP_DMA failures at qemu level because at kernel level, vfio_iommu_iova_dma_valid() check returns false on vfio_map_do_map().
This is due to the fact the virtio-iommu currently unconditionally exposes an IOVA range of 64b through its config input range fields.
This series removes this assumption by retrieving the usable IOVA regions through the VFIO_IOMMU_TYPE1_INFO_CAP_IOVA_RANGE UAPI when a VFIO device is attached. This info is communicated to the virtio-iommu memory region, transformed into the inversed info, ie. the host reserved IOVA regions. Then those latter are combined with the reserved IOVA regions set though the virtio-iommu reserved-regions property. That way, the guest virtio-iommu driver, unchanged, is able to probe the whole set of reserved regions and prevent any IOVA belonging to those ranges from beeing used, achieving the original goal.
Best Regards
Eric
This series can be found at:
https://github.com/eauger/qemu/tree/virtio-iommu_geometry_v1
Eric Auger (13):
memory: Let ReservedRegion use Range
memory: Introduce memory_region_iommu_set_iova_ranges
vfio: Collect container iova range info
virtio-iommu: Rename reserved_regions into prop_resv_regions
virtio-iommu: Introduce per IOMMUDevice reserved regions
range: Introduce range_inverse_array()
virtio-iommu: Implement set_iova_ranges() callback
range: Make range_compare() public
util/reserved-region: Add new ReservedRegion helpers
virtio-iommu: Consolidate host reserved regions and property set ones
test: Add some tests for range and resv-mem helpers
virtio-iommu: Resize memory region according to the max iova info
vfio: Remove 64-bit IOVA address space assumption
include/exec/memory.h | 30 ++++- include/hw/vfio/vfio-common.h | 2 + include/hw/virtio/virtio-iommu.h | 7 +- include/qemu/range.h | 9 ++ include/qemu/reserved-region.h | 32 +++++ hw/core/qdev-properties-system.c | 9 +- hw/vfio/common.c | 70 ++++++++--- hw/virtio/virtio-iommu-pci.c | 8 +- hw/virtio/virtio-iommu.c | 85 +++++++++++-- softmmu/memory.c | 15 +++ tests/unit/test-resv-mem.c | 198 +++++++++++++++++++++++++++++++ util/range.c | 41 ++++++- util/reserved-region.c | 94 +++++++++++++++ hw/virtio/trace-events | 1 + tests/unit/meson.build | 1 + util/meson.build | 1 + 16 files changed, 562 insertions(+), 41 deletions(-) create mode 100644 include/qemu/reserved-region.h create mode 100644 tests/unit/test-resv-mem.c create mode 100644 util/reserved-region.c
Signature · 1 line
2.41.0
PATCH 1/13 memory: Let ReservedRegion use Range
A reserved region is a range tagged with a type. Let's directly use the Range type in the prospect to reuse some of the library helpers shipped with the Range type.
--- include/exec/memory.h | 4 ++-- hw/core/qdev-properties-system.c | 9 ++++++--- hw/virtio/virtio-iommu.c | 6 +++--- 3 files changed, 11 insertions(+), 8 deletions(-)
include/exec/memory.h +2 -2
@@ -24,6 +24,7 @@#include "qemu/bswap.h" #include "qemu/queue.h" #include "qemu/int128.h" +#include "qemu/range.h" #include "qemu/notify.h" #include "qom/object.h" #include "qemu/rcu.h"
@@ -79,8 +80,7 @@ extern unsigned int global_dirty_tracking;typedef struct MemoryRegionOps MemoryRegionOps ;struct ReservedRegion { -hwaddr low; -hwaddr high; +Range range;unsigned type; };
hw/core/qdev-properties-system.c +6 -3
@@ -699,7 +699,7 @@ static void get_reserved_region(Object *obj, Visitor *v, const char *name,int rc; rc =snprintf (buffer,sizeof (buffer),"0x%" PRIx64":0x%" PRIx64":%u" , - rr->low, rr->high, rr->type); +range_lob (&rr->range),range_upb (&rr->range), rr->type);assert (rc <sizeof (buffer));visit_type_str (v, name, &p, errp);
@@ -711,6 +711,7 @@ static void set_reserved_region(Object *obj, Visitor *v, const char *name,Property *prop = opaque;ReservedRegion *rr =object_field_prop_ptr (obj, prop);const char *endptr; +uint64_t lob, upb;char *str;int ret;
@@ -718,7 +719,7 @@ static void set_reserved_region(Object *obj, Visitor *v, const char *name,return ; } - ret =qemu_strtou64 (str, &endptr,16 , &rr->low); + ret =qemu_strtou64 (str, &endptr,16 , &lob);if (ret) {error_setg (errp,"start address of '%s'" " must be a hexadecimal integer" , name);
@@ -728,7 +729,7 @@ static void set_reserved_region(Object *obj, Visitor *v, const char *name, goto separator_error; } - ret =qemu_strtou64 (endptr +1 , &endptr,16 , &rr->high); + ret =qemu_strtou64 (endptr +1 , &endptr,16 , &upb);if (ret) {error_setg (errp,"end address of '%s'" " must be a hexadecimal integer" , name);
@@ -738,6 +739,8 @@ static void set_reserved_region(Object *obj, Visitor *v, const char *name, goto separator_error; } +range_set_bounds (&rr->range, lob, upb); + ret =qemu_strtoui (endptr +1 , &endptr,10 , &rr->type);if (ret) { error_setg(errp, "type of '%s'"
hw/virtio/virtio-iommu.c +3 -3
@@ -645,8 +645,8 @@ static ssize_t virtio_iommu_fill_resv_mem_prop(VirtIOIOMMU *s, uint32_t ep, prop.head.type =cpu_to_le16 (VIRTIO_IOMMU_PROBE_T_RESV_MEM ); prop.head.length =cpu_to_le16 (length); prop.subtype = subtype; - prop.start =cpu_to_le64 (s->reserved_regions[i].low); - prop.end =cpu_to_le64 (s->reserved_regions[i].high); + prop.start =cpu_to_le64 (range_lob (&s->reserved_regions[i].range)); + prop.end =cpu_to_le64 (range_upb (&s->reserved_regions[i].range));memcpy (buf, &prop, size);
@@ -897,7 +897,7 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemoryRegion *mr, hwaddr addr,for (i =0 ; i < s->nb_reserved_regions; i++) {ReservedRegion *reg = &s->reserved_regions[i]; -if (addr >= reg->low && addr <= reg->high) { +if (range_contains (®->range, addr)) {switch (reg->type) {case VIRTIO_IOMMU_RESV_MEM_T_MSI : entry.perm = flag;
Signature · 1 line
2.41.0
Re: [PATCH 01/13] memory: Let ReservedRegion use Range
Quoted 10 lines · Eric Auger
A reserved region is a range tagged with a type. Let's directly use the Range type in the prospect to reuse some of the library helpers shipped with the Range type.
Signed-off-by Eric Auger <[email protected]>--- include/exec/memory.h | 4 ++-- hw/core/qdev-properties-system.c | 9 ++++++--- hw/virtio/virtio-iommu.c | 6 +++--- 3 files changed, 11 insertions(+), 8 deletions(-)
TIL "qemu/range.h" :)
Re: [PATCH 01/13] memory: Let ReservedRegion use Range
Quoted 5 lines · Philippe Mathieu-Daudé
Quoted 4 lines · Eric Auger
A reserved region is a range tagged with a type. Let's directly use the Range type in the prospect to reuse some of the library helpers shipped with the Range type.
Quoted 8 lines
Quoted 6 lines
Signed-off-by Eric Auger <[email protected]>---
include/exec/memory.h | 4 ++--
hw/core/qdev-properties-system.c| 9 ++++++---
hw/virtio/virtio-iommu.c | 6 +++---3 files changed, 11 insertions(+), 8 deletions(-)TIL "
qemu/range.h" :)
I did as well and on top of that I learnt "TIL" :)
Quoted 2 lines
Reviewed-by Philippe Mathieu-Daudé <[email protected]>
Thanks!
Eric
Re: [PATCH 01/13] memory: Let ReservedRegion use Range
In reply to Eric AugerQuoted 11 lines · Eric Auger
A reserved region is a range tagged with a type. Let's directly use the Range type in the prospect to reuse some of the library helpers shipped with the Range type.
Signed-off-by Eric Auger <[email protected]>--- include/exec/memory.h | 4 ++-- hw/core/qdev-properties-system.c | 9 ++++++--- hw/virtio/virtio-iommu.c | 6 +++--- 3 files changed, 11 insertions(+), 8 deletions(-)
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH 01/13] memory: Let ReservedRegion use Range
In reply to Eric AugerQuoted 5 lines · Eric Auger
A reserved region is a range tagged with a type. Let's directly use the Range type in the prospect to reuse some of the library helpers shipped with the Range type.
Signed-off-by Eric Auger <[email protected]>
Signature · 1 line
Peter Xu
PATCH 2/13 memory: Introduce memory_region_iommu_set_iova_ranges
In reply to Eric AugerThis helper will allow to convey information about valid IOVA ranges to virtual IOMMUS.
--- include/exec/memory.h | 26 ++++++++++++++++++++++++++ softmmu/memory.c | 15 +++++++++++++++ 2 files changed, 41 insertions(+)
include/exec/memory.h +26 -0
@@ -519,6 +519,27 @@ 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. +* +* @nr_ranges: number of IOVA ranges +* +* @iova_ranges: an array of @nr_ranges usable IOVA ranges +* +* 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, +uint32_t nr_ranges, +struct Range *iova_ranges, +Error **errp); };typedef struct RamDiscardListener RamDiscardListener ;
@@ -1845,6 +1866,11 @@ int memory_region_iommu_set_page_size_mask(IOMMUMemoryRegion *iommu_mr,uint64_t page_size_mask, Error **errp); +int memory_region_iommu_set_iova_ranges (IOMMUMemoryRegion *iommu, +uint32_t nr_ranges, +struct Range *iova_ranges, +Error **errp); +/** * memory_region_name: get a memory region's name *
softmmu/memory.c +15 -0
@@ -1905,6 +1905,21 @@ int memory_region_iommu_set_page_size_mask(IOMMUMemoryRegion *iommu_mr,return ret; } +int memory_region_iommu_set_iova_ranges (IOMMUMemoryRegion *iommu_mr, +uint32_t nr_ranges, +struct Range *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, nr_ranges, + iova_ranges, errp); + } +return ret; +} +int memory_region_register_iommu_notifier (MemoryRegion *mr,IOMMUNotifier *n,Error **errp) {
Signature · 1 line
2.41.0
Re: [PATCH 02/13] memory: Introduce memory_region_iommu_set_iova_ranges
Quoted 4 lines · Eric Auger
This helper will allow to convey information about valid IOVA ranges to virtual IOMMUS.
Signed-off-by Eric Auger <[email protected]>
Signature · 1 line
Peter Xu
Re: [PATCH 02/13] memory: Introduce memory_region_iommu_set_iova_ranges
In reply to Eric AugerQuoted 69 lines · Eric Auger
This helper will allow to convey information about valid IOVA ranges to virtual IOMMUS.
Signed-off-by Eric Auger <[email protected]>--- include/exec/memory.h | 26 ++++++++++++++++++++++++++ softmmu/memory.c | 15 +++++++++++++++ 2 files changed, 41 insertions(+)include/exec/memory.h +26
-0@@ -519,6 +519,27 @@ 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. +* +* @nr_ranges: number of IOVA ranges +* +* @iova_ranges: an array of @nr_ranges usable IOVA ranges +* +* 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, +uint32_t nr_ranges, +struct Range *iova_ranges, +Error **errp); };typedef struct RamDiscardListener RamDiscardListener ;@@ -1845,6 +1866,11 @@ int memory_region_iommu_set_page_size_mask(IOMMUMemoryRegion *iommu_mr,uint64_t page_size_mask, Error **errp); +int memory_region_iommu_set_iova_ranges (IOMMUMemoryRegion *iommu, +uint32_t nr_ranges, +struct Range *iova_ranges, +Error **errp); +/** * memory_region_name: get a memory region's name * softmmu/memory.c +8
-0@@ -1905,6 +1905,21 @@ int memory_region_iommu_set_page_size_mask(IOMMUMemoryRegion *iommu_mr,return ret; } +int memory_region_iommu_set_iova_ranges (IOMMUMemoryRegion *iommu_mr, +uint32_t nr_ranges, +struct Range *iova_ranges, +Error **errp) +{ +IOMMUMemoryRegionClass *imrc =IOMMU_MEMORY_REGION_GET_CLASS (iommu_mr); +int ret =0 ; +
softmmu/memory.c int memory_region_iommu_set_page_size_mask(IOMMUMemoryRegion *iommu_mr, · line 1925
Quoted 10 lines
softmmu/memory.c +7
-0+if (imrc->iommu_set_iova_ranges) { + ret = imrc->iommu_set_iova_ranges (iommu_mr, nr_ranges, + iova_ranges, errp); + } +return ret; +} +int memory_region_register_iommu_notifier (MemoryRegion *mr,IOMMUNotifier *n,Error **errp) {
Signature · 3 lines
Cheers, David / dhildenb
PATCH 3/13 vfio: Collect container iova range info
In reply to Eric AugerCollect iova range information if VFIO_IOMMU_TYPE1_INFO_CAP_IOVA_RANGE capability is supported.
This allows to propagate the information though the IOMMU MR set_iova_ranges() callback so that virtual IOMMUs get aware of those aperture constraints.
--- include/hw/vfio/vfio-common.h | 2 ++ hw/vfio/common.c | 45 +++++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-)
include/hw/vfio/vfio-common.h +2 -0
@@ -98,6 +98,8 @@ typedef struct VFIOContainer { QLIST_HEAD(, VFIOGroup) group_list; QLIST_HEAD(, VFIORamDiscardListener) vrdl_list;QLIST_ENTRY (VFIOContainer) next; +unsigned nr_iovas; +struct vfio_iova_range *iova_ranges; } VFIOContainer; typedef struct VFIOGuestIOMMU {
hw/vfio/common.c +43 -2
81 lines not on this page · raw message
Signature · 1 line
2.41.0
PATCH 4/13 virtio-iommu: Rename reserved_regions into prop_resv_regions
In reply to Eric AugerRename VirtIOIOMMU (nb_)reserved_regions fields with the "prop_" prefix to highlight those fields are set through a property, at machine level. They are IOMMU wide.
A subsequent patch will introduce per IOMMUDevice reserved regions that will include both those IOMMU wide property reserved regions plus, sometimes, host reserved regions, if the device is backed by a host device protected by a physical IOMMU. Also change nb_ prefix by nr_.
--- include/hw/virtio/virtio-iommu.h | 4 ++-- hw/virtio/virtio-iommu-pci.c | 8 ++++---- hw/virtio/virtio-iommu.c | 15 ++++++++------- 3 files changed, 14 insertions(+), 13 deletions(-)
include/hw/virtio/virtio-iommu.h +2 -2
@@ -55,8 +55,8 @@ struct VirtIOIOMMU {GHashTable *as_by_busptr;IOMMUPciBus *iommu_pcibus_by_bus_num[PCI_BUS_MAX ];PCIBus *primary_bus; -ReservedRegion *reserved_regions; -uint32_t nb_reserved_regions; +ReservedRegion *prop_resv_regions; +uint32_t nr_prop_resv_regions;GTree *domains;QemuRecMutex mutex;GTree *endpoints;
hw/virtio/virtio-iommu-pci.c +4 -4
@@ -37,7 +37,7 @@ struct VirtIOIOMMUPCI {static Property virtio_iommu_pci_properties[] = {DEFINE_PROP_UINT32 ("class" , VirtIOPCIProxy, class_code,0 ),DEFINE_PROP_ARRAY ("reserved-regions" , VirtIOIOMMUPCI, - vdev.nb_reserved_regions, vdev.reserved_regions, + vdev.nr_prop_resv_regions, vdev.prop_resv_regions, qdev_prop_reserved_region, ReservedRegion),DEFINE_PROP_END_OF_LIST (), };
@@ -54,9 +54,9 @@ static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) "for the virtio-iommu-pci device");return ; } -for (int i =0 ; i < s->nb_reserved_regions; i++) { -if (s->reserved_regions[i].type !=VIRTIO_IOMMU_RESV_MEM_T_RESERVED && - s->reserved_regions[i].type !=VIRTIO_IOMMU_RESV_MEM_T_MSI ) { +for (int i =0 ; i < s->nr_prop_resv_regions; i++) { +if (s->prop_resv_regions[i].type !=VIRTIO_IOMMU_RESV_MEM_T_RESERVED && + s->prop_resv_regions[i].type !=VIRTIO_IOMMU_RESV_MEM_T_MSI ) {error_setg (errp,"reserved region %d has an invalid type" , i);error_append_hint (errp,"Valid values are 0 and 1\n" );return ;
hw/virtio/virtio-iommu.c +8 -7
@@ -631,22 +631,23 @@ static ssize_t virtio_iommu_fill_resv_mem_prop(VirtIOIOMMU *s, uint32_t ep,size_t size =sizeof (prop), length = size -sizeof (prop.head), total;int i; - total = size * s->nb_reserved_regions; + total = size * s->nr_prop_resv_regions;if (total > free) {return -ENOSPC ; } -for (i =0 ; i < s->nb_reserved_regions; i++) { -unsigned subtype = s->reserved_regions[i].type; +for (i =0 ; i < s->nr_prop_resv_regions; i++) { +unsigned subtype = s->prop_resv_regions[i].type; +Range *range = &s->prop_resv_regions[i].range;assert (subtype ==VIRTIO_IOMMU_RESV_MEM_T_RESERVED || subtype ==VIRTIO_IOMMU_RESV_MEM_T_MSI ); prop.head.type =cpu_to_le16 (VIRTIO_IOMMU_PROBE_T_RESV_MEM ); prop.head.length =cpu_to_le16 (length); prop.subtype = subtype; - prop.start =cpu_to_le64 (range_lob (&s->reserved_regions[i].range)); - prop.end =cpu_to_le64 (range_upb (&s->reserved_regions[i].range)); + prop.start =cpu_to_le64 (range_lob (range)); + prop.end =cpu_to_le64 (range_upb (range));memcpy (buf, &prop, size);
@@ -894,8 +895,8 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemoryRegion *mr, hwaddr addr, goto unlock; } -for (i =0 ; i < s->nb_reserved_regions; i++) { -ReservedRegion *reg = &s->reserved_regions[i]; +for (i =0 ; i < s->nr_prop_resv_regions; i++) { +ReservedRegion *reg = &s->prop_resv_regions[i];if (range_contains (®->range, addr)) {switch (reg->type) {
Signature · 1 line
2.41.0
PATCH 5/13 virtio-iommu: Introduce per IOMMUDevice reserved regions
In reply to Eric AugerFor the time being the per device reserved regions are just a duplicate of IOMMU wide reserved regions. Subsequent patches will combine those with host reserved regions, if any.
--- include/hw/virtio/virtio-iommu.h | 1 + hw/virtio/virtio-iommu.c | 42 ++++++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 8 deletions(-)
include/hw/virtio/virtio-iommu.h +1 -0
@@ -39,6 +39,7 @@ typedef struct IOMMUDevice {AddressSpace as;MemoryRegion root;/* The root container of the device */ MemoryRegion bypass_mr;/* The alias of shared memory MR */ +GList *resv_regions; } IOMMUDevice; typedef struct IOMMUPciBus {
hw/virtio/virtio-iommu.c +34 -8
71 lines not on this page · raw message
Signature · 1 line
2.41.0
PATCH 6/13 range: Introduce range_inverse_array()
In reply to Eric AugerThis helper reverses an array of regions, turning original regions into holes and original holes into actual regions, covering the whole UINT64_MAX span.
--- include/qemu/range.h | 3 +++ util/range.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+)
include/qemu/range.h +3 -0
@@ -219,4 +219,7 @@ static inline int ranges_overlap(uint64_t first1, uint64_t len1,GList *range_list_insert (GList *list,Range *data); +void range_inverse_array (uint32_t nr_ranges,Range *ranges, +uint32_t *nr_inv_ranges,Range **inv_ranges); +#endif
util/range.c +35 -0
@@ -70,3 +70,38 @@ GList *range_list_insert(GList *list, Range *data)return list; } + +/* +* Inverse an array of sorted ranges over the UINT64_MAX span, ie. +* original ranges becomes holes in the newly allocated inv_ranges +*/ +void range_inverse_array (uint32_t nr_ranges,Range *ranges, +uint32_t *nr_inv_ranges,Range **inv_ranges) +{ +Range *resv; +int i =0 , j =0 ; + + resv =g_malloc0_n (nr_ranges +1 ,sizeof (Range)); + +/* first range lob is greater than 0, insert a first range */ +if (range_lob (&ranges[0 ]) >0 ) { +range_set_bounds (&resv[i++],0 , +range_lob (&ranges[0 ]) -1 ); + } + +/* insert a range inbetween each original range */ +for (; j < nr_ranges -1 ; j++) { +if (range_compare (&ranges[j], &ranges[j +1 ])) { +range_set_bounds (&resv[i++],range_upb (&ranges[j]) +1 , +range_lob (&ranges[j +1 ]) -1 ); + } + } +/* last range upb is less than UINT64_MAX, insert a last range */ +if (range_upb (&ranges[nr_ranges -1 ]) <UINT64_MAX ) { +range_set_bounds (&resv[i++], +range_upb (&ranges[nr_ranges -1 ]) +1 ,UINT64_MAX ); + } + *nr_inv_ranges = i; + resv =g_realloc (resv, i *sizeof (Range)); + *inv_ranges = resv; +}
Signature · 1 line
2.41.0
Re: [PATCH 06/13] range: Introduce range_inverse_array()
Quoted 27 lines · Eric Auger
This helper reverses an array of regions, turning original regions into holes and original holes into actual regions, covering the whole
UINT64_MAXspan.Signed-off-by Eric Auger <[email protected]>--- include/qemu/range.h | 3 +++ util/range.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+)include/qemu/range.h +3
-0@@ -219,4 +219,7 @@ static inline int ranges_overlap(uint64_t first1, uint64_t len1,GList *range_list_insert (GList *list,Range *data); +void range_inverse_array (uint32_t nr_ranges,Range *ranges, +uint32_t *nr_inv_ranges,Range **inv_ranges); +#endif util/range.c +0
-0
util/range.c GList *range_list_insert(GList *list, Range *data) · line 77
Quoted 8 lines
util/range.c +5
-0@@ -70,3 +70,38 @@ GList *range_list_insert(GList *list, Range *data)return list; } + +/* +* Inverse an array of sorted ranges over the UINT64_MAX span, ie. +* original ranges becomes holes in the newly allocated inv_ranges +*/
Most of the functions are described in the header; could you move this description with the declaration?
Quoted 12 lines
util/range.c +12
-0+void range_inverse_array (uint32_t nr_ranges,Range *ranges, +uint32_t *nr_inv_ranges,Range **inv_ranges) +{ +Range *resv; +int i =0 , j =0 ; + + resv =g_malloc0_n (nr_ranges +1 ,sizeof (Range)); + +/* first range lob is greater than 0, insert a first range */ +if (range_lob (&ranges[0 ]) >0 ) { +range_set_bounds (&resv[i++],0 , +range_lob (&ranges[0 ]) -1 );
util/range.c GList *range_list_insert(GList *list, Range *data) · line 99
Quoted 10 lines
util/range.c +10
-0+ } + +/* insert a range inbetween each original range */ +for (; j < nr_ranges -1 ; j++) { +if (range_compare (&ranges[j], &ranges[j +1 ])) { +range_set_bounds (&resv[i++],range_upb (&ranges[j]) +1 , +range_lob (&ranges[j +1 ]) -1 ); + } + } +/* last range upb is less than UINT64_MAX, insert a last range */
In order to use this new function with variable range sizes, can we pass UINT64_MAX as an 'inv_range_upb' argument?
Quoted 8 lines
util/range.c +8
-0+if (range_upb (&ranges[nr_ranges -1 ]) <UINT64_MAX ) { +range_set_bounds (&resv[i++], +range_upb (&ranges[nr_ranges -1 ]) +1 ,UINT64_MAX ); + } + *nr_inv_ranges = i; + resv =g_realloc (resv, i *sizeof (Range)); + *inv_ranges = resv; +}
Re: [PATCH 06/13] range: Introduce range_inverse_array()
Hi Philippe,
Quoted 29 lines · Philippe Mathieu-Daudé
Quoted 28 lines · Eric Auger
This helper reverses an array of regions, turning original regions into holes and original holes into actual regions, covering the whole
UINT64_MAXspan.Signed-off-by Eric Auger <[email protected]>---
include/qemu/range.h| 3 +++
util/range.c | 35 +++++++++++++++++++++++++++++++++++2 files changed, 38 insertions(+)include/qemu/range.h +2
-0@@ -219,4 +219,7 @@ static inline int ranges_overlap(uint64_t first1,uint64_t len1, GList *range_list_insert(GList *list, Range *data); +void range_inverse_array(uint32_t nr_ranges, Range *ranges, + uint32_t *nr_inv_ranges, Range **inv_ranges); + #endifutil/range.c +0
-0@@ -70,3 +70,38 @@ GList *range_list_insert(GList *list, Range *data) return list; }
Quoted 8 lines
util/range.c GList *range_list_insert(GList *list, Range *data) · line 77
Quoted 5 lines
util/range.c +5
-0+ +/* +* Inverse an array of sorted ranges over the UINT64_MAX span, ie. +* original ranges becomes holes in the newly allocated inv_ranges +*/ Most of the functions are described in the header; could you move this description with the declaration?
this is the case for all static inline primitives but not for range_list_insert(), hence that choice. Now I don't have a strong opinion.
Quoted 18 lines
Quoted 18 lines
util/range.c +17
-0+void range_inverse_array (uint32_t nr_ranges,Range *ranges, + uint32_t *nr_inv_ranges, Range **inv_ranges) +{ + Range *resv; + int i = 0, j = 0; + + resv = g_malloc0_n(nr_ranges + 1, sizeof(Range)); + + /* first range lob is greater than 0, insert a first range */ + if (range_lob(&ranges[0]) > 0) { + range_set_bounds(&resv[i++], 0, + range_lob(&ranges[0]) - 1); + } + + /* insert a range inbetween each original range */ + for (; j < nr_ranges - 1; j++) { + if (range_compare(&ranges[j], &ranges[j + 1])) {
Quoted 8 lines
util/range.c GList *range_list_insert(GList *list, Range *data) · line 99
Quoted 5 lines
util/range.c +5
-0+ range_set_bounds(&resv[i++], range_upb(&ranges[j]) + 1, + range_lob(&ranges[j + 1]) - 1); + } + } + /* last range upb is less than UINT64_MAX, insert a last range */In order to use this new function with variable range sizes, can we pass
UINT64_MAXas an 'inv_range_upb' argument?
Indeed I hesitated to bring enhanced comodity by letting the caller pass the upper bound and allow values less than UINT64_MAX. But I was afraid this would complexify the implementation, hence the current choice. I will have a look & see.
Thanks
Eric
Quoted 11 lines
Quoted 10 lines
util/range.c +8
-0+ if (range_upb(&ranges[nr_ranges - 1]) < UINT64_MAX) { + range_set_bounds(&resv[i++], + range_upb(&ranges[nr_ranges - 1]) + 1,UINT64_MAX ); + } + *nr_inv_ranges = i; + resv = g_realloc(resv, i * sizeof(Range)); + *inv_ranges = resv; +}
PATCH 7/13 virtio-iommu: Implement set_iova_ranges() callback
In reply to Eric AugerThe implementation populates the array of per IOMMUDevice host reserved regions.
--- include/hw/virtio/virtio-iommu.h | 2 ++ hw/virtio/virtio-iommu.c | 17 +++++++++++++++++ 2 files changed, 19 insertions(+)
include/hw/virtio/virtio-iommu.h +2 -0
@@ -40,6 +40,8 @@ typedef struct IOMMUDevice {MemoryRegion root;/* The root container of the device */ MemoryRegion bypass_mr;/* The alias of shared memory MR */ GList *resv_regions; +Range *host_resv_regions; +uint32_t nr_host_resv_regions; } IOMMUDevice; typedef struct IOMMUPciBus {
hw/virtio/virtio-iommu.c +17 -0
@@ -20,6 +20,7 @@#include "qemu/osdep.h" #include "qemu/log.h" #include "qemu/iov.h" +#include "qemu/range.h" #include "exec/target_page.h" #include "hw/qdev-properties.h" #include "hw/virtio/virtio.h"
@@ -1158,6 +1159,21 @@ static int virtio_iommu_set_page_size_mask(IOMMUMemoryRegion *mr,return 0 ; } +static int virtio_iommu_set_iova_ranges (IOMMUMemoryRegion *mr, +uint32_t nr_ranges, +struct Range *iova_ranges, +Error **errp) +{ +IOMMUDevice *sdev =container_of (mr, IOMMUDevice, iommu_mr); +assert (nr_ranges); + + +range_inverse_array (nr_ranges, iova_ranges, + &sdev->nr_host_resv_regions, &sdev->host_resv_regions); + +return 0 ; +} +static void virtio_iommu_system_reset (void *opaque) {VirtIOIOMMU *s = opaque;
@@ -1453,6 +1469,7 @@ 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 8/13 range: Make range_compare() public
In reply to Eric AugerLet's expose range_compare() in the header so that it can be reused outside of util/range.c
--- include/qemu/range.h | 6 ++++++ util/range.c | 6 +----- 2 files changed, 7 insertions(+), 5 deletions(-)
include/qemu/range.h +6 -0
@@ -217,6 +217,12 @@ static inline int ranges_overlap(uint64_t first1, uint64_t len1,return !(last2 < first1 || last1 < first2); } +/* +* Return -1 if @a < @b, 1 @a > @b, and 0 if they touch or overlap. +* Both @a and @b must not be empty. +*/ +int range_compare (Range *a,Range *b); +GList *range_list_insert (GList *list,Range *data); void range_inverse_array(uint32_t nr_ranges, Range *ranges,
util/range.c +1 -5
@@ -20,11 +20,7 @@#include "qemu/osdep.h" #include "qemu/range.h" -/* -* Return -1 if @a < @b, 1 @a > @b, and 0 if they touch or overlap. -* Both @a and @b must not be empty. -*/ -static inline int range_compare (Range *a,Range *b) +int range_compare (Range *a,Range *b) {assert (!range_is_empty (a) && !range_is_empty (b));
Signature · 1 line
2.41.0
Re: [PATCH 08/13] range: Make range_compare() public
Quoted 8 lines · Eric Auger
Let's expose
range_compare()in the header so that it can be reused outside ofutil/range.cSigned-off-by Eric Auger <[email protected]>--- include/qemu/range.h | 6 ++++++ util/range.c | 6 +----- 2 files changed, 7 insertions(+), 5 deletions(-)
PATCH 9/13 util/reserved-region: Add new ReservedRegion helpers
In reply to Eric AugerIntroduce resv_region_list_insert() helper which inserts a new ReservedRegion into a sorted list of reserved region. In case of overlap, the new region has higher priority and hides the existing overlapped segments. If the overlap is partial, new regions are created for parts which are not overlapped. The new region has higher priority independently on the type of the regions.
--- include/qemu/reserved-region.h | 32 ++++++++++++ util/reserved-region.c | 94 ++++++++++++++++++++++++++++++++++ util/meson.build | 1 + 3 files changed, 127 insertions(+) create mode 100644 include/qemu/reserved-region.h create mode 100644 util/reserved-region.c
include/qemu/reserved-region.h new file +32 -0
@@ -0,0 +1,32 @@ +/* +* QEMU ReservedRegion helpers +* +* Copyright (c) 2023 Red Hat, Inc. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public +* License as published by the Free Software Foundation; either +* version 2 of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef QEMU_RESERVED_REGION_H +#define QEMU_RESERVED_REGION_H + +#include "exec/memory.h" + +/* +* Insert a new region into a sorted list of reserved regions. In case +* there is overlap with existing regions, the new added region has +* higher priority and replaces the overlapped segment. +*/ +GList *resv_region_list_insert (GList *list,ReservedRegion *reg); + +#endif
util/reserved-region.c new file +94 -0
94 lines not on this page · raw message
util/meson.build +1 -0
@@ -52,6 +52,7 @@ util_ss.add(files('qdist.c')) util_ss.add(files('qht.c')) util_ss.add(files('qsp.c')) util_ss.add(files('range.c')) +util_ss.add(files('reserved-region.c')) util_ss.add(files('stats64.c')) util_ss.add(files('systemd.c')) util_ss.add(files('transactions.c'))
Signature · 1 line
2.41.0
PATCH 10/13 virtio-iommu: Consolidate host reserved regions and property set ones
In reply to Eric AugerUp to now we were exposing to the RESV_MEM probe requests the reserved memory regions set though the reserved-regions array property.
Combine those with the host reserved memory regions if any. Those latter are tagged as RESERVED. We don't have more information about them besides then cannot be mapped. Reserved regions set by property have higher priority.
--- hw/virtio/virtio-iommu.c | 17 ++++++++++++++++- hw/virtio/trace-events | 1 + 2 files changed, 17 insertions(+), 1 deletion(-)
hw/virtio/virtio-iommu.c +16 -1
@@ -21,6 +21,7 @@#include "qemu/log.h" #include "qemu/iov.h" #include "qemu/range.h" +#include "qemu/reserved-region.h" #include "exec/target_page.h" #include "hw/qdev-properties.h" #include "hw/virtio/virtio.h"
@@ -630,11 +631,25 @@ static int consolidate_resv_regions(IOMMUDevice *sdev)VirtIOIOMMU *s = sdev->viommu;int i; +/* First add host reserved regions if any, all tagged as RESERVED */ +for (i =0 ; i < sdev->nr_host_resv_regions; i++) { +ReservedRegion *reg =g_new0 (ReservedRegion,1 ); + reg->range = sdev->host_resv_regions[i]; + reg->type =VIRTIO_IOMMU_RESV_MEM_T_RESERVED ; + 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)); + } +/* +* then add higher priority reserved regions set through properties by the +* machine +*/ for (i =0 ; i < s->nr_prop_resv_regions; i++) {ReservedRegion *reg =g_new0 (ReservedRegion,1 ); *reg = s->prop_resv_regions[i]; - sdev->resv_regions =g_list_append (sdev->resv_regions, reg); + sdev->resv_regions =resv_region_list_insert (sdev->resv_regions, reg); }return 0 ; }
hw/virtio/trace-events +1 -0
@@ -134,6 +134,7 @@ virtio_iommu_notify_flag_add(const char *name) "add notifier to mr %s" virtio_iommu_notify_flag_del(const char *name) "del notifier from mr %s" virtio_iommu_switch_address_space(uint8_t bus, uint8_t slot, uint8_t fn, bool on) "Device %02x:%02x.%x switching address space (iommu enabled=%d)" virtio_iommu_freeze_granule(uint64_t page_size_mask) "granule set to 0x%"PRIx64 +virtio_iommu_host_resv_regions(const char *name, uint32_t index, uint64_t lob, uint64_t upb) "mr=%s host-resv-reg[%d] = [0x%"PRIx64",0x%"PRIx64"]" # virtio-mem.c virtio_mem_send_response(uint16_t type) "type=%" PRIu16
Signature · 1 line
2.41.0
PATCH 11/13 test: Add some tests for range and resv-mem helpers
In reply to Eric AugerAdd unit tests for both resv_region_list_insert() and range_inverse_array().
--- tests/unit/test-resv-mem.c | 198 +++++++++++++++++++++++++++++++++++++ tests/unit/meson.build | 1 + 2 files changed, 199 insertions(+) create mode 100644 tests/unit/test-resv-mem.c
tests/unit/test-resv-mem.c new file +198 -0
@@ -0,0 +1,198 @@ +/* +* SPDX-License-Identifier: GPL-2.0-or-later +* +* reserved-region/range.c unit-tests. +* +* Copyright (C) 2023, Red Hat, Inc. +* +* Author: Eric Auger <[email protected]> +*/ + +#include "qemu/osdep.h" +#include "qemu/range.h" +#include "exec/memory.h" +#include "qemu/reserved-region.h" + +#define DEBUG 0 + +static void print_rev_array (const char *prefix,uint32_t nr_rev, +Range *rev,uint32_t expected_nr_rev) +{ + g_assert_cmpint(nr_rev, ==, expected_nr_rev); +#if DEBUG +printf ("%s nr_rev=%d\n" , prefix, nr_rev); +for (int i =0 ; i < nr_rev; i++) { +printf ("%s rev[%i] = [0x%" PRIx64",0x%" PRIx64"]\n" , + prefix, i,range_lob (&rev[i]),range_upb (&rev[i])); + } +#endif +} + +static void check_range_reverse_array (void ) +{ +Range r[10 ]; +Range *rev; +uint32_t nr_rev; + +range_set_bounds (&r[0 ],0x10000 ,UINT64_MAX ); +range_inverse_array (1 , r, &nr_rev, &rev); +print_rev_array ("test1" , nr_rev, rev,1 ); +g_free (rev); + +range_set_bounds (&r[0 ],0x10000 ,0xFFFFFFFFFFFF ); +range_inverse_array (1 , r, &nr_rev, &rev); +print_rev_array ("test2" , nr_rev, rev,2 ); +g_free (rev); + +range_set_bounds (&r[0 ],0x0 ,0xFFFF ); +range_set_bounds (&r[1 ],0x10000 ,0x2FFFF ); +range_inverse_array (2 , r, &nr_rev, &rev); +print_rev_array ("test3" , nr_rev, rev,1 ); +g_free (rev); + +range_set_bounds (&r[0 ],0x50000 ,0x5FFFF ); +range_set_bounds (&r[1 ],0x60000 ,0xFFFFFFFFFFFF ); +range_inverse_array (2 , r, &nr_rev, &rev); +print_rev_array ("test4" , nr_rev, rev,2 ); +g_free (rev); + +range_set_bounds (&r[0 ],0x0 ,UINT64_MAX ); +range_inverse_array (1 , r, &nr_rev, &rev); +print_rev_array ("test5" , nr_rev, rev,0 ); +g_free (rev); +} + +static ReservedRegion *alloc_resv_mem (unsigned type,uint64_t lob,uint64_t upb) +{ +ReservedRegion *r; + + r =g_new0 (ReservedRegion,1 ); + r->type = type; +range_set_bounds (&r->range, lob, upb); +return r; +} + +static void print_resv_region_list (const char *prefix,GList *list, +uint32_t expected_length) +{ +int i =g_list_length (list); + + g_assert_cmpint(i, ==, expected_length); +#if DEBUG + i =0 ; +for (GList *l = list; l; l = l->next) { +ReservedRegion *r = (ReservedRegion *)l->data; +Range *range = &r->range; + +printf ("%s item[%d]=[0x%x, 0x%" PRIx64", 0x%" PRIx64"]\n" , + prefix, i++, r->type,range_lob (range),range_upb (range)); + } +#endif +} + +static void free_resv_region (gpointer data) +{ +ReservedRegion *reg = (ReservedRegion *)data; + +g_free (reg); +} + +static void check_resv_region_list_insert (void ) +{ +ReservedRegion *r[10 ]; +GList *l =NULL ; + + r[0 ] =alloc_resv_mem (0xA ,0 ,0xFFFF ); + r[1 ] =alloc_resv_mem (0xA ,0x20000 ,0x2FFFF ); + l =resv_region_list_insert (l, r[0 ]); + l =resv_region_list_insert (l, r[1 ]); +print_resv_region_list ("test1" , l,2 ); + +/* adjacent on left */ + r[2 ] =alloc_resv_mem (0xB ,0x0 ,0xFFF ); + l =resv_region_list_insert (l, r[2 ]); +/* adjacent on right */ + r[3 ] =alloc_resv_mem (0xC ,0x21000 ,0x2FFFF ); + l =resv_region_list_insert (l, r[3 ]); +print_resv_region_list ("test2" , l,4 ); + +/* exact overlap of D into C*/ + r[4 ] =alloc_resv_mem (0xD ,0x21000 ,0x2FFFF ); + l =resv_region_list_insert (l, r[4 ]); +print_resv_region_list ("test3" , l,4 ); + +/* in the middle */ + r[5 ] =alloc_resv_mem (0xE ,0x22000 ,0x23FFF ); + l =resv_region_list_insert (l, r[5 ]); +print_resv_region_list ("test4" , l,6 ); + +/* overwrites several existing ones */ + r[6 ] =alloc_resv_mem (0xF ,0x10000 ,0x2FFFF ); + l =resv_region_list_insert (l, r[6 ]); +print_resv_region_list ("test5" , l,3 ); + +/* contiguous at the end */ + r[7 ] =alloc_resv_mem (0x0 ,0x30000 ,0x40000 ); + l =resv_region_list_insert (l, r[7 ]); +print_resv_region_list ("test6" , l,4 ); + +g_list_free_full (l, free_resv_region); + l =NULL ; + + r[0 ] =alloc_resv_mem (0x0 ,0x10000 ,0x1FFFF ); + l =resv_region_list_insert (l, r[0 ]); +/* insertion before the 1st item */ + r[1 ] =alloc_resv_mem (0x1 ,0x0 ,0xFF ); + l =resv_region_list_insert (l, r[1 ]); +print_resv_region_list ("test8" , l,2 ); + +/* collision on the left side */ + r[2 ] =alloc_resv_mem (0xA ,0x1200 ,0x11FFF ); + l =resv_region_list_insert (l, r[2 ]); +print_resv_region_list ("test9" , l,3 ); + +/* collision on the right side */ + r[3 ] =alloc_resv_mem (0xA ,0x1F000 ,0x2FFFF ); + l =resv_region_list_insert (l, r[3 ]); +print_resv_region_list ("test10" , l,4 ); + +/* override everything */ + r[4 ] =alloc_resv_mem (0xF ,0x0 ,UINT64_MAX ); + l =resv_region_list_insert (l, r[4 ]); +print_resv_region_list ("test11" , l,1 ); + +g_list_free_full (l, free_resv_region); + l =NULL ; + + r[0 ] =alloc_resv_mem (0xF ,0x1000000000000 ,UINT64_MAX ); + l =resv_region_list_insert (l, r[0 ]); +print_resv_region_list ("test12" , l,1 ); + + r[1 ] =alloc_resv_mem (0xA ,0x0 ,0xFFFFFFF ); + l =resv_region_list_insert (l, r[1 ]); +print_resv_region_list ("test12" , l,2 ); + + r[2 ] =alloc_resv_mem (0xB ,0x100000000 ,0x1FFFFFFFF ); + l =resv_region_list_insert (l, r[2 ]); +print_resv_region_list ("test12" , l,3 ); + + r[3 ] =alloc_resv_mem (0x0 ,0x010000000 ,0x2FFFFFFFF ); + l =resv_region_list_insert (l, r[3 ]); +print_resv_region_list ("test12" , l,3 ); + +g_list_free_full (l, free_resv_region); +} + +int main (int argc,char **argv) +{ +g_test_init (&argc, &argv,NULL ); + +g_test_add_func ("/resv-mem/range_reverse_array" , + check_range_reverse_array); +g_test_add_func ("/resv-mem/resv_region_list_insert" , + check_resv_region_list_insert); + +g_test_run (); + +return 0 ; +}
tests/unit/meson.build +1 -0
@@ -21,6 +21,7 @@ tests = { 'test-opts-visitor': [testqapi], 'test-visitor-serialization': [testqapi], 'test-bitmap': [], + 'test-resv-mem': [], # all code tested by test-x86-cpuid is inside topology.h 'test-x86-cpuid': [], 'test-cutils': [],
Signature · 1 line
2.41.0
PATCH 12/13 virtio-iommu: Resize memory region according to the max iova info
In reply to Eric AugerBy default the virtio-iommu MR has a 64b span. As we intend to remove the VFIO assumption of 64b IOVA, let's make sure the MR is resized according to the actual GAW of the physical IOMMU. Otherwise we will get a failure on vfio vfio_find_hostwin().
--- hw/virtio/virtio-iommu.c | 6 ++++++ 1 file changed, 6 insertions(+)
hw/virtio/virtio-iommu.c +6 -0
@@ -1180,8 +1180,14 @@ static int virtio_iommu_set_iova_ranges(IOMMUMemoryRegion *mr, Error **errp) {IOMMUDevice *sdev =container_of (mr, IOMMUDevice, iommu_mr); +uint64_t max_iova; +assert (nr_ranges); + max_iova =range_upb (&iova_ranges[nr_ranges -1 ]); +if (max_iova <UINT64_MAX ) { +memory_region_set_size (&mr->parent_obj, max_iova +1 ); + }range_inverse_array (nr_ranges, iova_ranges, &sdev->nr_host_resv_regions, &sdev->host_resv_regions);
Signature · 1 line
2.41.0
Re: [PATCH 12/13] virtio-iommu: Resize memory region according to the max iova info
Quoted 9 lines · Eric Auger
By default the virtio-iommu MR has a 64b span. As we intend to remove the VFIO assumption of 64b IOVA, let's make sure the MR is resized according to the actual GAW of the physical IOMMU. Otherwise we will get a failure on vfio
vfio_find_hostwin().Signed-off-by Eric Auger <[email protected]>--- hw/virtio/virtio-iommu.c | 6 ++++++ 1 file changed, 6 insertions(+)
PATCH 13/13 vfio: Remove 64-bit IOVA address space assumption
In reply to Eric AugerNow we retrieve the usable IOVA ranges from the host, let's remove the assumption of 64b IOVA space when calling vfio_host_win_add().
Make sure the vfio_find_hostwin() gets called after we get a chance to call set_iova_regions and resize the IOMMU MR according to the actual physical GAW.
--- hw/vfio/common.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-)
hw/vfio/common.c +12 -13
@@ -1112,13 +1112,6 @@ static void vfio_listener_region_add(MemoryListener *listener,#endif } - hostwin =vfio_find_hostwin (container, iova, end); -if (!hostwin) { -error_setg (&err,"Container %p can't map guest IOVA region" -" 0x%" HWADDR_PRIx"..0x%" HWADDR_PRIx, container, iova, end); - goto fail; - } -memory_region_ref (section->mr);if (memory_region_is_iommu (section->mr)) {
@@ -1177,6 +1170,14 @@ static void vfio_listener_region_add(MemoryListener *listener,return ; } + hostwin =vfio_find_hostwin (container, iova, end); +if (!hostwin) { +error_setg (&err,"Container %p can't map guest IOVA region" +" 0x%" HWADDR_PRIx"..0x%" HWADDR_PRIx, container, iova, end); + goto fail; + } + +/* Here we assume that memory_region_is_ram(section->mr)==true */ /*
@@ -2594,12 +2595,10 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as,vfio_get_iommu_info_migration (container, info);g_free (info); -/* -* FIXME: We should parse VFIO_IOMMU_TYPE1_INFO_CAP_IOVA_RANGE -* information to get the actual window extent rather than assume -* a 64-bit IOVA address space. -*/ -vfio_host_win_add (container,0 , (hwaddr)-1 , container->pgsizes); +g_assert (container->nr_iovas); +vfio_host_win_add (container,0 , + container->iova_ranges[container->nr_iovas -1 ].end, + container->pgsizes);break ; }
Signature · 1 line
2.41.0
I have runned the following two tests, but both tests failed:
[1] start a VM with virtio-iommu + 2 ice PFs only via qemu-kvm 8.1.5 Test result : the qemu-kvm keeps throwing the error: VFIO_MAP_DMA
failed: File exists. vfio_dma_map(0x56443d20fbe0, 0xffffe000, 0x1000, 0x7fb545709000) = -17 (File exists)
[2] start a VM with virtio-iommu + 2 ice PFs via libvirt-9.5 + qemu-kvm 8.1.5 Test result: the qemu-kvm core dump with ERROR:../qom/object.c:1198:object_unref: assertion failed: (obj->ref > 0). Bail out! ERROR:../qom/object.c:1198:object_unref: assertion
failed: (obj->ref > 0)
After removing the 2 PF from the VM, both tests passed.
Best Regards,
YangHang Liu
Quoted 67 lines · Eric Auger
On x86, when assigning VFIO-PCI devices protected with virtio-iommu we encounter the case where the guest tries to map IOVAs beyond 48b whereas the physical VTD IOMMU only supports 48b. This ends up with
VFIO_MAP_DMAfailures at qemu level because at kernel level,vfio_iommu_iova_dma_valid()check returns false onvfio_map_do_map().This is due to the fact the virtio-iommu currently unconditionally exposes an IOVA range of 64b through its config input range fields.
This series removes this assumption by retrieving the usable IOVA regions through the
VFIO_IOMMU_TYPE1_INFO_CAP_IOVA_RANGEUAPI when a VFIO device is attached. This info is communicated to the virtio-iommu memory region, transformed into the inversed info, ie. the host reserved IOVA regions. Then those latter are combined with the reserved IOVA regions set though the virtio-iommu reserved-regions property. That way, the guest virtio-iommu driver, unchanged, is able to probe the whole set of reserved regions and prevent any IOVA belonging to those ranges from beeing used, achieving the original goal.Best Regards
Eric
This series can be found at:
https://github.com/eauger/qemu/tree/virtio-iommu_geometry_v1Eric Auger (13):
memory: Let ReservedRegion use Range
memory: Introducememory_region_iommu_set_iova_ranges
vfio: Collect container iova range info
virtio-iommu: Renamereserved_regionsintoprop_resv_regions
virtio-iommu: Introduce per IOMMUDevice reserved regions
range: Introducerange_inverse_array()
virtio-iommu: Implementset_iova_ranges()callback
range: Makerange_compare()public
util/reserved-region: Add new ReservedRegion helpers
virtio-iommu: Consolidate host reserved regions and property set ones
test: Add some tests for range and resv-mem helpers
virtio-iommu: Resize memory region according to the max iova info
vfio: Remove 64-bit IOVA address space assumptioninclude/exec/memory.h | 30 ++++- include/hw/vfio/vfio-common.h | 2 + include/hw/virtio/virtio-iommu.h | 7 +- include/qemu/range.h | 9 ++ include/qemu/reserved-region.h | 32 +++++ hw/core/qdev-properties-system.c | 9 +- hw/vfio/common.c | 70 ++++++++--- hw/virtio/virtio-iommu-pci.c | 8 +- hw/virtio/virtio-iommu.c | 85 +++++++++++-- softmmu/memory.c | 15 +++ tests/unit/test-resv-mem.c | 198 +++++++++++++++++++++++++++++++ util/range.c | 41 ++++++- util/reserved-region.c | 94 +++++++++++++++ hw/virtio/trace-events | 1 + tests/unit/meson.build | 1 + util/meson.build | 1 + 16 files changed, 562 insertions(+), 41 deletions(-) create mode 100644 include/qemu/reserved-region.h create mode 100644 tests/unit/test-resv-mem.c create mode 100644 util/reserved-region.c--
2.41.0
Hi Yanghang,
Quoted 11 lines · YangHang Liu
I have runned the following two tests, but both tests failed:
[1] start a VM with virtio-iommu + 2 ice PFs only via qemu-kvm 8.1.5 Test result : the qemu-kvm keeps throwing the error:VFIO_MAP_DMA
failed: File exists.vfio_dma_map(0x56443d20fbe0, 0xffffe000, 0x1000, 0x7fb545709000)= -17 (File exists)
[2] start a VM with virtio-iommu + 2 ice PFs via libvirt-9.5 + qemu-kvm 8.1.5 Test result: the qemu-kvm core dump with ERROR:../qom/object.c:1198:object_unref: assertion failed: (obj->ref> 0). Bail out! ERROR:../qom/object.c:1198:object_unref: assertion
failed: (obj->ref> 0)
Quoted 2 lines
After removing the 2 PF from the VM, both tests passed.
Tested-by Yanghang Liu <[email protected]>
thank you for testing. If my understanding is correct you still encountered some issues with/after the series. If this is correct you shall not offer your Tested-by which means you tested the series and it works fine for you/fixes your issue.
Coming back to the above mentionned issues:
1) the File Exists issue is known and is linked to the replay. This will be handled separately, ie.I need to resume working on it as my first approach was flawed: See
https://lore.kernel.org/all/[email protected]/
This is unrelated to this series. Note this shouldn't prevent your passthroughed device from working. Those should be just spurious warnings that need to be removed.
2) the object_unref assertion most probaly is linked to that series and I will to investigate asap.
Thank you again!
Eric
Quoted 72 lines
Best Regards,
YangHang LiuQuoted 66 lines · Eric Auger
On x86, when assigning VFIO-PCI devices protected with virtio-iommu we encounter the case where the guest tries to map IOVAs beyond 48b whereas the physical VTD IOMMU only supports 48b. This ends up with
VFIO_MAP_DMAfailures at qemu level because at kernel level,vfio_iommu_iova_dma_valid()check returns false onvfio_map_do_map().This is due to the fact the virtio-iommu currently unconditionally exposes an IOVA range of 64b through its config input range fields.
This series removes this assumption by retrieving the usable IOVA regions through the
VFIO_IOMMU_TYPE1_INFO_CAP_IOVA_RANGEUAPI when a VFIO device is attached. This info is communicated to the virtio-iommu memory region, transformed into the inversed info, ie. the host reserved IOVA regions. Then those latter are combined with the reserved IOVA regions set though the virtio-iommu reserved-regions property. That way, the guest virtio-iommu driver, unchanged, is able to probe the whole set of reserved regions and prevent any IOVA belonging to those ranges from beeing used, achieving the original goal.Best Regards
Eric
This series can be found at:
https://github.com/eauger/qemu/tree/virtio-iommu_geometry_v1Eric Auger (13):
memory: Let ReservedRegion use Range
memory: Introducememory_region_iommu_set_iova_ranges
vfio: Collect container iova range info
virtio-iommu: Renamereserved_regionsintoprop_resv_regions
virtio-iommu: Introduce per IOMMUDevice reserved regions
range: Introducerange_inverse_array()
virtio-iommu: Implementset_iova_ranges()callback
range: Makerange_compare()public
util/reserved-region: Add new ReservedRegion helpers
virtio-iommu: Consolidate host reserved regions and property set ones
test: Add some tests for range and resv-mem helpers
virtio-iommu: Resize memory region according to the max iova info
vfio: Remove 64-bit IOVA address space assumptioninclude/exec/memory.h | 30 ++++- include/hw/vfio/vfio-common.h | 2 + include/hw/virtio/virtio-iommu.h | 7 +- include/qemu/range.h | 9 ++ include/qemu/reserved-region.h | 32 +++++ hw/core/qdev-properties-system.c | 9 +- hw/vfio/common.c | 70 ++++++++--- hw/virtio/virtio-iommu-pci.c | 8 +- hw/virtio/virtio-iommu.c | 85 +++++++++++-- softmmu/memory.c | 15 +++ tests/unit/test-resv-mem.c | 198 +++++++++++++++++++++++++++++++ util/range.c | 41 ++++++- util/reserved-region.c | 94 +++++++++++++++ hw/virtio/trace-events | 1 + tests/unit/meson.build | 1 + util/meson.build | 1 + 16 files changed, 562 insertions(+), 41 deletions(-) create mode 100644 include/qemu/reserved-region.h create mode 100644 tests/unit/test-resv-mem.c create mode 100644 util/reserved-region.c--
2.41.0
Hi,
Quoted 10 lines · YangHang Liu
I have runned the following two tests, but both tests failed:
[1] start a VM with virtio-iommu + 2 ice PFs only via qemu-kvm 8.1.5 Test result : the qemu-kvm keeps throwing the error:VFIO_MAP_DMA
failed: File exists.vfio_dma_map(0x56443d20fbe0, 0xffffe000, 0x1000, 0x7fb545709000)= -17 (File exists)
[2] start a VM with virtio-iommu + 2 ice PFs via libvirt-9.5 + qemu-kvm 8.1.5 Test result: the qemu-kvm core dump with ERROR:../qom/object.c:1198:object_unref: assertion failed: (obj->ref> 0). Bail out! ERROR:../qom/object.c:1198:object_unref: assertion
failed: (obj->ref> 0)
This latter issue is introduced by patch
[PATCH 12/13] virtio-iommu: Resize memory region according to the max iova info
and especially the call to
memory_region_set_size(&mr->parent_obj, max_iova + 1);
I don't really get why at the moment but I will investigate ... Eric
Quoted 76 lines
After removing the 2 PF from the VM, both tests passed.
Tested-by Yanghang Liu <[email protected]>Best Regards,
YangHang LiuQuoted 66 lines · Eric Auger
On x86, when assigning VFIO-PCI devices protected with virtio-iommu we encounter the case where the guest tries to map IOVAs beyond 48b whereas the physical VTD IOMMU only supports 48b. This ends up with
VFIO_MAP_DMAfailures at qemu level because at kernel level,vfio_iommu_iova_dma_valid()check returns false onvfio_map_do_map().This is due to the fact the virtio-iommu currently unconditionally exposes an IOVA range of 64b through its config input range fields.
This series removes this assumption by retrieving the usable IOVA regions through the
VFIO_IOMMU_TYPE1_INFO_CAP_IOVA_RANGEUAPI when a VFIO device is attached. This info is communicated to the virtio-iommu memory region, transformed into the inversed info, ie. the host reserved IOVA regions. Then those latter are combined with the reserved IOVA regions set though the virtio-iommu reserved-regions property. That way, the guest virtio-iommu driver, unchanged, is able to probe the whole set of reserved regions and prevent any IOVA belonging to those ranges from beeing used, achieving the original goal.Best Regards
Eric
This series can be found at:
https://github.com/eauger/qemu/tree/virtio-iommu_geometry_v1Eric Auger (13):
memory: Let ReservedRegion use Range
memory: Introducememory_region_iommu_set_iova_ranges
vfio: Collect container iova range info
virtio-iommu: Renamereserved_regionsintoprop_resv_regions
virtio-iommu: Introduce per IOMMUDevice reserved regions
range: Introducerange_inverse_array()
virtio-iommu: Implementset_iova_ranges()callback
range: Makerange_compare()public
util/reserved-region: Add new ReservedRegion helpers
virtio-iommu: Consolidate host reserved regions and property set ones
test: Add some tests for range and resv-mem helpers
virtio-iommu: Resize memory region according to the max iova info
vfio: Remove 64-bit IOVA address space assumptioninclude/exec/memory.h | 30 ++++- include/hw/vfio/vfio-common.h | 2 + include/hw/virtio/virtio-iommu.h | 7 +- include/qemu/range.h | 9 ++ include/qemu/reserved-region.h | 32 +++++ hw/core/qdev-properties-system.c | 9 +- hw/vfio/common.c | 70 ++++++++--- hw/virtio/virtio-iommu-pci.c | 8 +- hw/virtio/virtio-iommu.c | 85 +++++++++++-- softmmu/memory.c | 15 +++ tests/unit/test-resv-mem.c | 198 +++++++++++++++++++++++++++++++ util/range.c | 41 ++++++- util/reserved-region.c | 94 +++++++++++++++ hw/virtio/trace-events | 1 + tests/unit/meson.build | 1 + util/meson.build | 1 + 16 files changed, 562 insertions(+), 41 deletions(-) create mode 100644 include/qemu/reserved-region.h create mode 100644 tests/unit/test-resv-mem.c create mode 100644 util/reserved-region.c--
2.41.0
Quoted 9 lines · On Mon, 4 Sep 2023 10:03:43 +0200 Eric Auger <[email protected]> wrote:
On x86, when assigning VFIO-PCI devices protected with virtio-iommu we encounter the case where the guest tries to map IOVAs beyond 48b whereas the physical VTD IOMMU only supports 48b. This ends up with
VFIO_MAP_DMAfailures at qemu level because at kernel level,vfio_iommu_iova_dma_valid()check returns false onvfio_map_do_map().This is due to the fact the virtio-iommu currently unconditionally exposes an IOVA range of 64b through its config input range fields.
Quoted 9 lines
This series removes this assumption by retrieving the usable IOVA regions through the
VFIO_IOMMU_TYPE1_INFO_CAP_IOVA_RANGEUAPI when a VFIO device is attached. This info is communicated to the virtio-iommu memory region, transformed into the inversed info, ie. the host reserved IOVA regions. Then those latter are combined with the reserved IOVA regions set though the virtio-iommu reserved-regions property. That way, the guest virtio-iommu driver, unchanged, is able to probe the whole set of reserved regions and prevent any IOVA belonging to those ranges from beeing used, achieving the original goal.
Hi Eric,
I don't quite follow this relative to device hotplug. Are we manipulating a per-device memory region which is created at device add time? Is that memory region actually shared in some cases, for instance if we have a PCIe-to-PCI bridge aliasing devices on the conventional side? Thanks,
Alex
Quoted 39 lines
This series can be found at:
https://github.com/eauger/qemu/tree/virtio-iommu_geometry_v1Eric Auger (13):
memory: Let ReservedRegion use Range
memory: Introducememory_region_iommu_set_iova_ranges
vfio: Collect container iova range info
virtio-iommu: Renamereserved_regionsintoprop_resv_regions
virtio-iommu: Introduce per IOMMUDevice reserved regions
range: Introducerange_inverse_array()
virtio-iommu: Implementset_iova_ranges()callback
range: Makerange_compare()public
util/reserved-region: Add new ReservedRegion helpers
virtio-iommu: Consolidate host reserved regions and property set ones
test: Add some tests for range and resv-mem helpers
virtio-iommu: Resize memory region according to the max iova info
vfio: Remove 64-bit IOVA address space assumptioninclude/exec/memory.h | 30 ++++- include/hw/vfio/vfio-common.h | 2 + include/hw/virtio/virtio-iommu.h | 7 +- include/qemu/range.h | 9 ++ include/qemu/reserved-region.h | 32 +++++ hw/core/qdev-properties-system.c | 9 +- hw/vfio/common.c | 70 ++++++++--- hw/virtio/virtio-iommu-pci.c | 8 +- hw/virtio/virtio-iommu.c | 85 +++++++++++-- softmmu/memory.c | 15 +++ tests/unit/test-resv-mem.c | 198 +++++++++++++++++++++++++++++++ util/range.c | 41 ++++++- util/reserved-region.c | 94 +++++++++++++++ hw/virtio/trace-events | 1 + tests/unit/meson.build | 1 + util/meson.build | 1 + 16 files changed, 562 insertions(+), 41 deletions(-) create mode 100644 include/qemu/reserved-region.h create mode 100644 tests/unit/test-resv-mem.c create mode 100644 util/reserved-region.c
Hi Alex,
Quoted 21 lines · Alex Williamson
Quoted 9 lines · On Mon, 4 Sep 2023 10:03:43 +0200 Eric Auger <[email protected]> wrote:
On x86, when assigning VFIO-PCI devices protected with virtio-iommu we encounter the case where the guest tries to map IOVAs beyond 48b whereas the physical VTD IOMMU only supports 48b. This ends up with
VFIO_MAP_DMAfailures at qemu level because at kernel level,vfio_iommu_iova_dma_valid()check returns false onvfio_map_do_map().This is due to the fact the virtio-iommu currently unconditionally exposes an IOVA range of 64b through its config input range fields.
Quoted 9 lines
This series removes this assumption by retrieving the usable IOVA regions through the
VFIO_IOMMU_TYPE1_INFO_CAP_IOVA_RANGEUAPI when a VFIO device is attached. This info is communicated to the virtio-iommu memory region, transformed into the inversed info, ie. the host reserved IOVA regions. Then those latter are combined with the reserved IOVA regions set though the virtio-iommu reserved-regions property. That way, the guest virtio-iommu driver, unchanged, is able to probe the whole set of reserved regions and prevent any IOVA belonging to those ranges from beeing used, achieving the original goal.
Quoted 7 lines
Hi Eric,
I don't quite follow this relative to device hotplug. Are we manipulating a per-device memory region which is created at device add time? Is that memory region actually shared in some cases, for instance if we have a PCIe-to-PCI bridge aliasing devices on the conventional side? Thanks,
I agree this deserves more attention and testing in the case of hotplug and aliasing. Wrt PCIe to PCI bridge, virtio-iommu and smmu are known to be broken with this latter due to lack of kernel support (issue with group probing, but this might change in the future) so this is not a currently supported feature, as opposed to virtual intel iommu. Here I was mostly assuming one device per container and per IOMMU MR but maybe I have to detect & forbid more complex scenari.
Thanks
Eric
Quoted 41 lines
Alex
Quoted 39 lines
This series can be found at:
https://github.com/eauger/qemu/tree/virtio-iommu_geometry_v1Eric Auger (13):
memory: Let ReservedRegion use Range
memory: Introducememory_region_iommu_set_iova_ranges
vfio: Collect container iova range info
virtio-iommu: Renamereserved_regionsintoprop_resv_regions
virtio-iommu: Introduce per IOMMUDevice reserved regions
range: Introducerange_inverse_array()
virtio-iommu: Implementset_iova_ranges()callback
range: Makerange_compare()public
util/reserved-region: Add new ReservedRegion helpers
virtio-iommu: Consolidate host reserved regions and property set ones
test: Add some tests for range and resv-mem helpers
virtio-iommu: Resize memory region according to the max iova info
vfio: Remove 64-bit IOVA address space assumptioninclude/exec/memory.h | 30 ++++- include/hw/vfio/vfio-common.h | 2 + include/hw/virtio/virtio-iommu.h | 7 +- include/qemu/range.h | 9 ++ include/qemu/reserved-region.h | 32 +++++ hw/core/qdev-properties-system.c | 9 +- hw/vfio/common.c | 70 ++++++++--- hw/virtio/virtio-iommu-pci.c | 8 +- hw/virtio/virtio-iommu.c | 85 +++++++++++-- softmmu/memory.c | 15 +++ tests/unit/test-resv-mem.c | 198 +++++++++++++++++++++++++++++++ util/range.c | 41 ++++++- util/reserved-region.c | 94 +++++++++++++++ hw/virtio/trace-events | 1 + tests/unit/meson.build | 1 + util/meson.build | 1 + 16 files changed, 562 insertions(+), 41 deletions(-) create mode 100644 include/qemu/reserved-region.h create mode 100644 tests/unit/test-resv-mem.c create mode 100644 util/reserved-region.c