Skip to contentSkip to conversation

VIRTIO-IOMMU device

PATCH Superseded Eric Auger 4 participants 39 messages Jan 9, 2020 – Feb 10, 2020

v12 of 16 Superseded Latest is v16 →

lore
Share & tools
lore

Eric Auger

author
#0 lore raw

v12 cover VIRTIO-IOMMU device

This series implements the QEMU virtio-iommu device.

This matches the v0.12 spec (voted) and the corresponding virtio-iommu driver upstreamed in 5.3. All kernel dependencies are resolved for DT integration. The virtio-iommu can be instantiated in ARM virt using "-device virtio-iommu-pci". Non DT mode is not yet supported.

This feature targets 5.0.

For non DT integration, guest kernel dependencies are not resolved yet. Historically, an approach based on the ACPI IORT was used. However this is not considered as the best approach anymore. Alternative approaches are under discussion in [2] but they are still in RFC state. So considering the time needed to get this stabilized and the spec updated, I hope the DT integration for ARM can be merged first.

Integration with vhost devices and vfio devices is not part of this series. Please follow Bharat's respins [3].

Best Regards

Eric

This series can be found at:
https://github.com/eauger/qemu/tree/v4.2-virtio-iommu-v12
This branch also supports ACPI/IORT integration for both ARM and x86.

References:
[1] kernel branch to be used for guest for testing on ARM or x86 with ACPI/IORT integration:
https://github.com/eauger/linux/tree/v5.4-rc8-virtio-iommu-iort
for testing the DT boot on ARM, upstream kernel can be used.
[2] [RFC 00/13] virtio-iommu on non-devicetree platforms
[3] VFIO/VHOST integration is not part of this series. Please follow [PATCH RFC v5 0/5] virtio-iommu: VFIO integration respins

Testing:
- tested with guest using virtio-net-pci (,vhost=off,iommu_platform,disable-modern=off,disable-legacy=on) and virtio-blk-pci
- migration on ARM and x86
- on x86 PC machine AHCI unmapped transactions are observed at early boot stage. This does not prevent the guest from booting and behaving properly. Warnings look like:
qemu-system-x86_64: virtio_iommu_translate sid=250 is not known!!
qemu-system-x86_64: no buffer available in event queue to report event
qemu-system-x86_64: AHCI: Failed to start FIS receive engine: bad FIS receive buffer address
This is due to the fact that by default the virtio-iommu device blocks unmapped transations. This is likely to change in the future.

History:

v11 -> v12:
- took into account Peter and Jean's comments
- use guest features
- restore as_by_bus_num and when attaching devices, check they are actually protected by the IOMMU. Updated the tests accordingly.
- fix the mapping ref counting and make sure mappings are properly cleaned.
- Use CamelCase for data types
- simplify postload callback as suggested by Peter
- add R-bs
- fix mingw compilation issue
- add IOMMU migration priority
- qlist migration load simplified following Juan's suggestion

v10 -> v11:
- introduce virtio_iommu_handle_req macro
- migration support
- introduce DEFINE_PROP_INTERVAL and pass reserved regions through an array of those
- domain gtree simplification

v9 -> v10:
- rebase on 4.1.0-rc2, compliance with 0.12 spec
- removed ACPI part
- cleanup (see individual change logs)
- moved to a PATCH series

v8 -> v9:
- virtio-iommu-pci device needs to be instantiated from the command line (RID is not imposed anymore).
- tail structure properly initialized

v7 -> v8:
- virtio-iommu-pci added
- virt instantiation modified
- DT and ACPI modified to exclude the iommu RID from the mapping
- VIRTIO_IOMMU_F_BYPASS, VIRTIO_F_VERSION_1 features exposed

v6 -> v7:
- rebase on qemu 3.0.0-rc3
- minor update against v0.7
- fix issue with EP not on pci.0 and ACPI probing
- change the instantiation method

v5 -> v6:
- minor update against v0.6 spec
- fix g_hash_table_lookup in virtio_iommu_find_add_as
- replace some error_reports by qemu_log_mask(LOG_GUEST_ERROR, ...)

v4 -> v5:
- event queue and fault reporting
- we now return the IOAPIC MSI region if the virtio-iommu is instantiated in a PC machine.
- we bypass transactions on MSI HW region and fault on reserved ones.
- We support ACPI boot with mach-virt (based on IORT proposal)
- We moved to the new driver naming conventions
- simplified mach-virt instantiation
- worked around the disappearing of pci_find_primary_bus
- in virtio_iommu_translate, check the dev->as is not NULL
- initialize as->device_list in virtio_iommu_get_as
- initialize bufstate.error to false in virtio_iommu_probe

v3 -> v4:
- probe request support although no reserved region is returned at the moment
- unmap semantics less strict, as specified in v0.4
- device registration, attach/detach revisited
- split into smaller patches to ease review
- propose a way to inform the IOMMU mr about the page_size_mask of underlying HW IOMMU, if any
- remove warning associated with the translation of the MSI doorbell

v2 -> v3:
- rebase on top of 2.10-rc0 and especially
[PATCH qemu v9 0/2] memory/iommu: QOM'fy IOMMU MemoryRegion
- add mutex init
- fix as->mappings deletion using g_tree_ref/unref
- when a dev is attached whereas it is already attached to another address space, first detach it
- fix some error values
- page_sizes = TARGET_PAGE_MASK;
- I haven't changed the unmap() semantics yet, waiting for the next virtio-iommu spec revision.

v1 -> v2:
- fix redefinition of viommu_as typedef

Eric Auger (13):
migration: Support QLIST migration
virtio-iommu: Add skeleton
virtio-iommu: Decode the command payload
virtio-iommu: Add the iommu regions
virtio-iommu: Endpoint and domains structs and helpers
virtio-iommu: Implement attach/detach command
virtio-iommu: Implement map/unmap
virtio-iommu: Implement translate
virtio-iommu: Implement fault reporting
virtio-iommu-pci: Add virtio iommu pci support hw/arm/virt: Add the virtio-iommu device tree mappings
virtio-iommu: Support migration
tests: Add virtio-iommu test

 hw/arm/virt.c                    |  54 +-
 hw/virtio/Kconfig                |   5 +
 hw/virtio/Makefile.objs          |   2 +
 hw/virtio/trace-events           |  20 +
 hw/virtio/virtio-iommu-pci.c     |  88 +++
 hw/virtio/virtio-iommu.c         | 902 +++++++++++++++++++++++++++++++
 include/hw/arm/virt.h            |   2 +
 include/hw/pci/pci.h             |   1 +
 include/hw/virtio/virtio-iommu.h |  64 +++
 include/migration/vmstate.h      |  21 +
 include/qemu/queue.h             |  32 ++
 migration/trace-events           |   5 +
 migration/vmstate-types.c        |  74 +++
 qdev-monitor.c                   |   1 +
 tests/Makefile.include           |   2 +
 tests/libqos/virtio-iommu.c      | 177 ++++++
 tests/libqos/virtio-iommu.h      |  45 ++
 tests/test-vmstate.c             | 170 ++++++
 tests/virtio-iommu-test.c        | 308 +++++++++++
 19 files changed, 1965 insertions(+), 8 deletions(-)
 create mode 100644 hw/virtio/virtio-iommu-pci.c
 create mode 100644 hw/virtio/virtio-iommu.c
 create mode 100644 include/hw/virtio/virtio-iommu.h
 create mode 100644 tests/libqos/virtio-iommu.c
 create mode 100644 tests/libqos/virtio-iommu.h
 create mode 100644 tests/virtio-iommu-test.c
Signature · 1 line
2.20.1
author
#1 lore raw

PATCH v12 1/13 migration: Support QLIST migration

Support QLIST migration using the same principle as QTAILQ:
94869d5c52 ("migration: migrate QTAILQ").

The VMSTATE_QLIST_V macro has the same proto as VMSTATE_QTAILQ_V. The change mainly resides in QLIST RAW macros: QLIST_RAW_INSERT_HEAD and QLIST_RAW_INSERT_AFTER.

Tests also are provided.

---

v6 -> v7:
- use QLIST_RAW_INSERT_AFTER as suggested by Juan.

v5 - v6:
- by doing more advanced testing with virtio-iommu migration I noticed this was broken. "prev" field was not set properly. I improved the tests to manipulate both the next and prev fields.
- Removed Peter and Juan's R-b

---
 include/migration/vmstate.h |  21 +++++
 include/qemu/queue.h        |  32 +++++++
 migration/trace-events      |   5 ++
 migration/vmstate-types.c   |  74 ++++++++++++++++
 tests/test-vmstate.c        | 170 ++++++++++++++++++++++++++++++++++++
 5 files changed, 302 insertions(+)
include/migration/vmstate.h +21 -0
index 4aef72c426..0dc04fc48e 100644
@@ -229,6 +229,7 @@ extern const VMStateInfo vmstate_info_tmp;
 extern const VMStateInfo vmstate_info_bitmap;
 extern const VMStateInfo vmstate_info_qtailq;
 extern const VMStateInfo vmstate_info_gtree;
+extern const VMStateInfo vmstate_info_qlist;
 
 #define type_check_2darray(t1,t2,n,m) ((t1(*)[n][m])0 - (t2*)0)
 /*
@@ -798,6 +799,26 @@ extern const VMStateInfo vmstate_info_gtree;
     .offset       = offsetof(_state, _field),                                  \
 }
 
+/*
+ * For migrating a QLIST
+ * Target QLIST needs be properly initialized.
+ * _type: type of QLIST element
+ * _next: name of QLIST_ENTRY entry field in QLIST element
+ * _vmsd: VMSD for QLIST element
+ * size: size of QLIST element
+ * start: offset of QLIST_ENTRY in QTAILQ element
+ */
+#define VMSTATE_QLIST_V(_field, _state, _version, _vmsd, _type, _next)  \
+{                                                                        \
+    .name         = (stringify(_field)),                                 \
+    .version_id   = (_version),                                          \
+    .vmsd         = &(_vmsd),                                            \
+    .size         = sizeof(_type),                                       \
+    .info         = &vmstate_info_qlist,                                 \
+    .offset       = offsetof(_state, _field),                            \
+    .start        = offsetof(_type, _next),                              \
+}
+
 /* _f : field name
    _f_n : num of elements field_name
    _n : num of elements
include/qemu/queue.h +32 -0
index 4764d93ea3..19425f973f 100644
@@ -501,4 +501,36 @@ union {                                                                 \
         QTAILQ_RAW_TQH_CIRC(head)->tql_prev = QTAILQ_RAW_TQE_CIRC(elm, entry);  \
 } while (/*CONSTCOND*/0)
 
+#define QLIST_RAW_FIRST(head)                                                  \
+        field_at_offset(head, 0, void *)
+
+#define QLIST_RAW_NEXT(elm, entry)                                             \
+        field_at_offset(elm, entry, void *)
+
+#define QLIST_RAW_PREVIOUS(elm, entry)                                         \
+        field_at_offset(elm, entry + sizeof(void *), void *)
+
+#define QLIST_RAW_FOREACH(elm, head, entry)                                    \
+        for ((elm) = *QLIST_RAW_FIRST(head);                                   \
+             (elm);                                                            \
+             (elm) = *QLIST_RAW_NEXT(elm, entry))
+
+#define QLIST_RAW_INSERT_AFTER(head, prev, elem, entry) do {                   \
+        *QLIST_RAW_NEXT(prev, entry) = elem;                                   \
+        *QLIST_RAW_PREVIOUS(elem, entry) = QLIST_RAW_NEXT(prev, entry);        \
+        *QLIST_RAW_NEXT(elem, entry) = NULL;                                   \
+} while (0)
+
+#define QLIST_RAW_INSERT_HEAD(head, elm, entry) do {                           \
+        void *first = *QLIST_RAW_FIRST(head);                                  \
+        *QLIST_RAW_FIRST(head) = elm;                                          \
+        *QLIST_RAW_PREVIOUS(elm, entry) = QLIST_RAW_FIRST(head);               \
+        if (first) {                                                           \
+            *QLIST_RAW_NEXT(elm, entry) = first;                               \
+            *QLIST_RAW_PREVIOUS(first, entry) = QLIST_RAW_NEXT(elm, entry);    \
+        } else {                                                               \
+            *QLIST_RAW_NEXT(elm, entry) = NULL;                                \
+        }                                                                      \
+} while (0)
+
 #endif /* QEMU_SYS_QUEUE_H */
