Skip to contentSkip to conversation

vfio-user client

PATCH Superseded John Levon 3 participants 50 messages May 20, 2025 – Jun 5, 2025 Arm/Amlogic Meson SoC support

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

John Levon

author
#0 lore raw

v2 cover vfio-user client

The series contains an implement of a vfio-user client in QEMU, along with a few more preparatory patches.

The vfio-user protocol allows for implementing (PCI) devices in another userspace process; SPDK is one example, which includes a virtual NVMe implementation.

The vfio-user framework consists of 3 parts:
1) The VFIO user protocol specification.
2) A client - the VFIO device in QEMU that encapsulates VFIO messages and sends them to the server.
3) A server - a remote process that emulates a device.

This patchset implements parts 1 and 2.

It has been tested against libvfio-user test servers as well as SPDK.

A previous version of this series can be found at
https://lore.kernel.org/qemu-devel/[email protected]/T/#t

Changes since last series:

- rebased on v5 of vfio: return mr from vfio_get_xlat_addr
- added docs for VFIOIOMMUClass
- separated out vfio pci exports and renamed to vfio_pci namespace

John Levon (27):
vfio: add more VFIOIOMMUClass docs
vfio: move more cleanup into vfio_pci_put_device()
vfio: move config space read into vfio_pci_config_setup()
vfio: refactor out IRQ signalling setup
vfio: export PCI helpers needed for vfio-user
vfio: enable per-IRQ MSI-X masking
vfio: add per-region fd support
vfio: mark posted writes in region write callbacks vfio/container: pass MemoryRegion to DMA operations
vfio-user: add vfio-user class and container
vfio-user: connect vfio proxy to remote server
vfio-user: implement message receive infrastructure
vfio-user: implement message send infrastructure
vfio-user: implement VFIO_USER_DEVICE_GET_INFO
vfio-user: implement VFIO_USER_DEVICE_GET_REGION_INFO
vfio-user: implement VFIO_USER_REGION_READ/WRITE
vfio-user: set up PCI in vfio_user_pci_realize()
vfio-user: implement VFIO_USER_DEVICE_GET/SET_IRQ*
vfio-user: forward MSI-X PBA BAR accesses to server
vfio-user: set up container access to the proxy
vfio-user: implement VFIO_USER_DEVICE_RESET
vfio-user: implement VFIO_USER_DMA_MAP/UNMAP
vfio-user: implement VFIO_USER_DMA_READ/WRITE
vfio-user: add 'x-msg-timeout' option
vfio-user: support posted writes
vfio-user: add coalesced posted writes
docs: add vfio-user documentation

Steve Sistare (1):
vfio: return mr from vfio_get_xlat_addr

Thanos Makatos (1):
vfio-user: introduce vfio-user protocol specification

 MAINTAINERS                           |   10 +-
 docs/devel/index-internals.rst        |    1 +
 docs/devel/vfio-user.rst              | 1522 +++++++++++++++++++++++++
 docs/system/device-emulation.rst      |    1 +
 docs/system/devices/vfio-user.rst     |   24 +
 meson.build                           |    1 +
 hw/vfio-user/container.h              |   27 +
 hw/vfio-user/device.h                 |   27 +
 hw/vfio-user/protocol.h               |  245 ++++
 hw/vfio-user/proxy.h                  |  137 +++
 hw/vfio-user/trace.h                  |    1 +
 hw/vfio/pci.h                         |   16 +
 include/hw/vfio/vfio-container-base.h |   70 +-
 include/hw/vfio/vfio-device.h         |   15 +-
 include/hw/vfio/vfio-region.h         |    2 +
 include/system/memory.h               |   19 +-
 hw/vfio-user/container.c              |  348 ++++++
 hw/vfio-user/device.c                 |  389 +++++++
 hw/vfio-user/pci.c                    |  428 +++++++
 hw/vfio-user/proxy.c                  | 1314 +++++++++++++++++++++
 hw/vfio/container-base.c              |    4 +-
 hw/vfio/container.c                   |    3 +-
 hw/vfio/device.c                      |   57 +-
 hw/vfio/iommufd.c                     |    3 +-
 hw/vfio/listener.c                    |   39 +-
 hw/vfio/pci.c                         |  195 ++--
 hw/vfio/region.c                      |   10 +-
 hw/virtio/vhost-vdpa.c                |    9 +-
 system/memory.c                       |   32 +-
 hw/meson.build                        |    1 +
 hw/vfio-user/meson.build              |   11 +
 hw/vfio-user/trace-events             |   18 +
 hw/vfio/trace-events                  |    6 +-
 meson_options.txt                     |    2 +
 scripts/meson-buildoptions.sh         |    4 +
 35 files changed, 4845 insertions(+), 146 deletions(-)
 create mode 100644 docs/devel/vfio-user.rst
 create mode 100644 docs/system/devices/vfio-user.rst
 create mode 100644 hw/vfio-user/container.h
 create mode 100644 hw/vfio-user/device.h
 create mode 100644 hw/vfio-user/protocol.h
 create mode 100644 hw/vfio-user/proxy.h
 create mode 100644 hw/vfio-user/trace.h
 create mode 100644 hw/vfio-user/container.c
 create mode 100644 hw/vfio-user/device.c
 create mode 100644 hw/vfio-user/pci.c
 create mode 100644 hw/vfio-user/proxy.c
 create mode 100644 hw/vfio-user/meson.build
 create mode 100644 hw/vfio-user/trace-events
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 1/29 vfio: add more VFIOIOMMUClass docs

Add some additional doc comments for these class methods.

---
 include/hw/vfio/vfio-container-base.h | 64 ++++++++++++++++++++++++++-
 1 file changed, 63 insertions(+), 1 deletion(-)
include/hw/vfio/vfio-container-base.h +63 -1
index 3d392b0fd8..41c49b2aa5 100644
@@ -115,13 +115,52 @@ OBJECT_DECLARE_TYPE(VFIOContainerBase, VFIOIOMMUClass, VFIO_IOMMU)
 struct VFIOIOMMUClass {
     ObjectClass parent_class;
 
-    /* basic feature */
+    /**
+     * @setup
+     *
+     * Perform basic setup of the container. Returns true on success, or false
+     * with @errp filled in on failure.
+     *
+     * @bcontainer: #VFIOContainerBase
+     * @errp: error filled in on failure
+     */
     bool (*setup)(VFIOContainerBase *bcontainer, Error **errp);
+
+    /**
+     * @listener_begin
+     *
+     * Called at the beginning of an address space update transaction.
+     * See #MemoryListener.
+     *
+     * @bcontainer: #VFIOContainerBase
+     */
     void (*listener_begin)(VFIOContainerBase *bcontainer);
+
+    /**
+     * @listener_commit
+     *
+     * Called at the end of an address space update transaction,
+     * See #MemoryListener.
+     *
+     * @bcontainer: #VFIOContainerBase
+     */
     void (*listener_commit)(VFIOContainerBase *bcontainer);
+
+    /**
+     * @dma_map
+     *
+     * Map an address range into the container.
+     *
+     * @bcontainer: #VFIOContainerBase to use
+     * @iova: start address to map
+     * @size: size of the range to map
+     * @vaddr: process virtual address of mapping
+     * @readonly: true if mapping should be readonly
+     */
     int (*dma_map)(const VFIOContainerBase *bcontainer,
                    hwaddr iova, ram_addr_t size,
                    void *vaddr, bool readonly);
+
     /**
      * @dma_unmap
      *
@@ -136,8 +175,31 @@ struct VFIOIOMMUClass {
     int (*dma_unmap)(const VFIOContainerBase *bcontainer,
                      hwaddr iova, ram_addr_t size,
                      IOMMUTLBEntry *iotlb, bool unmap_all);
+
+
+    /**
+     * @attach_device
+     *
+     * Associate the given device with a container and do some related
+     * initialization of the device context. Returns true on success, or false
+     * with @errp filled in.
+     *
+     * @name: name of the device
+     * @vbasedev: the device
+     * @as: address space to use
+     * @errp: error filled in on failure
+     */
     bool (*attach_device)(const char *name, VFIODevice *vbasedev,
                           AddressSpace *as, Error **errp);
+
+    /*
+     * @detach_device
+     *
+     * Detach the given device from its container and clean up any necessary
+     * state.
+     *
+     * @vbasedev: the device to disassociate
+     */
     void (*detach_device)(VFIODevice *vbasedev);
 
     /* migration feature */
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v2 01/29] vfio: add more VFIOIOMMUClass docs

Quoted 12 lines · John Levon

Add some additional doc comments for these class methods.

---
  include/hw/vfio/vfio-container-base.h | 64 ++++++++++++++++++++++++++-
  1 file changed, 63 insertions(+), 1 deletion(-)
include/hw/vfio/vfio-container-base.h +0 -0
index 3d392b0fd8..41c49b2aa5 100644

include/hw/vfio/vfio-container-base.h OBJECT_DECLARE_TYPE(VFIOContainerBase, VFIOIOMMUClass, VFIO_IOMMU) · line 121

Quoted 8 lines
include/hw/vfio/vfio-container-base.h +4 -1
@@ -115,13 +115,52 @@ OBJECT_DECLARE_TYPE(VFIOContainerBase, VFIOIOMMUClass, VFIO_IOMMU)
  struct VFIOIOMMUClass {
      ObjectClass parent_class;
 
-    /* basic feature */
+    /**
+     * @setup
+     *
+     * Perform basic setup of the container. 

We could add a few details, something like :

"IOMMU capabilities, page sizes and IOVA ranges initialization."

Quoted 2 lines

Returns true on success, or false
+ * with @errp filled in on failure.

I would prefer the return statement to be at the end. See query_dirty_bitmap.

Quoted 26 lines
include/hw/vfio/vfio-container-base.h +23 -0
+     *
+     * @bcontainer: #VFIOContainerBase
+     * @errp: error filled in on failure
+     */
      bool (*setup)(VFIOContainerBase *bcontainer, Error **errp);
+
+    /**
+     * @listener_begin
+     *
+     * Called at the beginning of an address space update transaction.
+     * See #MemoryListener.
+     *
+     * @bcontainer: #VFIOContainerBase
+     */
      void (*listener_begin)(VFIOContainerBase *bcontainer);
+
+    /**
+     * @listener_commit
+     *
+     * Called at the end of an address space update transaction,
+     * See #MemoryListener.
+     *
+     * @bcontainer: #VFIOContainerBase
+     */
      void (*listener_commit)(VFIOContainerBase *bcontainer);
+

include/hw/vfio/vfio-container-base.h OBJECT_DECLARE_TYPE(VFIOContainerBase, VFIOIOMMUClass, VFIO_IOMMU) · line 158

Quoted 10 lines
include/hw/vfio/vfio-container-base.h +10 -0
+    /**
+     * @dma_map
+     *
+     * Map an address range into the container.
+     *
+     * @bcontainer: #VFIOContainerBase to use
+     * @iova: start address to map
+     * @size: size of the range to map
+     * @vaddr: process virtual address of mapping
+     * @readonly: true if mapping should be readonly

Returns zero to indicate success and negative for error.

In fact it returns -errno. Should be mentioned too.

Quoted 15 lines
include/hw/vfio/vfio-container-base.h +5 -0
+     */
      int (*dma_map)(const VFIOContainerBase *bcontainer,
                     hwaddr iova, ram_addr_t size,
                     void *vaddr, bool readonly);
+
      /**
       * @dma_unmap
       *
@@ -136,8 +175,31 @@ struct VFIOIOMMUClass {
      int (*dma_unmap)(const VFIOContainerBase *bcontainer,
                       hwaddr iova, ram_addr_t size,
                       IOMMUTLBEntry *iotlb, bool unmap_all);
+
+
+    /**

include/hw/vfio/vfio-container-base.h struct VFIOIOMMUClass { · line 190

Quoted 10 lines
include/hw/vfio/vfio-container-base.h +10 -0
+     * @attach_device
+     *
+     * Associate the given device with a container and do some related
+     * initialization of the device context. Returns true on success, or false
+     * with @errp filled in.
+     *
+     * @name: name of the device
+     * @vbasedev: the device
+     * @as: address space to use
+     * @errp: error filled in on failure

Return: true on success, else false setting @errp with error.

Quoted 4 lines
include/hw/vfio/vfio-container-base.h +3 -0
+     */
      bool (*attach_device)(const char *name, VFIODevice *vbasedev,>                             AddressSpace *as, Error **errp);
+
+    /*

include/hw/vfio/vfio-container-base.h struct VFIOIOMMUClass { · line 205

Quoted 10 lines
include/hw/vfio/vfio-container-base.h +7 -0
+     * @detach_device
+     *
+     * Detach the given device from its container and clean up any necessary
+     * state.
+     *
+     * @vbasedev: the device to disassociate
+     */
      void (*detach_device)(VFIODevice *vbasedev);
 
      /* migration feature */

Please resend independently of the series.

Thanks,

C.

author
#3 lore raw

PATCH v2 2/29 vfio: move more cleanup into vfio_pci_put_device()

In reply to John Levon

All of the cleanup can be done in the same place, and vfio-user will want to do the same.

---
 hw/vfio/pci.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
hw/vfio/pci.c +12 -11
index a1bfdfe375..d96b55f80c 100644
@@ -2854,6 +2854,18 @@ static bool vfio_populate_device(VFIOPCIDevice *vdev, Error **errp)
 
 static void vfio_pci_put_device(VFIOPCIDevice *vdev)
 {
+    vfio_display_finalize(vdev);
+    vfio_bars_finalize(vdev);
+    g_free(vdev->emulated_config_bits);
+    g_free(vdev->rom);
+    /*
+     * XXX Leaking igd_opregion is not an oversight, we can't remove the
+     * fw_cfg entry therefore leaking this allocation seems like the safest
+     * option.
+     *
+     * g_free(vdev->igd_opregion);
+     */
+
     vfio_device_detach(&vdev->vbasedev);
 
     g_free(vdev->vbasedev.name);
@@ -3302,17 +3314,6 @@ static void vfio_instance_finalize(Object *obj)
 {
     VFIOPCIDevice *vdev = VFIO_PCI_BASE(obj);
 
-    vfio_display_finalize(vdev);
-    vfio_bars_finalize(vdev);
-    g_free(vdev->emulated_config_bits);
-    g_free(vdev->rom);
-    /*
-     * XXX Leaking igd_opregion is not an oversight, we can't remove the
-     * fw_cfg entry therefore leaking this allocation seems like the safest
-     * option.
-     *
-     * g_free(vdev->igd_opregion);
-     */
     vfio_pci_put_device(vdev);
 }
 
Signature · 1 line
2.43.0
author
#4 lore raw

PATCH v2 3/29 vfio: move config space read into vfio_pci_config_setup()

In reply to John Levon

Small cleanup that reduces duplicate code for vfio-user and reduces the size of vfio_realize(); while we're here, correct that name to vfio_pci_realize().

---
 hw/vfio/pci.c | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)
hw/vfio/pci.c +16 -15
index d96b55f80c..a873f82aeb 100644
@@ -3017,6 +3017,19 @@ static bool vfio_pci_config_setup(VFIOPCIDevice *vdev, Error **errp)
 {
     PCIDevice *pdev = &vdev->pdev;
     VFIODevice *vbasedev = &vdev->vbasedev;
+    uint32_t config_space_size;
+    int ret;
+
+    config_space_size = MIN(pci_config_size(&vdev->pdev), vdev->config_size);
+
+    /* Get a copy of config space */
+    ret = vfio_pci_config_space_read(vdev, 0, config_space_size,
+                                     vdev->pdev.config);
+    if (ret < (int)config_space_size) {
+        ret = ret < 0 ? -ret : EFAULT;
+        error_setg_errno(errp, ret, "failed to read device config space");
+        return false;
+    }
 
     /* vfio emulates a lot for us, but some bits need extra love */
     vdev->emulated_config_bits = g_malloc0(vdev->config_size);
@@ -3138,15 +3151,14 @@ static bool vfio_interrupt_setup(VFIOPCIDevice *vdev, Error **errp)
     return true;
 }
 
-static void vfio_realize(PCIDevice *pdev, Error **errp)
+static void vfio_pci_realize(PCIDevice *pdev, Error **errp)
 {
     ERRP_GUARD();
     VFIOPCIDevice *vdev = VFIO_PCI_BASE(pdev);
     VFIODevice *vbasedev = &vdev->vbasedev;
-    int i, ret;
+    int i;
     char uuid[UUID_STR_LEN];
     g_autofree char *name = NULL;
-    uint32_t config_space_size;
 
     if (vbasedev->fd < 0 && !vbasedev->sysfsdev) {
         if (!(~vdev->host.domain || ~vdev->host.bus ||
@@ -3201,17 +3213,6 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
         goto error;
     }
 
-    config_space_size = MIN(pci_config_size(&vdev->pdev), vdev->config_size);
-
-    /* Get a copy of config space */
-    ret = vfio_pci_config_space_read(vdev, 0, config_space_size,
-                                     vdev->pdev.config);
-    if (ret < (int)config_space_size) {
-        ret = ret < 0 ? -ret : EFAULT;
-        error_setg_errno(errp, ret, "failed to read device config space");
-        goto error;
-    }
-
     if (!vfio_pci_config_setup(vdev, errp)) {
         goto error;
     }
@@ -3515,7 +3516,7 @@ static void vfio_pci_dev_class_init(ObjectClass *klass, const void *data)
     object_class_property_add_str(klass, "fd", NULL, vfio_pci_set_fd);
 #endif
     dc->desc = "VFIO-based PCI device assignment";
-    pdc->realize = vfio_realize;
+    pdc->realize = vfio_pci_realize;
 
     object_class_property_set_description(klass, /* 1.3 */
                                           "host",
Signature · 1 line
2.43.0
author
#5 lore raw

PATCH v2 4/29 vfio: refactor out IRQ signalling setup

In reply to John Levon

This makes for a slightly more readable vfio_msix_vector_do_use() implementation, and we will rely on this shortly.

---
 hw/vfio/pci.c | 35 ++++++++++++++++++++---------------
 1 file changed, 20 insertions(+), 15 deletions(-)
hw/vfio/pci.c +20 -15
index a873f82aeb..b1250d85bf 100644
@@ -511,6 +511,25 @@ static void vfio_update_kvm_msi_virq(VFIOMSIVector *vector, MSIMessage msg,
     kvm_irqchip_commit_routes(kvm_state);
 }
 
+static void set_irq_signalling(VFIODevice *vbasedev, VFIOMSIVector *vector,
+                               unsigned int nr)
+{
+    Error *err = NULL;
+    int32_t fd;
+
+    if (vector->virq >= 0) {
+        fd = event_notifier_get_fd(&vector->kvm_interrupt);
+    } else {
+        fd = event_notifier_get_fd(&vector->interrupt);
+    }
+
+    if (!vfio_device_irq_set_signaling(vbasedev, VFIO_PCI_MSIX_IRQ_INDEX, nr,
+                                       VFIO_IRQ_SET_ACTION_TRIGGER,
+                                       fd, &err)) {
+        error_reportf_err(err, VFIO_MSG_PREFIX, vbasedev->name);
+    }
+}
+
 static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
                                    MSIMessage *msg, IOHandler *handler)
 {
@@ -583,21 +602,7 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
                              strerror(-ret));
             }
         } else {
-            Error *err = NULL;
-            int32_t fd;
-
-            if (vector->virq >= 0) {
-                fd = event_notifier_get_fd(&vector->kvm_interrupt);
-            } else {
-                fd = event_notifier_get_fd(&vector->interrupt);
-            }
-
-            if (!vfio_device_irq_set_signaling(&vdev->vbasedev,
-                                        VFIO_PCI_MSIX_IRQ_INDEX, nr,
-                                        VFIO_IRQ_SET_ACTION_TRIGGER, fd,
-                                        &err)) {
-                error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
-            }
+            set_irq_signalling(&vdev->vbasedev, vector, nr);
         }
     }
 
Signature · 1 line
2.43.0
author
#6 lore raw

PATCH v2 5/29 vfio: export PCI helpers needed for vfio-user

In reply to John Levon

The vfio-user code will need to re-use various parts of the vfio PCI code. Export them in hw/vfio/pci.h, and rename them to the vfio_pci_* namespace.

---
 hw/vfio/pci.h        | 14 ++++++++++++
 hw/vfio/pci.c        | 54 ++++++++++++++++++++++----------------------
 hw/vfio/trace-events |  6 ++---
 3 files changed, 44 insertions(+), 30 deletions(-)
hw/vfio/pci.h +14 -0
index 5ce0fb916f..61fa385ddb 100644
@@ -248,4 +248,18 @@ void vfio_display_finalize(VFIOPCIDevice *vdev);
 
 extern const VMStateDescription vfio_display_vmstate;
 
+void vfio_pci_bars_exit(VFIOPCIDevice *vdev);
+bool vfio_pci_add_capabilities(VFIOPCIDevice *vdev, Error **errp);
+bool vfio_pci_config_setup(VFIOPCIDevice *vdev, Error **errp);
+Object *vfio_pci_get_object(VFIODevice *vbasedev);
+bool vfio_pci_interrupt_setup(VFIOPCIDevice *vdev, Error **errp);
+void vfio_pci_intx_eoi(VFIODevice *vbasedev);
+int vfio_pci_load_config(VFIODevice *vbasedev, QEMUFile *f);
+void vfio_pci_put_device(VFIOPCIDevice *vdev);
+bool vfio_pci_populate_device(VFIOPCIDevice *vdev, Error **errp);
+void vfio_pci_register_err_notifier(VFIOPCIDevice *vdev);
+void vfio_pci_register_req_notifier(VFIOPCIDevice *vdev);
+int vfio_pci_save_config(VFIODevice *vbasedev, QEMUFile *f, Error **errp);
+void vfio_pci_teardown_msi(VFIOPCIDevice *vdev);
+
 #endif /* HW_VFIO_VFIO_PCI_H */
hw/vfio/pci.c +27 -27

199 lines not on this page · raw message

hw/vfio/trace-events +3 -3
index e90ec9bff8..f06236f37b 100644
@@ -2,7 +2,7 @@
 
 # pci.c
 vfio_intx_interrupt(const char *name, char line) " (%s) Pin %c"
-vfio_intx_eoi(const char *name) " (%s) EOI"
+vfio_pci_intx_eoi(const char *name) " (%s) EOI"
 vfio_intx_enable_kvm(const char *name) " (%s) KVM INTx accel enabled"
 vfio_intx_disable_kvm(const char *name) " (%s) KVM INTx accel disabled"
 vfio_intx_update(const char *name, int new_irq, int target_irq) " (%s) IRQ moved %d -> %d"
@@ -35,8 +35,8 @@ vfio_pci_hot_reset(const char *name, const char *type) " (%s) %s"
 vfio_pci_hot_reset_has_dep_devices(const char *name) "%s: hot reset dependent devices:"
 vfio_pci_hot_reset_dep_devices(int domain, int bus, int slot, int function, int group_id) "\t%04x:%02x:%02x.%x group %d"
 vfio_pci_hot_reset_result(const char *name, const char *result) "%s hot reset: %s"
-vfio_populate_device_config(const char *name, unsigned long size, unsigned long offset, unsigned long flags) "Device '%s' config: size: 0x%lx, offset: 0x%lx, flags: 0x%lx"
-vfio_populate_device_get_irq_info_failure(const char *errstr) "VFIO_DEVICE_GET_IRQ_INFO failure: %s"
+vfio_pci_populate_device_config(const char *name, unsigned long size, unsigned long offset, unsigned long flags) "Device '%s' config: size: 0x%lx, offset: 0x%lx, flags: 0x%lx"
+vfio_pci_populate_device_get_irq_info_failure(const char *errstr) "VFIO_DEVICE_GET_IRQ_INFO failure: %s"
 vfio_mdev(const char *name, bool is_mdev) " (%s) is_mdev %d"
 vfio_add_ext_cap_dropped(const char *name, uint16_t cap, uint16_t offset) "%s 0x%x@0x%x"
 vfio_pci_reset(const char *name) " (%s)"
Signature · 1 line
2.43.0
#7 lore raw

Re: [PATCH v2 05/29] vfio: export PCI helpers needed for vfio-user

Quoted 23 lines · John Levon

The vfio-user code will need to re-use various parts of the vfio PCI code. Export them in hw/vfio/pci.h, and rename them to the vfio_pci_* namespace.

---
  hw/vfio/pci.h        | 14 ++++++++++++
  hw/vfio/pci.c        | 54 ++++++++++++++++++++++----------------------
  hw/vfio/trace-events |  6 ++---
  3 files changed, 44 insertions(+), 30 deletions(-)
hw/vfio/pci.h +4 -0
index 5ce0fb916f..61fa385ddb 100644
@@ -248,4 +248,18 @@ void vfio_display_finalize(VFIOPCIDevice *vdev);
 
  extern const VMStateDescription vfio_display_vmstate;
 
+void vfio_pci_bars_exit(VFIOPCIDevice *vdev);
+bool vfio_pci_add_capabilities(VFIOPCIDevice *vdev, Error **errp);
+bool vfio_pci_config_setup(VFIOPCIDevice *vdev, Error **errp);
+Object *vfio_pci_get_object(VFIODevice *vbasedev);

hw/vfio/pci.h void vfio_display_finalize(VFIOPCIDevice *vdev); · line 264

Quoted 10 lines
hw/vfio/pci.h +10 -0
+bool vfio_pci_interrupt_setup(VFIOPCIDevice *vdev, Error **errp);
+void vfio_pci_intx_eoi(VFIODevice *vbasedev);
+int vfio_pci_load_config(VFIODevice *vbasedev, QEMUFile *f);
+void vfio_pci_put_device(VFIOPCIDevice *vdev);
+bool vfio_pci_populate_device(VFIOPCIDevice *vdev, Error **errp);
+void vfio_pci_register_err_notifier(VFIOPCIDevice *vdev);
+void vfio_pci_register_req_notifier(VFIOPCIDevice *vdev);
+int vfio_pci_save_config(VFIODevice *vbasedev, QEMUFile *f, Error **errp);
+void vfio_pci_teardown_msi(VFIOPCIDevice *vdev);
+

The rename is a good thing. There are more to be done.

One comment,

These routines :

  Object *vfio_pci_get_object(VFIODevice *vbasedev);
  void vfio_pci_intx_eoi(VFIODevice *vbasedev);
  int vfio_pci_load_config(VFIODevice *vbasedev, QEMUFile *f);
  int vfio_pci_save_config(VFIODevice *vbasedev, QEMUFile *f, Error **errp);

are exported to simply define the VFIODeviceOps vfio_user_pci_ops struct (and they don't take a VFIOPCIDevice as first argument). I wonder if we could find a way to keep them private.

Also, does vfio-user support migration ? if not then, we only need to export vfio_pci_intx_eoi()

Thanks,

C.

Quoted 252 lines
hw/vfio/pci.h +0 -0
  #endif /* HW_VFIO_VFIO_PCI_H */
hw/vfio/pci.c +27 -27
index b1250d85bf..5159198bdb 100644
@@ -103,7 +103,7 @@ static void vfio_intx_interrupt(void *opaque)
      }
  }
 
-static void vfio_intx_eoi(VFIODevice *vbasedev)
+void vfio_pci_intx_eoi(VFIODevice *vbasedev)
  {
      VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
 
@@ -111,7 +111,7 @@ static void vfio_intx_eoi(VFIODevice *vbasedev)
          return;
      }
 
-    trace_vfio_intx_eoi(vbasedev->name);
+    trace_vfio_pci_intx_eoi(vbasedev->name);
 
      vdev->intx.pending = false;
      pci_irq_deassert(&vdev->pdev);
@@ -236,7 +236,7 @@ static void vfio_intx_update(VFIOPCIDevice *vdev, PCIINTxRoute *route)
      }
 
      /* Re-enable the interrupt in cased we missed an EOI */
-    vfio_intx_eoi(&vdev->vbasedev);
+    vfio_pci_intx_eoi(&vdev->vbasedev);
  }
 
  static void vfio_intx_routing_notifier(PCIDevice *pdev)
@@ -1743,7 +1743,7 @@ static bool vfio_msix_setup(VFIOPCIDevice *vdev, int pos, Error **errp)
      return true;
  }
 
-static void vfio_teardown_msi(VFIOPCIDevice *vdev)
+void vfio_pci_teardown_msi(VFIOPCIDevice *vdev)
  {
      msi_uninit(&vdev->pdev);
 
@@ -1839,7 +1839,7 @@ static void vfio_bars_register(VFIOPCIDevice *vdev)
      }
  }
 
-static void vfio_bars_exit(VFIOPCIDevice *vdev)
+void vfio_pci_bars_exit(VFIOPCIDevice *vdev)
  {
      int i;
 
@@ -2430,7 +2430,7 @@ static void vfio_add_ext_cap(VFIOPCIDevice *vdev)
      g_free(config);
  }
 
-static bool vfio_add_capabilities(VFIOPCIDevice *vdev, Error **errp)
+bool vfio_pci_add_capabilities(VFIOPCIDevice *vdev, Error **errp)
  {
      PCIDevice *pdev = &vdev->pdev;
 
@@ -2599,7 +2599,7 @@ static void vfio_pci_compute_needs_reset(VFIODevice *vbasedev)
      }
  }
 
-static Object *vfio_pci_get_object(VFIODevice *vbasedev)
+Object *vfio_pci_get_object(VFIODevice *vbasedev)
  {
      VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
 
@@ -2655,7 +2655,7 @@ static const VMStateDescription vmstate_vfio_pci_config = {
      }
  };
 
-static int vfio_pci_save_config(VFIODevice *vbasedev, QEMUFile *f, Error **errp)
+int vfio_pci_save_config(VFIODevice *vbasedev, QEMUFile *f, Error **errp)
  {
      VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
 
@@ -2663,7 +2663,7 @@ static int vfio_pci_save_config(VFIODevice *vbasedev, QEMUFile *f, Error **errp)
                                         errp);
  }
 
-static int vfio_pci_load_config(VFIODevice *vbasedev, QEMUFile *f)
+int vfio_pci_load_config(VFIODevice *vbasedev, QEMUFile *f)
  {
      VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
      PCIDevice *pdev = &vdev->pdev;
@@ -2706,7 +2706,7 @@ static int vfio_pci_load_config(VFIODevice *vbasedev, QEMUFile *f)
  static VFIODeviceOps vfio_pci_ops = {
      .vfio_compute_needs_reset = vfio_pci_compute_needs_reset,
      .vfio_hot_reset_multi = vfio_pci_hot_reset_multi,
-    .vfio_eoi = vfio_intx_eoi,
+    .vfio_eoi = vfio_pci_intx_eoi,
      .vfio_get_object = vfio_pci_get_object,
      .vfio_save_config = vfio_pci_save_config,
      .vfio_load_config = vfio_pci_load_config,
@@ -2777,7 +2777,7 @@ bool vfio_populate_vga(VFIOPCIDevice *vdev, Error **errp)
      return true;
  }
 
-static bool vfio_populate_device(VFIOPCIDevice *vdev, Error **errp)
+bool vfio_pci_populate_device(VFIOPCIDevice *vdev, Error **errp)
  {
      VFIODevice *vbasedev = &vdev->vbasedev;
      struct vfio_region_info *reg_info = NULL;
@@ -2823,7 +2823,7 @@ static bool vfio_populate_device(VFIOPCIDevice *vdev, Error **errp)
          return false;
      }
 
-    trace_vfio_populate_device_config(vdev->vbasedev.name,
+    trace_vfio_pci_populate_device_config(vdev->vbasedev.name,
                                        (unsigned long)reg_info->size,
                                        (unsigned long)reg_info->offset,
                                        (unsigned long)reg_info->flags);
@@ -2845,7 +2845,7 @@ static bool vfio_populate_device(VFIOPCIDevice *vdev, Error **errp)
      ret = vfio_device_get_irq_info(vbasedev, VFIO_PCI_ERR_IRQ_INDEX, &irq_info);
      if (ret) {
          /* This can fail for an old kernel or legacy PCI dev */
-        trace_vfio_populate_device_get_irq_info_failure(strerror(-ret));
+        trace_vfio_pci_populate_device_get_irq_info_failure(strerror(-ret));
      } else if (irq_info.count == 1) {
          vdev->pci_aer = true;
      } else {
@@ -2857,7 +2857,7 @@ static bool vfio_populate_device(VFIOPCIDevice *vdev, Error **errp)
      return true;
  }
 
-static void vfio_pci_put_device(VFIOPCIDevice *vdev)
+void vfio_pci_put_device(VFIOPCIDevice *vdev)
  {
      vfio_display_finalize(vdev);
      vfio_bars_finalize(vdev);
@@ -2905,7 +2905,7 @@ static void vfio_err_notifier_handler(void *opaque)
   * and continue after disabling error recovery support for the
   * device.
   */
-static void vfio_register_err_notifier(VFIOPCIDevice *vdev)
+void vfio_pci_register_err_notifier(VFIOPCIDevice *vdev)
  {
      Error *err = NULL;
      int32_t fd;
@@ -2964,7 +2964,7 @@ static void vfio_req_notifier_handler(void *opaque)
      }
  }
 
-static void vfio_register_req_notifier(VFIOPCIDevice *vdev)
+void vfio_pci_register_req_notifier(VFIOPCIDevice *vdev)
  {
      struct vfio_irq_info irq_info;
      Error *err = NULL;
@@ -3018,7 +3018,7 @@ static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)
      vdev->req_enabled = false;
  }
 
-static bool vfio_pci_config_setup(VFIOPCIDevice *vdev, Error **errp)
+bool vfio_pci_config_setup(VFIOPCIDevice *vdev, Error **errp)
  {
      PCIDevice *pdev = &vdev->pdev;
      VFIODevice *vbasedev = &vdev->vbasedev;
@@ -3124,7 +3124,7 @@ static bool vfio_pci_config_setup(VFIOPCIDevice *vdev, Error **errp)
      return true;
  }
 
-static bool vfio_interrupt_setup(VFIOPCIDevice *vdev, Error **errp)
+bool vfio_pci_interrupt_setup(VFIOPCIDevice *vdev, Error **errp)
  {
      PCIDevice *pdev = &vdev->pdev;
 
@@ -3214,7 +3214,7 @@ static void vfio_pci_realize(PCIDevice *pdev, Error **errp)
          goto error;
      }
 
-    if (!vfio_populate_device(vdev, errp)) {
+    if (!vfio_pci_populate_device(vdev, errp)) {
          goto error;
      }
 
@@ -3228,7 +3228,7 @@ static void vfio_pci_realize(PCIDevice *pdev, Error **errp)
          goto out_teardown;
      }
 
-    if (!vfio_add_capabilities(vdev, errp)) {
+    if (!vfio_pci_add_capabilities(vdev, errp)) {
          goto out_unset_idev;
      }
 
@@ -3244,7 +3244,7 @@ static void vfio_pci_realize(PCIDevice *pdev, Error **errp)
          vfio_bar_quirk_setup(vdev, i);
      }
 
-    if (!vfio_interrupt_setup(vdev, errp)) {
+    if (!vfio_pci_interrupt_setup(vdev, errp)) {
          goto out_unset_idev;
      }
 
@@ -3288,8 +3288,8 @@ static void vfio_pci_realize(PCIDevice *pdev, Error **errp)
          }
      }
 
-    vfio_register_err_notifier(vdev);
-    vfio_register_req_notifier(vdev);
+    vfio_pci_register_err_notifier(vdev);
+    vfio_pci_register_req_notifier(vdev);
      vfio_setup_resetfn_quirk(vdev);
 
      return;
@@ -3310,8 +3310,8 @@ out_unset_idev:
          pci_device_unset_iommu_device(pdev);
      }
  out_teardown:
-    vfio_teardown_msi(vdev);
-    vfio_bars_exit(vdev);
+    vfio_pci_teardown_msi(vdev);
+    vfio_pci_bars_exit(vdev);
  error:
      error_prepend(errp, VFIO_MSG_PREFIX, vbasedev->name);
  }
@@ -3338,9 +3338,9 @@ static void vfio_exitfn(PCIDevice *pdev)
      if (vdev->intx.mmap_timer) {
          timer_free(vdev->intx.mmap_timer);
      }
-    vfio_teardown_msi(vdev);
+    vfio_pci_teardown_msi(vdev);
      vfio_pci_disable_rp_atomics(vdev);