migration/trace-events +5 -0
index 6dee7b5389..e0a33cffca 100644
@@ -76,6 +76,11 @@ get_gtree_end(const char *field_name, const char *key_vmsd_name, const char *val
 put_gtree(const char *field_name, const char *key_vmsd_name, const char *val_vmsd_name, uint32_t nnodes) "%s(%s/%s) nnodes=%d"
 put_gtree_end(const char *field_name, const char *key_vmsd_name, const char *val_vmsd_name, int ret) "%s(%s/%s) %d"
 
+get_qlist(const char *field_name, const char *vmsd_name, int version_id) "%s(%s v%d)"
+get_qlist_end(const char *field_name, const char *vmsd_name) "%s(%s)"
+put_qlist(const char *field_name, const char *vmsd_name, int version_id) "%s(%s v%d)"
+put_qlist_end(const char *field_name, const char *vmsd_name) "%s(%s)"
+
 # qemu-file.c
 qemu_file_fclose(void) ""
 
migration/vmstate-types.c +74 -0

77 lines not on this page · raw message

tests/test-vmstate.c +170 -0

194 lines not on this page · raw message

Signature · 1 line
2.20.1
author
#2 lore raw

PATCH v12 2/13 virtio-iommu: Add skeleton

In reply to Eric Auger

This patchs adds the skeleton for the virtio-iommu device.

---

v11 -> v12:
- remove s_by_bus_num
- drop set_features (rely on default implementation) and acked_features

v9 -> v10:
- mutex initialized here
- initialize tail
- included hw/qdev-properties.h
- removed g_memdup
- removed s->config.domain_range.start = 0;

v9 -> v10:
- expose VIRTIO_IOMMU_F_MMIO feature
- s/domain_bits/domain_range struct
- change error codes
- enforce unmigratable
- Kconfig

v7 -> v8:
- expose VIRTIO_IOMMU_F_BYPASS and VIRTIO_F_VERSION_1 features
- set_config dummy implementation + tracing
- add trace in get_features
- set the features on realize() and store the acked ones
- remove inclusion of linux/virtio_iommu.h

v6 -> v7:
- removed qapi-event.h include
- add primary_bus and associated property

v4 -> v5:
- use the new v0.5 terminology (domain, endpoint)
- add the event virtqueue

v3 -> v4:
- use page_size_mask instead of page_sizes
- added set_features()
- added some traces (reset, set_status, set_features)
- empty virtio_iommu_set_config() as the driver MUST NOT write to device configuration fields
- add get_config trace

v2 -> v3:
- rebase on 2.10-rc0, ie. use IOMMUMemoryRegion and remove iommu_ops.
- advertise VIRTIO_IOMMU_F_MAP_UNMAP feature
- page_sizes set to TARGET_PAGE_SIZE

Conflicts:
hw/virtio/trace-events

---
 hw/virtio/Kconfig                |   5 +
 hw/virtio/Makefile.objs          |   1 +
 hw/virtio/trace-events           |   7 +
 hw/virtio/virtio-iommu.c         | 265 +++++++++++++++++++++++++++++++
 include/hw/virtio/virtio-iommu.h |  60 +++++++
 5 files changed, 338 insertions(+)
 create mode 100644 hw/virtio/virtio-iommu.c
 create mode 100644 include/hw/virtio/virtio-iommu.h
hw/virtio/Kconfig +5 -0
index f87def27a6..d29525b36f 100644
@@ -9,6 +9,11 @@ config VIRTIO_RNG
     default y
     depends on VIRTIO
 
+config VIRTIO_IOMMU
+    bool
+    default y
+    depends on VIRTIO
+
 config VIRTIO_PCI
     bool
     default y if PCI_DEVICES
hw/virtio/Makefile.objs +1 -0
index de0f5fc39b..2fd9da7410 100644
@@ -16,6 +16,7 @@ obj-$(call land,$(CONFIG_VIRTIO_CRYPTO),$(CONFIG_VIRTIO_PCI)) += virtio-crypto-p
 obj-$(CONFIG_VIRTIO_PMEM) += virtio-pmem.o
 common-obj-$(call land,$(CONFIG_VIRTIO_PMEM),$(CONFIG_VIRTIO_PCI)) += virtio-pmem-pci.o
 obj-$(call land,$(CONFIG_VHOST_USER_FS),$(CONFIG_VIRTIO_PCI)) += vhost-user-fs-pci.o
+obj-$(CONFIG_VIRTIO_IOMMU) += virtio-iommu.o
 obj-$(CONFIG_VHOST_VSOCK) += vhost-vsock.o
 
 ifeq ($(CONFIG_VIRTIO_PCI),y)
hw/virtio/trace-events +7 -0
index e28ba48da6..02d93d7f63 100644
@@ -53,3 +53,10 @@ virtio_mmio_write_offset(uint64_t offset, uint64_t value) "virtio_mmio_write off
 virtio_mmio_guest_page(uint64_t size, int shift) "guest page size 0x%" PRIx64 " shift %d"
 virtio_mmio_queue_write(uint64_t value, int max_size) "mmio_queue write 0x%" PRIx64 " max %d"
 virtio_mmio_setting_irq(int level) "virtio_mmio setting IRQ %d"
+
+# hw/virtio/virtio-iommu.c
+virtio_iommu_device_reset(void) "reset!"
+virtio_iommu_get_features(uint64_t features) "device supports features=0x%"PRIx64
+virtio_iommu_device_status(uint8_t status) "driver status = %d"
+virtio_iommu_get_config(uint64_t page_size_mask, uint64_t start, uint64_t end, uint32_t domain_range, uint32_t probe_size) "page_size_mask=0x%"PRIx64" start=0x%"PRIx64" end=0x%"PRIx64" domain_range=%d probe_size=0x%x"
+virtio_iommu_set_config(uint64_t page_size_mask, uint64_t start, uint64_t end, uint32_t domain_range, uint32_t probe_size) "page_size_mask=0x%"PRIx64" start=0x%"PRIx64" end=0x%"PRIx64" domain_bits=%d probe_size=0x%x"
hw/virtio/virtio-iommu.c new file +265 -0

265 lines not on this page · raw message

include/hw/virtio/virtio-iommu.h new file +60 -0

60 lines not on this page · raw message

Signature · 1 line
2.20.1
author
#4 lore raw

PATCH v12 3/13 virtio-iommu: Decode the command payload

In reply to Eric Auger

This patch adds the command payload decoding and introduces the functions that will do the actual command handling. Those functions are not yet implemented.

---

v11 -> v12:
- ADded Jean and Peter's R-b

v10 -> v11:
- use a macro for handle command functions

v9 -> v10:
- make virtio_iommu_handle_* more compact and
remove get_payload_size

v7 -> v8:
- handle new domain parameter in detach
- remove reserved checks

v5 -> v6:
- change map/unmap semantics (remove size)

v4 -> v5:
- adopt new v0.5 terminology

v3 -> v4:
- no flags field anymore in struct virtio_iommu_req_unmap
- test reserved on attach/detach, change trace proto
- rebase on v2.10.0.

---
 hw/virtio/trace-events   |  4 +++
 hw/virtio/virtio-iommu.c | 76 +++++++++++++++++++++++++++++++++-------
 2 files changed, 68 insertions(+), 12 deletions(-)
hw/virtio/trace-events +4 -0
index 02d93d7f63..f7141aa2f6 100644
@@ -60,3 +60,7 @@ virtio_iommu_get_features(uint64_t features) "device supports features=0x%"PRIx6
 virtio_iommu_device_status(uint8_t status) "driver status = %d"
 virtio_iommu_get_config(uint64_t page_size_mask, uint64_t start, uint64_t end, uint32_t domain_range, uint32_t probe_size) "page_size_mask=0x%"PRIx64" start=0x%"PRIx64" end=0x%"PRIx64" domain_range=%d probe_size=0x%x"
 virtio_iommu_set_config(uint64_t page_size_mask, uint64_t start, uint64_t end, uint32_t domain_range, uint32_t probe_size) "page_size_mask=0x%"PRIx64" start=0x%"PRIx64" end=0x%"PRIx64" domain_bits=%d probe_size=0x%x"
+virtio_iommu_attach(uint32_t domain_id, uint32_t ep_id) "domain=%d endpoint=%d"
+virtio_iommu_detach(uint32_t domain_id, uint32_t ep_id) "domain=%d endpoint=%d"
+virtio_iommu_map(uint32_t domain_id, uint64_t virt_start, uint64_t virt_end, uint64_t phys_start, uint32_t flags) "domain=%d virt_start=0x%"PRIx64" virt_end=0x%"PRIx64 " phys_start=0x%"PRIx64" flags=%d"
+virtio_iommu_unmap(uint32_t domain_id, uint64_t virt_start, uint64_t virt_end) "domain=%d virt_start=0x%"PRIx64" virt_end=0x%"PRIx64
hw/virtio/virtio-iommu.c +64 -12

95 lines not on this page · raw message

Signature · 1 line
2.20.1
author
#5 lore raw

PATCH v12 4/13 virtio-iommu: Add the iommu regions

In reply to Eric Auger

Implement a callback called on PCI bus enumeration that initializes for a given device on the bus hierarchy an IOMMU memory region. The PCI bus hierarchy is stored locally in IOMMUPciBus and IOMMUDevice objects.

At the time of the enumeration, the bus number may not be computed yet.

So operations that will need to retrieve the IOMMUdevice and its IOMMU memory region from the bus number and devfn, once the bus number is garanteed to be frozen, use an array of IOMMUPciBus, lazily populated.

virtio_iommu_mr() is the top helper that allows to retrieve the IOMMU memory region from the requester ID.

---
v11 -> v12:
- add the iommu_find_iommu_pcibus() mechanics. Without it, when attaching t device to a domain we could not check the device is effectively protected by this IOMMU

v10 -> v11:
- use g_hash_table_new_full for allocating as_by_busptr

v9 -> v10:
- remove pc/virt machine headers
- virtio_iommu_find_add_as: mr_index introduced in that patch and name properly freed

v6 -> v7:
- use primary_bus
- rebase on new translate proto featuring iommu_idx

v5 -> v6:
- include qapi/error.h
- fix g_hash_table_lookup key in virtio_iommu_find_add_as

v4 -> v5:
- use PCI bus handle as a key
- use get_primary_pci_bus() callback

v3 -> v4:
- add trace_virtio_iommu_init_iommu_mr

v2 -> v3:
- use IOMMUMemoryRegion
- iommu mr name built with BDF
- rename smmu_get_sid into virtio_iommu_get_sid and use PCI_BUILD_BDF

---
 hw/virtio/trace-events           |   2 +
 hw/virtio/virtio-iommu.c         | 135 +++++++++++++++++++++++++++++++
 include/hw/virtio/virtio-iommu.h |   3 +
 3 files changed, 140 insertions(+)
hw/virtio/trace-events +2 -0
index f7141aa2f6..10a2b120f3 100644
@@ -64,3 +64,5 @@ virtio_iommu_attach(uint32_t domain_id, uint32_t ep_id) "domain=%d endpoint=%d"
 virtio_iommu_detach(uint32_t domain_id, uint32_t ep_id) "domain=%d endpoint=%d"
 virtio_iommu_map(uint32_t domain_id, uint64_t virt_start, uint64_t virt_end, uint64_t phys_start, uint32_t flags) "domain=%d virt_start=0x%"PRIx64" virt_end=0x%"PRIx64 " phys_start=0x%"PRIx64" flags=%d"
 virtio_iommu_unmap(uint32_t domain_id, uint64_t virt_start, uint64_t virt_end) "domain=%d virt_start=0x%"PRIx64" virt_end=0x%"PRIx64
+virtio_iommu_translate(const char *name, uint32_t rid, uint64_t iova, int flag) "mr=%s rid=%d addr=0x%"PRIx64" flag=%d"
+virtio_iommu_init_iommu_mr(char *iommu_mr) "init %s"
hw/virtio/virtio-iommu.c +135 -0

180 lines not on this page · raw message

include/hw/virtio/virtio-iommu.h +3 -0
index 4ebf4f093e..c6c33b4af0 100644
@@ -28,6 +28,8 @@
 #define VIRTIO_IOMMU(obj) \
         OBJECT_CHECK(VirtIOIOMMU, (obj), TYPE_VIRTIO_IOMMU)
 
+#define TYPE_VIRTIO_IOMMU_MEMORY_REGION "virtio-iommu-memory-region"
+
 #define IOMMU_PCI_BUS_MAX      256
 #define IOMMU_PCI_DEVFN_MAX    256
 
@@ -51,6 +53,7 @@ typedef struct VirtIOIOMMU {
     struct virtio_iommu_config config;
     uint64_t features;
     GHashTable *as_by_busptr;
+    IOMMUPciBus *iommu_pcibus_by_bus_num[IOMMU_PCI_BUS_MAX];
     PCIBus *primary_bus;
     GTree *domains;
     QemuMutex mutex;
Signature · 1 line
2.20.1
#6 lore raw

Re: [PATCH v12 04/13] virtio-iommu: Add the iommu regions

Quoted 16 lines · Eric Auger

Implement a callback called on PCI bus enumeration that initializes for a given device on the bus hierarchy an IOMMU memory region. The PCI bus hierarchy is stored locally in IOMMUPciBus and IOMMUDevice objects.

At the time of the enumeration, the bus number may not be computed yet.

So operations that will need to retrieve the IOMMUdevice and its IOMMU memory region from the bus number and devfn, once the bus number is garanteed to be frozen, use an array of IOMMUPciBus, lazily populated.

virtio_iommu_mr() is the top helper that allows to retrieve the IOMMU memory region from the requester ID.

Quoted 7 lines

---
v11 -> v12:
- add the iommu_find_iommu_pcibus() mechanics. Without it, when attaching t device to a domain we could not check the device is effectively protected by this IOMMU

Sorry I probably lost the context again after read the previous version... Could you hint me what does this used for?

In all cases, I see that virtio_iommu_mr() is introduced but not used. Would be good to put it into the patch where it's firstly used.

Thanks,

Signature · 1 line
Peter Xu
author
#7 lore raw

Re: [PATCH v12 04/13] virtio-iommu: Add the iommu regions

Hi Peter,

Quoted 19 lines · Peter Xu
Quoted 18 lines · Eric Auger

Implement a callback called on PCI bus enumeration that initializes for a given device on the bus hierarchy an IOMMU memory region. The PCI bus hierarchy is stored locally in IOMMUPciBus and IOMMUDevice objects.

At the time of the enumeration, the bus number may not be computed yet.

So operations that will need to retrieve the IOMMUdevice and its IOMMU memory region from the bus number and devfn, once the bus number is garanteed to be frozen, use an array of IOMMUPciBus, lazily populated.

virtio_iommu_mr() is the top helper that allows to retrieve the IOMMU memory region from the requester ID.

Quoted 8 lines
Quoted 5 lines

---
v11 -> v12:
- add the iommu_find_iommu_pcibus() mechanics. Without it, when attaching t device to a domain we could not check the device is effectively protected by this IOMMU

Sorry I probably lost the context again after read the previous version... Could you hint me what does this used for?

In v11 Jean pointed out that as_by_bus_num was not used in my series. I first planned to remove it and then noticed that it could be useful to test on "attach" whether the RID of the device effectively corresponds to a device protected by the IOMMU and in the negative, return an error.

In https://patchwork.kernel.org/patch/11258269/#23067995

This is the same mechanics used in intel_iommu/smmu.

Quoted 3 lines

In all cases, I see that virtio_iommu_mr() is introduced but not used. Would be good to put it into the patch where it's firstly used.

OK fair enough, I will put the helper in the same patch as the user as you have requested that since the beginning ;-) The resulting patch may be huge. Just hope nobody will request me to split it back ;-)

Thanks

Eric

Quoted 3 lines

Thanks,

#8 lore raw

Re: [PATCH v12 04/13] virtio-iommu: Add the iommu regions

Quoted 5 lines · Auger Eric
Quoted 2 lines

In all cases, I see that virtio_iommu_mr() is introduced but not used. Would be good to put it into the patch where it's firstly used.

OK fair enough, I will put the helper in the same patch as the user as you have requested that since the beginning ;-) The resulting patch may be huge. Just hope nobody will request me to split it back ;-)

We can wait for a few more days and see whether someone will have a different answer before you start to squash things. :)

In all cases, I think we should get rid of the compiler trick at least.

Thanks,

Signature · 1 line
Peter Xu
#9 lore raw

Re: [PATCH v12 04/13] virtio-iommu: Add the iommu regions

In reply to Eric Auger
Quoted 10 lines · Eric Auger
hw/virtio/virtio-iommu.c +10 -0
+/**
+ * The bus number is used for lookup when SID based operations occur.
+ * In that case we lazily populate the IOMMUPciBus array from the bus hash
+ * table. At the time the IOMMUPciBus is created (iommu_find_add_as), the bus
+ * numbers may not be always initialized yet.
+ */
+static IOMMUPciBus *iommu_find_iommu_pcibus(VirtIOIOMMU *s, uint8_t bus_num)
+{
+    IOMMUPciBus *iommu_pci_bus = s->iommu_pcibus_by_bus_num[bus_num];
+

hw/virtio/virtio-iommu.c line 63

Quoted 10 lines
hw/virtio/virtio-iommu.c +10 -0
+    if (!iommu_pci_bus) {
+        GHashTableIter iter;
+
+        g_hash_table_iter_init(&iter, s->as_by_busptr);
+        while (g_hash_table_iter_next(&iter, NULL, (void **)&iommu_pci_bus)) {
+            if (pci_bus_num(iommu_pci_bus->bus) == bus_num) {
+                s->iommu_pcibus_by_bus_num[bus_num] = iommu_pci_bus;
+                return iommu_pci_bus;
+            }
+        }

Btw, we may need to:

           return NULL;

here.

Quoted 3 lines
hw/virtio/virtio-iommu.c +3 -0
+    }
+    return iommu_pci_bus;
+}
Signature · 1 line
Peter Xu
author
#10 lore raw

Re: [PATCH v12 04/13] virtio-iommu: Add the iommu regions

Hi Peter,

Quoted 24 lines · Peter Xu
Quoted 10 lines · Eric Auger
hw/virtio/virtio-iommu.c +10 -0
+/**
+ * The bus number is used for lookup when SID based operations occur.
+ * In that case we lazily populate the IOMMUPciBus array from the bus hash
+ * table. At the time the IOMMUPciBus is created (iommu_find_add_as), the bus
+ * numbers may not be always initialized yet.
+ */
+static IOMMUPciBus *iommu_find_iommu_pcibus(VirtIOIOMMU *s, uint8_t bus_num)
+{
+    IOMMUPciBus *iommu_pci_bus = s->iommu_pcibus_by_bus_num[bus_num];
+

hw/virtio/virtio-iommu.c line 63

Quoted 10 lines
hw/virtio/virtio-iommu.c +10 -0
+    if (!iommu_pci_bus) {
+        GHashTableIter iter;
+
+        g_hash_table_iter_init(&iter, s->as_by_busptr);
+        while (g_hash_table_iter_next(&iter, NULL, (void **)&iommu_pci_bus)) {
+            if (pci_bus_num(iommu_pci_bus->bus) == bus_num) {
+                s->iommu_pcibus_by_bus_num[bus_num] = iommu_pci_bus;
+                return iommu_pci_bus;
+            }
+        }

Btw, we may need to:

Quoted 1 line
           return NULL;

Yes. By the way Yi's patch "intel_iommu: a fix to vtd_find_as_from_bus_num()" also applies to SMMU code. I will send a patch.

Thanks

Eric

Quoted 7 lines

here.

Quoted 3 lines
hw/virtio/virtio-iommu.c +3 -0
+    }
+    return iommu_pci_bus;
+}
author
#11 lore raw

PATCH v12 5/13 virtio-iommu: Endpoint and domains structs and helpers

In reply to Eric Auger

This patch introduce domain and endpoint internal datatypes. Both are stored in RB trees. The domain owns a list of endpoints attached to it.

Helpers to get/put end points and domains are introduced. get() helpers will become static in subsequent patches.

---

v11 -> v12:
- s/uint/guint (docker-mingw@fedora build test failure)
- s/viommu_/VirtIOIOMMU + CamelCase
- Before creating an endpoint, check the sid is one from a device protected by the iommu, otherwise returns NULL
- add assert of endpoint put to check the endpoint was detached from any domain
- destroy and re-allocate the trees on reset

v10 -> v11:
- fixed interval_cmp (<= -> < and >= -> >)
- removed unused viommu field from endpoint
- removed Bharat's R-b

v9 -> v10:
- added Bharat's R-b

v6 -> v7:
- on virtio_iommu_find_add_as the bus number computation may not be finalized yet so we cannot register the EPs at that time. Hence, let's remove the get_endpoint and also do not use the bus number for building the memory region name string (only used for debug though).

v4 -> v5:
- initialize as->endpoint_list

v3 -> v4:
- new separate patch

---
 hw/virtio/trace-events   |   4 ++
 hw/virtio/virtio-iommu.c | 128 ++++++++++++++++++++++++++++++++++++++-
 2 files changed, 130 insertions(+), 2 deletions(-)