-    vfio_bars_exit(vdev);
+    vfio_pci_bars_exit(vdev);
      vfio_migration_exit(vbasedev);
      if (!vbasedev->mdev) {
          pci_device_unset_iommu_device(pdev);
hw/vfio/trace-events +3 -3
index e90ec9bff8..f06236f37b 100644
@@ -2,7 +2,7 @@
 
  # pci.c
  vfio_intx_interrupt(const char *name, char line) " (%s) Pin %c"
-vfio_intx_eoi(const char *name) " (%s) EOI"
+vfio_pci_intx_eoi(const char *name) " (%s) EOI"
  vfio_intx_enable_kvm(const char *name) " (%s) KVM INTx accel enabled"
  vfio_intx_disable_kvm(const char *name) " (%s) KVM INTx accel disabled"
  vfio_intx_update(const char *name, int new_irq, int target_irq) " (%s) IRQ moved %d -> %d"
@@ -35,8 +35,8 @@ vfio_pci_hot_reset(const char *name, const char *type) " (%s) %s"
  vfio_pci_hot_reset_has_dep_devices(const char *name) "%s: hot reset dependent devices:"
  vfio_pci_hot_reset_dep_devices(int domain, int bus, int slot, int function, int group_id) "\t%04x:%02x:%02x.%x group %d"
  vfio_pci_hot_reset_result(const char *name, const char *result) "%s hot reset: %s"
-vfio_populate_device_config(const char *name, unsigned long size, unsigned long offset, unsigned long flags) "Device '%s' config: size: 0x%lx, offset: 0x%lx, flags: 0x%lx"
-vfio_populate_device_get_irq_info_failure(const char *errstr) "VFIO_DEVICE_GET_IRQ_INFO failure: %s"
+vfio_pci_populate_device_config(const char *name, unsigned long size, unsigned long offset, unsigned long flags) "Device '%s' config: size: 0x%lx, offset: 0x%lx, flags: 0x%lx"
+vfio_pci_populate_device_get_irq_info_failure(const char *errstr) "VFIO_DEVICE_GET_IRQ_INFO failure: %s"
  vfio_mdev(const char *name, bool is_mdev) " (%s) is_mdev %d"
  vfio_add_ext_cap_dropped(const char *name, uint16_t cap, uint16_t offset) "%s 0x%x@0x%x"
  vfio_pci_reset(const char *name) " (%s)"
author
#8 lore raw

Re: [PATCH v2 05/29] vfio: export PCI helpers needed for vfio-user

Quoted 10 lines · Cédric Le Goater

These routines :

  Object *vfio_pci_get_object(VFIODevice *vbasedev);
  void vfio_pci_intx_eoi(VFIODevice *vbasedev);
  int vfio_pci_load_config(VFIODevice *vbasedev, QEMUFile *f);
  int vfio_pci_save_config(VFIODevice *vbasedev, QEMUFile *f, Error **errp);

are exported to simply define the VFIODeviceOps vfio_user_pci_ops struct (and they don't take a VFIOPCIDevice as first argument). I wonder if we could find a way to keep them private.

+static Object *vfio_user_pci_get_object(VFIODevice *vbasedev) +{
+ VFIOUserPCIDevice *vdev = container_of(vbasedev, VFIOUserPCIDevice,
+ device.vbasedev);
+
+ return OBJECT(vdev);
+}

seems to work.

Quoted 1 line

export vfio_pci_intx_eoi()

This is definitely required.

Quoted 1 line

Also, does vfio-user support migration ?

Not yet: we dropped support during the v2 changeover. Happy to drop the save/load config in the meantime.

regards
john

#9 lore raw

Re: [PATCH v2 05/29] vfio: export PCI helpers needed for vfio-user

Quoted 21 lines · John Levon
Quoted 10 lines · Cédric Le Goater

These routines :

   Object *vfio_pci_get_object(VFIODevice *vbasedev);
   void vfio_pci_intx_eoi(VFIODevice *vbasedev);
   int vfio_pci_load_config(VFIODevice *vbasedev, QEMUFile *f);
   int vfio_pci_save_config(VFIODevice *vbasedev, QEMUFile *f, Error **errp);

are exported to simply define the VFIODeviceOps vfio_user_pci_ops struct (and they don't take a VFIOPCIDevice as first argument). I wonder if we could find a way to keep them private.

(quoted diff) +7 -0
+static Object *vfio_user_pci_get_object(VFIODevice *vbasedev)
+{
+    VFIOUserPCIDevice *vdev = container_of(vbasedev, VFIOUserPCIDevice,
+                                           device.vbasedev);
+
+    return OBJECT(vdev);
+}
Quoted 5 lines

seems to work.

Quoted 1 line

export vfio_pci_intx_eoi()

This is definitely required.

yes. it makes sense.

Quoted 5 lines
Quoted 2 lines

Also, does vfio-user support migration ?

Not yet: we dropped support during the v2 changeover. Happy to drop the save/load config in the meantime.

Then please drop vfio_pci_get_object too.

Thanks,

C.

author
#10 lore raw

PATCH v2 6/29 vfio: enable per-IRQ MSI-X masking

In reply to John Levon

If VFIO_IRQ_INFO_MASKABLE is set for VFIO_PCI_MSIX_IRQ_INDEX, record this in ->can_mask_msix, and use it to individually mask MSI-X interrupts as needed.

---
 hw/vfio/pci.h                 |  1 +
 include/hw/vfio/vfio-device.h |  2 ++
 hw/vfio/device.c              | 26 +++++++++++++++++++
 hw/vfio/pci.c                 | 47 ++++++++++++++++++++++++++++++-----
 4 files changed, 70 insertions(+), 6 deletions(-)
hw/vfio/pci.h +1 -0
index 61fa385ddb..43c2d72b84 100644
@@ -191,6 +191,7 @@ struct VFIOPCIDevice {
     bool defer_kvm_irq_routing;
     bool clear_parent_atomics_on_exit;
     bool skip_vsc_check;
+    bool can_mask_msix;
     VFIODisplay *dpy;
     Notifier irqchip_change_notifier;
 };
include/hw/vfio/vfio-device.h +2 -0
index 8bcb3c19f6..923f9cd116 100644
@@ -133,7 +133,9 @@ struct VFIODeviceOps {
     (ret < 0 ? strerror(-ret) : "short write")
 
 void vfio_device_irq_disable(VFIODevice *vbasedev, int index);
+void vfio_device_irq_unmask_single(VFIODevice *vbasedev, int index, int irq);
 void vfio_device_irq_unmask(VFIODevice *vbasedev, int index);
+void vfio_device_irq_mask_single(VFIODevice *vbasedev, int index, int irq);
 void vfio_device_irq_mask(VFIODevice *vbasedev, int index);
 bool vfio_device_irq_set_signaling(VFIODevice *vbasedev, int index, int subindex,
                                    int action, int fd, Error **errp);
hw/vfio/device.c +26 -0
index 9fba2c7272..d0068086ae 100644
@@ -85,6 +85,19 @@ void vfio_device_irq_disable(VFIODevice *vbasedev, int index)
     vbasedev->io_ops->set_irqs(vbasedev, &irq_set);
 }
 
+void vfio_device_irq_unmask_single(VFIODevice *vbasedev, int index, int irq)
+{
+    struct vfio_irq_set irq_set = {
+        .argsz = sizeof(irq_set),
+        .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK,
+        .index = index,
+        .start = irq,
+        .count = 1,
+    };
+
+    vbasedev->io_ops->set_irqs(vbasedev, &irq_set);
+}
+
 void vfio_device_irq_unmask(VFIODevice *vbasedev, int index)
 {
     struct vfio_irq_set irq_set = {
@@ -98,6 +111,19 @@ void vfio_device_irq_unmask(VFIODevice *vbasedev, int index)
     vbasedev->io_ops->set_irqs(vbasedev, &irq_set);
 }
 
+void vfio_device_irq_mask_single(VFIODevice *vbasedev, int index, int irq)
+{
+    struct vfio_irq_set irq_set = {
+        .argsz = sizeof(irq_set),
+        .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_MASK,
+        .index = index,
+        .start = irq,
+        .count = 1,
+    };
+
+    vbasedev->io_ops->set_irqs(vbasedev, &irq_set);
+}
+
 void vfio_device_irq_mask(VFIODevice *vbasedev, int index)
 {
     struct vfio_irq_set irq_set = {
hw/vfio/pci.c +41 -6

96 lines not on this page · raw message

Signature · 1 line
2.43.0
#11 lore raw

Re: [PATCH v2 06/29] vfio: enable per-IRQ MSI-X masking

+Sẗeven

Quoted 8 lines · John Levon

If VFIO_IRQ_INFO_MASKABLE is set for VFIO_PCI_MSIX_IRQ_INDEX, record this in ->can_mask_msix, and use it to individually mask MSI-X interrupts as needed.

This one conflicts with the "live update" series. We will address it later.

Thanks,

C.

Quoted 185 lines
---
  hw/vfio/pci.h                 |  1 +
  include/hw/vfio/vfio-device.h |  2 ++
  hw/vfio/device.c              | 26 +++++++++++++++++++
  hw/vfio/pci.c                 | 47 ++++++++++++++++++++++++++++++-----
  4 files changed, 70 insertions(+), 6 deletions(-)
hw/vfio/pci.h +1 -0
index 61fa385ddb..43c2d72b84 100644
@@ -191,6 +191,7 @@ struct VFIOPCIDevice {
      bool defer_kvm_irq_routing;
      bool clear_parent_atomics_on_exit;
      bool skip_vsc_check;
+    bool can_mask_msix;
      VFIODisplay *dpy;
      Notifier irqchip_change_notifier;
  };
include/hw/vfio/vfio-device.h +2 -0
index 8bcb3c19f6..923f9cd116 100644
@@ -133,7 +133,9 @@ struct VFIODeviceOps {
      (ret < 0 ? strerror(-ret) : "short write")
 
  void vfio_device_irq_disable(VFIODevice *vbasedev, int index);
+void vfio_device_irq_unmask_single(VFIODevice *vbasedev, int index, int irq);
  void vfio_device_irq_unmask(VFIODevice *vbasedev, int index);
+void vfio_device_irq_mask_single(VFIODevice *vbasedev, int index, int irq);
  void vfio_device_irq_mask(VFIODevice *vbasedev, int index);
  bool vfio_device_irq_set_signaling(VFIODevice *vbasedev, int index, int subindex,
                                     int action, int fd, Error **errp);
hw/vfio/device.c +26 -0
index 9fba2c7272..d0068086ae 100644
@@ -85,6 +85,19 @@ void vfio_device_irq_disable(VFIODevice *vbasedev, int index)
      vbasedev->io_ops->set_irqs(vbasedev, &irq_set);
  }
 
+void vfio_device_irq_unmask_single(VFIODevice *vbasedev, int index, int irq)
+{
+    struct vfio_irq_set irq_set = {
+        .argsz = sizeof(irq_set),
+        .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK,
+        .index = index,
+        .start = irq,
+        .count = 1,
+    };
+
+    vbasedev->io_ops->set_irqs(vbasedev, &irq_set);
+}
+
  void vfio_device_irq_unmask(VFIODevice *vbasedev, int index)
  {
      struct vfio_irq_set irq_set = {
@@ -98,6 +111,19 @@ void vfio_device_irq_unmask(VFIODevice *vbasedev, int index)
      vbasedev->io_ops->set_irqs(vbasedev, &irq_set);
  }
 
+void vfio_device_irq_mask_single(VFIODevice *vbasedev, int index, int irq)
+{
+    struct vfio_irq_set irq_set = {
+        .argsz = sizeof(irq_set),
+        .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_MASK,
+        .index = index,
+        .start = irq,
+        .count = 1,
+    };
+
+    vbasedev->io_ops->set_irqs(vbasedev, &irq_set);
+}
+
  void vfio_device_irq_mask(VFIODevice *vbasedev, int index)
  {
      struct vfio_irq_set irq_set = {
hw/vfio/pci.c +41 -6
index 5159198bdb..ef38b4692a 100644
@@ -535,6 +535,7 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
  {
      VFIOPCIDevice *vdev = VFIO_PCI_BASE(pdev);
      VFIOMSIVector *vector;
+    bool new_vec = false;
      int ret;
      bool resizing = !!(vdev->nr_vectors < nr + 1);
 
@@ -549,6 +550,7 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
              error_report("vfio: Error: event_notifier_init failed");
          }
          vector->use = true;
+        new_vec = true;
          msix_vector_use(pdev, nr);
      }
 
@@ -575,6 +577,7 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
                  kvm_irqchip_commit_route_changes(&vfio_route_change);
                  vfio_connect_kvm_msi_virq(vector);
              }
+            new_vec = true;
          }
      }
 
@@ -584,6 +587,9 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
       * in use, so we shutdown and incrementally increase them as needed.
       * nr_vectors represents the total number of vectors allocated.
       *
+     * Otherwise, unmask the vector if the vector is already setup (and we can
+     * do so) or send the fd if not.
+     *
       * When dynamic allocation is supported, let the host only allocate
       * and enable a vector when it is in use in guest. nr_vectors represents
       * the upper bound of vectors being enabled (but not all of the ranges
@@ -594,13 +600,20 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
      }
 
      if (!vdev->defer_kvm_irq_routing) {
-        if (vdev->msix->noresize && resizing) {
-            vfio_device_irq_disable(&vdev->vbasedev, VFIO_PCI_MSIX_IRQ_INDEX);
-            ret = vfio_enable_vectors(vdev, true);
-            if (ret) {
-                error_report("vfio: failed to enable vectors, %s",
-                             strerror(-ret));
+        if (resizing) {
+            if (vdev->msix->noresize) {
+                vfio_device_irq_disable(&vdev->vbasedev,
+                                        VFIO_PCI_MSIX_IRQ_INDEX);
+                ret = vfio_enable_vectors(vdev, true);
+                if (ret) {
+                    error_report("vfio: failed to enable vectors, %d", ret);
+                }
+            } else {
+                set_irq_signalling(&vdev->vbasedev, vector, nr);
              }
+        } else if (vdev->can_mask_msix && !new_vec) {
+            vfio_device_irq_unmask_single(&vdev->vbasedev,
+                                          VFIO_PCI_MSIX_IRQ_INDEX, nr);
          } else {
              set_irq_signalling(&vdev->vbasedev, vector, nr);
          }
@@ -630,6 +643,13 @@ static void vfio_msix_vector_release(PCIDevice *pdev, unsigned int nr)
 
      trace_vfio_msix_vector_release(vdev->vbasedev.name, nr);
 
+    /* just mask vector if peer supports it */
+    if (vdev->can_mask_msix) {
+        vfio_device_irq_mask_single(&vdev->vbasedev, VFIO_PCI_MSIX_IRQ_INDEX,
+                                    nr);
+        return;
+    }
+
      /*
       * There are still old guests that mask and unmask vectors on every
       * interrupt.  If we're using QEMU bypass with a KVM irqfd, leave all of
@@ -702,6 +722,13 @@ static void vfio_msix_enable(VFIOPCIDevice *vdev)
              error_report("vfio: failed to enable vectors, %s",
                           strerror(-ret));
          }
+    } else if (vdev->can_mask_msix) {
+        /*
+         * If we can use single irq masking, send an invalid fd on vector 0
+         * to enable MSI-X without any vectors enabled.
+         */
+        vfio_device_irq_set_signaling(&vdev->vbasedev, VFIO_PCI_MSIX_IRQ_INDEX,
+                                      0, VFIO_IRQ_SET_ACTION_TRIGGER, -1, NULL);
      } else {
          /*
           * Some communication channels between VF & PF or PF & fw rely on the
@@ -2842,6 +2869,14 @@ bool vfio_pci_populate_device(VFIOPCIDevice *vdev, Error **errp)
          }
      }
 
+    ret = vfio_device_get_irq_info(vbasedev, VFIO_PCI_MSIX_IRQ_INDEX,
+                                   &irq_info);
+    if (ret == 0 && (irq_info.flags & VFIO_IRQ_INFO_MASKABLE)) {
+        vdev->can_mask_msix = true;
+    } else {
+        vdev->can_mask_msix = false;
+    }
+
      ret = vfio_device_get_irq_info(vbasedev, VFIO_PCI_ERR_IRQ_INDEX, &irq_info);
      if (ret) {
          /* This can fail for an old kernel or legacy PCI dev */
#12 lore raw

Re: [PATCH v2 06/29] vfio: enable per-IRQ MSI-X masking

Quoted 7 lines · Cédric Le Goater

+Sẗeven

Quoted 4 lines · John Levon

If VFIO_IRQ_INFO_MASKABLE is set for VFIO_PCI_MSIX_IRQ_INDEX, record this in ->can_mask_msix, and use it to individually mask MSI-X interrupts as needed.

Quoted 6 lines
Quoted 4 lines

This one conflicts with the "live update" series. We will address it later.

AFAICT there is only a trivial conflict versus my patch

vfio/pci: vfio_vector_init
https://lore.kernel.org/qemu-devel/[email protected]/

because I extract a handful of lines from vfio_msix_vector_do_use into a subroutine, at the same place vfio-user has added a local variable new_vec, but the new code will simply be:

vfio_msix_vector_do_use

    if (!vector->use) {
        new_vec = true;
        vfio_pci_vector_init(vdev, nr);
    }

Also, for now I am assuming that if vfio-user is being used, then I will add a CPR blocker. Thus I do not have to account for per-IRQ masking in the cpr load path, for now.

- Steve

Quoted 176 lines
Quoted 175 lines

---
  hw/vfio/pci.h                 |  1 + include/hw/vfio/vfio-device.h |  2 ++
  hw/vfio/device.c              | 26 +++++++++++++++++++ hw/vfio/pci.c                 | 47 ++++++++++++++++++++++++++++++-----

  4 files changed, 70 insertions(+), 6 deletions(-)
hw/vfio/pci.h +1 -0
index 61fa385ddb..43c2d72b84 100644
@@ -191,6 +191,7 @@ struct VFIOPCIDevice {
       bool defer_kvm_irq_routing;
       bool clear_parent_atomics_on_exit;
       bool skip_vsc_check;
+    bool can_mask_msix;
       VFIODisplay *dpy;
       Notifier irqchip_change_notifier;
   };
include/hw/vfio/vfio-device.h +2 -0
index 8bcb3c19f6..923f9cd116 100644
@@ -133,7 +133,9 @@ struct VFIODeviceOps {
       (ret < 0 ? strerror(-ret) : "short write")
   void vfio_device_irq_disable(VFIODevice *vbasedev, int index);
+void vfio_device_irq_unmask_single(VFIODevice *vbasedev, int index, int irq);
   void vfio_device_irq_unmask(VFIODevice *vbasedev, int index);
+void vfio_device_irq_mask_single(VFIODevice *vbasedev, int index, int irq);
   void vfio_device_irq_mask(VFIODevice *vbasedev, int index);
   bool vfio_device_irq_set_signaling(VFIODevice *vbasedev, int index, int subindex,
                                      int action, int fd, Error **errp);
hw/vfio/device.c +26 -0
index 9fba2c7272..d0068086ae 100644
@@ -85,6 +85,19 @@ void vfio_device_irq_disable(VFIODevice *vbasedev, int index)
       vbasedev->io_ops->set_irqs(vbasedev, &irq_set);
   }
+void vfio_device_irq_unmask_single(VFIODevice *vbasedev, int index, int irq)
+{
+    struct vfio_irq_set irq_set = {
+        .argsz = sizeof(irq_set),
+        .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK,
+        .index = index,
+        .start = irq,
+        .count = 1,
+    };
+
+    vbasedev->io_ops->set_irqs(vbasedev, &irq_set);
+}
+
   void vfio_device_irq_unmask(VFIODevice *vbasedev, int index)
   {
       struct vfio_irq_set irq_set = {
@@ -98,6 +111,19 @@ void vfio_device_irq_unmask(VFIODevice *vbasedev, int index)
       vbasedev->io_ops->set_irqs(vbasedev, &irq_set);
   }
+void vfio_device_irq_mask_single(VFIODevice *vbasedev, int index, int irq)
+{
+    struct vfio_irq_set irq_set = {
+        .argsz = sizeof(irq_set),
+        .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_MASK,
+        .index = index,
+        .start = irq,
+        .count = 1,
+    };
+
+    vbasedev->io_ops->set_irqs(vbasedev, &irq_set);
+}
+
   void vfio_device_irq_mask(VFIODevice *vbasedev, int index)
   {
       struct vfio_irq_set irq_set = {
hw/vfio/pci.c +41 -6
index 5159198bdb..ef38b4692a 100644
@@ -535,6 +535,7 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
   {
       VFIOPCIDevice *vdev = VFIO_PCI_BASE(pdev);
       VFIOMSIVector *vector;
+    bool new_vec = false;
       int ret;
       bool resizing = !!(vdev->nr_vectors < nr + 1);
@@ -549,6 +550,7 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
               error_report("vfio: Error: event_notifier_init failed");
           }
           vector->use = true;
+        new_vec = true;
           msix_vector_use(pdev, nr);
       }
@@ -575,6 +577,7 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
                   kvm_irqchip_commit_route_changes(&vfio_route_change);
                   vfio_connect_kvm_msi_virq(vector);
               }
+            new_vec = true;
           }
       }
@@ -584,6 +587,9 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
        * in use, so we shutdown and incrementally increase them as needed.
        * nr_vectors represents the total number of vectors allocated.
        *
+     * Otherwise, unmask the vector if the vector is already setup (and we can
+     * do so) or send the fd if not.
+     *
        * When dynamic allocation is supported, let the host only allocate
        * and enable a vector when it is in use in guest. nr_vectors represents
        * the upper bound of vectors being enabled (but not all of the ranges
@@ -594,13 +600,20 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
       }
       if (!vdev->defer_kvm_irq_routing) {
-        if (vdev->msix->noresize && resizing) {
-            vfio_device_irq_disable(&vdev->vbasedev, VFIO_PCI_MSIX_IRQ_INDEX);
-            ret = vfio_enable_vectors(vdev, true);
-            if (ret) {
-                error_report("vfio: failed to enable vectors, %s",
-                             strerror(-ret));
+        if (resizing) {
+            if (vdev->msix->noresize) {
+                vfio_device_irq_disable(&vdev->vbasedev,
+                                        VFIO_PCI_MSIX_IRQ_INDEX);
+                ret = vfio_enable_vectors(vdev, true);
+                if (ret) {
+                    error_report("vfio: failed to enable vectors, %d", ret);
+                }
+            } else {
+                set_irq_signalling(&vdev->vbasedev, vector, nr);
               }
+        } else if (vdev->can_mask_msix && !new_vec) {
+            vfio_device_irq_unmask_single(&vdev->vbasedev,
+                                          VFIO_PCI_MSIX_IRQ_INDEX, nr);
           } else {
               set_irq_signalling(&vdev->vbasedev, vector, nr);
           }
@@ -630,6 +643,13 @@ static void vfio_msix_vector_release(PCIDevice *pdev, unsigned int nr)
       trace_vfio_msix_vector_release(vdev->vbasedev.name, nr);
+    /* just mask vector if peer supports it */
+    if (vdev->can_mask_msix) {
+        vfio_device_irq_mask_single(&vdev->vbasedev, VFIO_PCI_MSIX_IRQ_INDEX,
+                                    nr);
+        return;
+    }
+
       /*
        * There are still old guests that mask and unmask vectors on every
        * interrupt.  If we're using QEMU bypass with a KVM irqfd, leave all of
@@ -702,6 +722,13 @@ static void vfio_msix_enable(VFIOPCIDevice *vdev)
               error_report("vfio: failed to enable vectors, %s",
                            strerror(-ret));
           }
+    } else if (vdev->can_mask_msix) {
+        /*
+         * If we can use single irq masking, send an invalid fd on vector 0
+         * to enable MSI-X without any vectors enabled.
+         */
+        vfio_device_irq_set_signaling(&vdev->vbasedev, VFIO_PCI_MSIX_IRQ_INDEX,
+                                      0, VFIO_IRQ_SET_ACTION_TRIGGER, -1, NULL);
       } else {
           /*
            * Some communication channels between VF & PF or PF & fw rely on the
@@ -2842,6 +2869,14 @@ bool vfio_pci_populate_device(VFIOPCIDevice *vdev, Error **errp)
           }
       }
+    ret = vfio_device_get_irq_info(vbasedev, VFIO_PCI_MSIX_IRQ_INDEX,
+                                   &irq_info);
+    if (ret == 0 && (irq_info.flags & VFIO_IRQ_INFO_MASKABLE)) {
+        vdev->can_mask_msix = true;
+    } else {
+        vdev->can_mask_msix = false;
+    }
+
       ret = vfio_device_get_irq_info(vbasedev, VFIO_PCI_ERR_IRQ_INDEX, &irq_info);
       if (ret) {
           /* This can fail for an old kernel or legacy PCI dev */
author
#13 lore raw

Re: [PATCH v2 06/29] vfio: enable per-IRQ MSI-X masking

Quoted 21 lines · Steven Sistare
Quoted 11 lines · John Levon
Quoted 8 lines

If VFIO_IRQ_INFO_MASKABLE is set for VFIO_PCI_MSIX_IRQ_INDEX, record this in ->can_mask_msix, and use it to individually mask MSI-X interrupts as needed.

This one conflicts with the "live update" series. We will address it later.

AFAICT there is only a trivial conflict versus my patch

vfio/pci: vfio_vector_init
https://urldefense.proofpoint.com/v2/url?u=https-3A__lore.kernel.org_qemu-2Ddevel_1747063973-2D124548-2D17-2Dgit-2Dsend-2Demail-2Dsteven.sistare-40oracle.com_&d=DwIDaQ&c=s883GpUCOChKOHiocYtGcg&r=v7SNLJqx7b9Vfc7ZO82Wg4nnZ8O5XkACFQ30bVKxotI&m=us5Q7sdvPzuJ8qd0-KHuGcuoxQPDRJYPJ9dFmIeHxb0i8WnpPqrLiu3UuGSoq95U&s=Pd5fbQX8J7_CHHWD16ZuvXKJ13kipONxypuqS_cjGI0&e=

because I extract a handful of lines from vfio_msix_vector_do_use into a subroutine, at the same place vfio-user has added a local variable new_vec, but the new code will simply be:

Quoted 5 lines

vfio_msix_vector_do_use

    if (!vector->use) {
        new_vec = true;
        vfio_pci_vector_init(vdev, nr);
    }

I'll take a look when rebasing on top of CPR and ask you if I get stuck.

Quoted 2 lines

Also, for now I am assuming that if vfio-user is being used, then I will add a CPR blocker.

That's fine (and I will take care of it in my series), but I *think* this per-interrupt masking can apply outside of vfio-user.

regards
john

author
#14 lore raw

PATCH v2 7/29 vfio: add per-region fd support

In reply to John Levon

For vfio-user, each region has its own fd rather than sharing vbasedev's. Add the necessary plumbing to support this. For vfio backends with a shared fd, initialize region->fd to the shared one.

---
 include/hw/vfio/vfio-device.h |  7 +++++--
 include/hw/vfio/vfio-region.h |  1 +
 hw/vfio/device.c              | 28 ++++++++++++++++++++++++++--
 hw/vfio/region.c              |  7 ++++++-
 4 files changed, 38 insertions(+), 5 deletions(-)
include/hw/vfio/vfio-device.h +5 -2
index 923f9cd116..5cb817fd6a 100644
@@ -66,6 +66,7 @@ typedef struct VFIODevice {
     OnOffAuto enable_migration;
     OnOffAuto migration_multifd_transfer;
     bool migration_events;
+    bool use_region_fds;
     VFIODeviceOps *ops;
     VFIODeviceIOOps *io_ops;
     unsigned int num_irqs;
@@ -84,6 +85,7 @@ typedef struct VFIODevice {
     VFIOIOASHwpt *hwpt;
     QLIST_ENTRY(VFIODevice) hwpt_next;
     struct vfio_region_info **reginfo;
+    int *region_fds;
 } VFIODevice;
 
 struct VFIODeviceOps {
@@ -172,10 +174,11 @@ struct VFIODeviceIOOps {
     /**
      * @get_region_info
      *
-     * Fill in @info with information on the region given by @info->index.
+     * Fill in @info (and optionally @fd) with information on the region given
+     * by @info->index.
      */
     int (*get_region_info)(VFIODevice *vdev,
-                           struct vfio_region_info *info);
+                           struct vfio_region_info *info, int *fd);
 
     /**
      * @get_irq_info
include/hw/vfio/vfio-region.h +1 -0
index cbffb26962..80e83b23fd 100644
@@ -29,6 +29,7 @@ typedef struct VFIORegion {
     uint32_t nr_mmaps;
     VFIOMmap *mmaps;
     uint8_t nr; /* cache the region number for debug */
+    int fd; /* fd to mmap() region */
 } VFIORegion;
 
 
hw/vfio/device.c +26 -2
index d0068086ae..41db403992 100644
@@ -226,6 +226,7 @@ int vfio_device_get_region_info(VFIODevice *vbasedev, int index,
                                 struct vfio_region_info **info)
 {
     size_t argsz = sizeof(struct vfio_region_info);
+    int fd = -1;
     int ret;
 
     /* check cache */
@@ -240,7 +241,7 @@ int vfio_device_get_region_info(VFIODevice *vbasedev, int index,
 retry:
     (*info)->argsz = argsz;
 
-    ret = vbasedev->io_ops->get_region_info(vbasedev, *info);
+    ret = vbasedev->io_ops->get_region_info(vbasedev, *info, &fd);
     if (ret != 0) {
         g_free(*info);
         *info = NULL;
@@ -251,11 +252,19 @@ retry:
         argsz = (*info)->argsz;
         *info = g_realloc(*info, argsz);
 
+        if (fd != -1) {
+            close(fd);
+            fd = -1;
+        }
+
         goto retry;
     }
 
     /* fill cache */
     vbasedev->reginfo[index] = *info;
+    if (vbasedev->region_fds != NULL) {
+        vbasedev->region_fds[index] = fd;
+    }
 
     return 0;
 }
@@ -360,6 +369,7 @@ void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops,
     vbasedev->io_ops = &vfio_device_io_ops_ioctl;
     vbasedev->dev = dev;
     vbasedev->fd = -1;
+    vbasedev->use_region_fds = false;
 
     vbasedev->ram_block_discard_allowed = ram_discard;
 }
@@ -470,6 +480,9 @@ void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainerBase *bcontainer,
 
     vbasedev->reginfo = g_new0(struct vfio_region_info *,
                                vbasedev->num_regions);
+    if (vbasedev->use_region_fds) {
+        vbasedev->region_fds = g_new0(int, vbasedev->num_regions);
+    }
 }
 
 void vfio_device_unprepare(VFIODevice *vbasedev)
@@ -478,9 +491,17 @@ void vfio_device_unprepare(VFIODevice *vbasedev)
 
     for (i = 0; i < vbasedev->num_regions; i++) {
         g_free(vbasedev->reginfo[i]);
+        if (vbasedev->region_fds != NULL && vbasedev->region_fds[i] != -1) {
+            close(vbasedev->region_fds[i]);
+        }
+
     }
     g_free(vbasedev->reginfo);
     vbasedev->reginfo = NULL;
+    if (vbasedev->region_fds != NULL) {
+        g_free(vbasedev->region_fds);
+        vbasedev->region_fds = NULL;
+    }
 
     QLIST_REMOVE(vbasedev, container_next);
     QLIST_REMOVE(vbasedev, global_next);
@@ -502,10 +523,13 @@ static int vfio_device_io_device_feature(VFIODevice *vbasedev,
 }
 
 static int vfio_device_io_get_region_info(VFIODevice *vbasedev,
-                                          struct vfio_region_info *info)
+                                          struct vfio_region_info *info,
+                                          int *fd)
 {
     int ret;
 
+    *fd = -1;
+
     ret = ioctl(vbasedev->fd, VFIO_DEVICE_GET_REGION_INFO, info);
 
     return ret < 0 ? -errno : ret;
hw/vfio/region.c +6 -1
index 34752c3f65..3c93da91d8 100644
@@ -200,6 +200,11 @@ int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
     region->size = info->size;
     region->fd_offset = info->offset;
     region->nr = index;
+    if (vbasedev->region_fds != NULL) {
+        region->fd = vbasedev->region_fds[index];
+    } else {
+        region->fd = vbasedev->fd;
+    }
 
     if (region->size) {
         region->mem = g_new0(MemoryRegion, 1);
@@ -278,7 +283,7 @@ int vfio_region_mmap(VFIORegion *region)
 
         region->mmaps[i].mmap = mmap(map_align, region->mmaps[i].size, prot,
                                      MAP_SHARED | MAP_FIXED,
-                                     region->vbasedev->fd,
+                                     region->fd,
                                      region->fd_offset +
                                      region->mmaps[i].offset);
         if (region->mmaps[i].mmap == MAP_FAILED) {
Signature · 1 line
2.43.0
#15 lore raw

Re: [PATCH v2 07/29] vfio: add per-region fd support

Quoted 33 lines · John Levon

For vfio-user, each region has its own fd rather than sharing vbasedev's. Add the necessary plumbing to support this. For vfio backends with a shared fd, initialize region->fd to the shared one.

---
  include/hw/vfio/vfio-device.h |  7 +++++--
  include/hw/vfio/vfio-region.h |  1 +
  hw/vfio/device.c              | 28 ++++++++++++++++++++++++++--
  hw/vfio/region.c              |  7 ++++++-
  4 files changed, 38 insertions(+), 5 deletions(-)
include/hw/vfio/vfio-device.h +2 -0
index 923f9cd116..5cb817fd6a 100644
@@ -66,6 +66,7 @@ typedef struct VFIODevice {
      OnOffAuto enable_migration;
      OnOffAuto migration_multifd_transfer;
      bool migration_events;
+    bool use_region_fds;
      VFIODeviceOps *ops;
      VFIODeviceIOOps *io_ops;
      unsigned int num_irqs;
@@ -84,6 +85,7 @@ typedef struct VFIODevice {
      VFIOIOASHwpt *hwpt;
      QLIST_ENTRY(VFIODevice) hwpt_next;
      struct vfio_region_info **reginfo;
+    int *region_fds;
  } VFIODevice;
 
  struct VFIODeviceOps {

include/hw/vfio/vfio-device.h struct VFIODeviceIOOps { · line 178

Quoted 6 lines
include/hw/vfio/vfio-device.h +2 -1
@@ -172,10 +174,11 @@ struct VFIODeviceIOOps {
      /**
       * @get_region_info
       *
-     * Fill in @info with information on the region given by @info->index.
+     * Fill in @info (and optionally @fd) with information on the region given
+     * by @info->index.

The whole VFIODeviceIOOps struct needs better documentation. The arguments are missing.

Quoted 12 lines
include/hw/vfio/vfio-device.h +1 -1
       */
      int (*get_region_info)(VFIODevice *vdev,
-                           struct vfio_region_info *info);
+                           struct vfio_region_info *info, int *fd);
 
      /**
       * @get_irq_info
include/hw/vfio/vfio-region.h +0 -0
index cbffb26962..80e83b23fd 100644

include/hw/vfio/vfio-region.h typedef struct VFIORegion { · line 32

Quoted 4 lines
include/hw/vfio/vfio-region.h +1 -0
@@ -29,6 +29,7 @@ typedef struct VFIORegion {
      uint32_t nr_mmaps;
      VFIOMmap *mmaps;
      uint8_t nr; /* cache the region number for debug */
+    int fd; /* fd to mmap() region */

Could you split this change ? I am not sure it is needed.

Quoted 45 lines
include/hw/vfio/vfio-region.h +0 -0
  } VFIORegion;
 
 
hw/vfio/device.c +10 -1
index d0068086ae..41db403992 100644
@@ -226,6 +226,7 @@ int vfio_device_get_region_info(VFIODevice *vbasedev, int index,
                                  struct vfio_region_info **info)
  {
      size_t argsz = sizeof(struct vfio_region_info);
+    int fd = -1;
      int ret;
 
      /* check cache */
@@ -240,7 +241,7 @@ int vfio_device_get_region_info(VFIODevice *vbasedev, int index,
  retry:
      (*info)->argsz = argsz;
 
-    ret = vbasedev->io_ops->get_region_info(vbasedev, *info);
+    ret = vbasedev->io_ops->get_region_info(vbasedev, *info, &fd);
      if (ret != 0) {
          g_free(*info);
          *info = NULL;
@@ -251,11 +252,19 @@ retry:
          argsz = (*info)->argsz;
          *info = g_realloc(*info, argsz);
 
+        if (fd != -1) {
+            close(fd);
+            fd = -1;
+        }
+
          goto retry;
      }
 
      /* fill cache */
      vbasedev->reginfo[index] = *info;
+    if (vbasedev->region_fds != NULL) {
+        vbasedev->region_fds[index] = fd;
+    }
 
      return 0;
  }

hw/vfio/device.c void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops, · line 372

Quoted 4 lines
hw/vfio/device.c +1 -0
@@ -360,6 +369,7 @@ void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops,
      vbasedev->io_ops = &vfio_device_io_ops_ioctl;
      vbasedev->dev = dev;
      vbasedev->fd = -1;
+    vbasedev->use_region_fds = false;

I wish we could avoid this bool. I have no idea yet.

Quoted 15 lines
hw/vfio/device.c +3 -0
      vbasedev->ram_block_discard_allowed = ram_discard;
  }
@@ -470,6 +480,9 @@ void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainerBase *bcontainer,
 
      vbasedev->reginfo = g_new0(struct vfio_region_info *,
                                 vbasedev->num_regions);
+    if (vbasedev->use_region_fds) {
+        vbasedev->region_fds = g_new0(int, vbasedev->num_regions);
+    }
  }
 
  void vfio_device_unprepare(VFIODevice *vbasedev)
@@ -478,9 +491,17 @@ void vfio_device_unprepare(VFIODevice *vbasedev)
 

hw/vfio/device.c void vfio_device_unprepare(VFIODevice *vbasedev) · line 501

Quoted 10 lines
hw/vfio/device.c +5 -0
      for (i = 0; i < vbasedev->num_regions; i++) {
          g_free(vbasedev->reginfo[i]);
+        if (vbasedev->region_fds != NULL && vbasedev->region_fds[i] != -1) {
+            close(vbasedev->region_fds[i]);
+        }
+
      }
      g_free(vbasedev->reginfo);
      vbasedev->reginfo = NULL;
+    if (vbasedev->region_fds != NULL) {

g_free is NULL safe. No need to test for it. g_clear_pointer() is a nice helper too.

Quoted 38 lines
hw/vfio/device.c +7 -1
+        g_free(vbasedev->region_fds);
+        vbasedev->region_fds = NULL;
+    }
 
      QLIST_REMOVE(vbasedev, container_next);
      QLIST_REMOVE(vbasedev, global_next);
@@ -502,10 +523,13 @@ static int vfio_device_io_device_feature(VFIODevice *vbasedev,
  }
 
  static int vfio_device_io_get_region_info(VFIODevice *vbasedev,
-                                          struct vfio_region_info *info)
+                                          struct vfio_region_info *info,
+                                          int *fd)
  {
      int ret;
 
+    *fd = -1;
+
      ret = ioctl(vbasedev->fd, VFIO_DEVICE_GET_REGION_INFO, info);
 
      return ret < 0 ? -errno : ret;
hw/vfio/region.c +5 -0
index 34752c3f65..3c93da91d8 100644
@@ -200,6 +200,11 @@ int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
      region->size = info->size;
      region->fd_offset = info->offset;
      region->nr = index;
+    if (vbasedev->region_fds != NULL) {
+        region->fd = vbasedev->region_fds[index];
+    } else {
+        region->fd = vbasedev->fd;
+    }
 
      if (region->size) {
          region->mem = g_new0(MemoryRegion, 1);

hw/vfio/region.c int vfio_region_mmap(VFIORegion *region) · line 286

Quoted 5 lines
hw/vfio/region.c +1 -1
@@ -278,7 +283,7 @@ int vfio_region_mmap(VFIORegion *region)
 
          region->mmaps[i].mmap = mmap(map_align, region->mmaps[i].size, prot,
                                       MAP_SHARED | MAP_FIXED,
-                                     region->vbasedev->fd,
+                                     region->fd,

Would this work ?

		vbasedev->region_fds ? vbasedev->region_fds[region->nr] : vbasedev->fd,

Thanks,

C.

Quoted 3 lines
hw/vfio/region.c +0 -0
                                       region->fd_offset +
                                       region->mmaps[i].offset);
          if (region->mmaps[i].mmap == MAP_FAILED) {
author
#16 lore raw

Re: [PATCH v2 07/29] vfio: add per-region fd support

Quoted 6 lines · Cédric Le Goater

include/hw/vfio/vfio-device.h struct VFIODeviceIOOps { · line 178

Quoted 3 lines
include/hw/vfio/vfio-device.h +2 -1
-     * Fill in @info with information on the region given by @info->index.
+     * Fill in @info (and optionally @fd) with information on the region given
+     * by @info->index.

The whole VFIODeviceIOOps struct needs better documentation. The arguments are missing.

Will add another preparatory patch, thanks.

Quoted 7 lines

include/hw/vfio/vfio-region.h typedef struct VFIORegion { · line 32

Quoted 5 lines
include/hw/vfio/vfio-region.h +1 -0
@@ -29,6 +29,7 @@ typedef struct VFIORegion {
      uint32_t nr_mmaps;
      VFIOMmap *mmaps;
      uint8_t nr; /* cache the region number for debug */
+    int fd; /* fd to mmap() region */

Could you split this change ? I am not sure it is needed.

The idea was to avoid having every bit of code that needed the region fd having to remember to do:

Quoted 9 lines

hw/vfio/region.c int vfio_region_mmap(VFIORegion *region) · line 285

Quoted 5 lines
hw/vfio/region.c +1 -1
@@ -278,7 +283,7 @@ int vfio_region_mmap(VFIORegion *region)
          region->mmaps[i].mmap = mmap(map_align, region->mmaps[i].size, prot,
                                       MAP_SHARED | MAP_FIXED,
-                                     region->vbasedev->fd,
+                                     region->fd,

Would this work ?

		vbasedev->region_fds ? vbasedev->region_fds[region->nr] : vbasedev->fd,

That is, region->fd is *always* correct, and there is less chance of a bug where somebody incorrectly uses vbasedev fd instead. IMO "region->fd" is much cleaner/clearer.

But, if you don't like that, yes, I can drop region->fd in favour of the above.

thanks
john

#17 lore raw

Re: [PATCH v2 07/29] vfio: add per-region fd support

Quoted 30 lines · John Levon
Quoted 6 lines · Cédric Le Goater

include/hw/vfio/vfio-device.h struct VFIODeviceIOOps { · line 178

Quoted 3 lines
include/hw/vfio/vfio-device.h +2 -1
-     * Fill in @info with information on the region given by @info->index.
+     * Fill in @info (and optionally @fd) with information on the region given
+     * by @info->index.

The whole VFIODeviceIOOps struct needs better documentation. The arguments are missing.

Will add another preparatory patch, thanks.

Quoted 7 lines

include/hw/vfio/vfio-region.h typedef struct VFIORegion { · line 32

Quoted 5 lines
include/hw/vfio/vfio-region.h +1 -0
@@ -29,6 +29,7 @@ typedef struct VFIORegion {
       uint32_t nr_mmaps;
       VFIOMmap *mmaps;
       uint8_t nr; /* cache the region number for debug */
+    int fd; /* fd to mmap() region */

Could you split this change ? I am not sure it is needed.

The idea was to avoid having every bit of code that needed the region fd having to remember to do:

Quoted 8 lines

hw/vfio/region.c int vfio_region_mmap(VFIORegion *region) · line 285

Quoted 5 lines
hw/vfio/region.c +1 -1
@@ -278,7 +283,7 @@ int vfio_region_mmap(VFIORegion *region)
           region->mmaps[i].mmap = mmap(map_align, region->mmaps[i].size, prot,
                                        MAP_SHARED | MAP_FIXED,
-                                     region->vbasedev->fd,
+                                     region->fd,

Would this work ?

Quoted 5 lines
Quoted 1 line
		vbasedev->region_fds ? vbasedev->region_fds[region->nr] : vbasedev->fd,

That is, region->fd is *always* correct, and there is less chance of a bug where somebody incorrectly uses vbasedev fd instead. IMO "region->fd" is much cleaner/clearer.

maybe. It's only used in one place : vfio_region_mmap(). I think caching the fd value under VFIORegion is overkill.

Thanks,

C.

Quoted 6 lines

But, if you don't like that, yes, I can drop region->fd in favour of the above.

thanks
john

author
#18 lore raw

Re: [PATCH v2 07/29] vfio: add per-region fd support

Quoted 10 lines · Cédric Le Goater
Quoted 7 lines
Quoted 3 lines

Would this work ?

		vbasedev->region_fds ? vbasedev->region_fds[region->nr] : vbasedev->fd,

That is, region->fd is *always* correct, and there is less chance of a bug where somebody incorrectly uses vbasedev fd instead. IMO "region->fd" is much cleaner/clearer.

maybe. It's only used in one place : vfio_region_mmap(). I think caching the fd value under VFIORegion is overkill.

OK, will do.

thanks
john

author
#19 lore raw

PATCH v2 8/29 vfio: mark posted writes in region write callbacks

In reply to John Levon

For vfio-user, the region write implementation needs to know if the write is posted; add the necessary plumbing to support this.

---
 include/hw/vfio/vfio-device.h | 4 ++--
 include/hw/vfio/vfio-region.h | 1 +
 hw/vfio/device.c              | 3 ++-
 hw/vfio/pci.c                 | 5 ++++-
 hw/vfio/region.c              | 3 ++-
 5 files changed, 11 insertions(+), 5 deletions(-)
include/hw/vfio/vfio-device.h +2 -2
index 5cb817fd6a..a23ef4ea13 100644
@@ -207,10 +207,10 @@ struct VFIODeviceIOOps {
      * @region_write
      *
      * Write @size bytes to the region @nr at offset @off from the buffer
-     * @data.
+     * @data; if @post, the write is posted.
      */
     int (*region_write)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size,
-                        void *data);
+                        void *data, bool post);
 };
 
 void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainerBase *bcontainer,
include/hw/vfio/vfio-region.h +1 -0
index 80e83b23fd..f1ae5125ba 100644
@@ -30,6 +30,7 @@ typedef struct VFIORegion {
     VFIOMmap *mmaps;
     uint8_t nr; /* cache the region number for debug */
     int fd; /* fd to mmap() region */
+    bool post_wr; /* writes can be posted */
 } VFIORegion;
 
 
hw/vfio/device.c +2 -1
index 41db403992..cb1a212b8f 100644
@@ -572,7 +572,8 @@ static int vfio_device_io_region_read(VFIODevice *vbasedev, uint8_t index,
 }
 
 static int vfio_device_io_region_write(VFIODevice *vbasedev, uint8_t index,
-                                       off_t off, uint32_t size, void *data)
+                                       off_t off, uint32_t size, void *data,
+                                       bool post)
 {
     struct vfio_region_info *info;
     int ret;
hw/vfio/pci.c +4 -1
index ef38b4692a..3d1dbdac38 100644
@@ -1016,7 +1016,7 @@ static int vfio_pci_config_space_write(VFIOPCIDevice *vdev, off_t offset,
 {
     return vdev->vbasedev.io_ops->region_write(&vdev->vbasedev,
                                                VFIO_PCI_CONFIG_REGION_INDEX,
-                                               offset, size, data);
+                                               offset, size, data, false);
 }
 
 static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
@@ -1820,6 +1820,9 @@ static void vfio_bar_prepare(VFIOPCIDevice *vdev, int nr)
     bar->type = pci_bar & (bar->ioport ? ~PCI_BASE_ADDRESS_IO_MASK :
                                          ~PCI_BASE_ADDRESS_MEM_MASK);
     bar->size = bar->region.size;
+
+    /* IO regions are sync, memory can be async */
+    bar->region.post_wr = (bar->ioport == 0);
 }
 
 static void vfio_bars_prepare(VFIOPCIDevice *vdev)
hw/vfio/region.c +2 -1
index 3c93da91d8..936502b37a 100644
@@ -66,7 +66,7 @@ void vfio_region_write(void *opaque, hwaddr addr,
     }
 
     ret = vbasedev->io_ops->region_write(vbasedev, region->nr,
-                                         addr, size, &buf);
+                                         addr, size, &buf, region->post_wr);
     if (ret != size) {
         error_report("%s(%s:region%d+0x%"HWADDR_PRIx", 0x%"PRIx64
                      ",%d) failed: %s",
@@ -200,6 +200,7 @@ int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
     region->size = info->size;
     region->fd_offset = info->offset;
     region->nr = index;
+    region->post_wr = false;
     if (vbasedev->region_fds != NULL) {
         region->fd = vbasedev->region_fds[index];
     } else {
Signature · 1 line
2.43.0
Reviewed-by on v2 8/29
#20 lore raw

Re: [PATCH v2 08/29] vfio: mark posted writes in region write callbacks

Quoted 4 lines · John Levon

For vfio-user, the region write implementation needs to know if the write is posted; add the necessary plumbing to support this.

Thanks,

C.

Quoted 95 lines
---
  include/hw/vfio/vfio-device.h | 4 ++--
  include/hw/vfio/vfio-region.h | 1 +
  hw/vfio/device.c              | 3 ++-
  hw/vfio/pci.c                 | 5 ++++-
  hw/vfio/region.c              | 3 ++-
  5 files changed, 11 insertions(+), 5 deletions(-)
include/hw/vfio/vfio-device.h +2 -2
index 5cb817fd6a..a23ef4ea13 100644
@@ -207,10 +207,10 @@ struct VFIODeviceIOOps {
       * @region_write
       *
       * Write @size bytes to the region @nr at offset @off from the buffer
-     * @data.
+     * @data; if @post, the write is posted.
       */
      int (*region_write)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size,
-                        void *data);
+                        void *data, bool post);
  };
 
  void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainerBase *bcontainer,
include/hw/vfio/vfio-region.h +1 -0
index 80e83b23fd..f1ae5125ba 100644
@@ -30,6 +30,7 @@ typedef struct VFIORegion {
      VFIOMmap *mmaps;
      uint8_t nr; /* cache the region number for debug */
      int fd; /* fd to mmap() region */
+    bool post_wr; /* writes can be posted */
  } VFIORegion;
 
 
hw/vfio/device.c +2 -1
index 41db403992..cb1a212b8f 100644
@@ -572,7 +572,8 @@ static int vfio_device_io_region_read(VFIODevice *vbasedev, uint8_t index,
  }
 
  static int vfio_device_io_region_write(VFIODevice *vbasedev, uint8_t index,
-                                       off_t off, uint32_t size, void *data)
+                                       off_t off, uint32_t size, void *data,
+                                       bool post)
  {
      struct vfio_region_info *info;
      int ret;
hw/vfio/pci.c +4 -1
index ef38b4692a..3d1dbdac38 100644
@@ -1016,7 +1016,7 @@ static int vfio_pci_config_space_write(VFIOPCIDevice *vdev, off_t offset,
  {
      return vdev->vbasedev.io_ops->region_write(&vdev->vbasedev,
                                                 VFIO_PCI_CONFIG_REGION_INDEX,
-                                               offset, size, data);
+                                               offset, size, data, false);
  }
 
  static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
@@ -1820,6 +1820,9 @@ static void vfio_bar_prepare(VFIOPCIDevice *vdev, int nr)
      bar->type = pci_bar & (bar->ioport ? ~PCI_BASE_ADDRESS_IO_MASK :
                                           ~PCI_BASE_ADDRESS_MEM_MASK);
      bar->size = bar->region.size;
+
+    /* IO regions are sync, memory can be async */
+    bar->region.post_wr = (bar->ioport == 0);
  }
 
  static void vfio_bars_prepare(VFIOPCIDevice *vdev)
hw/vfio/region.c +2 -1
index 3c93da91d8..936502b37a 100644
@@ -66,7 +66,7 @@ void vfio_region_write(void *opaque, hwaddr addr,
      }
 
      ret = vbasedev->io_ops->region_write(vbasedev, region->nr,
-                                         addr, size, &buf);
+                                         addr, size, &buf, region->post_wr);
      if (ret != size) {
          error_report("%s(%s:region%d+0x%"HWADDR_PRIx", 0x%"PRIx64
                       ",%d) failed: %s",
@@ -200,6 +200,7 @@ int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
      region->size = info->size;
      region->fd_offset = info->offset;
      region->nr = index;
+    region->post_wr = false;
      if (vbasedev->region_fds != NULL) {
          region->fd = vbasedev->region_fds[index];
      } else {
author
#21 lore raw

PATCH v2 9/29 vfio: return mr from vfio_get_xlat_addr

In reply to John Levon

From: Steve Sistare <[email protected]>

Modify memory_get_xlat_addr and vfio_get_xlat_addr to return the memory region that the translated address is found in. This will be needed by CPR in a subsequent patch to map blocks using IOMMU_IOAS_MAP_FILE.

Also return the xlat offset, so we can simplify the interface by removing the out parameters that can be trivially derived from mr and xlat.

Lastly, rename the functions to to memory_translate_iotlb() and vfio_translate_iotlb().

---
 include/system/memory.h | 19 +++++++++----------
 hw/vfio/listener.c      | 33 ++++++++++++++++++++++-----------
 hw/virtio/vhost-vdpa.c  |  9 +++++++--
 system/memory.c         | 32 +++++++-------------------------
 4 files changed, 45 insertions(+), 48 deletions(-)
include/system/memory.h +9 -10
index fbbf4cf911..13416d79b9 100644
@@ -738,21 +738,20 @@ void ram_discard_manager_unregister_listener(RamDiscardManager *rdm,
                                              RamDiscardListener *rdl);
 
 /**
- * memory_get_xlat_addr: Extract addresses from a TLB entry
+ * memory_translate_iotlb: Extract addresses from a TLB entry.
+ *                         Called with rcu_read_lock held.
  *
  * @iotlb: pointer to an #IOMMUTLBEntry
- * @vaddr: virtual address
- * @ram_addr: RAM address
- * @read_only: indicates if writes are allowed
- * @mr_has_discard_manager: indicates memory is controlled by a
- *                          RamDiscardManager
+ * @xlat_p: return the offset of the entry from the start of the returned
+ *          MemoryRegion.
  * @errp: pointer to Error*, to store an error if it happens.
  *
- * Return: true on success, else false setting @errp with error.
+ * Return: On success, return the MemoryRegion containing the @iotlb translated
+ *         addr.  The MemoryRegion must not be accessed after rcu_read_unlock.
+ *         On failure, return NULL, setting @errp with error.
  */
-bool memory_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr,
-                          ram_addr_t *ram_addr, bool *read_only,
-                          bool *mr_has_discard_manager, Error **errp);
+MemoryRegion *memory_translate_iotlb(IOMMUTLBEntry *iotlb, hwaddr *xlat_p,
+                                     Error **errp);
 
 typedef struct CoalescedMemoryRange CoalescedMemoryRange;
 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
hw/vfio/listener.c +22 -11
index bfacb3d8d9..0afafe3464 100644
@@ -90,16 +90,17 @@ static bool vfio_listener_skipped_section(MemoryRegionSection *section)
            section->offset_within_address_space & (1ULL << 63);
 }
 
-/* Called with rcu_read_lock held.  */
-static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr,
-                               ram_addr_t *ram_addr, bool *read_only,
-                               Error **errp)
+/*
+ * Called with rcu_read_lock held.
+ * The returned MemoryRegion must not be accessed after calling rcu_read_unlock.
+ */
+static MemoryRegion *vfio_translate_iotlb(IOMMUTLBEntry *iotlb, hwaddr *xlat_p,
+                                          Error **errp)
 {
-    bool ret, mr_has_discard_manager;
+    MemoryRegion *mr;
 
-    ret = memory_get_xlat_addr(iotlb, vaddr, ram_addr, read_only,
-                               &mr_has_discard_manager, errp);
-    if (ret && mr_has_discard_manager) {
+    mr = memory_translate_iotlb(iotlb, xlat_p, errp);
+    if (mr && memory_region_has_ram_discard_manager(mr)) {
         /*
          * Malicious VMs might trigger discarding of IOMMU-mapped memory. The
          * pages will remain pinned inside vfio until unmapped, resulting in a
@@ -118,7 +119,7 @@ static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr,
                          " intended via an IOMMU. It's possible to mitigate "
                          " by setting/adjusting RLIMIT_MEMLOCK.");
     }
-    return ret;
+    return mr;
 }
 
 static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
@@ -126,6 +127,8 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
     VFIOGuestIOMMU *giommu = container_of(n, VFIOGuestIOMMU, n);
     VFIOContainerBase *bcontainer = giommu->bcontainer;
     hwaddr iova = iotlb->iova + giommu->iommu_offset;
+    MemoryRegion *mr;
+    hwaddr xlat;
     void *vaddr;
     int ret;
     Error *local_err = NULL;
@@ -150,10 +153,14 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
     if ((iotlb->perm & IOMMU_RW) != IOMMU_NONE) {
         bool read_only;
 
-        if (!vfio_get_xlat_addr(iotlb, &vaddr, NULL, &read_only, &local_err)) {
+        mr = vfio_translate_iotlb(iotlb, &xlat, &local_err);
+        if (!mr) {
             error_report_err(local_err);
             goto out;
         }
+        vaddr = memory_region_get_ram_ptr(mr) + xlat;
+        read_only = !(iotlb->perm & IOMMU_WO) || mr->readonly;
+
         /*
          * vaddr is only valid until rcu_read_unlock(). But after
          * vfio_dma_map has set up the mapping the pages will be
@@ -1010,6 +1017,8 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
     ram_addr_t translated_addr;
     Error *local_err = NULL;
     int ret = -EINVAL;
+    MemoryRegion *mr;
+    ram_addr_t xlat;
 
     trace_vfio_iommu_map_dirty_notify(iova, iova + iotlb->addr_mask);
 
@@ -1021,9 +1030,11 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
     }
 
     rcu_read_lock();
-    if (!vfio_get_xlat_addr(iotlb, NULL, &translated_addr, NULL, &local_err)) {
+    mr = vfio_translate_iotlb(iotlb, &xlat, &local_err);
+    if (!mr) {
         goto out_unlock;
     }
+    translated_addr = memory_region_get_ram_addr(mr) + xlat;
 
     ret = vfio_container_query_dirty_bitmap(bcontainer, iova, iotlb->addr_mask + 1,
                                 translated_addr, &local_err);
hw/virtio/vhost-vdpa.c +7 -2
index 1ab2c11fa8..a1dd9e17a6 100644
@@ -209,6 +209,8 @@ static void vhost_vdpa_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
     int ret;
     Int128 llend;
     Error *local_err = NULL;
+    MemoryRegion *mr;
+    hwaddr xlat;
 
     if (iotlb->target_as != &address_space_memory) {
         error_report("Wrong target AS \"%s\", only system memory is allowed",
@@ -228,11 +230,14 @@ static void vhost_vdpa_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
     if ((iotlb->perm & IOMMU_RW) != IOMMU_NONE) {
         bool read_only;
 
-        if (!memory_get_xlat_addr(iotlb, &vaddr, NULL, &read_only, NULL,
-                                  &local_err)) {
+        mr = memory_translate_iotlb(iotlb, &xlat, &local_err);
+        if (!mr) {
             error_report_err(local_err);
             return;
         }
+        vaddr = memory_region_get_ram_ptr(mr) + xlat;
+        read_only = !(iotlb->perm & IOMMU_WO) || mr->readonly;
+
         ret = vhost_vdpa_dma_map(s, VHOST_VDPA_GUEST_PA_ASID, iova,
                                  iotlb->addr_mask + 1, vaddr, read_only);
         if (ret) {
system/memory.c +7 -25
index 63b983efcd..306e9ff9eb 100644
@@ -2174,18 +2174,14 @@ void ram_discard_manager_unregister_listener(RamDiscardManager *rdm,
 }
 
 /* Called with rcu_read_lock held.  */
-bool memory_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr,
-                          ram_addr_t *ram_addr, bool *read_only,
-                          bool *mr_has_discard_manager, Error **errp)
+MemoryRegion *memory_translate_iotlb(IOMMUTLBEntry *iotlb, hwaddr *xlat_p,
+                                     Error **errp)
 {
     MemoryRegion *mr;
     hwaddr xlat;
     hwaddr len = iotlb->addr_mask + 1;
     bool writable = iotlb->perm & IOMMU_WO;
 
-    if (mr_has_discard_manager) {
-        *mr_has_discard_manager = false;
-    }
     /*
      * The IOMMU TLB entry we have just covers translation through
      * this IOMMU to its immediate target.  We need to translate
@@ -2195,7 +2191,7 @@ bool memory_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr,
                                  &xlat, &len, writable, MEMTXATTRS_UNSPECIFIED);
     if (!memory_region_is_ram(mr)) {
         error_setg(errp, "iommu map to non memory area %" HWADDR_PRIx "", xlat);
-        return false;
+        return NULL;
     } else if (memory_region_has_ram_discard_manager(mr)) {
         RamDiscardManager *rdm = memory_region_get_ram_discard_manager(mr);
         MemoryRegionSection tmp = {
@@ -2203,9 +2199,6 @@ bool memory_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr,
             .offset_within_region = xlat,
             .size = int128_make64(len),
         };
-        if (mr_has_discard_manager) {
-            *mr_has_discard_manager = true;
-        }
         /*
          * Malicious VMs can map memory into the IOMMU, which is expected
          * to remain discarded. vfio will pin all pages, populating memory.
@@ -2216,7 +2209,7 @@ bool memory_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr,
             error_setg(errp, "iommu map to discarded memory (e.g., unplugged"
                          " via virtio-mem): %" HWADDR_PRIx "",
                          iotlb->translated_addr);
-            return false;
+            return NULL;
         }
     }
 
@@ -2226,22 +2219,11 @@ bool memory_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr,
      */
     if (len & iotlb->addr_mask) {
         error_setg(errp, "iommu has granularity incompatible with target AS");
-        return false;
-    }
-
-    if (vaddr) {
-        *vaddr = memory_region_get_ram_ptr(mr) + xlat;
-    }
-
-    if (ram_addr) {
-        *ram_addr = memory_region_get_ram_addr(mr) + xlat;
-    }
-
-    if (read_only) {
-        *read_only = !writable || mr->readonly;
+        return NULL;
     }
 
-    return true;
+    *xlat_p = xlat;
+    return mr;
 }
 
 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)
Signature · 1 line
2.43.0
author
#22 lore raw

PATCH v2 10/29 vfio/container: pass MemoryRegion to DMA operations

In reply to John Levon

Pass through the MemoryRegion to DMA operation handlers of vfio containers. The vfio-user container will need this later, to translate the vaddr into an offset for the dma map vfio-user message.

---
 include/hw/vfio/vfio-container-base.h | 9 +++++----
 hw/vfio/container-base.c              | 4 ++--
 hw/vfio/container.c                   | 3 ++-
 hw/vfio/iommufd.c                     | 3 ++-
 hw/vfio/listener.c                    | 6 +++---
 5 files changed, 14 insertions(+), 11 deletions(-)
include/hw/vfio/vfio-container-base.h +5 -4
index 41c49b2aa5..c18986a621 100644
@@ -78,7 +78,7 @@ void vfio_address_space_insert(VFIOAddressSpace *space,
 
 int vfio_container_dma_map(VFIOContainerBase *bcontainer,
                            hwaddr iova, ram_addr_t size,
-                           void *vaddr, bool readonly);
+                           void *vaddr, bool readonly, MemoryRegion *mrp);
 int vfio_container_dma_unmap(VFIOContainerBase *bcontainer,
                              hwaddr iova, ram_addr_t size,
                              IOMMUTLBEntry *iotlb, bool unmap_all);
@@ -149,18 +149,19 @@ struct VFIOIOMMUClass {
     /**
      * @dma_map
      *
-     * Map an address range into the container.
+     * Map an address range into the container. Note that @mrp will within an
+     * RCU read lock region across this call.
      *
      * @bcontainer: #VFIOContainerBase to use
      * @iova: start address to map
      * @size: size of the range to map
      * @vaddr: process virtual address of mapping
      * @readonly: true if mapping should be readonly
+     * @mrp: the memory region for this mapping
      */
     int (*dma_map)(const VFIOContainerBase *bcontainer,
                    hwaddr iova, ram_addr_t size,
-                   void *vaddr, bool readonly);
-
+                   void *vaddr, bool readonly, MemoryRegion *mrp);
     /**
      * @dma_unmap
      *
hw/vfio/container-base.c +2 -2
index 1c6ca94b60..a677bb6694 100644
@@ -75,12 +75,12 @@ void vfio_address_space_insert(VFIOAddressSpace *space,
 
 int vfio_container_dma_map(VFIOContainerBase *bcontainer,
                            hwaddr iova, ram_addr_t size,
-                           void *vaddr, bool readonly)
+                           void *vaddr, bool readonly, MemoryRegion *mrp)
 {
     VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
 
     g_assert(vioc->dma_map);
-    return vioc->dma_map(bcontainer, iova, size, vaddr, readonly);
+    return vioc->dma_map(bcontainer, iova, size, vaddr, readonly, mrp);
 }
 
 int vfio_container_dma_unmap(VFIOContainerBase *bcontainer,
hw/vfio/container.c +2 -1
index a9f0dbaec4..98d6b9f90c 100644
@@ -207,7 +207,8 @@ static int vfio_legacy_dma_unmap(const VFIOContainerBase *bcontainer,
 }
 
 static int vfio_legacy_dma_map(const VFIOContainerBase *bcontainer, hwaddr iova,
-                               ram_addr_t size, void *vaddr, bool readonly)
+                               ram_addr_t size, void *vaddr, bool readonly,
+                               MemoryRegion *mrp)
 {
     const VFIOContainer *container = container_of(bcontainer, VFIOContainer,
                                                   bcontainer);
hw/vfio/iommufd.c +2 -1
index af1c7ab10a..a2518c4a5d 100644
@@ -34,7 +34,8 @@
             TYPE_HOST_IOMMU_DEVICE_IOMMUFD "-vfio"
 
 static int iommufd_cdev_map(const VFIOContainerBase *bcontainer, hwaddr iova,
-                            ram_addr_t size, void *vaddr, bool readonly)
+                            ram_addr_t size, void *vaddr, bool readonly,
+                            MemoryRegion *mrp)
 {
     const VFIOIOMMUFDContainer *container =
         container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
hw/vfio/listener.c +3 -3
index 0afafe3464..a1d2d2561d 100644
@@ -170,7 +170,7 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
          */
         ret = vfio_container_dma_map(bcontainer, iova,
                                      iotlb->addr_mask + 1, vaddr,
-                                     read_only);
+                                     read_only, mr);
         if (ret) {
             error_report("vfio_container_dma_map(%p, 0x%"HWADDR_PRIx", "
                          "0x%"HWADDR_PRIx", %p) = %d (%s)",
@@ -240,7 +240,7 @@ static int vfio_ram_discard_notify_populate(RamDiscardListener *rdl,
         vaddr = memory_region_get_ram_ptr(section->mr) + start;
 
         ret = vfio_container_dma_map(bcontainer, iova, next - start,
-                                     vaddr, section->readonly);
+                                     vaddr, section->readonly, section->mr);
         if (ret) {
             /* Rollback */
             vfio_ram_discard_notify_discard(rdl, section);
@@ -564,7 +564,7 @@ static void vfio_listener_region_add(MemoryListener *listener,
     }
 
     ret = vfio_container_dma_map(bcontainer, iova, int128_get64(llsize),
-                                 vaddr, section->readonly);
+                                 vaddr, section->readonly, section->mr);
     if (ret) {
         error_setg(&err, "vfio_container_dma_map(%p, 0x%"HWADDR_PRIx", "
                    "0x%"HWADDR_PRIx", %p) = %d (%s)",
Signature · 1 line
2.43.0
#23 lore raw

Re: [PATCH v2 10/29] vfio/container: pass MemoryRegion to DMA operations

+Steven,

Quoted 8 lines · John Levon

Pass through the MemoryRegion to DMA operation handlers of vfio containers. The vfio-user container will need this later, to translate the vaddr into an offset for the dma map vfio-user message.

This change conflicts with :

https://lore.kernel.org/qemu-devel/[email protected]

Steven,

Could we use this version instead for the "live update" series ?

Thanks,

C.

Quoted 122 lines
---
  include/hw/vfio/vfio-container-base.h | 9 +++++----
  hw/vfio/container-base.c              | 4 ++--
  hw/vfio/container.c                   | 3 ++-
  hw/vfio/iommufd.c                     | 3 ++-
  hw/vfio/listener.c                    | 6 +++---
  5 files changed, 14 insertions(+), 11 deletions(-)
include/hw/vfio/vfio-container-base.h +5 -4
index 41c49b2aa5..c18986a621 100644
@@ -78,7 +78,7 @@ void vfio_address_space_insert(VFIOAddressSpace *space,
 
  int vfio_container_dma_map(VFIOContainerBase *bcontainer,
                             hwaddr iova, ram_addr_t size,
-                           void *vaddr, bool readonly);
+                           void *vaddr, bool readonly, MemoryRegion *mrp);
  int vfio_container_dma_unmap(VFIOContainerBase *bcontainer,
                               hwaddr iova, ram_addr_t size,
                               IOMMUTLBEntry *iotlb, bool unmap_all);
@@ -149,18 +149,19 @@ struct VFIOIOMMUClass {
      /**
       * @dma_map
       *
-     * Map an address range into the container.
+     * Map an address range into the container. Note that @mrp will within an
+     * RCU read lock region across this call.
       *
       * @bcontainer: #VFIOContainerBase to use
       * @iova: start address to map
       * @size: size of the range to map
       * @vaddr: process virtual address of mapping
       * @readonly: true if mapping should be readonly
+     * @mrp: the memory region for this mapping
       */
      int (*dma_map)(const VFIOContainerBase *bcontainer,
                     hwaddr iova, ram_addr_t size,
-                   void *vaddr, bool readonly);
-
+                   void *vaddr, bool readonly, MemoryRegion *mrp);
      /**
       * @dma_unmap
       *
hw/vfio/container-base.c +2 -2
index 1c6ca94b60..a677bb6694 100644
@@ -75,12 +75,12 @@ void vfio_address_space_insert(VFIOAddressSpace *space,
 
  int vfio_container_dma_map(VFIOContainerBase *bcontainer,
                             hwaddr iova, ram_addr_t size,
-                           void *vaddr, bool readonly)
+                           void *vaddr, bool readonly, MemoryRegion *mrp)
  {
      VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
 
      g_assert(vioc->dma_map);
-    return vioc->dma_map(bcontainer, iova, size, vaddr, readonly);
+    return vioc->dma_map(bcontainer, iova, size, vaddr, readonly, mrp);
  }
 
  int vfio_container_dma_unmap(VFIOContainerBase *bcontainer,
hw/vfio/container.c +2 -1
index a9f0dbaec4..98d6b9f90c 100644
@@ -207,7 +207,8 @@ static int vfio_legacy_dma_unmap(const VFIOContainerBase *bcontainer,
  }
 
  static int vfio_legacy_dma_map(const VFIOContainerBase *bcontainer, hwaddr iova,
-                               ram_addr_t size, void *vaddr, bool readonly)
+                               ram_addr_t size, void *vaddr, bool readonly,
+                               MemoryRegion *mrp)
  {
      const VFIOContainer *container = container_of(bcontainer, VFIOContainer,
                                                    bcontainer);
hw/vfio/iommufd.c +2 -1
index af1c7ab10a..a2518c4a5d 100644
@@ -34,7 +34,8 @@
              TYPE_HOST_IOMMU_DEVICE_IOMMUFD "-vfio"
 
  static int iommufd_cdev_map(const VFIOContainerBase *bcontainer, hwaddr iova,
-                            ram_addr_t size, void *vaddr, bool readonly)
+                            ram_addr_t size, void *vaddr, bool readonly,
+                            MemoryRegion *mrp)
  {
      const VFIOIOMMUFDContainer *container =
          container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
hw/vfio/listener.c +3 -3
index 0afafe3464..a1d2d2561d 100644
@@ -170,7 +170,7 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
           */
          ret = vfio_container_dma_map(bcontainer, iova,
                                       iotlb->addr_mask + 1, vaddr,
-                                     read_only);
+                                     read_only, mr);
          if (ret) {
              error_report("vfio_container_dma_map(%p, 0x%"HWADDR_PRIx", "
                           "0x%"HWADDR_PRIx", %p) = %d (%s)",
@@ -240,7 +240,7 @@ static int vfio_ram_discard_notify_populate(RamDiscardListener *rdl,
          vaddr = memory_region_get_ram_ptr(section->mr) + start;
 
          ret = vfio_container_dma_map(bcontainer, iova, next - start,
-                                     vaddr, section->readonly);
+                                     vaddr, section->readonly, section->mr);
          if (ret) {
              /* Rollback */
              vfio_ram_discard_notify_discard(rdl, section);
@@ -564,7 +564,7 @@ static void vfio_listener_region_add(MemoryListener *listener,
      }
 
      ret = vfio_container_dma_map(bcontainer, iova, int128_get64(llsize),
-                                 vaddr, section->readonly);
+                                 vaddr, section->readonly, section->mr);
      if (ret) {
          error_setg(&err, "vfio_container_dma_map(%p, 0x%"HWADDR_PRIx", "
                     "0x%"HWADDR_PRIx", %p) = %d (%s)",
author
#24 lore raw

Re: [PATCH v2 10/29] vfio/container: pass MemoryRegion to DMA operations

Quoted 18 lines · Cédric Le Goater

!-------------------------------------------------------------------|
CAUTION: External Email

|-------------------------------------------------------------------!

+Steven,

Quoted 8 lines · John Levon

Pass through the MemoryRegion to DMA operation handlers of vfio containers. The vfio-user container will need this later, to translate the vaddr into an offset for the dma map vfio-user message.

Quoted 7 lines

This change conflicts with :

https://urldefense.proofpoint.com/v2/url?u=https-3A__lore.kernel.org_qemu-2Ddevel_1747063973-2D124548-2D28-2Dgit-2Dsend-2Demail-2Dsteven.sistare-40oracle.com&d=DwICaQ&c=s883GpUCOChKOHiocYtGcg&r=v7SNLJqx7b9Vfc7ZO82Wg4nnZ8O5XkACFQ30bVKxotI&m=ZGgaPL84zJdxC-NJiZdGWRzh1WIXiIKzDUt8YcytF-NfxOT9I_gSOxNCph5DfyVM&s=V-_fptKbd0WpQ_JDJWze9TxsbOrvVfEm2EIPPH6HJ2k&e=

Steven,

Could we use this version instead for the "live update" series ?

I could use the ram block for the offset but the mrp is also used here:

  86     int fd = memory_region_get_fd(mrp);

which is

  2406 int memory_region_get_fd(MemoryRegion *mr)
  2407 {
  2408     RCU_READ_LOCK_GUARD();
  2409     while (mr->alias) {
  2410         mr = mr->alias;
  2411     }
  2412     return mr->ram_block->fd;
  2413 }

In the tests I've done, mr->alias is always NULL, but I don't know enough about MR handling to be sure this will always be the case? Perhaps others can, in which case I only need the ram block too.

regards
john

#25 lore raw

Re: [PATCH v2 10/29] vfio/container: pass MemoryRegion to DMA operations

In reply to Cédric Le Goater
Quoted 13 lines · Cédric Le Goater

+Steven,

Quoted 8 lines · John Levon

Pass through the MemoryRegion to DMA operation handlers of vfio containers. The vfio-user container will need this later, to translate the vaddr into an offset for the dma map vfio-user message.

Quoted 7 lines

This change conflicts with :

  https://lore.kernel.org/qemu-devel/[email protected]

Steven,

Could we use this version instead for the "live update" series ?

Sure, I will drop my patch and use this.

A few nits, iff there is another version of the series.

I suggest "mrp" be renamed "mr", to be consistent with all other MemoryRegion* variables. A MemoryRegion is never passed by value, so the "p" is understood.

This comment for @dma_map needs polishing for clarity:

+ * Map an address range into the container. Note that @mrp will within an
+ * RCU read lock region across this call.

- Steve

Quoted 115 lines
Quoted 114 lines

---
  include/hw/vfio/vfio-container-base.h | 9 +++++---- hw/vfio/container-base.c              | 4 ++-- hw/vfio/container.c                   | 3 ++- hw/vfio/iommufd.c                     | 3 ++- hw/vfio/listener.c                    | 6 +++---

  5 files changed, 14 insertions(+), 11 deletions(-)
include/hw/vfio/vfio-container-base.h +5 -4
index 41c49b2aa5..c18986a621 100644
@@ -78,7 +78,7 @@ void vfio_address_space_insert(VFIOAddressSpace *space,
   int vfio_container_dma_map(VFIOContainerBase *bcontainer,
                              hwaddr iova, ram_addr_t size,
-                           void *vaddr, bool readonly);
+                           void *vaddr, bool readonly, MemoryRegion *mrp);
   int vfio_container_dma_unmap(VFIOContainerBase *bcontainer,
                                hwaddr iova, ram_addr_t size,
                                IOMMUTLBEntry *iotlb, bool unmap_all);
@@ -149,18 +149,19 @@ struct VFIOIOMMUClass {
       /**
        * @dma_map
        *
-     * Map an address range into the container.
+     * Map an address range into the container. Note that @mrp will within an
+     * RCU read lock region across this call.
        *
        * @bcontainer: #VFIOContainerBase to use
        * @iova: start address to map
        * @size: size of the range to map
        * @vaddr: process virtual address of mapping
        * @readonly: true if mapping should be readonly
+     * @mrp: the memory region for this mapping
        */
       int (*dma_map)(const VFIOContainerBase *bcontainer,
                      hwaddr iova, ram_addr_t size,
-                   void *vaddr, bool readonly);
-
+                   void *vaddr, bool readonly, MemoryRegion *mrp);
       /**
        * @dma_unmap
        *
hw/vfio/container-base.c +2 -2
index 1c6ca94b60..a677bb6694 100644
@@ -75,12 +75,12 @@ void vfio_address_space_insert(VFIOAddressSpace *space,
   int vfio_container_dma_map(VFIOContainerBase *bcontainer,
                              hwaddr iova, ram_addr_t size,
-                           void *vaddr, bool readonly)
+                           void *vaddr, bool readonly, MemoryRegion *mrp)
   {
       VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
       g_assert(vioc->dma_map);
-    return vioc->dma_map(bcontainer, iova, size, vaddr, readonly);
+    return vioc->dma_map(bcontainer, iova, size, vaddr, readonly, mrp);
   }
   int vfio_container_dma_unmap(VFIOContainerBase *bcontainer,
hw/vfio/container.c +2 -1
index a9f0dbaec4..98d6b9f90c 100644
@@ -207,7 +207,8 @@ static int vfio_legacy_dma_unmap(const VFIOContainerBase *bcontainer,
   }
   static int vfio_legacy_dma_map(const VFIOContainerBase *bcontainer, hwaddr iova,
-                               ram_addr_t size, void *vaddr, bool readonly)
+                               ram_addr_t size, void *vaddr, bool readonly,
+                               MemoryRegion *mrp)
   {
       const VFIOContainer *container = container_of(bcontainer, VFIOContainer,
                                                     bcontainer);
hw/vfio/iommufd.c +0 -0
index af1c7ab10a..a2518c4a5d 100644
@@ -34,7 +34,8 @@

              TYPE_HOST_IOMMU_DEVICE_IOMMUFD "-vfio"
  static int iommufd_cdev_map(const VFIOContainerBase *bcontainer, hwaddr iova, -                            ram_addr_t size, void *vaddr, bool readonly) +                            ram_addr_t size, void *vaddr, bool readonly, +                            MemoryRegion *mrp)
  {
      const VFIOIOMMUFDContainer *container = container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);

hw/vfio/listener.c +1 -1
index 0afafe3464..a1d2d2561d 100644
@@ -170,7 +170,7 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
            */
           ret = vfio_container_dma_map(bcontainer, iova,
                                        iotlb->addr_mask + 1, vaddr,
-                                     read_only);
+                                     read_only, mr);
           if (ret) {

              error_report("vfio_container_dma_map(%p, 0x%"HWADDR_PRIx", " "0x%"HWADDR_PRIx", %p) = %d (%s)",

hw/vfio/listener.c +2 -2
@@ -240,7 +240,7 @@ static int vfio_ram_discard_notify_populate(RamDiscardListener *rdl,
           vaddr = memory_region_get_ram_ptr(section->mr) + start;
           ret = vfio_container_dma_map(bcontainer, iova, next - start,
-                                     vaddr, section->readonly);
+                                     vaddr, section->readonly, section->mr);
           if (ret) {
               /* Rollback */
               vfio_ram_discard_notify_discard(rdl, section);
@@ -564,7 +564,7 @@ static void vfio_listener_region_add(MemoryListener *listener,
       }
       ret = vfio_container_dma_map(bcontainer, iova, int128_get64(llsize),
-                                 vaddr, section->readonly);
+                                 vaddr, section->readonly, section->mr);
       if (ret) {

          error_setg(&err, "vfio_container_dma_map(%p, 0x%"HWADDR_PRIx", " "0x%"HWADDR_PRIx", %p) = %d (%s)",

author
#26 lore raw

Re: [PATCH v2 10/29] vfio/container: pass MemoryRegion to DMA operations

Quoted 5 lines · Steven Sistare

A few nits, iff there is another version of the series.

I suggest "mrp" be renamed "mr", to be consistent with all other MemoryRegion* variables. A MemoryRegion is never passed by value, so the "p" is understood.

Sure.

include/hw/vfio/vfio-container-base.h struct VFIOIOMMUClass { · line 153

Quoted 4 lines

This comment for @dma_map needs polishing for clarity:

include/hw/vfio/vfio-container-base.h +2 -0
+     * Map an address range into the container. Note that @mrp will within an
+     * RCU read lock region across this call.

Will fix

thanks
john

author
#27 lore raw

PATCH v2 11/29 vfio-user: introduce vfio-user protocol specification

In reply to John Levon

From: Thanos Makatos <[email protected]>

This patch introduces the vfio-user protocol specification (formerly known as VFIO-over-socket), which is designed to allow devices to be emulated outside QEMU, in a separate process. vfio-user reuses the existing VFIO defines, structs and concepts.

It has been earlier discussed as an RFC in:
"RFC: use VFIO over a UNIX domain socket to implement device offloading"

---
 MAINTAINERS                    |    8 +-
 docs/devel/index-internals.rst |    1 +
 docs/devel/vfio-user.rst       | 1522 ++++++++++++++++++++++++++++++++
 3 files changed, 1530 insertions(+), 1 deletion(-)
 create mode 100644 docs/devel/vfio-user.rst
MAINTAINERS +7 -1
index 6dacd6d004..71a7819448 100644
@@ -4218,13 +4218,19 @@ F: hw/remote/proxy-memory-listener.c
 F: include/hw/remote/proxy-memory-listener.h
 F: hw/remote/iohub.c
 F: include/hw/remote/iohub.h
-F: subprojects/libvfio-user
 F: hw/remote/vfio-user-obj.c
 F: include/hw/remote/vfio-user-obj.h
 F: hw/remote/iommu.c
 F: include/hw/remote/iommu.h
 F: tests/functional/test_multiprocess.py
 
+VFIO-USER:
+M: John Levon <[email protected]>
+M: Thanos Makatos <[email protected]>
+S: Supported
+F: docs/devel/vfio-user.rst
+F: subprojects/libvfio-user
+
 EBPF:
 M: Jason Wang <[email protected]>
 R: Andrew Melnychenko <[email protected]>
docs/devel/index-internals.rst +1 -0
index 7a0678cbdd..1b04311f06 100644
@@ -22,6 +22,7 @@ Details about QEMU's various subsystems including how to add features to them.
    tracing
    uefi-vars
    vfio-iommufd
+   vfio-user
    writing-monitor-commands
    virtio-backends
    crypto
docs/devel/vfio-user.rst new file +1522 -0

1,522 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#28 lore raw

PATCH v2 12/29 vfio-user: add vfio-user class and container

In reply to John Levon

Introduce basic plumbing for vfio-user behind a new --enable-vfio-user-client option.

We introduce VFIOUserContainer in hw/vfio-user/container.c, which is a container type for the "IOMMU" type "vfio-iommu-user", and share some common container code from hw/vfio/container.c.

Add hw/vfio-user/pci.c for instantiating VFIOUserPCIDevice objects, sharing some common code from hw/vfio/pci.c.

---
 MAINTAINERS                           |   2 +
 hw/vfio-user/container.h              |  25 +++
 include/hw/vfio/vfio-container-base.h |   1 +
 hw/vfio-user/container.c              | 212 ++++++++++++++++++++++++++
 hw/vfio-user/pci.c                    | 146 ++++++++++++++++++
 hw/meson.build                        |   1 +
 hw/vfio-user/meson.build              |   9 ++
 meson_options.txt                     |   2 +
 scripts/meson-buildoptions.sh         |   4 +
 9 files changed, 402 insertions(+)
 create mode 100644 hw/vfio-user/container.h
 create mode 100644 hw/vfio-user/container.c
 create mode 100644 hw/vfio-user/pci.c
 create mode 100644 hw/vfio-user/meson.build
MAINTAINERS +2 -0
index 71a7819448..c3d224e54c 100644
@@ -4229,6 +4229,8 @@ M: John Levon <[email protected]>
 M: Thanos Makatos <[email protected]>
 S: Supported
 F: docs/devel/vfio-user.rst
+F: hw/vfio-user/*
+F: include/hw/vfio-user/*
 F: subprojects/libvfio-user
 
 EBPF:
hw/vfio-user/container.h new file +25 -0
new file mode 100644
index 0000000000..3cd3303e68
@@ -0,0 +1,25 @@
+/*
+ * vfio-user specific definitions.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef HW_VFIO_USER_CONTAINER_H
+#define HW_VFIO_USER_CONTAINER_H
+
+#include <inttypes.h>
+#include <stdbool.h>
+
+#include "hw/vfio/vfio-container-base.h"
+
+/* MMU container sub-class for vfio-user. */
+typedef struct VFIOUserContainer {
+    VFIOContainerBase bcontainer;
+} VFIOUserContainer;
+
+OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserContainer, VFIO_IOMMU_USER);
+
+#endif /* HW_VFIO_USER_CONTAINER_H */
include/hw/vfio/vfio-container-base.h +1 -0
index c18986a621..c854f902ed 100644
@@ -109,6 +109,7 @@ vfio_container_get_page_size_mask(const VFIOContainerBase *bcontainer)
 #define TYPE_VFIO_IOMMU_LEGACY TYPE_VFIO_IOMMU "-legacy"
 #define TYPE_VFIO_IOMMU_SPAPR TYPE_VFIO_IOMMU "-spapr"
 #define TYPE_VFIO_IOMMU_IOMMUFD TYPE_VFIO_IOMMU "-iommufd"
+#define TYPE_VFIO_IOMMU_USER TYPE_VFIO_IOMMU "-user"
 
 OBJECT_DECLARE_TYPE(VFIOContainerBase, VFIOIOMMUClass, VFIO_IOMMU)
 
hw/vfio-user/container.c new file +212 -0

212 lines not on this page · raw message

hw/vfio-user/pci.c new file +146 -0

146 lines not on this page · raw message

hw/meson.build +1 -0
index b91f761fe0..791ce21ab4 100644
@@ -39,6 +39,7 @@ subdir('uefi')
 subdir('ufs')
 subdir('usb')
 subdir('vfio')
+subdir('vfio-user')
 subdir('virtio')
 subdir('vmapple')
 subdir('watchdog')
hw/vfio-user/meson.build new file +9 -0
new file mode 100644
index 0000000000..f1fee70c85
@@ -0,0 +1,9 @@
+vfio_user_ss = ss.source_set()
+vfio_user_ss.add(files(
+  'container.c',
+  'pci.c',
+))
+
+if get_option('vfio_user_client').enabled()
+  specific_ss.add_all(vfio_user_ss)
+endif
meson_options.txt +2 -0
index a442be2995..97d3db44cd 100644
@@ -109,6 +109,8 @@ option('multiprocess', type: 'feature', value: 'auto',
        description: 'Out of process device emulation support')
 option('relocatable', type : 'boolean', value : true,
        description: 'toggle relocatable install')
+option('vfio_user_client', type: 'feature', value: 'disabled',
+       description: 'vfio-user client support')
 option('vfio_user_server', type: 'feature', value: 'disabled',
        description: 'vfio-user server support')
 option('dbus_display', type: 'feature', value: 'auto',
scripts/meson-buildoptions.sh +4 -0
index f09ef9604f..2c5673769a 100644
@@ -201,6 +201,8 @@ meson_options_help() {
   printf "%s\n" '  vdi             vdi image format support'
   printf "%s\n" '  vduse-blk-export'
   printf "%s\n" '                  VDUSE block export support'
+  printf "%s\n" '  vfio-user-client'
+  printf "%s\n" '                  vfio-user client support'
   printf "%s\n" '  vfio-user-server'
   printf "%s\n" '                  vfio-user server support'
   printf "%s\n" '  vhdx            vhdx image format support'
@@ -529,6 +531,8 @@ _meson_option_parse() {
     --disable-vdi) printf "%s" -Dvdi=disabled ;;
     --enable-vduse-blk-export) printf "%s" -Dvduse_blk_export=enabled ;;
     --disable-vduse-blk-export) printf "%s" -Dvduse_blk_export=disabled ;;
+    --enable-vfio-user-client) printf "%s" -Dvfio_user_client=enabled ;;
+    --disable-vfio-user-client) printf "%s" -Dvfio_user_client=disabled ;;
     --enable-vfio-user-server) printf "%s" -Dvfio_user_server=enabled ;;
     --disable-vfio-user-server) printf "%s" -Dvfio_user_server=disabled ;;
     --enable-vhdx) printf "%s" -Dvhdx=enabled ;;
Signature · 1 line
2.43.0
author
#29 lore raw

PATCH v2 13/29 vfio-user: connect vfio proxy to remote server

In reply to John Levon

Introduce the vfio-user "proxy": this is the client code responsible for sending and receiving vfio-user messages across the control socket.

The new files hw/vfio-user/proxy.[ch] contain some basic plumbing for managing the proxy; initialize the proxy during realization of the VFIOUserPCIDevice instance.

---
 hw/vfio-user/proxy.h          |  82 +++++++++++++++++
 include/hw/vfio/vfio-device.h |   2 +
 hw/vfio-user/pci.c            |  17 ++++
 hw/vfio-user/proxy.c          | 165 ++++++++++++++++++++++++++++++++++
 hw/vfio-user/meson.build      |   1 +
 5 files changed, 267 insertions(+)
 create mode 100644 hw/vfio-user/proxy.h
 create mode 100644 hw/vfio-user/proxy.c
hw/vfio-user/proxy.h new file +82 -0
new file mode 100644
index 0000000000..60fe3e0b6d
@@ -0,0 +1,82 @@
+#ifndef VFIO_USER_PROXY_H
+#define VFIO_USER_PROXY_H
+
+/*
+ * vfio protocol over a UNIX socket.
+ *
+ * Copyright © 2018, 2021 Oracle and/or its affiliates.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "io/channel.h"
+#include "io/channel-socket.h"
+
+typedef struct {
+    int send_fds;
+    int recv_fds;
+    int *fds;
+} VFIOUserFDs;
+
+enum msg_type {
+    VFIO_MSG_NONE,
+    VFIO_MSG_ASYNC,
+    VFIO_MSG_WAIT,
+    VFIO_MSG_NOWAIT,
+    VFIO_MSG_REQ,
+};
+
+typedef struct VFIOUserMsg {
+    QTAILQ_ENTRY(VFIOUserMsg) next;
+    VFIOUserFDs *fds;
+    uint32_t rsize;
+    uint32_t id;
+    QemuCond cv;
+    bool complete;
+    enum msg_type type;
+} VFIOUserMsg;
+
+
+enum proxy_state {
+    VFIO_PROXY_CONNECTED = 1,
+    VFIO_PROXY_ERROR = 2,
+    VFIO_PROXY_CLOSING = 3,
+    VFIO_PROXY_CLOSED = 4,
+};
+
+typedef QTAILQ_HEAD(VFIOUserMsgQ, VFIOUserMsg) VFIOUserMsgQ;
+
+typedef struct VFIOUserProxy {
+    QLIST_ENTRY(VFIOUserProxy) next;
+    char *sockname;
+    struct QIOChannel *ioc;
+    void (*request)(void *opaque, VFIOUserMsg *msg);
+    void *req_arg;
+    int flags;
+    QemuCond close_cv;
+    AioContext *ctx;
+    QEMUBH *req_bh;
+
+    /*
+     * above only changed when BQL is held
+     * below are protected by per-proxy lock
+     */
+    QemuMutex lock;
+    VFIOUserMsgQ free;
+    VFIOUserMsgQ pending;
+    VFIOUserMsgQ incoming;
+    VFIOUserMsgQ outgoing;
+    VFIOUserMsg *last_nowait;
+    enum proxy_state state;
+} VFIOUserProxy;
+
+/* VFIOProxy flags */
+#define VFIO_PROXY_CLIENT        0x1
+
+VFIOUserProxy *vfio_user_connect_dev(SocketAddress *addr, Error **errp);
+void vfio_user_disconnect(VFIOUserProxy *proxy);
+
+#endif /* VFIO_USER_PROXY_H */
include/hw/vfio/vfio-device.h +2 -0
index a23ef4ea13..09f1a21bf8 100644
@@ -46,6 +46,7 @@ typedef struct VFIOMigration VFIOMigration;
 
 typedef struct IOMMUFDBackend IOMMUFDBackend;
 typedef struct VFIOIOASHwpt VFIOIOASHwpt;
+typedef struct VFIOUserProxy VFIOUserProxy;
 
 typedef struct VFIODevice {
     QLIST_ENTRY(VFIODevice) next;
@@ -86,6 +87,7 @@ typedef struct VFIODevice {
     QLIST_ENTRY(VFIODevice) hwpt_next;
     struct vfio_region_info **reginfo;
     int *region_fds;
+    VFIOUserProxy *proxy;
 } VFIODevice;
 
 struct VFIODeviceOps {
hw/vfio-user/pci.c +17 -0
index 181fe0f02e..f77d0e497d 100644
@@ -14,6 +14,7 @@
 
 #include "hw/qdev-properties.h"
 #include "hw/vfio/pci.h"
+#include "hw/vfio-user/proxy.h"
 
 #define TYPE_VFIO_USER_PCI "vfio-user-pci"
 OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI)
@@ -46,6 +47,8 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)
     VFIOPCIDevice *vdev = VFIO_PCI_BASE(pdev);
     VFIODevice *vbasedev = &vdev->vbasedev;
     AddressSpace *as;
+    SocketAddress addr;
+    VFIOUserProxy *proxy;
 
     /*
      * TODO: make option parser understand SocketAddress
@@ -58,6 +61,15 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)
         return;
     }
 
+    memset(&addr, 0, sizeof(addr));
+    addr.type = SOCKET_ADDRESS_TYPE_UNIX;
+    addr.u.q_unix.path = udev->sock_name;
+    proxy = vfio_user_connect_dev(&addr, errp);
+    if (!proxy) {
+        return;
+    }
+    vbasedev->proxy = proxy;
+
     vbasedev->name = g_strdup_printf("VFIO user <%s>", udev->sock_name);
 
     /*
@@ -103,8 +115,13 @@ static void vfio_user_instance_init(Object *obj)
 static void vfio_user_instance_finalize(Object *obj)
 {
     VFIOPCIDevice *vdev = VFIO_PCI_BASE(obj);
+    VFIODevice *vbasedev = &vdev->vbasedev;
 
     vfio_pci_put_device(vdev);
+
+    if (vbasedev->proxy != NULL) {
+        vfio_user_disconnect(vbasedev->proxy);
+    }
 }
 
 static const Property vfio_user_pci_dev_properties[] = {
hw/vfio-user/proxy.c new file +165 -0

165 lines not on this page · raw message

hw/vfio-user/meson.build +1 -0
index f1fee70c85..695b341547 100644
@@ -2,6 +2,7 @@ vfio_user_ss = ss.source_set()
 vfio_user_ss.add(files(
   'container.c',
   'pci.c',
+  'proxy.c',
 ))
 
 if get_option('vfio_user_client').enabled()
Signature · 1 line
2.43.0
author
#30 lore raw

PATCH v2 14/29 vfio-user: implement message receive infrastructure

In reply to John Levon

Add the basic implementation for receiving vfio-user messages from the control socket.

---
 meson.build               |   1 +
 hw/vfio-user/protocol.h   |  56 ++++++
 hw/vfio-user/proxy.h      |  11 +
 hw/vfio-user/trace.h      |   1 +
 hw/vfio-user/pci.c        |  11 +
 hw/vfio-user/proxy.c      | 409 ++++++++++++++++++++++++++++++++++++++
 hw/vfio-user/trace-events |   6 +
 7 files changed, 495 insertions(+)
 create mode 100644 hw/vfio-user/protocol.h
 create mode 100644 hw/vfio-user/trace.h
 create mode 100644 hw/vfio-user/trace-events
meson.build +1 -0
index 7f91500bb7..1873f92313 100644
@@ -3642,6 +3642,7 @@ if have_system
     'hw/ufs',
     'hw/usb',
     'hw/vfio',
+    'hw/vfio-user',
     'hw/virtio',
     'hw/vmapple',
     'hw/watchdog',
hw/vfio-user/protocol.h new file +56 -0

56 lines not on this page · raw message

hw/vfio-user/proxy.h +11 -0
index 60fe3e0b6d..0105479d02 100644
@@ -15,6 +15,9 @@
 #include "io/channel.h"
 #include "io/channel-socket.h"
 
+#include "qemu/sockets.h"
+#include "hw/vfio-user/protocol.h"
+
 typedef struct {
     int send_fds;
     int recv_fds;
@@ -31,6 +34,7 @@ enum msg_type {
 
 typedef struct VFIOUserMsg {
     QTAILQ_ENTRY(VFIOUserMsg) next;
+    VFIOUserHdr *hdr;
     VFIOUserFDs *fds;
     uint32_t rsize;
     uint32_t id;
@@ -70,13 +74,20 @@ typedef struct VFIOUserProxy {
     VFIOUserMsgQ incoming;
     VFIOUserMsgQ outgoing;
     VFIOUserMsg *last_nowait;
+    VFIOUserMsg *part_recv;
+    size_t recv_left;
     enum proxy_state state;
 } VFIOUserProxy;
 
 /* VFIOProxy flags */
 #define VFIO_PROXY_CLIENT        0x1
 
+typedef struct VFIODevice VFIODevice;
+
 VFIOUserProxy *vfio_user_connect_dev(SocketAddress *addr, Error **errp);
 void vfio_user_disconnect(VFIOUserProxy *proxy);
+void vfio_user_set_handler(VFIODevice *vbasedev,
+                           void (*handler)(void *opaque, VFIOUserMsg *msg),
+                           void *reqarg);
 
 #endif /* VFIO_USER_PROXY_H */
hw/vfio-user/trace.h new file +1 -0
new file mode 100644
index 0000000000..574b59aa89
@@ -0,0 +1 @@
+#include "trace/trace-hw_vfio_user.h"
hw/vfio-user/pci.c +11 -0
index f77d0e497d..894130b575 100644
@@ -24,6 +24,16 @@ struct VFIOUserPCIDevice {
     char *sock_name;
 };
 
+/*
+ * Incoming request message callback.
+ *
+ * Runs off main loop, so BQL held.
+ */
+static void vfio_user_pci_process_req(void *opaque, VFIOUserMsg *msg)
+{
+
+}
+
 /*
  * Emulated devices don't use host hot reset
  */
@@ -69,6 +79,7 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)
         return;
     }
     vbasedev->proxy = proxy;
+    vfio_user_set_handler(vbasedev, vfio_user_pci_process_req, vdev);
 
     vbasedev->name = g_strdup_printf("VFIO user <%s>", udev->sock_name);
 
hw/vfio-user/proxy.c +409 -0

458 lines not on this page · raw message

hw/vfio-user/trace-events new file +6 -0
new file mode 100644
index 0000000000..89d6c11c4c
@@ -0,0 +1,6 @@
+# See docs/devel/tracing.rst for syntax documentation.
+
+# common.c
+vfio_user_recv_hdr(const char *name, uint16_t id, uint16_t cmd, uint32_t size, uint32_t flags) " (%s) id 0x%x cmd 0x%x size 0x%x flags 0x%x"
+vfio_user_recv_read(uint16_t id, int read) " id 0x%x read 0x%x"
+vfio_user_recv_request(uint16_t cmd) " command 0x%x"
Signature · 1 line
2.43.0
author
#31 lore raw

PATCH v2 15/29 vfio-user: implement message send infrastructure

In reply to John Levon

Add plumbing for sending vfio-user messages on the control socket. Add initial version negotation on connection.

---
 hw/vfio-user/protocol.h   |  62 +++++
 hw/vfio-user/proxy.h      |   9 +
 hw/vfio-user/pci.c        |  18 +-
 hw/vfio-user/proxy.c      | 494 ++++++++++++++++++++++++++++++++++++++
 hw/vfio-user/trace-events |   2 +
 5 files changed, 583 insertions(+), 2 deletions(-)
hw/vfio-user/protocol.h +62 -0
index 6300c681e7..74ea2a94e6 100644
@@ -53,4 +53,66 @@ enum vfio_user_command {
 #define VFIO_USER_NO_REPLY      0x10
 #define VFIO_USER_ERROR         0x20
 
+
+/*
+ * VFIO_USER_VERSION
+ */
+typedef struct {
+    VFIOUserHdr hdr;
+    uint16_t major;
+    uint16_t minor;
+    char capabilities[];
+} VFIOUserVersion;
+
+#define VFIO_USER_MAJOR_VER     0
+#define VFIO_USER_MINOR_VER     0
+
+#define VFIO_USER_CAP           "capabilities"
+
+/* "capabilities" members */
+#define VFIO_USER_CAP_MAX_FDS   "max_msg_fds"
+#define VFIO_USER_CAP_MAX_XFER  "max_data_xfer_size"
+#define VFIO_USER_CAP_PGSIZES   "pgsizes"
+#define VFIO_USER_CAP_MAP_MAX   "max_dma_maps"
+#define VFIO_USER_CAP_MIGR      "migration"
+
+/* "migration" members */
+#define VFIO_USER_CAP_PGSIZE            "pgsize"
+#define VFIO_USER_CAP_MAX_BITMAP        "max_bitmap_size"
+
+/*
+ * Max FDs mainly comes into play when a device supports multiple interrupts
+ * where each ones uses an eventfd to inject it into the guest.
+ * It is clamped by the the number of FDs the qio channel supports in a
+ * single message.
+ */
+#define VFIO_USER_DEF_MAX_FDS   8
+#define VFIO_USER_MAX_MAX_FDS   16
+
+/*
+ * Max transfer limits the amount of data in region and DMA messages.
+ * Region R/W will be very small (limited by how much a single instruction
+ * can process) so just use a reasonable limit here.
+ */
+#define VFIO_USER_DEF_MAX_XFER  (1024 * 1024)
+#define VFIO_USER_MAX_MAX_XFER  (64 * 1024 * 1024)
+
+/*
+ * Default pagesizes supported is 4k.
+ */
+#define VFIO_USER_DEF_PGSIZE    4096
+
+/*
+ * Default max number of DMA mappings is stolen from the
+ * linux kernel "dma_entry_limit"
+ */
+#define VFIO_USER_DEF_MAP_MAX   65535
+
+/*
+ * Default max bitmap size is also take from the linux kernel,
+ * where usage of signed ints limits the VA range to 2^31 bytes.
+ * Dividing that by the number of bits per byte yields 256MB
+ */
+#define VFIO_USER_DEF_MAX_BITMAP (256 * 1024 * 1024)
+
 #endif /* VFIO_USER_PROTOCOL_H */
hw/vfio-user/proxy.h +9 -0
index 0105479d02..33e8a4d83b 100644
@@ -40,6 +40,7 @@ typedef struct VFIOUserMsg {
     uint32_t id;
     QemuCond cv;
     bool complete;
+    bool pending;
     enum msg_type type;
 } VFIOUserMsg;
 
@@ -59,6 +60,12 @@ typedef struct VFIOUserProxy {
     struct QIOChannel *ioc;
     void (*request)(void *opaque, VFIOUserMsg *msg);
     void *req_arg;
+    uint64_t max_xfer_size;
+    uint64_t max_send_fds;
+    uint64_t max_dma;
+    uint64_t dma_pgsizes;
+    uint64_t max_bitmap;
+    uint64_t migr_pgsize;
     int flags;
     QemuCond close_cv;
     AioContext *ctx;
@@ -81,6 +88,7 @@ typedef struct VFIOUserProxy {
 
 /* VFIOProxy flags */
 #define VFIO_PROXY_CLIENT        0x1
+#define VFIO_PROXY_FORCE_QUEUED  0x4
 
 typedef struct VFIODevice VFIODevice;
 
@@ -89,5 +97,6 @@ void vfio_user_disconnect(VFIOUserProxy *proxy);
 void vfio_user_set_handler(VFIODevice *vbasedev,
                            void (*handler)(void *opaque, VFIOUserMsg *msg),
                            void *reqarg);
+bool vfio_user_validate_version(VFIOUserProxy *proxy, Error **errp);
 
 #endif /* VFIO_USER_PROXY_H */
hw/vfio-user/pci.c +16 -2
index 894130b575..391864412c 100644
@@ -22,6 +22,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI)
 struct VFIOUserPCIDevice {
     VFIOPCIDevice device;
     char *sock_name;
+    bool send_queued;   /* all sends are queued */
 };
 
 /*
@@ -81,6 +82,14 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)
     vbasedev->proxy = proxy;
     vfio_user_set_handler(vbasedev, vfio_user_pci_process_req, vdev);
 
+    if (udev->send_queued) {
+        proxy->flags |= VFIO_PROXY_FORCE_QUEUED;
+    }
+
+    if (!vfio_user_validate_version(proxy, errp)) {
+        goto error;
+    }
+
     vbasedev->name = g_strdup_printf("VFIO user <%s>", udev->sock_name);
 
     /*
@@ -92,9 +101,13 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)
     if (!vfio_device_attach_by_iommu_type(TYPE_VFIO_IOMMU_USER,
                                           vbasedev->name, vbasedev,
                                           as, errp)) {
-        error_prepend(errp, VFIO_MSG_PREFIX, vbasedev->name);
-        return;
+        goto error;
     }
+
+    return;
+
+error:
+    error_prepend(errp, VFIO_MSG_PREFIX, vdev->vbasedev.name);
 }
 
 static void vfio_user_instance_init(Object *obj)
@@ -145,6 +158,7 @@ static const Property vfio_user_pci_dev_properties[] = {
     DEFINE_PROP_UINT32("x-pci-sub-device-id", VFIOPCIDevice,
                        sub_device_id, PCI_ANY_ID),
     DEFINE_PROP_STRING("socket", VFIOUserPCIDevice, sock_name),
+    DEFINE_PROP_BOOL("x-send-queued", VFIOUserPCIDevice, send_queued, false),
 };
 
 static void vfio_user_pci_dev_class_init(ObjectClass *klass, const void *data)
hw/vfio-user/proxy.c +494 -0

550 lines not on this page · raw message

hw/vfio-user/trace-events +2 -0
index 89d6c11c4c..7a3645024f 100644
@@ -4,3 +4,5 @@
 vfio_user_recv_hdr(const char *name, uint16_t id, uint16_t cmd, uint32_t size, uint32_t flags) " (%s) id 0x%x cmd 0x%x size 0x%x flags 0x%x"
 vfio_user_recv_read(uint16_t id, int read) " id 0x%x read 0x%x"
 vfio_user_recv_request(uint16_t cmd) " command 0x%x"
+vfio_user_send_write(uint16_t id, int wrote) " id 0x%x wrote 0x%x"
+vfio_user_version(uint16_t major, uint16_t minor, const char *caps) " major %d minor %d caps: %s"
Signature · 1 line
2.43.0
author
#32 lore raw

PATCH v2 16/29 vfio-user: implement VFIO_USER_DEVICE_GET_INFO

In reply to John Levon

Add support for getting basic device information.

---
 hw/vfio-user/device.h     | 23 ++++++++++++++++++
 hw/vfio-user/protocol.h   | 12 ++++++++++
 hw/vfio-user/proxy.h      |  7 ++++++
 hw/vfio-user/container.c  | 10 +++++++-
 hw/vfio-user/device.c     | 50 +++++++++++++++++++++++++++++++++++++++
 hw/vfio-user/proxy.c      | 12 ++++------
 hw/vfio-user/meson.build  |  1 +
 hw/vfio-user/trace-events |  1 +
 8 files changed, 107 insertions(+), 9 deletions(-)
 create mode 100644 hw/vfio-user/device.h
 create mode 100644 hw/vfio-user/device.c
hw/vfio-user/device.h new file +23 -0
new file mode 100644
index 0000000000..f27f26b479
@@ -0,0 +1,23 @@
+#ifndef VFIO_USER_DEVICE_H
+#define VFIO_USER_DEVICE_H
+
+/*
+ * vfio protocol over a UNIX socket device handling.
+ *
+ * Copyright © 2018, 2021 Oracle and/or its affiliates.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "linux/vfio.h"
+
+#include "hw/vfio-user/proxy.h"
+
+int vfio_user_get_device_info(VFIOUserProxy *proxy,
+                              struct vfio_device_info *info);
+
+#endif /* VFIO_USER_DEVICE_H */
hw/vfio-user/protocol.h +12 -0
index 74ea2a94e6..648badff46 100644
@@ -115,4 +115,16 @@ typedef struct {
  */
 #define VFIO_USER_DEF_MAX_BITMAP (256 * 1024 * 1024)
 
+/*
+ * VFIO_USER_DEVICE_GET_INFO
+ * imported from struct vfio_device_info
+ */
+typedef struct {
+    VFIOUserHdr hdr;
+    uint32_t argsz;
+    uint32_t flags;
+    uint32_t num_regions;
+    uint32_t num_irqs;
+} VFIOUserDeviceInfo;
+
 #endif /* VFIO_USER_PROTOCOL_H */
hw/vfio-user/proxy.h +7 -0
index 33e8a4d83b..9c6b2cdf35 100644
@@ -15,7 +15,9 @@
 #include "io/channel.h"
 #include "io/channel-socket.h"
 
+#include "qemu/queue.h"
 #include "qemu/sockets.h"
+#include "qemu/thread.h"
 #include "hw/vfio-user/protocol.h"
 
 typedef struct {
@@ -99,4 +101,9 @@ void vfio_user_set_handler(VFIODevice *vbasedev,
                            void *reqarg);
 bool vfio_user_validate_version(VFIOUserProxy *proxy, Error **errp);
 
+void vfio_user_request_msg(VFIOUserHdr *hdr, uint16_t cmd,
+                           uint32_t size, uint32_t flags);
+void vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
+                         VFIOUserFDs *fds, int rsize);
+
 #endif /* VFIO_USER_PROXY_H */
hw/vfio-user/container.c +9 -1
index 2892845b4f..6f0eb86a75 100644
@@ -14,6 +14,7 @@
 #include "qemu/osdep.h"
 
 #include "hw/vfio-user/container.h"
+#include "hw/vfio-user/device.h"
 #include "hw/vfio/vfio-cpr.h"
 #include "hw/vfio/vfio-device.h"
 #include "hw/vfio/vfio-listener.h"
@@ -144,7 +145,14 @@ static void vfio_user_container_disconnect(VFIOUserContainer *container)
 static bool vfio_user_device_get(VFIOUserContainer *container,
                                  VFIODevice *vbasedev, Error **errp)
 {
-    struct vfio_device_info info = { 0 };
+    struct vfio_device_info info = { .argsz = sizeof(info) };
+    int ret;
+
+    ret = vfio_user_get_device_info(vbasedev->proxy, &info);
+    if (ret) {
+        error_setg_errno(errp, -ret, "get info failure");
+        return ret;
+    }
 
     vbasedev->fd = -1;
 
hw/vfio-user/device.c new file +50 -0

50 lines not on this page · raw message

hw/vfio-user/proxy.c +4 -8
index 0bc12da865..34df9bba7b 100644
@@ -42,10 +42,6 @@ static void vfio_user_cb(void *opaque);
 
 static void vfio_user_request(void *opaque);
 static int vfio_user_send_queued(VFIOUserProxy *proxy, VFIOUserMsg *msg);
-static void vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
-                                VFIOUserFDs *fds, int rsize);
-static void vfio_user_request_msg(VFIOUserHdr *hdr, uint16_t cmd,
-                                  uint32_t size, uint32_t flags);
 
 static inline void vfio_user_set_error(VFIOUserHdr *hdr, uint32_t err)
 {
@@ -613,8 +609,8 @@ static int vfio_user_send_queued(VFIOUserProxy *proxy, VFIOUserMsg *msg)
     return 0;
 }
 
-static void vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
-                                VFIOUserFDs *fds, int rsize)
+void vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
+                         VFIOUserFDs *fds, int rsize)
 {
     VFIOUserMsg *msg;
     int ret;
@@ -785,8 +781,8 @@ void vfio_user_disconnect(VFIOUserProxy *proxy)
     g_free(proxy);
 }
 
-static void vfio_user_request_msg(VFIOUserHdr *hdr, uint16_t cmd,
-                                  uint32_t size, uint32_t flags)
+void vfio_user_request_msg(VFIOUserHdr *hdr, uint16_t cmd,
+                           uint32_t size, uint32_t flags)
 {
     static uint16_t next_id;
 
hw/vfio-user/meson.build +1 -0
index 695b341547..19bdee688c 100644
@@ -1,6 +1,7 @@
 vfio_user_ss = ss.source_set()
 vfio_user_ss.add(files(
   'container.c',
+  'device.c',
   'pci.c',
   'proxy.c',
 ))
hw/vfio-user/trace-events +1 -0
index 7a3645024f..6b06a3ed82 100644
@@ -6,3 +6,4 @@ vfio_user_recv_read(uint16_t id, int read) " id 0x%x read 0x%x"
 vfio_user_recv_request(uint16_t cmd) " command 0x%x"
 vfio_user_send_write(uint16_t id, int wrote) " id 0x%x wrote 0x%x"
 vfio_user_version(uint16_t major, uint16_t minor, const char *caps) " major %d minor %d caps: %s"
+vfio_user_get_info(uint32_t nregions, uint32_t nirqs) " #regions %d #irqs %d"
Signature · 1 line
2.43.0
author
#33 lore raw

PATCH v2 17/29 vfio-user: implement VFIO_USER_DEVICE_GET_REGION_INFO

In reply to John Levon

Add support for getting region info for vfio-user. As vfio-user has one fd per region, enable ->use_region_fds.

---
 hw/vfio-user/device.h     |  2 ++
 hw/vfio-user/protocol.h   | 14 ++++++++
 hw/vfio-user/proxy.h      |  1 +
 hw/vfio-user/device.c     | 68 +++++++++++++++++++++++++++++++++++++++
 hw/vfio-user/pci.c        | 11 +++++++
 hw/vfio-user/trace-events |  1 +
 6 files changed, 97 insertions(+)
hw/vfio-user/device.h +2 -0
index f27f26b479..a5a2aa9ba6 100644
@@ -20,4 +20,6 @@
 int vfio_user_get_device_info(VFIOUserProxy *proxy,
                               struct vfio_device_info *info);
 
+extern VFIODeviceIOOps vfio_user_device_io_ops_sock;
+
 #endif /* VFIO_USER_DEVICE_H */
hw/vfio-user/protocol.h +14 -0
index 648badff46..1878d44c51 100644
@@ -127,4 +127,18 @@ typedef struct {
     uint32_t num_irqs;
 } VFIOUserDeviceInfo;
 
+/*
+ * VFIO_USER_DEVICE_GET_REGION_INFO
+ * imported from struct vfio_region_info
+ */
+typedef struct {
+    VFIOUserHdr hdr;
+    uint32_t argsz;
+    uint32_t flags;
+    uint32_t index;
+    uint32_t cap_offset;
+    uint64_t size;
+    uint64_t offset;
+} VFIOUserRegionInfo;
+
 #endif /* VFIO_USER_PROTOCOL_H */
hw/vfio-user/proxy.h +1 -0
index 9c6b2cdf35..e2cfaeead8 100644
@@ -18,6 +18,7 @@
 #include "qemu/queue.h"
 #include "qemu/sockets.h"
 #include "qemu/thread.h"
+#include "hw/vfio/vfio-device.h"
 #include "hw/vfio-user/protocol.h"
 
 typedef struct {
hw/vfio-user/device.c +68 -0

71 lines not on this page · raw message

hw/vfio-user/pci.c +11 -0
index 391864412c..3dfbc33c52 100644
@@ -14,6 +14,7 @@
 
 #include "hw/qdev-properties.h"
 #include "hw/vfio/pci.h"
+#include "hw/vfio-user/device.h"
 #include "hw/vfio-user/proxy.h"
 
 #define TYPE_VFIO_USER_PCI "vfio-user-pci"
@@ -92,11 +93,21 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)
 
     vbasedev->name = g_strdup_printf("VFIO user <%s>", udev->sock_name);
 
+    /*
+     * Use socket-based device I/O instead of vfio kernel driver.
+     */
+    vbasedev->io_ops = &vfio_user_device_io_ops_sock;
+
     /*
      * vfio-user devices are effectively mdevs (don't use a host iommu).
      */
     vbasedev->mdev = true;
 
+    /*
+     * Enable per-region fds.
+     */
+    vbasedev->use_region_fds = true;
+
     as = pci_device_iommu_address_space(pdev);
     if (!vfio_device_attach_by_iommu_type(TYPE_VFIO_IOMMU_USER,
                                           vbasedev->name, vbasedev,
hw/vfio-user/trace-events +1 -0
index 6b06a3ed82..1860430301 100644
@@ -7,3 +7,4 @@ vfio_user_recv_request(uint16_t cmd) " command 0x%x"
 vfio_user_send_write(uint16_t id, int wrote) " id 0x%x wrote 0x%x"
 vfio_user_version(uint16_t major, uint16_t minor, const char *caps) " major %d minor %d caps: %s"
 vfio_user_get_info(uint32_t nregions, uint32_t nirqs) " #regions %d #irqs %d"
+vfio_user_get_region_info(uint32_t index, uint32_t flags, uint64_t size) " index %d flags 0x%x size 0x%"PRIx64
Signature · 1 line
2.43.0
author
#34 lore raw

PATCH v2 18/29 vfio-user: implement VFIO_USER_REGION_READ/WRITE

In reply to John Levon
---
 hw/vfio-user/protocol.h   | 12 +++++++
 hw/vfio-user/device.c     | 67 +++++++++++++++++++++++++++++++++++++++
 hw/vfio-user/trace-events |  1 +
 3 files changed, 80 insertions(+)
hw/vfio-user/protocol.h +12 -0
index 1878d44c51..8e6ccbe9e8 100644
@@ -141,4 +141,16 @@ typedef struct {
     uint64_t offset;
 } VFIOUserRegionInfo;
 
+/*
+ * VFIO_USER_REGION_READ
+ * VFIO_USER_REGION_WRITE
+ */
+typedef struct {
+    VFIOUserHdr hdr;
+    uint64_t offset;
+    uint32_t region;
+    uint32_t count;
+    char data[];
+} VFIOUserRegionRW;
+
 #endif /* VFIO_USER_PROTOCOL_H */
hw/vfio-user/device.c +67 -0

76 lines not on this page · raw message

hw/vfio-user/trace-events +1 -0
index 1860430301..3f5aebe7ac 100644
@@ -8,3 +8,4 @@ vfio_user_send_write(uint16_t id, int wrote) " id 0x%x wrote 0x%x"
 vfio_user_version(uint16_t major, uint16_t minor, const char *caps) " major %d minor %d caps: %s"
 vfio_user_get_info(uint32_t nregions, uint32_t nirqs) " #regions %d #irqs %d"
 vfio_user_get_region_info(uint32_t index, uint32_t flags, uint64_t size) " index %d flags 0x%x size 0x%"PRIx64
+vfio_user_region_rw(uint32_t region, uint64_t off, uint32_t count) " region %d offset 0x%"PRIx64" count %d"
Signature · 1 line
2.43.0
author
#35 lore raw

PATCH v2 19/29 vfio-user: set up PCI in vfio_user_pci_realize()

In reply to John Levon

Re-use PCI setup functions from hw/vfio/pci.c to realize the vfio-user PCI device.

---
 hw/vfio-user/pci.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
hw/vfio-user/pci.c +28 -0
index 3dfbc33c52..481c6cdef7 100644
@@ -115,8 +115,36 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)
         goto error;
     }
 
+    if (!vfio_pci_populate_device(vdev, errp)) {
+        goto error;
+    }
+
+    if (!vfio_pci_config_setup(vdev, errp)) {
+        goto error;
+    }
+
+    /*
+     * vfio_pci_config_setup will have registered the device's BARs
+     * and setup any MSIX BARs, so errors after it succeeds must
+     * use out_teardown
+     */
+
+    if (!vfio_pci_add_capabilities(vdev, errp)) {
+        goto out_teardown;
+    }
+
+    if (!vfio_pci_interrupt_setup(vdev, errp)) {
+        goto out_teardown;
+    }
+
+    vfio_pci_register_err_notifier(vdev);
+    vfio_pci_register_req_notifier(vdev);
+
     return;
 
+out_teardown:
+    vfio_pci_teardown_msi(vdev);
+    vfio_pci_bars_exit(vdev);
 error:
     error_prepend(errp, VFIO_MSG_PREFIX, vdev->vbasedev.name);
 }
Signature · 1 line
2.43.0
author
#36 lore raw

PATCH v2 20/29 vfio-user: implement VFIO_USER_DEVICE_GET/SET_IRQ*

In reply to John Levon

IRQ setup uses the same semantics as the traditional vfio path, but we need to share the corresponding file descriptors with the server as necessary.

---
 hw/vfio-user/protocol.h   |  25 ++++++++
 hw/vfio-user/device.c     | 121 ++++++++++++++++++++++++++++++++++++++
 hw/vfio-user/trace-events |   2 +
 3 files changed, 148 insertions(+)
hw/vfio-user/protocol.h +25 -0
index 8e6ccbe9e8..920b9c44ec 100644
@@ -141,6 +141,31 @@ typedef struct {
     uint64_t offset;
 } VFIOUserRegionInfo;
 
+/*
+ * VFIO_USER_DEVICE_GET_IRQ_INFO
+ * imported from struct vfio_irq_info
+ */
+typedef struct {
+    VFIOUserHdr hdr;
+    uint32_t argsz;
+    uint32_t flags;
+    uint32_t index;
+    uint32_t count;
+} VFIOUserIRQInfo;
+
+/*
+ * VFIO_USER_DEVICE_SET_IRQS
+ * imported from struct vfio_irq_set
+ */
+typedef struct {
+    VFIOUserHdr hdr;
+    uint32_t argsz;
+    uint32_t flags;
+    uint32_t index;
+    uint32_t start;
+    uint32_t count;
+} VFIOUserIRQSet;
+
 /*
  * VFIO_USER_REGION_READ
  * VFIO_USER_REGION_WRITE
hw/vfio-user/device.c +121 -0

133 lines not on this page · raw message

hw/vfio-user/trace-events +2 -0
index 3f5aebe7ac..053f5932eb 100644
@@ -9,3 +9,5 @@ vfio_user_version(uint16_t major, uint16_t minor, const char *caps) " major %d m
 vfio_user_get_info(uint32_t nregions, uint32_t nirqs) " #regions %d #irqs %d"
 vfio_user_get_region_info(uint32_t index, uint32_t flags, uint64_t size) " index %d flags 0x%x size 0x%"PRIx64
 vfio_user_region_rw(uint32_t region, uint64_t off, uint32_t count) " region %d offset 0x%"PRIx64" count %d"
+vfio_user_get_irq_info(uint32_t index, uint32_t flags, uint32_t count) " index %d flags 0x%x count %d"
+vfio_user_set_irqs(uint32_t index, uint32_t start, uint32_t count, uint32_t flags) " index %d start %d count %d flags 0x%x"
Signature · 1 line
2.43.0
author
#37 lore raw

PATCH v2 21/29 vfio-user: forward MSI-X PBA BAR accesses to server

In reply to John Levon

For vfio-user, the server holds the pending IRQ state; set up an I/O region for the MSI-X PBA so we can ask the server for this state on a PBA read.

---
 hw/vfio/pci.h      |  1 +
 hw/vfio-user/pci.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+)
hw/vfio/pci.h +1 -0
index 43c2d72b84..434d73a1ab 100644
@@ -116,6 +116,7 @@ typedef struct VFIOMSIXInfo {
     uint32_t pba_offset;
     unsigned long *pending;
     bool noresize;
+    MemoryRegion *pba_region;
 } VFIOMSIXInfo;
 
 /*
hw/vfio-user/pci.c +64 -0

82 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#38 lore raw

PATCH v2 22/29 vfio-user: set up container access to the proxy

In reply to John Levon

The user container will shortly need access to the underlying vfio-user proxy; set this up.

---
 hw/vfio-user/container.h |  2 ++
 hw/vfio-user/container.c | 43 +++++++++++++++++++++++++++++++---------
 2 files changed, 36 insertions(+), 9 deletions(-)
hw/vfio-user/container.h +2 -0
index 3cd3303e68..59e14cf5b3 100644
@@ -14,10 +14,12 @@
 #include <stdbool.h>
 
 #include "hw/vfio/vfio-container-base.h"
+#include "hw/vfio-user/proxy.h"
 
 /* MMU container sub-class for vfio-user. */
 typedef struct VFIOUserContainer {
     VFIOContainerBase bcontainer;
+    VFIOUserProxy *proxy;
 } VFIOUserContainer;
 
 OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserContainer, VFIO_IOMMU_USER);
hw/vfio-user/container.c +34 -9

103 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#39 lore raw

PATCH v2 23/29 vfio-user: implement VFIO_USER_DEVICE_RESET

In reply to John Levon

Hook this call up to the legacy reset handler for vfio-user-pci.

---
 hw/vfio-user/device.h |  2 ++
 hw/vfio-user/device.c | 12 ++++++++++++
 hw/vfio-user/pci.c    | 15 +++++++++++++++
 3 files changed, 29 insertions(+)
hw/vfio-user/device.h +2 -0
index a5a2aa9ba6..25cd4c8a00 100644
@@ -20,6 +20,8 @@
 int vfio_user_get_device_info(VFIOUserProxy *proxy,
                               struct vfio_device_info *info);
 
+void vfio_user_device_reset(VFIOUserProxy *proxy);
+
 extern VFIODeviceIOOps vfio_user_device_io_ops_sock;
 
 #endif /* VFIO_USER_DEVICE_H */
hw/vfio-user/device.c +12 -0
index 45b91d22f0..b37f7329a1 100644
@@ -49,6 +49,18 @@ int vfio_user_get_device_info(VFIOUserProxy *proxy,
     return 0;
 }
 
+void vfio_user_device_reset(VFIOUserProxy *proxy)
+{
+    VFIOUserHdr msg;
+
+    vfio_user_request_msg(&msg, VFIO_USER_DEVICE_RESET, sizeof(msg), 0);
+
+    vfio_user_send_wait(proxy, &msg, NULL, 0);
+    if (msg.flags & VFIO_USER_ERROR) {
+        error_printf("reset reply error %d\n", msg.error_reply);
+    }
+}
+
 static int vfio_user_get_region_info(VFIOUserProxy *proxy,
                                      struct vfio_region_info *info,
                                      VFIOUserFDs *fds)
hw/vfio-user/pci.c +15 -0
index 5904e7ebc5..2c3edb7378 100644
@@ -251,6 +251,20 @@ static void vfio_user_instance_finalize(Object *obj)
     }
 }
 
+static void vfio_user_pci_reset(DeviceState *dev)
+{
+    VFIOPCIDevice *vdev = VFIO_PCI_BASE(dev);
+    VFIODevice *vbasedev = &vdev->vbasedev;
+
+    vfio_pci_pre_reset(vdev);
+
+    if (vbasedev->reset_works) {
+        vfio_user_device_reset(vbasedev->proxy);
+    }
+
+    vfio_pci_post_reset(vdev);
+}
+
 static const Property vfio_user_pci_dev_properties[] = {
     DEFINE_PROP_UINT32("x-pci-vendor-id", VFIOPCIDevice,
                        vendor_id, PCI_ANY_ID),
@@ -269,6 +283,7 @@ static void vfio_user_pci_dev_class_init(ObjectClass *klass, const void *data)
     DeviceClass *dc = DEVICE_CLASS(klass);
     PCIDeviceClass *pdc = PCI_DEVICE_CLASS(klass);
 
+    device_class_set_legacy_reset(dc, vfio_user_pci_reset);
     device_class_set_props(dc, vfio_user_pci_dev_properties);
     dc->desc = "VFIO over socket PCI device assignment";
     pdc->realize = vfio_user_pci_realize;
Signature · 1 line
2.43.0
author
#40 lore raw

PATCH v2 24/29 vfio-user: implement VFIO_USER_DMA_MAP/UNMAP

In reply to John Levon

From: John Levon <[email protected]>

When the vfio-user container gets mapping updates, share them with the vfio-user by sending a message; this can include the region fd, allowing the server to directly mmap() the region as needed.

For performance, we only wait for the message responses when we're doing with a series of updates via the listener_commit() callback.

---
 hw/vfio-user/protocol.h   |  32 ++++++++++++
 hw/vfio-user/proxy.h      |   6 +++
 hw/vfio-user/container.c  | 107 +++++++++++++++++++++++++++++++++++++-
 hw/vfio-user/proxy.c      |  77 ++++++++++++++++++++++++++-
 hw/vfio-user/trace-events |   4 ++
 5 files changed, 222 insertions(+), 4 deletions(-)
hw/vfio-user/protocol.h +32 -0
index 920b9c44ec..4fa6d03b3f 100644
@@ -115,6 +115,31 @@ typedef struct {
  */
 #define VFIO_USER_DEF_MAX_BITMAP (256 * 1024 * 1024)
 
+/*
+ * VFIO_USER_DMA_MAP
+ * imported from struct vfio_iommu_type1_dma_map
+ */
+typedef struct {
+    VFIOUserHdr hdr;
+    uint32_t argsz;
+    uint32_t flags;
+    uint64_t offset;    /* FD offset */
+    uint64_t iova;
+    uint64_t size;
+} VFIOUserDMAMap;
+
+/*
+ * VFIO_USER_DMA_UNMAP
+ * imported from struct vfio_iommu_type1_dma_unmap
+ */
+typedef struct {
+    VFIOUserHdr hdr;
+    uint32_t argsz;
+    uint32_t flags;
+    uint64_t iova;
+    uint64_t size;
+} VFIOUserDMAUnmap;
+
 /*
  * VFIO_USER_DEVICE_GET_INFO
  * imported from struct vfio_device_info
@@ -178,4 +203,11 @@ typedef struct {
     char data[];
 } VFIOUserRegionRW;
 
+/*imported from struct vfio_bitmap */
+typedef struct {
+    uint64_t pgsize;
+    uint64_t size;
+    char data[];
+} VFIOUserBitmap;
+
 #endif /* VFIO_USER_PROTOCOL_H */
hw/vfio-user/proxy.h +6 -0
index e2cfaeead8..01fed3f17d 100644
@@ -73,6 +73,7 @@ typedef struct VFIOUserProxy {
     QemuCond close_cv;
     AioContext *ctx;
     QEMUBH *req_bh;
+    bool async_ops;
 
     /*
      * above only changed when BQL is held
@@ -102,9 +103,14 @@ void vfio_user_set_handler(VFIODevice *vbasedev,
                            void *reqarg);
 bool vfio_user_validate_version(VFIOUserProxy *proxy, Error **errp);
 
+VFIOUserFDs *vfio_user_getfds(int numfds);
+
 void vfio_user_request_msg(VFIOUserHdr *hdr, uint16_t cmd,
                            uint32_t size, uint32_t flags);
+void vfio_user_wait_reqs(VFIOUserProxy *proxy);
 void vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
                          VFIOUserFDs *fds, int rsize);
+void vfio_user_send_nowait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
+                           VFIOUserFDs *fds, int rsize);
 
 #endif /* VFIO_USER_PROXY_H */
hw/vfio-user/container.c +105 -2

129 lines not on this page · raw message

hw/vfio-user/proxy.c +75 -2

101 lines not on this page · raw message

hw/vfio-user/trace-events +4 -0
index 053f5932eb..7ef98813b3 100644
@@ -11,3 +11,7 @@ vfio_user_get_region_info(uint32_t index, uint32_t flags, uint64_t size) " index
 vfio_user_region_rw(uint32_t region, uint64_t off, uint32_t count) " region %d offset 0x%"PRIx64" count %d"
 vfio_user_get_irq_info(uint32_t index, uint32_t flags, uint32_t count) " index %d flags 0x%x count %d"
 vfio_user_set_irqs(uint32_t index, uint32_t start, uint32_t count, uint32_t flags) " index %d start %d count %d flags 0x%x"
+
+# container.c
+vfio_user_dma_map(uint64_t iova, uint64_t size, uint64_t off, uint32_t flags, bool async_ops) " iova 0x%"PRIx64" size 0x%"PRIx64" off 0x%"PRIx64" flags 0x%x async_ops %d"
+vfio_user_dma_unmap(uint64_t iova, uint64_t size, uint32_t flags,  bool async_ops) " iova 0x%"PRIx64" size 0x%"PRIx64" flags 0x%x async_ops %d"
Signature · 1 line
2.43.0
author
#41 lore raw

PATCH v2 25/29 vfio-user: implement VFIO_USER_DMA_READ/WRITE

In reply to John Levon

Unlike most other messages, this is a server->client message, for when a server wants to do "DMA"; this is slow, so normally the server has memory directly mapped instead.

---
 hw/vfio-user/protocol.h |  13 ++++-
 hw/vfio-user/proxy.h    |   3 ++
 hw/vfio-user/pci.c      | 111 ++++++++++++++++++++++++++++++++++++++++
 hw/vfio-user/proxy.c    |  84 ++++++++++++++++++++++++++++++
 4 files changed, 210 insertions(+), 1 deletion(-)
hw/vfio-user/protocol.h +12 -1
index 4fa6d03b3f..8f589faef4 100644
@@ -203,7 +203,18 @@ typedef struct {
     char data[];
 } VFIOUserRegionRW;
 
-/*imported from struct vfio_bitmap */
+/*
+ * VFIO_USER_DMA_READ
+ * VFIO_USER_DMA_WRITE
+ */
+typedef struct {
+    VFIOUserHdr hdr;
+    uint64_t offset;
+    uint32_t count;
+    char data[];
+} VFIOUserDMARW;
+
+/* imported from struct vfio_bitmap */
 typedef struct {
     uint64_t pgsize;
     uint64_t size;
hw/vfio-user/proxy.h +3 -0
index 01fed3f17d..8f2d529846 100644
@@ -104,6 +104,7 @@ void vfio_user_set_handler(VFIODevice *vbasedev,
 bool vfio_user_validate_version(VFIOUserProxy *proxy, Error **errp);
 
 VFIOUserFDs *vfio_user_getfds(int numfds);
+void vfio_user_putfds(VFIOUserMsg *msg);
 
 void vfio_user_request_msg(VFIOUserHdr *hdr, uint16_t cmd,
                            uint32_t size, uint32_t flags);
@@ -112,5 +113,7 @@ void vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
                          VFIOUserFDs *fds, int rsize);
 void vfio_user_send_nowait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
                            VFIOUserFDs *fds, int rsize);
+void vfio_user_send_reply(VFIOUserProxy *proxy, VFIOUserHdr *hdr, int size);
+void vfio_user_send_error(VFIOUserProxy *proxy, VFIOUserHdr *hdr, int error);
 
 #endif /* VFIO_USER_PROXY_H */
hw/vfio-user/pci.c +111 -0

130 lines not on this page · raw message

hw/vfio-user/proxy.c +84 -0

102 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#42 lore raw

PATCH v2 26/29 vfio-user: add 'x-msg-timeout' option

In reply to John Levon

By default, the vfio-user subsystem will wait 5 seconds for a message reply from the server. Add an option to allow this to be configurable.

---
 hw/vfio-user/proxy.h | 1 +
 hw/vfio-user/pci.c   | 5 +++++
 hw/vfio-user/proxy.c | 7 ++++---
 3 files changed, 10 insertions(+), 3 deletions(-)
hw/vfio-user/proxy.h +1 -0
index 8f2d529846..da29674fdc 100644
@@ -70,6 +70,7 @@ typedef struct VFIOUserProxy {
     uint64_t max_bitmap;
     uint64_t migr_pgsize;
     int flags;
+    uint32_t wait_time;
     QemuCond close_cv;
     AioContext *ctx;
     QEMUBH *req_bh;
hw/vfio-user/pci.c +5 -0
index dff6efaaac..e89400ba03 100644
@@ -25,6 +25,7 @@ struct VFIOUserPCIDevice {
     VFIOPCIDevice device;
     char *sock_name;
     bool send_queued;   /* all sends are queued */
+    uint32_t wait_time; /* timeout for message replies */
 };
 
 /*
@@ -254,6 +255,9 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)
         proxy->flags |= VFIO_PROXY_FORCE_QUEUED;
     }
 
+    /* user specified or 5 sec default */
+    proxy->wait_time = udev->wait_time;
+
     if (!vfio_user_validate_version(proxy, errp)) {
         goto error;
     }
@@ -387,6 +391,7 @@ static const Property vfio_user_pci_dev_properties[] = {
                        sub_device_id, PCI_ANY_ID),
     DEFINE_PROP_STRING("socket", VFIOUserPCIDevice, sock_name),
     DEFINE_PROP_BOOL("x-send-queued", VFIOUserPCIDevice, send_queued, false),
+    DEFINE_PROP_UINT32("x-msg-timeout", VFIOUserPCIDevice, wait_time, 5000),
 };
 
 static void vfio_user_pci_dev_class_init(ObjectClass *klass, const void *data)
hw/vfio-user/proxy.c +4 -3
index 0935b72e9f..74cfaff6fa 100644
@@ -24,7 +24,6 @@
 #include "qemu/main-loop.h"
 #include "system/iothread.h"
 
-static int wait_time = 5000;   /* wait up to 5 sec for busy servers */
 static IOThread *vfio_user_iothread;
 
 static void vfio_user_shutdown(VFIOUserProxy *proxy);
@@ -665,7 +664,8 @@ void vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
 
     if (ret == 0) {
         while (!msg->complete) {
-            if (!qemu_cond_timedwait(&msg->cv, &proxy->lock, wait_time)) {
+            if (!qemu_cond_timedwait(&msg->cv, &proxy->lock,
+                                     proxy->wait_time)) {
                 VFIOUserMsgQ *list;
 
                 list = msg->pending ? &proxy->pending : &proxy->outgoing;
@@ -725,7 +725,8 @@ void vfio_user_wait_reqs(VFIOUserProxy *proxy)
         msg->type = VFIO_MSG_WAIT;
         proxy->last_nowait = NULL;
         while (!msg->complete) {
-            if (!qemu_cond_timedwait(&msg->cv, &proxy->lock, wait_time)) {
+            if (!qemu_cond_timedwait(&msg->cv, &proxy->lock,
+                                     proxy->wait_time)) {
                 VFIOUserMsgQ *list;
 
                 list = msg->pending ? &proxy->pending : &proxy->outgoing;
Signature · 1 line
2.43.0
author
#43 lore raw

PATCH v2 27/29 vfio-user: support posted writes

In reply to John Levon

Support an asynchronous send of a vfio-user socket message (no wait for a reply) when the write is posted. This is only safe when no regions are mappable by the VM. Add an option to explicitly disable this as well.

---
 hw/vfio-user/proxy.h  |  5 +++++
 hw/vfio-user/device.c | 37 ++++++++++++++++++++++++++++++++++---
 hw/vfio-user/pci.c    |  6 ++++++
 hw/vfio-user/proxy.c  | 12 ++++++++++--
 4 files changed, 55 insertions(+), 5 deletions(-)
hw/vfio-user/proxy.h +5 -0
index da29674fdc..22ed66c54f 100644
@@ -94,6 +94,7 @@ typedef struct VFIOUserProxy {
 /* VFIOProxy flags */
 #define VFIO_PROXY_CLIENT        0x1
 #define VFIO_PROXY_FORCE_QUEUED  0x4
+#define VFIO_PROXY_NO_POST       0x8
 
 typedef struct VFIODevice VFIODevice;
 
@@ -107,6 +108,8 @@ bool vfio_user_validate_version(VFIOUserProxy *proxy, Error **errp);
 VFIOUserFDs *vfio_user_getfds(int numfds);
 void vfio_user_putfds(VFIOUserMsg *msg);
 
+void vfio_user_disable_posted_writes(VFIOUserProxy *proxy);
+
 void vfio_user_request_msg(VFIOUserHdr *hdr, uint16_t cmd,
                            uint32_t size, uint32_t flags);
 void vfio_user_wait_reqs(VFIOUserProxy *proxy);
@@ -114,6 +117,8 @@ void vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
                          VFIOUserFDs *fds, int rsize);
 void vfio_user_send_nowait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
                            VFIOUserFDs *fds, int rsize);
+void vfio_user_send_async(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
+                          VFIOUserFDs *fds);
 void vfio_user_send_reply(VFIOUserProxy *proxy, VFIOUserHdr *hdr, int size);
 void vfio_user_send_error(VFIOUserProxy *proxy, VFIOUserHdr *hdr, int error);
 
hw/vfio-user/device.c +34 -3
index b37f7329a1..eb2194c0eb 100644
@@ -93,10 +93,21 @@ static int vfio_user_get_region_info(VFIOUserProxy *proxy,
     trace_vfio_user_get_region_info(msgp->index, msgp->flags, msgp->size);
 
     memcpy(info, &msgp->argsz, info->argsz);
+
+    /*
+     * If at least one region is directly mapped into the VM, then we can no
+     * longer rely on the sequential nature of vfio-user request handling to
+     * ensure that posted writes are completed before a subsequent read. In this
+     * case, disable posted write support. This is a per-device property, not
+     * per-region.
+     */
+    if (info->flags & VFIO_REGION_INFO_FLAG_MMAP) {
+        vfio_user_disable_posted_writes(proxy);
+    }
+
     return 0;
 }
 
-
 static int vfio_user_device_io_get_region_info(VFIODevice *vbasedev,
                                                struct vfio_region_info *info,
                                                int *fd)
@@ -272,6 +283,12 @@ static int vfio_user_device_io_region_read(VFIODevice *vbasedev, uint8_t index,
     return msgp->count;
 }
 
+/*
+ * If this is a posted write, and VFIO_PROXY_NO_POST is not set, then we are OK
+ * to send the write to the socket without waiting for the server's reply:
+ * a subsequent read (of any region) will not pass the posted write, as all
+ * messages are handled sequentially.
+ */
 static int vfio_user_device_io_region_write(VFIODevice *vbasedev, uint8_t index,
                                             off_t off, unsigned count,
                                             void *data, bool post)
@@ -279,21 +296,35 @@ static int vfio_user_device_io_region_write(VFIODevice *vbasedev, uint8_t index,
     VFIOUserRegionRW *msgp = NULL;
     VFIOUserProxy *proxy = vbasedev->proxy;
     int size = sizeof(*msgp) + count;
+    int flags = 0;
     int ret;
 
     if (count > proxy->max_xfer_size) {
         return -EINVAL;
     }
 
+    if (proxy->flags & VFIO_PROXY_NO_POST) {
+        post = false;
+    }
+
+    if (post) {
+        flags |= VFIO_USER_NO_REPLY;
+    }
+
     msgp = g_malloc0(size);
-    vfio_user_request_msg(&msgp->hdr, VFIO_USER_REGION_WRITE, size, 0);
+    vfio_user_request_msg(&msgp->hdr, VFIO_USER_REGION_WRITE, size, flags);
     msgp->offset = off;
     msgp->region = index;
     msgp->count = count;
     memcpy(&msgp->data, data, count);
     trace_vfio_user_region_rw(msgp->region, msgp->offset, msgp->count);
 
-    /* Ignore post: all writes are synchronous/non-posted. */
+    /* async send will free msg after it's sent */
+    if (post) {
+        vfio_user_send_async(proxy, &msgp->hdr, NULL);
+        return count;
+    }
+
     vfio_user_send_wait(proxy, &msgp->hdr, NULL, 0);
     if (msgp->hdr.flags & VFIO_USER_ERROR) {
         ret = -msgp->hdr.error_reply;
hw/vfio-user/pci.c +6 -0
index e89400ba03..07fa340c17 100644
@@ -26,6 +26,7 @@ struct VFIOUserPCIDevice {
     char *sock_name;
     bool send_queued;   /* all sends are queued */
     uint32_t wait_time; /* timeout for message replies */
+    bool no_post;       /* all region writes are sync */
 };
 
 /*
@@ -255,6 +256,10 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)
         proxy->flags |= VFIO_PROXY_FORCE_QUEUED;
     }
 
+    if (udev->no_post) {
+        proxy->flags |= VFIO_PROXY_NO_POST;
+    }
+
     /* user specified or 5 sec default */
     proxy->wait_time = udev->wait_time;
 
@@ -392,6 +397,7 @@ static const Property vfio_user_pci_dev_properties[] = {
     DEFINE_PROP_STRING("socket", VFIOUserPCIDevice, sock_name),
     DEFINE_PROP_BOOL("x-send-queued", VFIOUserPCIDevice, send_queued, false),
     DEFINE_PROP_UINT32("x-msg-timeout", VFIOUserPCIDevice, wait_time, 5000),
+    DEFINE_PROP_BOOL("x-no-posted-writes", VFIOUserPCIDevice, no_post, false),
 };
 
 static void vfio_user_pci_dev_class_init(ObjectClass *klass, const void *data)
hw/vfio-user/proxy.c +10 -2
index 74cfaff6fa..13f2407845 100644
@@ -683,8 +683,8 @@ void vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
 /*
  * async send - msg can be queued, but will be freed when sent
  */
-static void vfio_user_send_async(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
-                                 VFIOUserFDs *fds)
+void vfio_user_send_async(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
+                          VFIOUserFDs *fds)
 {
     VFIOUserMsg *msg;
     int ret;
@@ -805,6 +805,14 @@ void vfio_user_putfds(VFIOUserMsg *msg)
     msg->fds = NULL;
 }
 
+void
+vfio_user_disable_posted_writes(VFIOUserProxy *proxy)
+{
+    WITH_QEMU_LOCK_GUARD(&proxy->lock) {
+         proxy->flags |= VFIO_PROXY_NO_POST;
+    }
+}
+
 static QLIST_HEAD(, VFIOUserProxy) vfio_user_sockets =
     QLIST_HEAD_INITIALIZER(vfio_user_sockets);
 
Signature · 1 line
2.43.0
author
#44 lore raw

PATCH v2 28/29 vfio-user: add coalesced posted writes

In reply to John Levon

Add new message to send multiple writes to server in a single message. Prevents the outgoing queue from overflowing when a long latency operation is followed by a series of posted writes.

---
 hw/vfio-user/protocol.h   | 21 ++++++++++
 hw/vfio-user/proxy.h      | 12 ++++++
 hw/vfio-user/device.c     | 40 +++++++++++++++++++
 hw/vfio-user/proxy.c      | 84 +++++++++++++++++++++++++++++++++++++++
 hw/vfio-user/trace-events |  1 +
 5 files changed, 158 insertions(+)
hw/vfio-user/protocol.h +21 -0
index 8f589faef4..f747e70e50 100644
@@ -42,6 +42,7 @@ enum vfio_user_command {
     VFIO_USER_DMA_WRITE                 = 12,
     VFIO_USER_DEVICE_RESET              = 13,
     VFIO_USER_DIRTY_PAGES               = 14,
+    VFIO_USER_REGION_WRITE_MULTI        = 15,
     VFIO_USER_MAX,
 };
 
@@ -75,6 +76,7 @@ typedef struct {
 #define VFIO_USER_CAP_PGSIZES   "pgsizes"
 #define VFIO_USER_CAP_MAP_MAX   "max_dma_maps"
 #define VFIO_USER_CAP_MIGR      "migration"
+#define VFIO_USER_CAP_MULTI     "write_multiple"
 
 /* "migration" members */
 #define VFIO_USER_CAP_PGSIZE            "pgsize"
@@ -221,4 +223,23 @@ typedef struct {
     char data[];
 } VFIOUserBitmap;
 
+/*
+ * VFIO_USER_REGION_WRITE_MULTI
+ */
+#define VFIO_USER_MULTI_DATA  8
+#define VFIO_USER_MULTI_MAX   200
+
+typedef struct {
+    uint64_t offset;
+    uint32_t region;
+    uint32_t count;
+    char data[VFIO_USER_MULTI_DATA];
+} VFIOUserWROne;
+
+typedef struct {
+    VFIOUserHdr hdr;
+    uint64_t wr_cnt;
+    VFIOUserWROne wrs[VFIO_USER_MULTI_MAX];
+} VFIOUserWRMulti;
+
 #endif /* VFIO_USER_PROTOCOL_H */
hw/vfio-user/proxy.h +12 -0
index 22ed66c54f..ae09b9cc60 100644
@@ -88,6 +88,8 @@ typedef struct VFIOUserProxy {
     VFIOUserMsg *last_nowait;
     VFIOUserMsg *part_recv;
     size_t recv_left;
+    VFIOUserWRMulti *wr_multi;
+    int num_outgoing;
     enum proxy_state state;
 } VFIOUserProxy;
 
@@ -95,6 +97,11 @@ typedef struct VFIOUserProxy {
 #define VFIO_PROXY_CLIENT        0x1
 #define VFIO_PROXY_FORCE_QUEUED  0x4
 #define VFIO_PROXY_NO_POST       0x8
+#define VFIO_PROXY_USE_MULTI     0x16
+
+/* coalescing high and low water marks for VFIOProxy num_outgoing */
+#define VFIO_USER_OUT_HIGH       1024
+#define VFIO_USER_OUT_LOW        128
 
 typedef struct VFIODevice VFIODevice;
 
@@ -122,4 +129,9 @@ void vfio_user_send_async(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
 void vfio_user_send_reply(VFIOUserProxy *proxy, VFIOUserHdr *hdr, int size);
 void vfio_user_send_error(VFIOUserProxy *proxy, VFIOUserHdr *hdr, int error);
 
+void vfio_user_flush_multi(VFIOUserProxy *proxy);
+void vfio_user_create_multi(VFIOUserProxy *proxy);
+void vfio_user_add_multi(VFIOUserProxy *proxy, uint8_t index,
+                         off_t offset, uint32_t count, void *data);
+
 #endif /* VFIO_USER_PROXY_H */
hw/vfio-user/device.c +40 -0
index eb2194c0eb..79375ddc96 100644
@@ -10,6 +10,8 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/lockable.h"
+#include "qemu/thread.h"
 
 #include "hw/vfio-user/device.h"
 #include "hw/vfio-user/trace.h"
@@ -296,6 +298,7 @@ static int vfio_user_device_io_region_write(VFIODevice *vbasedev, uint8_t index,
     VFIOUserRegionRW *msgp = NULL;
     VFIOUserProxy *proxy = vbasedev->proxy;
     int size = sizeof(*msgp) + count;
+    bool can_multi;
     int flags = 0;
     int ret;
 
@@ -311,6 +314,43 @@ static int vfio_user_device_io_region_write(VFIODevice *vbasedev, uint8_t index,
         flags |= VFIO_USER_NO_REPLY;
     }
 
+    /* write eligible to be in a WRITE_MULTI msg ? */
+    can_multi = (proxy->flags & VFIO_PROXY_USE_MULTI) && post &&
+        count <= VFIO_USER_MULTI_DATA;
+
+    /*
+     * This should be a rare case, so first check without the lock,
+     * if we're wrong, vfio_send_queued() will flush any posted writes
+     * we missed here
+     */
+    if (proxy->wr_multi != NULL ||
+        (proxy->num_outgoing > VFIO_USER_OUT_HIGH && can_multi)) {
+
+        /*
+         * re-check with lock
+         *
+         * if already building a WRITE_MULTI msg,
+         *  add this one if possible else flush pending before
+         *  sending the current one
+         *
+         * else if outgoing queue is over the highwater,
+         *  start a new WRITE_MULTI message
+         */
+        WITH_QEMU_LOCK_GUARD(&proxy->lock) {
+            if (proxy->wr_multi != NULL) {
+                if (can_multi) {
+                    vfio_user_add_multi(proxy, index, off, count, data);
+                    return count;
+                }
+                vfio_user_flush_multi(proxy);
+            } else if (proxy->num_outgoing > VFIO_USER_OUT_HIGH && can_multi) {
+                vfio_user_create_multi(proxy);
+                vfio_user_add_multi(proxy, index, off, count, data);
+                return count;
+            }
+        }
+    }
+
     msgp = g_malloc0(size);
     vfio_user_request_msg(&msgp->hdr, VFIO_USER_REGION_WRITE, size, flags);
     msgp->offset = off;
hw/vfio-user/proxy.c +84 -0

146 lines not on this page · raw message

hw/vfio-user/trace-events +1 -0
index 7ef98813b3..64fac9137f 100644
@@ -11,6 +11,7 @@ vfio_user_get_region_info(uint32_t index, uint32_t flags, uint64_t size) " index
 vfio_user_region_rw(uint32_t region, uint64_t off, uint32_t count) " region %d offset 0x%"PRIx64" count %d"
 vfio_user_get_irq_info(uint32_t index, uint32_t flags, uint32_t count) " index %d flags 0x%x count %d"
 vfio_user_set_irqs(uint32_t index, uint32_t start, uint32_t count, uint32_t flags) " index %d start %d count %d flags 0x%x"
+vfio_user_wrmulti(const char *s, uint64_t wr_cnt) " %s count 0x%"PRIx64
 
 # container.c
 vfio_user_dma_map(uint64_t iova, uint64_t size, uint64_t off, uint32_t flags, bool async_ops) " iova 0x%"PRIx64" size 0x%"PRIx64" off 0x%"PRIx64" flags 0x%x async_ops %d"
Signature · 1 line
2.43.0
author
#45 lore raw

PATCH v2 29/29 docs: add vfio-user documentation

In reply to John Levon

Add some basic documentation on vfio-user usage.

---
 docs/system/device-emulation.rst  |  1 +
 docs/system/devices/vfio-user.rst | 24 ++++++++++++++++++++++++
 2 files changed, 25 insertions(+)
 create mode 100644 docs/system/devices/vfio-user.rst
docs/system/device-emulation.rst +1 -0
index a1b0d7997e..911381643f 100644
@@ -85,6 +85,7 @@ Emulated Devices
    devices/can.rst
    devices/ccid.rst
    devices/cxl.rst
+   devices/vfio-user.rst
    devices/ivshmem.rst
    devices/ivshmem-flat.rst
    devices/keyboard.rst
docs/system/devices/vfio-user.rst new file +24 -0
new file mode 100644
index 0000000000..e33e49d283
@@ -0,0 +1,24 @@
+=========
+vfio-user
+=========
+
+QEMU includes a ``vfio-user`` client. The ``vfio-user`` specification allows for
+implementing (PCI) devices in userspace outside of QEMU; it is similar to
+``vhost-user`` in this respect (see :doc:`vhost-user`), but can emulate arbitrary
+PCI devices, not just ``virtio``. Whereas ``vfio`` is handled by the host
+kernel, ``vfio-user``, while similar in implementation, is handled entirely in
+userspace.
+
+For example, SPDK includes a virtual PCI NVMe controller implementation; by
+setting up a ``vfio-user`` UNIX socket between QEMU and SPDK, a VM can send NVMe
+I/O to the SPDK process.
+
+Presuming a suitable ``vfio-user`` server has opened a socket at
+``/tmp/vfio-user.sock``, a device can be configured with for example:
+
+.. code-block:: console
+
+-device vfio-user-pci,socket=/tmp/vfio-user.sock,bus=pcie.0,addr=0x9
+
+See `libvfio-user <https://github.com/nutanix/libvfio-user/>`_ for further
+information.
Signature · 1 line
2.43.0
#46 lore raw
In reply to John Levon

John

Quoted 97 lines · John Levon

The series contains an implement of a vfio-user client in QEMU, along with a few more preparatory patches.

The vfio-user protocol allows for implementing (PCI) devices in another userspace process; SPDK is one example, which includes a virtual NVMe implementation.

The vfio-user framework consists of 3 parts:
1) The VFIO user protocol specification.
2) A client - the VFIO device in QEMU that encapsulates VFIO messages and sends them to the server.
3) A server - a remote process that emulates a device.

This patchset implements parts 1 and 2.

It has been tested against libvfio-user test servers as well as SPDK.

A previous version of this series can be found at
https://lore.kernel.org/qemu-devel/[email protected]/T/#t

Changes since last series:

- rebased on v5 of vfio: return mr from vfio_get_xlat_addr
- added docs for VFIOIOMMUClass
- separated out vfio pci exports and renamed to vfio_pci namespace

John Levon (27):
vfio: add more VFIOIOMMUClass docs
vfio: move more cleanup into vfio_pci_put_device()
vfio: move config space read into vfio_pci_config_setup()
vfio: refactor out IRQ signalling setup
vfio: export PCI helpers needed for vfio-user
vfio: enable per-IRQ MSI-X masking
vfio: add per-region fd support
vfio: mark posted writes in region write callbacks vfio/container: pass MemoryRegion to DMA operations
vfio-user: add vfio-user class and container
vfio-user: connect vfio proxy to remote server
vfio-user: implement message receive infrastructure
vfio-user: implement message send infrastructure
vfio-user: implement VFIO_USER_DEVICE_GET_INFO
vfio-user: implement VFIO_USER_DEVICE_GET_REGION_INFO
vfio-user: implement VFIO_USER_REGION_READ/WRITE
vfio-user: set up PCI in vfio_user_pci_realize()
vfio-user: implement VFIO_USER_DEVICE_GET/SET_IRQ*
vfio-user: forward MSI-X PBA BAR accesses to server
vfio-user: set up container access to the proxy
vfio-user: implement VFIO_USER_DEVICE_RESET
vfio-user: implement VFIO_USER_DMA_MAP/UNMAP
vfio-user: implement VFIO_USER_DMA_READ/WRITE
vfio-user: add 'x-msg-timeout' option
vfio-user: support posted writes
vfio-user: add coalesced posted writes
docs: add vfio-user documentation

Steve Sistare (1):
vfio: return mr from vfio_get_xlat_addr

Thanos Makatos (1):
vfio-user: introduce vfio-user protocol specification

  MAINTAINERS                           |   10 +-
  docs/devel/index-internals.rst        |    1 +
  docs/devel/vfio-user.rst              | 1522 +++++++++++++++++++++++++
  docs/system/device-emulation.rst      |    1 +
  docs/system/devices/vfio-user.rst     |   24 +
  meson.build                           |    1 +
  hw/vfio-user/container.h              |   27 +
  hw/vfio-user/device.h                 |   27 +
  hw/vfio-user/protocol.h               |  245 ++++
  hw/vfio-user/proxy.h                  |  137 +++
  hw/vfio-user/trace.h                  |    1 +
  hw/vfio/pci.h                         |   16 +
  include/hw/vfio/vfio-container-base.h |   70 +-
  include/hw/vfio/vfio-device.h         |   15 +-
  include/hw/vfio/vfio-region.h         |    2 +
  include/system/memory.h               |   19 +-
  hw/vfio-user/container.c              |  348 ++++++
  hw/vfio-user/device.c                 |  389 +++++++
  hw/vfio-user/pci.c                    |  428 +++++++
  hw/vfio-user/proxy.c                  | 1314 +++++++++++++++++++++
  hw/vfio/container-base.c              |    4 +-
  hw/vfio/container.c                   |    3 +-
  hw/vfio/device.c                      |   57 +-
  hw/vfio/iommufd.c                     |    3 +-
  hw/vfio/listener.c                    |   39 +-
  hw/vfio/pci.c                         |  195 ++--
  hw/vfio/region.c                      |   10 +-
  hw/virtio/vhost-vdpa.c                |    9 +-
  system/memory.c                       |   32 +-
  hw/meson.build                        |    1 +
  hw/vfio-user/meson.build              |   11 +
  hw/vfio-user/trace-events             |   18 +
  hw/vfio/trace-events                  |    6 +-
  meson_options.txt                     |    2 +
  scripts/meson-buildoptions.sh         |    4 +
  35 files changed, 4845 insertions(+), 146 deletions(-)
Quoted 14 lines

create mode 100644 docs/devel/vfio-user.rst create mode 100644 docs/system/devices/vfio-user.rst create mode 100644 hw/vfio-user/container.h create mode 100644 hw/vfio-user/device.h create mode 100644 hw/vfio-user/protocol.h create mode 100644 hw/vfio-user/proxy.h create mode 100644 hw/vfio-user/trace.h create mode 100644 hw/vfio-user/container.c create mode 100644 hw/vfio-user/device.c create mode 100644 hw/vfio-user/pci.c create mode 100644 hw/vfio-user/proxy.c create mode 100644 hw/vfio-user/meson.build create mode 100644 hw/vfio-user/trace-events

I am planning to take first patches 2,3,4 in vfio-next. The rest either requires rework or conflicts with the live update series. Tell me if you are ok with that.

Thanks,

C.

author
#47 lore raw
Quoted 7 lines · Cédric Le Goater
Quoted 2 lines · John Levon

The series contains an implement of a vfio-user client in QEMU, along with a few more preparatory patches.

I am planning to take first patches 2,3,4 in vfio-next. The rest either requires rework or conflicts with the live update series. Tell me if you are ok with that.

Sure, I'm fine with that.

thanks
john

#48 lore raw
Quoted 11 lines · John Levon
Quoted 7 lines · Cédric Le Goater
Quoted 2 lines · John Levon

The series contains an implement of a vfio-user client in QEMU, along with a few more preparatory patches.

I am planning to take first patches 2,3,4 in vfio-next. The rest either requires rework or conflicts with the live update series. Tell me if you are ok with that.

Sure, I'm fine with that.

done.

Thanks,

C.

#49 lore raw
In reply to John Levon
Quoted 6 lines · John Levon
Quoted 4 lines · Cédric Le Goater
Quoted 2 lines · John Levon

The series contains an implement of a vfio-user client in QEMU, along with a few more preparatory patches.

Quoted 9 lines
Quoted 3 lines

I am planning to take first patches 2,3,4 in vfio-next. The rest either requires rework or conflicts with the live update series. Tell me if you are ok with that.

Sure, I'm fine with that.

thanks
john

John, could you resend a v3 on top of upstream please ?

Thanks,

C.