hw/virtio/trace-events +4 -0
index 10a2b120f3..15595f8cd7 100644
@@ -66,3 +66,7 @@ virtio_iommu_map(uint32_t domain_id, uint64_t virt_start, uint64_t virt_end, uin
 virtio_iommu_unmap(uint32_t domain_id, uint64_t virt_start, uint64_t virt_end) "domain=%d virt_start=0x%"PRIx64" virt_end=0x%"PRIx64
 virtio_iommu_translate(const char *name, uint32_t rid, uint64_t iova, int flag) "mr=%s rid=%d addr=0x%"PRIx64" flag=%d"
 virtio_iommu_init_iommu_mr(char *iommu_mr) "init %s"
+virtio_iommu_get_endpoint(uint32_t ep_id) "Alloc endpoint=%d"
+virtio_iommu_put_endpoint(uint32_t ep_id) "Free endpoint=%d"
+virtio_iommu_get_domain(uint32_t domain_id) "Alloc domain=%d"
+virtio_iommu_put_domain(uint32_t domain_id) "Free domain=%d"
hw/virtio/virtio-iommu.c +126 -2

169 lines not on this page · raw message

Signature · 1 line
2.20.1
#12 lore raw

Re: [PATCH v12 05/13] virtio-iommu: Endpoint and domains structs and helpers

On Thu, Jan 09, 2020 at 03:43:11PM +0100, Eric Auger wrote:

[...]

hw/virtio/virtio-iommu.c IOMMUMemoryRegion *virtio_iommu_mr(VirtIOIOMMU *s, uint32_t sid) · line 126

Quoted 2 lines
hw/virtio/virtio-iommu.c +2 -0
+VirtIOIOMMUEndpoint *virtio_iommu_get_endpoint(VirtIOIOMMU *s, uint32_t ep_id);
+VirtIOIOMMUEndpoint *virtio_iommu_get_endpoint(VirtIOIOMMU *s, uint32_t ep_id)

Is the extra definition trying to workaround the compiler warning/error?

I'm not sure whether it's only me who prefer this, but again I'd really perfer we move the function into the caller patch, add "static" as needed altogether, even if that patch can be big.

hw/virtio/virtio-iommu.c IOMMUMemoryRegion *virtio_iommu_mr(VirtIOIOMMU *s, uint32_t sid) · line 134

Quoted 8 lines
hw/virtio/virtio-iommu.c +8 -0
+{
+    VirtIOIOMMUEndpoint *ep;
+
+    ep = g_tree_lookup(s->endpoints, GUINT_TO_POINTER(ep_id));
+    if (ep) {
+        return ep;
+    }
+    if (!virtio_iommu_mr(s, ep_id)) {

Could I ask when this will trigger?

hw/virtio/virtio-iommu.c IOMMUMemoryRegion *virtio_iommu_mr(VirtIOIOMMU *s, uint32_t sid) · line 142

Quoted 8 lines
hw/virtio/virtio-iommu.c +8 -0
+        return NULL;
+    }
+    ep = g_malloc0(sizeof(*ep));
+    ep->id = ep_id;
+    trace_virtio_iommu_get_endpoint(ep_id);
+    g_tree_insert(s->endpoints, GUINT_TO_POINTER(ep_id), ep);
+    return ep;
+}

Thanks,

Signature · 1 line
Peter Xu
author
#13 lore raw

Re: [PATCH v12 05/13] virtio-iommu: Endpoint and domains structs and helpers

Hi Peter,

Quoted 9 lines · Peter Xu

On Thu, Jan 09, 2020 at 03:43:11PM +0100, Eric Auger wrote:

[...]

hw/virtio/virtio-iommu.c IOMMUMemoryRegion *virtio_iommu_mr(VirtIOIOMMU *s, uint32_t sid) · line 126

Quoted 2 lines
hw/virtio/virtio-iommu.c +2 -0
+VirtIOIOMMUEndpoint *virtio_iommu_get_endpoint(VirtIOIOMMU *s, uint32_t ep_id);
+VirtIOIOMMUEndpoint *virtio_iommu_get_endpoint(VirtIOIOMMU *s, uint32_t ep_id)

Is the extra definition trying to workaround the compiler warning/error?

yes it does

Quoted 4 lines

I'm not sure whether it's only me who prefer this, but again I'd really perfer we move the function into the caller patch, add "static" as needed altogether, even if that patch can be big.

OK I will do that.

Quoted 11 lines

hw/virtio/virtio-iommu.c IOMMUMemoryRegion *virtio_iommu_mr(VirtIOIOMMU *s, uint32_t sid) · line 134

Quoted 9 lines
hw/virtio/virtio-iommu.c +8 -0
+{
+    VirtIOIOMMUEndpoint *ep;
+
+    ep = g_tree_lookup(s->endpoints, GUINT_TO_POINTER(ep_id));
+    if (ep) {
+        return ep;
+    }
+    if (!virtio_iommu_mr(s, ep_id)) {

Could I ask when this will trigger?

This can happen when a device is attached to a domain and its RID does not correspond to one of the devices protected by the iommu.

Thanks

Eric

Quoted 12 lines

hw/virtio/virtio-iommu.c IOMMUMemoryRegion *virtio_iommu_mr(VirtIOIOMMU *s, uint32_t sid) · line 142

Quoted 9 lines
hw/virtio/virtio-iommu.c +8 -0
+        return NULL;
+    }
+    ep = g_malloc0(sizeof(*ep));
+    ep->id = ep_id;
+    trace_virtio_iommu_get_endpoint(ep_id);
+    g_tree_insert(s->endpoints, GUINT_TO_POINTER(ep_id), ep);
+    return ep;
+}

Thanks,

#14 lore raw

Re: [PATCH v12 05/13] virtio-iommu: Endpoint and domains structs and helpers

Quoted 1 line · Auger Eric

Hi Peter,

Hi, Eric,

[...]

Quoted 12 lines
Quoted 11 lines

hw/virtio/virtio-iommu.c IOMMUMemoryRegion *virtio_iommu_mr(VirtIOIOMMU *s, uint32_t sid) · line 134

Quoted 9 lines
hw/virtio/virtio-iommu.c +8 -0
+{
+    VirtIOIOMMUEndpoint *ep;
+
+    ep = g_tree_lookup(s->endpoints, GUINT_TO_POINTER(ep_id));
+    if (ep) {
+        return ep;
+    }
+    if (!virtio_iommu_mr(s, ep_id)) {

Could I ask when this will trigger?

Quoted 2 lines

This can happen when a device is attached to a domain and its RID does not correspond to one of the devices protected by the iommu.

So will it happen only because of a kernel driver bug?

Also, I think the name of "virtio_iommu_mr" is confusing on that it returned explicitly a MemoryRegion however it's never been used:

(since they're not in the same patch I'm pasting)

static IOMMUMemoryRegion *virtio_iommu_mr(VirtIOIOMMU *s, uint32_t sid)

{
    uint8_t bus_n, devfn;
    IOMMUPciBus *iommu_pci_bus;
    IOMMUDevice *dev;

    bus_n = PCI_BUS_NUM(sid);
    iommu_pci_bus = iommu_find_iommu_pcibus(s, bus_n);
    if (iommu_pci_bus) {
        devfn = sid & 0xFF;
        dev = iommu_pci_bus->pbdev[devfn];
        if (dev) {
            return &dev->iommu_mr;
        }
    }
    return NULL;
}

Maybe "return !!dev" would be enough, then make the return a boolean? Then we can rename it to virtio_iommu_has_device().

PS. I think we can also drop IOMMU_PCI_DEVFN_MAX (after all you even didn't use it here!) and use PCI_DEVFN_MAX, and replace 0xFF.

Thanks,

Signature · 1 line
Peter Xu
author
#15 lore raw

Re: [PATCH v12 05/13] virtio-iommu: Endpoint and domains structs and helpers

Hi Peter,

Quoted 19 lines · Peter Xu
Quoted 1 line · Auger Eric

Hi Peter,

Hi, Eric,

[...]

Quoted 12 lines
Quoted 11 lines

hw/virtio/virtio-iommu.c IOMMUMemoryRegion *virtio_iommu_mr(VirtIOIOMMU *s, uint32_t sid) · line 134

Quoted 9 lines
hw/virtio/virtio-iommu.c +8 -0
+{
+    VirtIOIOMMUEndpoint *ep;
+
+    ep = g_tree_lookup(s->endpoints, GUINT_TO_POINTER(ep_id));
+    if (ep) {
+        return ep;
+    }
+    if (!virtio_iommu_mr(s, ep_id)) {

Could I ask when this will trigger?

Quoted 5 lines
Quoted 2 lines

This can happen when a device is attached to a domain and its RID does not correspond to one of the devices protected by the iommu.

So will it happen only because of a kernel driver bug?

Yes, at the moment, because virtio_iommu_mr() only gets called on device attach to a domain.

The spec says:
"If the endpoint identified by endpoint doesn’t exist, the device MUST reject the request and set status to VIRTIO_IOMMU_S_NOENT"

Quoted 3 lines

Also, I think the name of "virtio_iommu_mr" is confusing on that it returned explicitly a MemoryRegion however it's never been used:

I use the same prototype as for smmu_iommu_mr(). Returning the iommu mr will allow to proceed with further RID based operations like invalidations.

The same logic is used in vtd_context_device_invalidate.

Quoted 21 lines

(since they're not in the same patch I'm pasting)

static IOMMUMemoryRegion *virtio_iommu_mr(VirtIOIOMMU *s, uint32_t sid)

{
    uint8_t bus_n, devfn;
    IOMMUPciBus *iommu_pci_bus;
    IOMMUDevice *dev;

    bus_n = PCI_BUS_NUM(sid);
    iommu_pci_bus = iommu_find_iommu_pcibus(s, bus_n);
    if (iommu_pci_bus) {
        devfn = sid & 0xFF;
        dev = iommu_pci_bus->pbdev[devfn];
        if (dev) {
            return &dev->iommu_mr;
        }
    }
    return NULL;
}
Quoted 5 lines

Maybe "return !!dev" would be enough, then make the return a boolean? Then we can rename it to virtio_iommu_has_device().

PS. I think we can also drop IOMMU_PCI_DEVFN_MAX (after all you even didn't use it here!) and use PCI_DEVFN_MAX, and replace 0xFF.

well intel iommu and smmu use a similar constant (PCI_DEVFN_MAX, SMMU_PCI_DEVFN_MAX resp.). I use it in virtio_iommu_find_add_as

Thanks

Eric

Quoted 3 lines

Thanks,

#16 lore raw

Re: [PATCH v12 05/13] virtio-iommu: Endpoint and domains structs and helpers

Quoted 27 lines · Auger Eric

Hi Peter,

Quoted 23 lines · Peter Xu
Quoted 1 line · Auger Eric

Hi Peter,

Hi, Eric,

[...]

Quoted 14 lines
Quoted 11 lines

hw/virtio/virtio-iommu.c IOMMUMemoryRegion *virtio_iommu_mr(VirtIOIOMMU *s, uint32_t sid) · line 134

Quoted 9 lines
hw/virtio/virtio-iommu.c +8 -0
+{
+    VirtIOIOMMUEndpoint *ep;
+
+    ep = g_tree_lookup(s->endpoints, GUINT_TO_POINTER(ep_id));
+    if (ep) {
+        return ep;
+    }
+    if (!virtio_iommu_mr(s, ep_id)) {

Could I ask when this will trigger?

This can happen when a device is attached to a domain and its RID does not correspond to one of the devices protected by the iommu.

Quoted 8 lines
Quoted 1 line

So will it happen only because of a kernel driver bug?

Yes, at the moment, because virtio_iommu_mr() only gets called on device attach to a domain.

The spec says:
"If the endpoint identified by endpoint doesn’t exist, the device MUST reject the request and set status to VIRTIO_IOMMU_S_NOENT"

Sure. I just wanted to make sure I didn't miss anything, because I really can't see when this extra logic can help, say, right now we only have one vIOMMU at least for VT-d, so all devices will be managed by that. But yeah if that's explicitly mentioned in the spec, I'd agree we should follow that.

Quoted 8 lines
Quoted 3 lines

Also, I think the name of "virtio_iommu_mr" is confusing on that it returned explicitly a MemoryRegion however it's never been used:

I use the same prototype as for smmu_iommu_mr(). Returning the iommu mr will allow to proceed with further RID based operations like invalidations.

The same logic is used in vtd_context_device_invalidate.

I'm fine with this. Let's keep virtio_iommu_mr() as you prefer.

Another thing I'd like to mention is that, I don't think "the same logic is used in VT-d" matters much. If we think something is wrong (even if it's the same in VT-d), why not we fix both? :-)

Thanks,

Quoted 30 lines
Quoted 23 lines

(since they're not in the same patch I'm pasting)

static IOMMUMemoryRegion *virtio_iommu_mr(VirtIOIOMMU *s, uint32_t sid)

{
    uint8_t bus_n, devfn;
    IOMMUPciBus *iommu_pci_bus;
    IOMMUDevice *dev;

    bus_n = PCI_BUS_NUM(sid);
    iommu_pci_bus = iommu_find_iommu_pcibus(s, bus_n);
    if (iommu_pci_bus) {
        devfn = sid & 0xFF;
        dev = iommu_pci_bus->pbdev[devfn];
        if (dev) {
            return &dev->iommu_mr;
        }
    }
    return NULL;
}
Quoted 5 lines

Maybe "return !!dev" would be enough, then make the return a boolean? Then we can rename it to virtio_iommu_has_device().

PS. I think we can also drop IOMMU_PCI_DEVFN_MAX (after all you even didn't use it here!) and use PCI_DEVFN_MAX, and replace 0xFF.

well intel iommu and smmu use a similar constant (PCI_DEVFN_MAX, SMMU_PCI_DEVFN_MAX resp.). I use it in virtio_iommu_find_add_as

Signature · 1 line
Peter Xu
author
#17 lore raw

Re: [PATCH v12 05/13] virtio-iommu: Endpoint and domains structs and helpers

Hi Peter,

Quoted 50 lines · Peter Xu
Quoted 27 lines · Auger Eric

Hi Peter,

Quoted 23 lines · Peter Xu
Quoted 1 line · Auger Eric

Hi Peter,

Hi, Eric,

[...]

Quoted 14 lines
Quoted 11 lines

hw/virtio/virtio-iommu.c IOMMUMemoryRegion *virtio_iommu_mr(VirtIOIOMMU *s, uint32_t sid) · line 134

Quoted 9 lines
hw/virtio/virtio-iommu.c +8 -0
+{
+    VirtIOIOMMUEndpoint *ep;
+
+    ep = g_tree_lookup(s->endpoints, GUINT_TO_POINTER(ep_id));
+    if (ep) {
+        return ep;
+    }
+    if (!virtio_iommu_mr(s, ep_id)) {

Could I ask when this will trigger?

This can happen when a device is attached to a domain and its RID does not correspond to one of the devices protected by the iommu.

Quoted 8 lines
Quoted 1 line

So will it happen only because of a kernel driver bug?

Yes, at the moment, because virtio_iommu_mr() only gets called on device attach to a domain.

The spec says:
"If the endpoint identified by endpoint doesn’t exist, the device MUST reject the request and set status to VIRTIO_IOMMU_S_NOENT"

Sure. I just wanted to make sure I didn't miss anything, because I really can't see when this extra logic can help, say, right now we only have one vIOMMU at least for VT-d, so all devices will be managed by that. But yeah if that's explicitly mentioned in the spec, I'd agree we should follow that.

Quoted 7 lines
Quoted 3 lines

Also, I think the name of "virtio_iommu_mr" is confusing on that it returned explicitly a MemoryRegion however it's never been used:

I use the same prototype as for smmu_iommu_mr(). Returning the iommu mr will allow to proceed with further RID based operations like invalidations.

Quoted 7 lines
Quoted 1 line

The same logic is used in vtd_context_device_invalidate.

I'm fine with this. Let's keep virtio_iommu_mr() as you prefer.

Another thing I'd like to mention is that, I don't think "the same logic is used in VT-d" matters much. If we think something is wrong (even if it's the same in VT-d), why not we fix both? :-)

Sure ;-)

Thank you for your time

Eric

Quoted 34 lines

Thanks,

Quoted 30 lines
Quoted 23 lines

(since they're not in the same patch I'm pasting)

static IOMMUMemoryRegion *virtio_iommu_mr(VirtIOIOMMU *s, uint32_t sid)

{
    uint8_t bus_n, devfn;
    IOMMUPciBus *iommu_pci_bus;
    IOMMUDevice *dev;

    bus_n = PCI_BUS_NUM(sid);
    iommu_pci_bus = iommu_find_iommu_pcibus(s, bus_n);
    if (iommu_pci_bus) {
        devfn = sid & 0xFF;
        dev = iommu_pci_bus->pbdev[devfn];
        if (dev) {
            return &dev->iommu_mr;
        }
    }
    return NULL;
}
Quoted 5 lines

Maybe "return !!dev" would be enough, then make the return a boolean? Then we can rename it to virtio_iommu_has_device().

PS. I think we can also drop IOMMU_PCI_DEVFN_MAX (after all you even didn't use it here!) and use PCI_DEVFN_MAX, and replace 0xFF.

well intel iommu and smmu use a similar constant (PCI_DEVFN_MAX, SMMU_PCI_DEVFN_MAX resp.). I use it in virtio_iommu_find_add_as

author
#18 lore raw

Re: [PATCH v12 05/13] virtio-iommu: Endpoint and domains structs and helpers

In reply to Peter Xu

Hi Peter,

Quoted 47 lines · Peter Xu
Quoted 1 line · Auger Eric

Hi Peter,

Hi, Eric,

[...]

Quoted 12 lines
Quoted 11 lines

hw/virtio/virtio-iommu.c IOMMUMemoryRegion *virtio_iommu_mr(VirtIOIOMMU *s, uint32_t sid) · line 134

Quoted 9 lines
hw/virtio/virtio-iommu.c +8 -0
+{
+    VirtIOIOMMUEndpoint *ep;
+
+    ep = g_tree_lookup(s->endpoints, GUINT_TO_POINTER(ep_id));
+    if (ep) {
+        return ep;
+    }
+    if (!virtio_iommu_mr(s, ep_id)) {

Could I ask when this will trigger?

Quoted 2 lines

This can happen when a device is attached to a domain and its RID does not correspond to one of the devices protected by the iommu.

So will it happen only because of a kernel driver bug?

Also, I think the name of "virtio_iommu_mr" is confusing on that it returned explicitly a MemoryRegion however it's never been used:

(since they're not in the same patch I'm pasting)

static IOMMUMemoryRegion *virtio_iommu_mr(VirtIOIOMMU *s, uint32_t sid)

{
    uint8_t bus_n, devfn;
    IOMMUPciBus *iommu_pci_bus;
    IOMMUDevice *dev;

    bus_n = PCI_BUS_NUM(sid);
    iommu_pci_bus = iommu_find_iommu_pcibus(s, bus_n);
    if (iommu_pci_bus) {
        devfn = sid & 0xFF;
        dev = iommu_pci_bus->pbdev[devfn];
        if (dev) {
            return &dev->iommu_mr;
        }
    }
    return NULL;
}
Quoted 5 lines

Maybe "return !!dev" would be enough, then make the return a boolean? Then we can rename it to virtio_iommu_has_device().

PS. I think we can also drop IOMMU_PCI_DEVFN_MAX (after all you even didn't use it here!) and use PCI_DEVFN_MAX, and replace 0xFF.

Oh yes I can use PCI_DEVFN_MAX directly. Sorry.

Eric

Quoted 3 lines

Thanks,

author
#19 lore raw

PATCH v12 6/13 virtio-iommu: Implement attach/detach command

In reply to Eric Auger

This patch implements the endpoint attach/detach to/from a domain.

---

v11 -> v12:
- check the device is protected by the iommu on attach
- on detach, check the domain id the device is attached to matches the one used in the detach command
- fix mapping ref counter and destroy the domain when no end-points are attached anymore

---
 hw/virtio/virtio-iommu.c | 65 +++++++++++++++++++++++++++++++++++-----
 1 file changed, 58 insertions(+), 7 deletions(-)
hw/virtio/virtio-iommu.c +58 -7
index 6a03c3d7ae..63b2d30f28 100644
@@ -119,11 +119,12 @@ static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
 static void virtio_iommu_detach_endpoint_from_domain(VirtIOIOMMUEndpoint *ep)
 {
     QLIST_REMOVE(ep, next);
+    g_tree_unref(ep->domain->mappings);
     ep->domain = NULL;
 }
 
-VirtIOIOMMUEndpoint *virtio_iommu_get_endpoint(VirtIOIOMMU *s, uint32_t ep_id);
-VirtIOIOMMUEndpoint *virtio_iommu_get_endpoint(VirtIOIOMMU *s, uint32_t ep_id)
+static VirtIOIOMMUEndpoint *virtio_iommu_get_endpoint(VirtIOIOMMU *s,
+                                                      uint32_t ep_id)
 {
     VirtIOIOMMUEndpoint *ep;
 
@@ -151,8 +152,8 @@ static void virtio_iommu_put_endpoint(gpointer data)
     g_free(ep);
 }
 
-VirtIOIOMMUDomain *virtio_iommu_get_domain(VirtIOIOMMU *s, uint32_t domain_id);
-VirtIOIOMMUDomain *virtio_iommu_get_domain(VirtIOIOMMU *s, uint32_t domain_id)
+static VirtIOIOMMUDomain *virtio_iommu_get_domain(VirtIOIOMMU *s,
+                                                  uint32_t domain_id)
 {
     VirtIOIOMMUDomain *domain;
 
@@ -179,7 +180,6 @@ static void virtio_iommu_put_domain(gpointer data)
     QLIST_FOREACH_SAFE(iter, &domain->endpoint_list, next, tmp) {
         virtio_iommu_detach_endpoint_from_domain(iter);
     }
-    g_tree_destroy(domain->mappings);
     trace_virtio_iommu_put_domain(domain->id);
     g_free(domain);
 }
@@ -228,10 +228,37 @@ static int virtio_iommu_attach(VirtIOIOMMU *s,
 {
     uint32_t domain_id = le32_to_cpu(req->domain);
     uint32_t ep_id = le32_to_cpu(req->endpoint);
+    VirtIOIOMMUDomain *domain;
+    VirtIOIOMMUEndpoint *ep;
 
     trace_virtio_iommu_attach(domain_id, ep_id);
 
-    return VIRTIO_IOMMU_S_UNSUPP;
+    ep = virtio_iommu_get_endpoint(s, ep_id);
+    if (!ep) {
+        return VIRTIO_IOMMU_S_NOENT;
+    }
+
+    if (ep->domain) {
+        VirtIOIOMMUDomain *previous_domain = ep->domain;
+        /*
+         * the device is already attached to a domain,
+         * detach it first
+         */
+        virtio_iommu_detach_endpoint_from_domain(ep);
+        if (QLIST_EMPTY(&previous_domain->endpoint_list)) {
+            g_tree_remove(s->domains, GUINT_TO_POINTER(previous_domain->id));
+        }
+    }
+
+    domain = virtio_iommu_get_domain(s, domain_id);
+    if (!QLIST_EMPTY(&domain->endpoint_list)) {
+        g_tree_ref(domain->mappings);
+    }
+    QLIST_INSERT_HEAD(&domain->endpoint_list, ep, next);
+
+    ep->domain = domain;
+
+    return VIRTIO_IOMMU_S_OK;
 }
 
 static int virtio_iommu_detach(VirtIOIOMMU *s,
@@ -239,10 +266,34 @@ static int virtio_iommu_detach(VirtIOIOMMU *s,
 {
     uint32_t domain_id = le32_to_cpu(req->domain);
     uint32_t ep_id = le32_to_cpu(req->endpoint);
+    VirtIOIOMMUDomain *domain;
+    VirtIOIOMMUEndpoint *ep;
 
     trace_virtio_iommu_detach(domain_id, ep_id);
 
-    return VIRTIO_IOMMU_S_UNSUPP;
+    ep = g_tree_lookup(s->endpoints, GUINT_TO_POINTER(ep_id));
+    if (!ep) {
+        return VIRTIO_IOMMU_S_NOENT;
+    }
+
+    domain = ep->domain;
+
+    if (!domain || domain->id != domain_id) {
+        return VIRTIO_IOMMU_S_INVAL;
+    }
+
+    virtio_iommu_detach_endpoint_from_domain(ep);
+
+    /*
+     * when the last EP is detached, simply remove the domain for
+     * the domain list and destroy it. Note its mappings were already
+     * freed by the ref count mechanism. Next operation involving
+     * the same domain id will re-create one domain object.
+     */
+    if (QLIST_EMPTY(&domain->endpoint_list)) {
+        g_tree_remove(s->domains, GUINT_TO_POINTER(domain->id));
+    }
+    return VIRTIO_IOMMU_S_OK;
 }
 
 static int virtio_iommu_map(VirtIOIOMMU *s,
Signature · 1 line
2.20.1
author
#20 lore raw

PATCH v12 7/13 virtio-iommu: Implement map/unmap

In reply to Eric Auger

This patch implements virtio_iommu_map/unmap.

---

v11 -> v12:
- check unmanaged managed flags on map
- fix leak

v10 -> v11:
- revisit the implementation of unmap according to Peter's suggestion
- removed virt_addr and size from viommu_mapping struct
- use g_tree_lookup_extended()
- return VIRTIO_IOMMU_S_RANGE in case a mapping were
to be split on unmap (instead of INVAL)

v5 -> v6:
- use new v0.6 fields
- replace error_report by qemu_log_mask

v3 -> v4:
- implement unmap semantics as specified in v0.4

---
 hw/virtio/trace-events   |  1 +
 hw/virtio/virtio-iommu.c | 69 ++++++++++++++++++++++++++++++++++++++--
 2 files changed, 68 insertions(+), 2 deletions(-)
hw/virtio/trace-events +1 -0
index 15595f8cd7..22162d6583 100644
@@ -64,6 +64,7 @@ virtio_iommu_attach(uint32_t domain_id, uint32_t ep_id) "domain=%d endpoint=%d"
 virtio_iommu_detach(uint32_t domain_id, uint32_t ep_id) "domain=%d endpoint=%d"
 virtio_iommu_map(uint32_t domain_id, uint64_t virt_start, uint64_t virt_end, uint64_t phys_start, uint32_t flags) "domain=%d virt_start=0x%"PRIx64" virt_end=0x%"PRIx64 " phys_start=0x%"PRIx64" flags=%d"
 virtio_iommu_unmap(uint32_t domain_id, uint64_t virt_start, uint64_t virt_end) "domain=%d virt_start=0x%"PRIx64" virt_end=0x%"PRIx64
+virtio_iommu_unmap_done(uint32_t domain_id, uint64_t virt_start, uint64_t virt_end) "domain=%d virt_start=0x%"PRIx64" virt_end=0x%"PRIx64
 virtio_iommu_translate(const char *name, uint32_t rid, uint64_t iova, int flag) "mr=%s rid=%d addr=0x%"PRIx64" flag=%d"
 virtio_iommu_init_iommu_mr(char *iommu_mr) "init %s"
 virtio_iommu_get_endpoint(uint32_t ep_id) "Alloc endpoint=%d"
hw/virtio/virtio-iommu.c +67 -2

99 lines not on this page · raw message

Signature · 1 line
2.20.1
author
#22 lore raw

PATCH v12 8/13 virtio-iommu: Implement translate

In reply to Eric Auger

This patch implements the translate callback

---

v11 -> v12:
- Added Jean's R-b
- s/qemu_log_mask/error_report_once

v10 -> v11:
- take into account the new value struct and use
g_tree_lookup_extended
- switched to error_report_once

v6 -> v7:
- implemented bypass-mode

v5 -> v6:
- replace error_report by qemu_log_mask

v4 -> v5:
- check the device domain is not NULL
- s/printf/error_report
- set flags to IOMMU_NONE in case of all translation faults

---
 hw/virtio/trace-events   |  1 +
 hw/virtio/virtio-iommu.c | 60 +++++++++++++++++++++++++++++++++++++++-
 2 files changed, 60 insertions(+), 1 deletion(-)
hw/virtio/trace-events +1 -0
index 22162d6583..095aa8b509 100644
@@ -71,3 +71,4 @@ virtio_iommu_get_endpoint(uint32_t ep_id) "Alloc endpoint=%d"
 virtio_iommu_put_endpoint(uint32_t ep_id) "Free endpoint=%d"
 virtio_iommu_get_domain(uint32_t domain_id) "Alloc domain=%d"
 virtio_iommu_put_domain(uint32_t domain_id) "Free domain=%d"
+virtio_iommu_translate_out(uint64_t virt_addr, uint64_t phys_addr, uint32_t sid) "0x%"PRIx64" -> 0x%"PRIx64 " for sid=%d"
hw/virtio/virtio-iommu.c +59 -1

78 lines not on this page · raw message

Signature · 1 line
2.20.1
author
#23 lore raw

PATCH v12 9/13 virtio-iommu: Implement fault reporting

In reply to Eric Auger

The event queue allows to report asynchronous errors. The translate function now injects faults when relevant.

---

v11 -> v12:
- reporting the addr associated with the fault and set the VIRTIO_IOMMU_FAULT_F_ADDRESS flag.
- added cpu_to_le<n>

v10 -> v11:
- change a virtio_error into an error_report_once
(no buffer available for output faults)

---
 hw/virtio/trace-events   |  1 +
 hw/virtio/virtio-iommu.c | 73 +++++++++++++++++++++++++++++++++++++---
 2 files changed, 69 insertions(+), 5 deletions(-)
hw/virtio/trace-events +1 -0
index 095aa8b509..e83500bee9 100644
@@ -72,3 +72,4 @@ virtio_iommu_put_endpoint(uint32_t ep_id) "Free endpoint=%d"
 virtio_iommu_get_domain(uint32_t domain_id) "Alloc domain=%d"
 virtio_iommu_put_domain(uint32_t domain_id) "Free domain=%d"
 virtio_iommu_translate_out(uint64_t virt_addr, uint64_t phys_addr, uint32_t sid) "0x%"PRIx64" -> 0x%"PRIx64 " for sid=%d"
+virtio_iommu_report_fault(uint8_t reason, uint32_t flags, uint32_t endpoint, uint64_t addr) "FAULT reason=%d flags=%d endpoint=%d address =0x%"PRIx64
hw/virtio/virtio-iommu.c +68 -5

110 lines not on this page · raw message

Signature · 1 line
2.20.1
#24 lore raw

Re: [PATCH v12 09/13] virtio-iommu: Implement fault reporting

Quoted 49 lines · Eric Auger

The event queue allows to report asynchronous errors. The translate function now injects faults when relevant.

---

v11 -> v12:
- reporting the addr associated with the fault and set the VIRTIO_IOMMU_FAULT_F_ADDRESS flag.
- added cpu_to_le<n>

v10 -> v11:
- change a virtio_error into an error_report_once
(no buffer available for output faults)

---
 hw/virtio/trace-events   |  1 +
 hw/virtio/virtio-iommu.c | 73 +++++++++++++++++++++++++++++++++++++---
 2 files changed, 69 insertions(+), 5 deletions(-)
hw/virtio/trace-events +1 -0
index 095aa8b509..e83500bee9 100644
@@ -72,3 +72,4 @@ virtio_iommu_put_endpoint(uint32_t ep_id) "Free endpoint=%d"
 virtio_iommu_get_domain(uint32_t domain_id) "Alloc domain=%d"
 virtio_iommu_put_domain(uint32_t domain_id) "Free domain=%d"
 virtio_iommu_translate_out(uint64_t virt_addr, uint64_t phys_addr, uint32_t sid) "0x%"PRIx64" -> 0x%"PRIx64 " for sid=%d"
+virtio_iommu_report_fault(uint8_t reason, uint32_t flags, uint32_t endpoint, uint64_t addr) "FAULT reason=%d flags=%d endpoint=%d address =0x%"PRIx64
hw/virtio/virtio-iommu.c +12 -0
index d192bcb505..09193970ee 100644
@@ -477,6 +477,51 @@ out:
     }
 }
 
+static void virtio_iommu_report_fault(VirtIOIOMMU *viommu, uint8_t reason,
+                                      int flags, uint32_t endpoint,
+                                      uint64_t address)
+{
+    VirtIODevice *vdev = &viommu->parent_obj;
+    VirtQueue *vq = viommu->event_vq;
+    struct virtio_iommu_fault fault;
+    VirtQueueElement *elem;
+    size_t sz;
+
+    memset(&fault, 0, sizeof(fault));
+    fault.reason = reason;

hw/virtio/virtio-iommu.c out: · line 501

Quoted 10 lines
hw/virtio/virtio-iommu.c +10 -0
+    fault.flags = cpu_to_le32(flags);
+    fault.endpoint = cpu_to_le32(endpoint);
+    fault.address = cpu_to_le64(address);
+
+    for (;;) {
+        elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
+
+        if (!elem) {
+            error_report_once(
+                "no buffer available in event queue to report event");

(Should this also be a guest issue? IIRC you are still using a
mixture of both qemu_log_mask and error_report()... I'll stop commenting on this, assuming that you prefer both ways to be used...)

hw/virtio/virtio-iommu.c out: · line 509

Quoted 8 lines
hw/virtio/virtio-iommu.c +8 -0
+            return;
+        }
+
+        if (iov_size(elem->in_sg, elem->in_num) < sizeof(fault)) {
+            virtio_error(vdev, "error buffer of wrong size");
+            virtqueue_detach_element(vq, elem, 0);
+            g_free(elem);
+            continue;

If virtio_error(), should we stop rather than continue?

Quoted 84 lines
hw/virtio/virtio-iommu.c +38 -5
+        }
+        break;
+    }
+    /* we have a buffer to fill in */
+    sz = iov_from_buf(elem->in_sg, elem->in_num, 0,
+                      &fault, sizeof(fault));
+    assert(sz == sizeof(fault));
+
+    trace_virtio_iommu_report_fault(reason, flags, endpoint, address);
+    virtqueue_push(vq, elem, sz);
+    virtio_notify(vdev, vq);
+    g_free(elem);
+
+}
+
 static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemoryRegion *mr, hwaddr addr,
                                             IOMMUAccessFlags flag,
                                             int iommu_idx)
@@ -485,9 +530,10 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemoryRegion *mr, hwaddr addr,
     VirtIOIOMMUInterval interval, *mapping_key;
     VirtIOIOMMUMapping *mapping_value;
     VirtIOIOMMU *s = sdev->viommu;
+    bool read_fault, write_fault;
     VirtIOIOMMUEndpoint *ep;
+    uint32_t sid, flags;
     bool bypass_allowed;
-    uint32_t sid;
     bool found;
 
     interval.low = addr;
@@ -513,6 +559,9 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemoryRegion *mr, hwaddr addr,
     if (!ep) {
         if (!bypass_allowed) {
             error_report_once("%s sid=%d is not known!!", __func__, sid);
+            virtio_iommu_report_fault(s, VIRTIO_IOMMU_FAULT_R_UNKNOWN,
+                                      VIRTIO_IOMMU_FAULT_F_ADDRESS,
+                                      sid, addr);
         } else {
             entry.perm = flag;
         }
@@ -524,6 +573,9 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemoryRegion *mr, hwaddr addr,
             error_report_once("%s %02x:%02x.%01x not attached to any domain",
                               __func__, PCI_BUS_NUM(sid),
                               PCI_SLOT(sid), PCI_FUNC(sid));
+            virtio_iommu_report_fault(s, VIRTIO_IOMMU_FAULT_R_DOMAIN,
+                                      VIRTIO_IOMMU_FAULT_F_ADDRESS,
+                                      sid, addr);
         } else {
             entry.perm = flag;
         }
@@ -536,15 +588,26 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemoryRegion *mr, hwaddr addr,
     if (!found) {
         error_report_once("%s no mapping for 0x%"PRIx64" for sid=%d",
                           __func__, addr, sid);
+        virtio_iommu_report_fault(s, VIRTIO_IOMMU_FAULT_R_MAPPING,
+                                  VIRTIO_IOMMU_FAULT_F_ADDRESS,
+                                  sid, addr);
         goto unlock;
     }
 
-    if (((flag & IOMMU_RO) &&
-            !(mapping_value->flags & VIRTIO_IOMMU_MAP_F_READ)) ||
-        ((flag & IOMMU_WO) &&
-            !(mapping_value->flags & VIRTIO_IOMMU_MAP_F_WRITE))) {
+    read_fault = (flag & IOMMU_RO) &&
+                    !(mapping_value->flags & VIRTIO_IOMMU_MAP_F_READ);
+    write_fault = (flag & IOMMU_WO) &&
+                    !(mapping_value->flags & VIRTIO_IOMMU_MAP_F_WRITE);
+
+    flags = read_fault ? VIRTIO_IOMMU_FAULT_F_READ : 0;
+    flags |= write_fault ? VIRTIO_IOMMU_FAULT_F_WRITE : 0;
+    if (flags) {
         error_report_once("%s permission error on 0x%"PRIx64"(%d): allowed=%d",
                           __func__, addr, flag, mapping_value->flags);
+        flags |= VIRTIO_IOMMU_FAULT_F_ADDRESS;
+        virtio_iommu_report_fault(s, VIRTIO_IOMMU_FAULT_R_MAPPING,
+                                  flags | VIRTIO_IOMMU_FAULT_F_ADDRESS,
+                                  sid, addr);
         goto unlock;
     }
     entry.translated_addr = addr - mapping_key->low + mapping_value->phys_addr;
Signature · 1 line
2.20.1
Signature · 1 line
Peter Xu
author
#25 lore raw

Re: [PATCH v12 09/13] virtio-iommu: Implement fault reporting

Hi Peter,

Quoted 56 lines · Peter Xu
Quoted 55 lines · Eric Auger

The event queue allows to report asynchronous errors. The translate function now injects faults when relevant.

---

v11 -> v12:
- reporting the addr associated with the fault and set the VIRTIO_IOMMU_FAULT_F_ADDRESS flag.
- added cpu_to_le<n>

v10 -> v11:
- change a virtio_error into an error_report_once
(no buffer available for output faults)

---
 hw/virtio/trace-events   |  1 +
 hw/virtio/virtio-iommu.c | 73 +++++++++++++++++++++++++++++++++++++---
 2 files changed, 69 insertions(+), 5 deletions(-)
hw/virtio/trace-events +1 -0
index 095aa8b509..e83500bee9 100644
@@ -72,3 +72,4 @@ virtio_iommu_put_endpoint(uint32_t ep_id) "Free endpoint=%d"
 virtio_iommu_get_domain(uint32_t domain_id) "Alloc domain=%d"
 virtio_iommu_put_domain(uint32_t domain_id) "Free domain=%d"
 virtio_iommu_translate_out(uint64_t virt_addr, uint64_t phys_addr, uint32_t sid) "0x%"PRIx64" -> 0x%"PRIx64 " for sid=%d"
+virtio_iommu_report_fault(uint8_t reason, uint32_t flags, uint32_t endpoint, uint64_t addr) "FAULT reason=%d flags=%d endpoint=%d address =0x%"PRIx64
hw/virtio/virtio-iommu.c +18 -0
index d192bcb505..09193970ee 100644
@@ -477,6 +477,51 @@ out:
     }
 }
 
+static void virtio_iommu_report_fault(VirtIOIOMMU *viommu, uint8_t reason,
+                                      int flags, uint32_t endpoint,
+                                      uint64_t address)
+{
+    VirtIODevice *vdev = &viommu->parent_obj;
+    VirtQueue *vq = viommu->event_vq;
+    struct virtio_iommu_fault fault;
+    VirtQueueElement *elem;
+    size_t sz;
+
+    memset(&fault, 0, sizeof(fault));
+    fault.reason = reason;
+    fault.flags = cpu_to_le32(flags);
+    fault.endpoint = cpu_to_le32(endpoint);
+    fault.address = cpu_to_le64(address);
+
+    for (;;) {
+        elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
Quoted 8 lines

hw/virtio/virtio-iommu.c out: · line 501

Quoted 4 lines
hw/virtio/virtio-iommu.c +4 -0
+
+        if (!elem) {
+            error_report_once(
+                "no buffer available in event queue to report event");

(Should this also be a guest issue? IIRC you are still using a
mixture of both qemu_log_mask and error_report()... I'll stop commenting on this, assuming that you prefer both ways to be used...)

I've just removed the qemu_log_mask in virtio_iommu_unmap(). So now you should not find any qemu_log_mask anymore. Sorry for the oversight.

Quoted 11 lines

hw/virtio/virtio-iommu.c out: · line 509

Quoted 9 lines
hw/virtio/virtio-iommu.c +8 -0
+            return;
+        }
+
+        if (iov_size(elem->in_sg, elem->in_num) < sizeof(fault)) {
+            virtio_error(vdev, "error buffer of wrong size");
+            virtqueue_detach_element(vq, elem, 0);
+            g_free(elem);
+            continue;

If virtio_error(), should we stop rather than continue?

My understanding is the buffer just popped had a wrong size so it is not usable. We skip it we try to use another one if any. Does it make sense?

Thanks

Eric

Quoted 86 lines
Quoted 85 lines
hw/virtio/virtio-iommu.c +38 -5
+        }
+        break;
+    }
+    /* we have a buffer to fill in */
+    sz = iov_from_buf(elem->in_sg, elem->in_num, 0,
+                      &fault, sizeof(fault));
+    assert(sz == sizeof(fault));
+
+    trace_virtio_iommu_report_fault(reason, flags, endpoint, address);
+    virtqueue_push(vq, elem, sz);
+    virtio_notify(vdev, vq);
+    g_free(elem);
+
+}
+
 static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemoryRegion *mr, hwaddr addr,
                                             IOMMUAccessFlags flag,
                                             int iommu_idx)
@@ -485,9 +530,10 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemoryRegion *mr, hwaddr addr,
     VirtIOIOMMUInterval interval, *mapping_key;
     VirtIOIOMMUMapping *mapping_value;
     VirtIOIOMMU *s = sdev->viommu;
+    bool read_fault, write_fault;
     VirtIOIOMMUEndpoint *ep;
+    uint32_t sid, flags;
     bool bypass_allowed;
-    uint32_t sid;
     bool found;
 
     interval.low = addr;
@@ -513,6 +559,9 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemoryRegion *mr, hwaddr addr,
     if (!ep) {
         if (!bypass_allowed) {
             error_report_once("%s sid=%d is not known!!", __func__, sid);
+            virtio_iommu_report_fault(s, VIRTIO_IOMMU_FAULT_R_UNKNOWN,
+                                      VIRTIO_IOMMU_FAULT_F_ADDRESS,
+                                      sid, addr);
         } else {
             entry.perm = flag;
         }
@@ -524,6 +573,9 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemoryRegion *mr, hwaddr addr,
             error_report_once("%s %02x:%02x.%01x not attached to any domain",
                               __func__, PCI_BUS_NUM(sid),
                               PCI_SLOT(sid), PCI_FUNC(sid));
+            virtio_iommu_report_fault(s, VIRTIO_IOMMU_FAULT_R_DOMAIN,
+                                      VIRTIO_IOMMU_FAULT_F_ADDRESS,
+                                      sid, addr);
         } else {
             entry.perm = flag;
         }
@@ -536,15 +588,26 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemoryRegion *mr, hwaddr addr,
     if (!found) {
         error_report_once("%s no mapping for 0x%"PRIx64" for sid=%d",
                           __func__, addr, sid);
+        virtio_iommu_report_fault(s, VIRTIO_IOMMU_FAULT_R_MAPPING,
+                                  VIRTIO_IOMMU_FAULT_F_ADDRESS,
+                                  sid, addr);
         goto unlock;
     }
 
-    if (((flag & IOMMU_RO) &&
-            !(mapping_value->flags & VIRTIO_IOMMU_MAP_F_READ)) ||
-        ((flag & IOMMU_WO) &&
-            !(mapping_value->flags & VIRTIO_IOMMU_MAP_F_WRITE))) {
+    read_fault = (flag & IOMMU_RO) &&
+                    !(mapping_value->flags & VIRTIO_IOMMU_MAP_F_READ);
+    write_fault = (flag & IOMMU_WO) &&
+                    !(mapping_value->flags & VIRTIO_IOMMU_MAP_F_WRITE);
+
+    flags = read_fault ? VIRTIO_IOMMU_FAULT_F_READ : 0;
+    flags |= write_fault ? VIRTIO_IOMMU_FAULT_F_WRITE : 0;
+    if (flags) {
         error_report_once("%s permission error on 0x%"PRIx64"(%d): allowed=%d",
                           __func__, addr, flag, mapping_value->flags);
+        flags |= VIRTIO_IOMMU_FAULT_F_ADDRESS;
+        virtio_iommu_report_fault(s, VIRTIO_IOMMU_FAULT_R_MAPPING,
+                                  flags | VIRTIO_IOMMU_FAULT_F_ADDRESS,
+                                  sid, addr);
         goto unlock;
     }
     entry.translated_addr = addr - mapping_key->low + mapping_value->phys_addr;
Signature · 1 line
2.20.1
#26 lore raw

Re: [PATCH v12 09/13] virtio-iommu: Implement fault reporting

Quoted 4 lines · Auger Eric
Quoted 4 lines
Quoted 4 lines
hw/virtio/virtio-iommu.c +4 -0
+static void virtio_iommu_report_fault(VirtIOIOMMU *viommu, uint8_t reason,
+                                      int flags, uint32_t endpoint,
+                                      uint64_t address)
+{

[...]

Quoted 9 lines
Quoted 7 lines

hw/virtio/virtio-iommu.c out: · line 509

Quoted 5 lines
hw/virtio/virtio-iommu.c +5 -0
+        if (iov_size(elem->in_sg, elem->in_num) < sizeof(fault)) {
+            virtio_error(vdev, "error buffer of wrong size");
+            virtqueue_detach_element(vq, elem, 0);
+            g_free(elem);
+            continue;

If virtio_error(), should we stop rather than continue?

My understanding is the buffer just popped had a wrong size so it is not usable. We skip it we try to use another one if any. Does it make sense?

I'm not very familiar to virtio, but I see that virtio_error marks vdev->broken to true. If with that iiuc the next virtqueue_pop() will fail directly (see the first call to virtio_device_disabled in virtqueue_pop). Then I don't see why retry any more...

Thanks,

Signature · 1 line
Peter Xu
author
#27 lore raw

Re: [PATCH v12 09/13] virtio-iommu: Implement fault reporting

Hi Peter,

Quoted 14 lines · Peter Xu
Quoted 4 lines · Auger Eric
Quoted 4 lines
Quoted 4 lines
hw/virtio/virtio-iommu.c +4 -0
+static void virtio_iommu_report_fault(VirtIOIOMMU *viommu, uint8_t reason,
+                                      int flags, uint32_t endpoint,
+                                      uint64_t address)
+{

[...]

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
hw/virtio/virtio-iommu.c +5 -0
+        if (iov_size(elem->in_sg, elem->in_num) < sizeof(fault)) {
+            virtio_error(vdev, "error buffer of wrong size");
+            virtqueue_detach_element(vq, elem, 0);
+            g_free(elem);
+            continue;
Quoted 8 lines
Quoted 3 lines
Quoted 1 line

If virtio_error(), should we stop rather than continue?

My understanding is the buffer just popped had a wrong size so it is not usable. We skip it we try to use another one if any. Does it make sense?

I'm not very familiar to virtio, but I see that virtio_error marks vdev->broken to true. If with that iiuc the next virtqueue_pop() will fail directly (see the first call to virtio_device_disabled in virtqueue_pop). Then I don't see why retry any more...

You're right. I will fix it.

Thanks

Eric

Quoted 3 lines

Thanks,

author
#28 lore raw

PATCH v12 10/13 virtio-iommu-pci: Add virtio iommu pci support

In reply to Eric Auger

This patch adds virtio-iommu-pci, which is the pci proxy for the virtio-iommu device.

---

v11 -> v12:
- added Jean's R-b
- remove the array of intervals. Will be introduced later?

v10 -> v11:
- add the reserved_regions array property

v9 -> v10:
- include "hw/qdev-properties.h" header

v8 -> v9:
- add the msi-bypass property
- create virtio-iommu-pci.c

---
 hw/virtio/Makefile.objs          |  1 +
 hw/virtio/virtio-iommu-pci.c     | 88 ++++++++++++++++++++++++++++++++
 include/hw/pci/pci.h             |  1 +
 include/hw/virtio/virtio-iommu.h |  1 +
 qdev-monitor.c                   |  1 +
 5 files changed, 92 insertions(+)
 create mode 100644 hw/virtio/virtio-iommu-pci.c
hw/virtio/Makefile.objs +1 -0
index 2fd9da7410..4e4d39a0a4 100644
@@ -29,6 +29,7 @@ obj-$(CONFIG_VIRTIO_INPUT_HOST) += virtio-input-host-pci.o
 obj-$(CONFIG_VIRTIO_INPUT) += virtio-input-pci.o
 obj-$(CONFIG_VIRTIO_RNG) += virtio-rng-pci.o
 obj-$(CONFIG_VIRTIO_BALLOON) += virtio-balloon-pci.o
+obj-$(CONFIG_VIRTIO_IOMMU) += virtio-iommu-pci.o
 obj-$(CONFIG_VIRTIO_9P) += virtio-9p-pci.o
 obj-$(CONFIG_VIRTIO_SCSI) += virtio-scsi-pci.o
 obj-$(CONFIG_VIRTIO_BLK) += virtio-blk-pci.o
hw/virtio/virtio-iommu-pci.c new file +88 -0

88 lines not on this page · raw message

include/hw/pci/pci.h +1 -0
index 2acd8321af..cfedf5a995 100644
@@ -86,6 +86,7 @@ extern bool pci_available;
 #define PCI_DEVICE_ID_VIRTIO_9P          0x1009
 #define PCI_DEVICE_ID_VIRTIO_VSOCK       0x1012
 #define PCI_DEVICE_ID_VIRTIO_PMEM        0x1013
+#define PCI_DEVICE_ID_VIRTIO_IOMMU       0x1014
 
 #define PCI_VENDOR_ID_REDHAT             0x1b36
 #define PCI_DEVICE_ID_REDHAT_BRIDGE      0x0001
include/hw/virtio/virtio-iommu.h +1 -0
index c6c33b4af0..ce695bf827 100644
@@ -25,6 +25,7 @@
 #include "hw/pci/pci.h"
 
 #define TYPE_VIRTIO_IOMMU "virtio-iommu-device"
+#define TYPE_VIRTIO_IOMMU_PCI "virtio-iommu-device-base"
 #define VIRTIO_IOMMU(obj) \
         OBJECT_CHECK(VirtIOIOMMU, (obj), TYPE_VIRTIO_IOMMU)
 
qdev-monitor.c +1 -0
index 3465a1e2d0..97f4022b51 100644
@@ -66,6 +66,7 @@ static const QDevAlias qdev_alias_table[] = {
     { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_S390X },
     { "virtio-input-host-pci", "virtio-input-host",
             QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
+    { "virtio-iommu-pci", "virtio-iommu", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
     { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_S390X },
     { "virtio-keyboard-pci", "virtio-keyboard",
             QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Signature · 1 line
2.20.1
author
#29 lore raw

PATCH v12 11/13 hw/arm/virt: Add the virtio-iommu device tree mappings

In reply to Eric Auger

Adds the "virtio,pci-iommu" node in the host bridge node and the RID mapping, excluding the IOMMU RID.

---

v11 -> v12:
- Added Jean's R-b

v10 -> v11:
- remove msi_bypass

v8 -> v9:
- disable msi-bypass property
- addition of the subnode is handled is the hotplug handler and IOMMU RID is notimposed anymore

v6 -> v7:
- align to the smmu instantiation code

v4 -> v5:
- VirtMachineClass no_iommu added in this patch
- Use object_resolve_path_type

---
 hw/arm/virt.c         | 54 ++++++++++++++++++++++++++++++++++++-------
 include/hw/arm/virt.h |  2 ++
 2 files changed, 48 insertions(+), 8 deletions(-)
hw/arm/virt.c +46 -8
index 39ab5f47e0..689e5a3bd5 100644
@@ -32,6 +32,7 @@
 #include "qemu-common.h"
 #include "qemu/units.h"
 #include "qemu/option.h"
+#include "monitor/qdev.h"
 #include "qapi/error.h"
 #include "hw/sysbus.h"
 #include "hw/boards.h"
@@ -54,6 +55,7 @@
 #include "qemu/error-report.h"
 #include "qemu/module.h"
 #include "hw/pci-host/gpex.h"
+#include "hw/virtio/virtio-pci.h"
 #include "hw/arm/sysbus-fdt.h"
 #include "hw/platform-bus.h"
 #include "hw/qdev-properties.h"
@@ -71,6 +73,7 @@
 #include "hw/mem/pc-dimm.h"
 #include "hw/mem/nvdimm.h"
 #include "hw/acpi/generic_event_device.h"
+#include "hw/virtio/virtio-iommu.h"
 
 #define DEFINE_VIRT_MACHINE_LATEST(major, minor, latest) \
     static void virt_##major##_##minor##_class_init(ObjectClass *oc, \
@@ -1180,6 +1183,30 @@ static void create_smmu(const VirtMachineState *vms,
     g_free(node);
 }
 
+static void create_virtio_iommu(VirtMachineState *vms, Error **errp)
+{
+    const char compat[] = "virtio,pci-iommu";
+    uint16_t bdf = vms->virtio_iommu_bdf;
+    char *node;
+
+    vms->iommu_phandle = qemu_fdt_alloc_phandle(vms->fdt);
+
+    node = g_strdup_printf("%s/virtio_iommu@%d", vms->pciehb_nodename, bdf);
+    qemu_fdt_add_subnode(vms->fdt, node);
+    qemu_fdt_setprop(vms->fdt, node, "compatible", compat, sizeof(compat));
+    qemu_fdt_setprop_sized_cells(vms->fdt, node, "reg",
+                                 1, bdf << 8, 1, 0, 1, 0,
+                                 1, 0, 1, 0);
+
+    qemu_fdt_setprop_cell(vms->fdt, node, "#iommu-cells", 1);
+    qemu_fdt_setprop_cell(vms->fdt, node, "phandle", vms->iommu_phandle);
+    g_free(node);
+
+    qemu_fdt_setprop_cells(vms->fdt, vms->pciehb_nodename, "iommu-map",
+                           0x0, vms->iommu_phandle, 0x0, bdf,
+                           bdf + 1, vms->iommu_phandle, bdf + 1, 0xffff - bdf);
+}
+
 static void create_pcie(VirtMachineState *vms)
 {
     hwaddr base_mmio = vms->memmap[VIRT_PCIE_MMIO].base;
@@ -1258,7 +1285,7 @@ static void create_pcie(VirtMachineState *vms)
         }
     }
 
-    nodename = g_strdup_printf("/pcie@%" PRIx64, base);
+    nodename = vms->pciehb_nodename = g_strdup_printf("/pcie@%" PRIx64, base);
     qemu_fdt_add_subnode(vms->fdt, nodename);
     qemu_fdt_setprop_string(vms->fdt, nodename,
                             "compatible", "pci-host-ecam-generic");
@@ -1301,13 +1328,16 @@ static void create_pcie(VirtMachineState *vms)
     if (vms->iommu) {
         vms->iommu_phandle = qemu_fdt_alloc_phandle(vms->fdt);
 
-        create_smmu(vms, pci->bus);
-
-        qemu_fdt_setprop_cells(vms->fdt, nodename, "iommu-map",
-                               0x0, vms->iommu_phandle, 0x0, 0x10000);
+        switch (vms->iommu) {
+        case VIRT_IOMMU_SMMUV3:
+            create_smmu(vms, pci->bus);
+            qemu_fdt_setprop_cells(vms->fdt, nodename, "iommu-map",
+                                   0x0, vms->iommu_phandle, 0x0, 0x10000);
+            break;
+        default:
+            g_assert_not_reached();
+        }
     }
-
-    g_free(nodename);
 }
 
 static void create_platform_bus(VirtMachineState *vms)
@@ -1971,6 +2001,13 @@ static void virt_machine_device_plug_cb(HotplugHandler *hotplug_dev,
     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
         virt_memory_plug(hotplug_dev, dev, errp);
     }
+    if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) {
+        PCIDevice *pdev = PCI_DEVICE(dev);
+
+        vms->iommu = VIRT_IOMMU_VIRTIO;
+        vms->virtio_iommu_bdf = pci_get_bdf(pdev);
+        create_virtio_iommu(vms, errp);
+    }
 }
 
 static void virt_machine_device_unplug_request_cb(HotplugHandler *hotplug_dev,
@@ -1984,7 +2021,8 @@ static HotplugHandler *virt_machine_get_hotplug_handler(MachineState *machine,
                                                         DeviceState *dev)
 {
     if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE) ||
-       (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM))) {
+       (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) ||
+       (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI))) {
         return HOTPLUG_HANDLER(machine);
     }
 
include/hw/arm/virt.h +2 -0
index 38f0c33c77..165035fa8f 100644
@@ -124,8 +124,10 @@ typedef struct {
     bool virt;
     int32_t gic_version;
     VirtIOMMUType iommu;
+    uint16_t virtio_iommu_bdf;
     struct arm_boot_info bootinfo;
     MemMapEntry *memmap;
+    char *pciehb_nodename;
     const int *irqmap;
     int smp_cpus;
     void *fdt;
Signature · 1 line
2.20.1
author
#30 lore raw

PATCH v12 12/13 virtio-iommu: Support migration

In reply to Eric Auger

Add Migration support. We rely on recently added gtree and qlist migration. We only migrate the domain gtree. The endpoint gtree is re-constructed in a post-load operation.

---

v11 -> v12:
- do not migrate the endpoint gtree but reconstruct it from the domain gtree (Peter's suggestion)
- add MIG_PRI_IOMMU

---
 hw/virtio/virtio-iommu.c | 109 +++++++++++++++++++++++++++++++++++----
 1 file changed, 99 insertions(+), 10 deletions(-)
hw/virtio/virtio-iommu.c +99 -10
index 09193970ee..b0fde3af6f 100644
@@ -655,16 +655,6 @@ static uint64_t virtio_iommu_get_features(VirtIODevice *vdev, uint64_t f,
     return f;
 }
 
-/*
- * Migration is not yet supported: most of the state consists
- * of balanced binary trees which are not yet ready for getting
- * migrated
- */
-static const VMStateDescription vmstate_virtio_iommu_device = {
-    .name = "virtio-iommu-device",
-    .unmigratable = 1,
-};
-
 static gint int_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
 {
     guint ua = GPOINTER_TO_UINT(a);
@@ -748,9 +738,108 @@ static void virtio_iommu_instance_init(Object *obj)
 {
 }
 
+#define VMSTATE_INTERVAL                               \
+{                                                      \
+    .name = "interval",                                \
+    .version_id = 1,                                   \
+    .minimum_version_id = 1,                           \
+    .fields = (VMStateField[]) {                       \
+        VMSTATE_UINT64(low, VirtIOIOMMUInterval),      \
+        VMSTATE_UINT64(high, VirtIOIOMMUInterval),     \
+        VMSTATE_END_OF_LIST()                          \
+    }                                                  \
+}
+
+#define VMSTATE_MAPPING                               \
+{                                                     \
+    .name = "mapping",                                \
+    .version_id = 1,                                  \
+    .minimum_version_id = 1,                          \
+    .fields = (VMStateField[]) {                      \
+        VMSTATE_UINT64(phys_addr, VirtIOIOMMUMapping),\
+        VMSTATE_UINT32(flags, VirtIOIOMMUMapping),    \
+        VMSTATE_END_OF_LIST()                         \
+    },                                                \
+}
+
+static const VMStateDescription vmstate_interval_mapping[2] = {
+    VMSTATE_MAPPING,   /* value */
+    VMSTATE_INTERVAL   /* key   */
+};
+
+static int domain_preload(void *opaque)
+{
+    VirtIOIOMMUDomain *domain = opaque;
+
+    domain->mappings = g_tree_new_full((GCompareDataFunc)interval_cmp,
+                                       NULL, g_free, g_free);
+    return 0;
+}
+
+static const VMStateDescription vmstate_endpoint = {
+    .name = "endpoint",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT32(id, VirtIOIOMMUEndpoint),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static const VMStateDescription vmstate_domain = {
+    .name = "domain",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .pre_load = domain_preload,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT32(id, VirtIOIOMMUDomain),
+        VMSTATE_GTREE_V(mappings, VirtIOIOMMUDomain, 1,
+                        vmstate_interval_mapping,
+                        VirtIOIOMMUInterval, VirtIOIOMMUMapping),
+        VMSTATE_QLIST_V(endpoint_list, VirtIOIOMMUDomain, 1,
+                        vmstate_endpoint, VirtIOIOMMUEndpoint, next),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static gboolean reconstruct_endpoints(gpointer key, gpointer value,
+                                      gpointer data)
+{
+    VirtIOIOMMU *s = (VirtIOIOMMU *)data;
+    VirtIOIOMMUDomain *d = (VirtIOIOMMUDomain *)value;
+    VirtIOIOMMUEndpoint *iter;
+
+    QLIST_FOREACH(iter, &d->endpoint_list, next) {
+        iter->domain = d;
+        g_tree_insert(s->endpoints, GUINT_TO_POINTER(iter->id), iter);
+    }
+    return false; /* continue the domain traversal */
+}
+
+static int iommu_post_load(void *opaque, int version_id)
+{
+    VirtIOIOMMU *s = opaque;
+
+    g_tree_foreach(s->domains, reconstruct_endpoints, s);
+    return 0;
+}
+
+static const VMStateDescription vmstate_virtio_iommu_device = {
+    .name = "virtio-iommu-device",
+    .minimum_version_id = 1,
+    .version_id = 1,
+    .post_load = iommu_post_load,
+    .fields = (VMStateField[]) {
+        VMSTATE_GTREE_DIRECT_KEY_V(domains, VirtIOIOMMU, 1,
+                                   &vmstate_domain, VirtIOIOMMUDomain),
+        VMSTATE_END_OF_LIST()
+    },
+};
+
 static const VMStateDescription vmstate_virtio_iommu = {
     .name = "virtio-iommu",
     .minimum_version_id = 1,
+    .priority = MIG_PRI_IOMMU,
     .version_id = 1,
     .fields = (VMStateField[]) {
         VMSTATE_VIRTIO_DEVICE,
Signature · 1 line
2.20.1
Acked-by on v12 12/13
#31 lore raw

Re: [PATCH v12 12/13] virtio-iommu: Support migration

Quoted 5 lines · Eric Auger

Add Migration support. We rely on recently added gtree and qlist migration. We only migrate the domain gtree. The endpoint gtree is re-constructed in a post-load operation.

Signature · 1 line
Peter Xu
Reviewed-by on v12 12/13
#32 lore raw

Re: [PATCH v12 12/13] virtio-iommu: Support migration

In reply to Eric Auger
Quoted 5 lines · Eric Auger

Add Migration support. We rely on recently added gtree and qlist migration. We only migrate the domain gtree. The endpoint gtree is re-constructed in a post-load operation.

And yes, this is as confusing as it can be:
a tree of mappings (i.e. key,value)
a list of endpoints

I will propose this as most complex structure migrated ever.

Later, Juan.

author
#33 lore raw

Re: [PATCH v12 12/13] virtio-iommu: Support migration

Hi Juan,

Quoted 7 lines · Juan Quintela
Quoted 5 lines · Eric Auger

Add Migration support. We rely on recently added gtree and qlist migration. We only migrate the domain gtree. The endpoint gtree is re-constructed in a post-load operation.

Quoted 7 lines

And yes, this is as confusing as it can be:
a tree of mappings (i.e. key,value)
a list of endpoints

I will propose this as most complex structure migrated ever.

:-) Thank you Juan, Dave, and Peter for your support

Best Regards

Eric

Quoted 3 lines

Later, Juan.

author
#34 lore raw

Re: [PATCH v12 12/13] virtio-iommu: Support migration

Hi Juan,

Quoted 10 lines · Auger Eric

Hi Juan,

Quoted 7 lines · Juan Quintela
Quoted 5 lines · Eric Auger

Add Migration support. We rely on recently added gtree and qlist migration. We only migrate the domain gtree. The endpoint gtree is re-constructed in a post-load operation.

Quoted 8 lines
Quoted 7 lines

And yes, this is as confusing as it can be:
a tree of mappings (i.e. key,value)
a list of endpoints

I will propose this as most complex structure migrated ever.

:-) Thank you Juan, Dave, and Peter for your support

I did not notice this at the first glimpse but could you send the R-b on the v15. I have been running like a hamster on my wheel during last week.

Code has not changed though :-).

Thanks

Eric

Quoted 9 lines

Best Regards

Eric

Quoted 3 lines

Later, Juan.

#35 lore raw

Re: [PATCH v12 12/13] virtio-iommu: Support migration

Quoted 19 lines · Auger Eric

Hi Juan,

Quoted 16 lines · Auger Eric

Hi Juan,

Quoted 13 lines · Juan Quintela
Quoted 5 lines · Eric Auger

Add Migration support. We rely on recently added gtree and qlist migration. We only migrate the domain gtree. The endpoint gtree is re-constructed in a post-load operation.

And yes, this is as confusing as it can be:
a tree of mappings (i.e. key,value)
a list of endpoints

Quoted 7 lines
Quoted 2 lines
Quoted 1 line

I will propose this as most complex structure migrated ever.

:-) Thank you Juan, Dave, and Peter for your support

I did not notice this at the first glimpse but could you send the R-b on the v15. I have been running like a hamster on my wheel during last week.

Code has not changed though :-).

Sure.

Later, Juan.

author
#36 lore raw

PATCH v12 13/13 tests: Add virtio-iommu test

In reply to Eric Auger

This adds the framework to test the virtio-iommu-pci device and tests exercising the attach/detach, map/unmap API.

To run the tests:
make tests/qos-test QTEST_QEMU_BINARY=x86_64-softmmu/qemu-system-x86_64 tests/qos-test V=1

---
 tests/Makefile.include      |   2 +
 tests/libqos/virtio-iommu.c | 177 +++++++++++++++++++++
 tests/libqos/virtio-iommu.h |  45 ++++++
 tests/virtio-iommu-test.c   | 308 ++++++++++++++++++++++++++++++++++++
 4 files changed, 532 insertions(+)
 create mode 100644 tests/libqos/virtio-iommu.c
 create mode 100644 tests/libqos/virtio-iommu.h
 create mode 100644 tests/virtio-iommu-test.c
tests/Makefile.include +2 -0
index 49e3b0d319..049e06269d 100644
@@ -751,6 +751,7 @@ qos-test-obj-y += tests/libqos/virtio-net.o
 qos-test-obj-y += tests/libqos/virtio-pci.o
 qos-test-obj-y += tests/libqos/virtio-pci-modern.o
 qos-test-obj-y += tests/libqos/virtio-rng.o
+qos-test-obj-y += tests/libqos/virtio-iommu.o
 qos-test-obj-y += tests/libqos/virtio-scsi.o
 qos-test-obj-y += tests/libqos/virtio-serial.o
 
@@ -790,6 +791,7 @@ qos-test-obj-$(CONFIG_VIRTFS) += tests/virtio-9p-test.o
 qos-test-obj-y += tests/virtio-blk-test.o
 qos-test-obj-y += tests/virtio-net-test.o
 qos-test-obj-y += tests/virtio-rng-test.o
+qos-test-obj-y += tests/virtio-iommu-test.o
 qos-test-obj-y += tests/virtio-scsi-test.o
 qos-test-obj-y += tests/virtio-serial-test.o
 qos-test-obj-y += tests/vmxnet3-test.o
tests/libqos/virtio-iommu.c new file +177 -0

177 lines not on this page · raw message

tests/libqos/virtio-iommu.h new file +45 -0

45 lines not on this page · raw message

tests/virtio-iommu-test.c new file +308 -0

308 lines not on this page · raw message

Signature · 1 line
2.20.1
#37 lore raw
In reply to Eric Auger
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series failed the docker-quick@centos7 build test. Please find the testing commands and their output below. If you have Docker installed, you can probably reproduce it locally.

=== TEST SCRIPT BEGIN ===
#!/bin/bash
make docker-image-centos7 V=1 NETWORK=1
time make docker-test-quick@centos7 SHOW_ENV=1 J=14 NETWORK=1 === TEST SCRIPT END ===

TEST check-qtest-x86_64: tests/test-hmp
TEST check-qtest-x86_64: tests/qos-test
**
ERROR:/tmp/qemu-test/src/tests/virtio-iommu-test.c:46:pci_config: assertion failed (probe_size == 0x200): (0x00000000 == 0x00000200)
ERROR - Bail out! ERROR:/tmp/qemu-test/src/tests/virtio-iommu-test.c:46:pci_config: assertion failed (probe_size == 0x200): (0x00000000 == 0x00000200)
make: *** [check-qtest-x86_64] Error 1
make: *** Waiting for unfinished jobs....
TEST check-qtest-aarch64: tests/test-hmp
TEST iotest-qcow2: 220
---
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['sudo', '-n', 'docker', 'run', '--label', 'com.qemu.instance.uuid=de2154161ccf4468af5ebd35cbcbdc03', '-u', '1003', '--security-opt', 'seccomp=unconfined', '--rm', '-e', 'TARGET_LIST=', '-e', 'EXTRA_CONFIGURE_OPTS=', '-e', 'V=', '-e', 'J=14', '-e', 'DEBUG=', '-e', 'SHOW_ENV=1', '-e', 'CCACHE_DIR=/var/tmp/ccache', '-v', '/home/patchew2/.cache/qemu-docker-ccache:/var/tmp/ccache:z', '-v', '/var/tmp/patchew-tester-tmp-_ap6ulch/src/docker-src.2020-01-09-10.01.05.32472:/var/tmp/qemu:z,ro', 'qemu:centos7', '/var/tmp/qemu/run', 'test-quick']' returned non-zero exit status 2. filter=--filter=label=com.qemu.instance.uuid=de2154161ccf4468af5ebd35cbcbdc03
make[1]: *** [docker-run] Error 1
make[1]: Leaving directory `/var/tmp/patchew-tester-tmp-_ap6ulch/src'
make: *** [docker-run-test-quick@centos7] Error 2

real 11m50.898s
user 0m8.023s

The full log is available at
http://patchew.org/logs/[email protected]/testing.docker-quick@centos7/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]

author
#38 lore raw

Hi,

Quoted 16 lines · On 1/9/20 4:12 PM, [email protected] wrote:

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series failed the docker-quick@centos7 build test. Please find the testing commands and their output below. If you have Docker installed, you can probably reproduce it locally.

=== TEST SCRIPT BEGIN ===
#!/bin/bash
make docker-image-centos7 V=1 NETWORK=1
time make docker-test-quick@centos7 SHOW_ENV=1 J=14 NETWORK=1 === TEST SCRIPT END ===

Quoted 5 lines

TEST check-qtest-x86_64: tests/test-hmp
TEST check-qtest-x86_64: tests/qos-test
**
ERROR:/tmp/qemu-test/src/tests/virtio-iommu-test.c:46:pci_config: assertion failed (probe_size == 0x200): (0x00000000 == 0x00000200) ERROR - Bail out! ERROR:/tmp/qemu-test/src/tests/virtio-iommu-test.c:46:pci_config: assertion failed (probe_size == 0x200): (0x00000000 == 0x00000200)

OK sorry that's because I eventually removed "virtio-iommu: Implement probe request" patch from the sent series. I will remove that probe_size field test on next round.

Thanks

Eric

Quoted 22 lines

make: *** [check-qtest-x86_64] Error 1
make: *** Waiting for unfinished jobs....
TEST check-qtest-aarch64: tests/test-hmp
TEST iotest-qcow2: 220
---
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['sudo', '-n', 'docker', 'run', '--label', 'com.qemu.instance.uuid=de2154161ccf4468af5ebd35cbcbdc03', '-u', '1003', '--security-opt', 'seccomp=unconfined', '--rm', '-e', 'TARGET_LIST=', '-e', 'EXTRA_CONFIGURE_OPTS=', '-e', 'V=', '-e', 'J=14', '-e', 'DEBUG=', '-e', 'SHOW_ENV=1', '-e', 'CCACHE_DIR=/var/tmp/ccache', '-v', '/home/patchew2/.cache/qemu-docker-ccache:/var/tmp/ccache:z', '-v', '/var/tmp/patchew-tester-tmp-_ap6ulch/src/docker-src.2020-01-09-10.01.05.32472:/var/tmp/qemu:z,ro', 'qemu:centos7', '/var/tmp/qemu/run', 'test-quick']' returned non-zero exit status 2. filter=--filter=label=com.qemu.instance.uuid=de2154161ccf4468af5ebd35cbcbdc03
make[1]: *** [docker-run] Error 1
make[1]: Leaving directory `/var/tmp/patchew-tester-tmp-_ap6ulch/src'
make: *** [docker-run-test-quick@centos7] Error 2

real 11m50.898s
user 0m8.023s

The full log is available at
http://patchew.org/logs/[email protected]/testing.docker-quick@centos7/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]