Skip to contentSkip to conversation

vfio-user client

PATCH In review John Johnson 2 participants 38 messages Feb 2, 2023 – Feb 10, 2023 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

v2 cover vfio-user client

Hello,

This is the 2nd patch revision of the vfio-user client implementation.

First of all, thank you for your time reviewing the previous versions.

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.

The libvfio-user project (https://github.com/nutanix/libvfio-user)
can be used by a remote process to handle the protocol to implement the third part. We also have upstreamed a patch series that implement a server using QEMU.

Contributors:

John G Johnson <[email protected]>
John Levon <[email protected]>
Thanos Makatos <[email protected]>
Elena Ufimtseva <[email protected]>
Jagannathan Raman <[email protected]>

Changes fron v1:

John Johnson (22):
vfio-user: add VFIO base abstract class

rename VFIOKernPCIDevice to VFIOKernelPCIDevice

vfio-user: add container IO ops vector

make vfio_cont_io_ioctl static
remove VFIOContIO CONT_* macros
rename VFIOContIO to VFIOContainerIO
rename VFIOContainer io_ops field to io

vfio-user: add region cache

none

vfio-user: add device IO ops vector

remove VFIODevIO VDEV_* macros
rename VFIODevIO to VFIODeviceIO
remove get_info operation from VFIODeviceIO
move short read/write check to patch 12

vfio-user: Define type vfio_user_pci_dev_info

removed reset tests
add user-pci.c file for PCI specific vfio-user code move pci.[ch] changes to user-pci.c

vfio-user: connect vfio proxy to remote server

rename VFIOProxy to VFIOUserProxy
propoagate error return from vfio_user_connect_dev() move pci.c changes to user-pci.c

vfio-user: define socket receive functions

vfio_user_process: fix function name in error case move pci.c changes to user-pci.c

vfio-user: define socket send functions

set message error in vfio_user_send_wait() on invalid flags use warn_report in caps_parse()
move pci.[ch] changes to user-pci.c

vfio-user: get device info

remove caps_offset from VFIOUserDeviceInfo add vfio_get_device() to user.c
change get_info operation to a vfio_get_device() call
typo: struct_device_info -> struct vfio_device_info move pci.c changes to user-pci.c

vfio-user: get region info

add use_regfds boolean to VFIODevice instead of testing proxy make vfio_user_get_region_info() return E2BIG on invalid argsz
typo: struct_vfio -> struct vfio

vfio-user: region read/write

add short read/write check from patch 05
move pci.c changes to user-pci.c

vfio-user: pci_user_realize PCI setup

add pci notifier calls from patch 14
add user_instance finalize code from patch 16
move pci.c changes to user-pci.c

vfio-user: get and set IRQs

move pci notifier calls to patch 13

vfio-user: forward msix BAR accesses to server

rename irq_mask_works to can_mask_irq
move pci.c changes to user-pci.c

vfio-user: proxy container connect/disconnect

refactor common IOMMU group code to be shared move user_instance finalize code to patch 13 add user.h prototypes for vfio_user_{get,put}_group() move common.c changes to user.c
move pci.c changes to user-pci.c

vfio-user: dma map/unmap operations
vfio-user: add dma_unmap_all

none

vfio-user: no-mmap DMA support

rename secure-dma to no-mmap
move pci.[ch] changes to user-pci.c

vfio-user: dma read/write operations

vfio_user_procss_req: fix function name in error case move pci.c changes to user-pci.c

vfio-user: pci reset

move pci.c changes to user-pci.c

vfio-user: add 'x-msg-timeout' option that specifies msg wait times

initialize wait_time directly from command line property list move pci.[ch] changes to user-pci.c

vfio-user: add coalesced posted writes

elaborated coalesced write usage in commit msg

Removed from v1:

vfio-user: add trace points

trace points distributed to other patches

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

 docs/devel/index-internals.rst |    1 +
 docs/devel/vfio-user.rst       | 1522 +++++++++++++++++++++++++++++
 hw/vfio/pci.h                  |   35 +-
 hw/vfio/user-protocol.h        |  243 +++++
 hw/vfio/user.h                 |  116 +++
 include/hw/vfio/vfio-common.h  |   65 ++
 hw/vfio/ap.c                   |    2 +
 hw/vfio/ccw.c                  |    8 +-
 hw/vfio/common.c               |  548 ++++++++---
 hw/vfio/igd.c                  |   23 +-
 hw/vfio/migration.c            |    2 -
 hw/vfio/pci-quirks.c           |   19 +-
 hw/vfio/pci.c                  |  557 ++++++-----
 hw/vfio/platform.c             |    3 +
 hw/vfio/user-pci.c             |  435 +++++++++
 hw/vfio/user.c                 | 2057 ++++++++++++++++++++++++++++++++++++++++
 MAINTAINERS                    |    9 +
 hw/vfio/Kconfig                |   10 +
 hw/vfio/meson.build            |    2 +
 hw/vfio/trace-events           |   15 +
 20 files changed, 5283 insertions(+), 389 deletions(-)
 create mode 100644 docs/devel/vfio-user.rst
 create mode 100644 hw/vfio/user-protocol.h
 create mode 100644 hw/vfio/user.h
 create mode 100644 hw/vfio/user-pci.c
 create mode 100644 hw/vfio/user.c
Signature · 1 line
1.9.4
#1 lore raw

PATCH v2 1/23 vfio-user: introduce vfio-user protocol specification

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"

---
 docs/devel/index-internals.rst |    1 +
 docs/devel/vfio-user.rst       | 1522 ++++++++++++++++++++++++++++++++++++++++
 MAINTAINERS                    |    6 +
 3 files changed, 1529 insertions(+)
 create mode 100644 docs/devel/vfio-user.rst
docs/devel/index-internals.rst +1 -0
index e1a93df..0ecb5c6 100644
@@ -17,5 +17,6 @@ Details about QEMU's various subsystems including how to add features to them.
    s390-dasd-ipl
    tracing
    vfio-migration
+   vfio-user
    writing-monitor-commands
    virtio-backends
docs/devel/vfio-user.rst new file +1522 -0

1,522 lines not on this page · raw message

MAINTAINERS +6 -0
index 738c4eb..999340d 100644
@@ -1984,6 +1984,12 @@ F: hw/vfio/ap.c
 F: docs/system/s390x/vfio-ap.rst
 L: [email protected]
 
+vfio-user
+M: John G Johnson <[email protected]>
+M: Thanos Makatos <[email protected]>
+S: Supported
+F: docs/devel/vfio-user.rst
+
 vhost
 M: Michael S. Tsirkin <[email protected]>
 S: Supported
Signature · 1 line
1.9.4
#2 lore raw

PATCH v2 2/23 vfio-user: add VFIO base abstract class

In reply to John Johnson

Add an abstract base class both the kernel driver and user socket implementations can use to share code.

---
 hw/vfio/pci.h |  16 +++++++--
 hw/vfio/pci.c | 106 +++++++++++++++++++++++++++++++++++-----------------------
 2 files changed, 78 insertions(+), 44 deletions(-)
hw/vfio/pci.h +14 -2
index 7c236a5..7fb656c 100644
@@ -115,8 +115,13 @@ typedef struct VFIOMSIXInfo {
     unsigned long *pending;
 } VFIOMSIXInfo;
 
-#define TYPE_VFIO_PCI "vfio-pci"
-OBJECT_DECLARE_SIMPLE_TYPE(VFIOPCIDevice, VFIO_PCI)
+/*
+ * TYPE_VFIO_PCI_BASE is an abstract type used to share code
+ * between VFIO implementations that use a kernel driver
+ * with those that use user sockets.
+ */
+#define TYPE_VFIO_PCI_BASE "vfio-pci-base"
+OBJECT_DECLARE_SIMPLE_TYPE(VFIOPCIDevice, VFIO_PCI_BASE)
 
 struct VFIOPCIDevice {
     PCIDevice pdev;
@@ -177,6 +182,13 @@ struct VFIOPCIDevice {
     Notifier irqchip_change_notifier;
 };
 
+#define TYPE_VFIO_PCI "vfio-pci"
+OBJECT_DECLARE_SIMPLE_TYPE(VFIOKernelPCIDevice, VFIO_PCI)
+
+struct VFIOKernelPCIDevice {
+    VFIOPCIDevice device;
+};
+
 /* Use uin32_t for vendor & device so PCI_ANY_ID expands and cannot match hw */
 static inline bool vfio_pci_is(VFIOPCIDevice *vdev, uint32_t vendor, uint32_t device)
 {
hw/vfio/pci.c +64 -42

229 lines not on this page · raw message

Signature · 1 line
1.9.4
#3 lore raw

PATCH v2 3/23 vfio-user: add container IO ops vector

In reply to John Johnson

Used for communication with VFIO driver
(prep work for vfio-user, which will communicate over a socket)

---
 include/hw/vfio/vfio-common.h |  24 ++++++++
 hw/vfio/common.c              | 128 ++++++++++++++++++++++++++++--------------
 2 files changed, 110 insertions(+), 42 deletions(-)
include/hw/vfio/vfio-common.h +24 -0
index e573f5a..953bc0f 100644
@@ -75,6 +75,7 @@ typedef struct VFIOAddressSpace {
 } VFIOAddressSpace;
 
 struct VFIOGroup;
+typedef struct VFIOContainerIO VFIOContainerIO;
 
 typedef struct VFIOContainer {
     VFIOAddressSpace *space;
@@ -83,6 +84,7 @@ typedef struct VFIOContainer {
     MemoryListener prereg_listener;
     unsigned iommu_type;
     Error *error;
+    VFIOContainerIO *io;
     bool initialized;
     bool dirty_pages_supported;
     uint64_t dirty_pgsizes;
@@ -154,6 +156,28 @@ struct VFIODeviceOps {
     int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f);
 };
 
+#ifdef CONFIG_LINUX
+
+/*
+ * The next 2 ops vectors are how Devices and Containers
+ * communicate with the server.  The default option is
+ * through ioctl() to the kernel VFIO driver, but vfio-user
+ * can use a socket to a remote process.
+ */
+
+struct VFIOContainerIO {
+    int (*dma_map)(VFIOContainer *container,
+                   struct vfio_iommu_type1_dma_map *map);
+    int (*dma_unmap)(VFIOContainer *container,
+                     struct vfio_iommu_type1_dma_unmap *unmap,
+                     struct vfio_bitmap *bitmap);
+    int (*dirty_bitmap)(VFIOContainer *container,
+                        struct vfio_iommu_type1_dirty_bitmap *bitmap,
+                        struct vfio_iommu_type1_dirty_bitmap_get *range);
+};
+
+#endif /* CONFIG_LINUX */
+
 typedef struct VFIOGroup {
     int fd;
     int groupid;
hw/vfio/common.c +86 -42

188 lines not on this page · raw message

Signature · 1 line
1.9.4
#4 lore raw

Re: [PATCH v2 03/23] vfio-user: add container IO ops vector

Quoted 72 lines · On Wed, 1 Feb 2023 21:55:39 -0800 John Johnson <[email protected]>…

Used for communication with VFIO driver
(prep work for vfio-user, which will communicate over a socket)

---
 include/hw/vfio/vfio-common.h |  24 ++++++++
 hw/vfio/common.c              | 128 ++++++++++++++++++++++++++++--------------
 2 files changed, 110 insertions(+), 42 deletions(-)
include/hw/vfio/vfio-common.h +24 -0
index e573f5a..953bc0f 100644
@@ -75,6 +75,7 @@ typedef struct VFIOAddressSpace {
 } VFIOAddressSpace;
 
 struct VFIOGroup;
+typedef struct VFIOContainerIO VFIOContainerIO;
 
 typedef struct VFIOContainer {
     VFIOAddressSpace *space;
@@ -83,6 +84,7 @@ typedef struct VFIOContainer {
     MemoryListener prereg_listener;
     unsigned iommu_type;
     Error *error;
+    VFIOContainerIO *io;
     bool initialized;
     bool dirty_pages_supported;
     uint64_t dirty_pgsizes;
@@ -154,6 +156,28 @@ struct VFIODeviceOps {
     int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f);
 };
 
+#ifdef CONFIG_LINUX
+
+/*
+ * The next 2 ops vectors are how Devices and Containers
+ * communicate with the server.  The default option is
+ * through ioctl() to the kernel VFIO driver, but vfio-user
+ * can use a socket to a remote process.
+ */
+
+struct VFIOContainerIO {
+    int (*dma_map)(VFIOContainer *container,
+                   struct vfio_iommu_type1_dma_map *map);
+    int (*dma_unmap)(VFIOContainer *container,
+                     struct vfio_iommu_type1_dma_unmap *unmap,
+                     struct vfio_bitmap *bitmap);
+    int (*dirty_bitmap)(VFIOContainer *container,
+                        struct vfio_iommu_type1_dirty_bitmap *bitmap,
+                        struct vfio_iommu_type1_dirty_bitmap_get *range);
+};
+
+#endif /* CONFIG_LINUX */
+
 typedef struct VFIOGroup {
     int fd;
     int groupid;
hw/vfio/common.c +2 -0
index ace9562..9310a7f 100644
@@ -58,6 +58,8 @@ static QLIST_HEAD(, VFIOAddressSpace) vfio_address_spaces =
 static int vfio_kvm_device_fd = -1;
 #endif
 
+static VFIOContainerIO vfio_cont_io_ioctl;
+
 /*
  * Common VFIO interrupt disable
  */

hw/vfio/common.c static int vfio_dma_unmap_bitmap(VFIOContainer *container, · line 437

Quoted 5 lines
hw/vfio/common.c +1 -1
@@ -432,12 +434,12 @@ static int vfio_dma_unmap_bitmap(VFIOContainer *container,
         goto unmap_exit;
     }
 
-    ret = ioctl(container->fd, VFIO_IOMMU_UNMAP_DMA, unmap);
+    ret = container->io->dma_unmap(container, unmap, bitmap);

The bitmap arg doesn't make a lot of sense here, its use doesn't come around until vfio_user_dma_unmap() is added, but even then, it's readily available at &unmap->data with validity determined by unmap->flags. The eventual sanity test in vfio_user_dma_unmap() really only seems to prove the arg is unnecessary. Thanks,

Alex

#5 lore raw

Re: [PATCH v2 03/23] vfio-user: add container IO ops vector

Quoted 78 lines · On Fri, 3 Feb 2023 15:21:09 -0700 Alex Williamson <[email protected]>…
Quoted 75 lines · On Wed, 1 Feb 2023 21:55:39 -0800 John Johnson <[email protected]>…

Used for communication with VFIO driver
(prep work for vfio-user, which will communicate over a socket)

---
 include/hw/vfio/vfio-common.h |  24 ++++++++
 hw/vfio/common.c              | 128 ++++++++++++++++++++++++++++--------------
 2 files changed, 110 insertions(+), 42 deletions(-)
include/hw/vfio/vfio-common.h +24 -0
index e573f5a..953bc0f 100644
@@ -75,6 +75,7 @@ typedef struct VFIOAddressSpace {
 } VFIOAddressSpace;
 
 struct VFIOGroup;
+typedef struct VFIOContainerIO VFIOContainerIO;
 
 typedef struct VFIOContainer {
     VFIOAddressSpace *space;
@@ -83,6 +84,7 @@ typedef struct VFIOContainer {
     MemoryListener prereg_listener;
     unsigned iommu_type;
     Error *error;
+    VFIOContainerIO *io;
     bool initialized;
     bool dirty_pages_supported;
     uint64_t dirty_pgsizes;
@@ -154,6 +156,28 @@ struct VFIODeviceOps {
     int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f);
 };
 
+#ifdef CONFIG_LINUX
+
+/*
+ * The next 2 ops vectors are how Devices and Containers
+ * communicate with the server.  The default option is
+ * through ioctl() to the kernel VFIO driver, but vfio-user
+ * can use a socket to a remote process.
+ */
+
+struct VFIOContainerIO {
+    int (*dma_map)(VFIOContainer *container,
+                   struct vfio_iommu_type1_dma_map *map);
+    int (*dma_unmap)(VFIOContainer *container,
+                     struct vfio_iommu_type1_dma_unmap *unmap,
+                     struct vfio_bitmap *bitmap);
+    int (*dirty_bitmap)(VFIOContainer *container,
+                        struct vfio_iommu_type1_dirty_bitmap *bitmap,
+                        struct vfio_iommu_type1_dirty_bitmap_get *range);
+};
+
+#endif /* CONFIG_LINUX */
+
 typedef struct VFIOGroup {
     int fd;
     int groupid;
hw/vfio/common.c +2 -0
index ace9562..9310a7f 100644
@@ -58,6 +58,8 @@ static QLIST_HEAD(, VFIOAddressSpace) vfio_address_spaces =
 static int vfio_kvm_device_fd = -1;
 #endif
 
+static VFIOContainerIO vfio_cont_io_ioctl;
+
 /*
  * Common VFIO interrupt disable
  */
@@ -432,12 +434,12 @@ static int vfio_dma_unmap_bitmap(VFIOContainer *container,
         goto unmap_exit;
     }
 
Quoted 8 lines

hw/vfio/common.c static int vfio_dma_unmap_bitmap(VFIOContainer *container, · line 437

Quoted 2 lines
hw/vfio/common.c +1 -1
-    ret = ioctl(container->fd, VFIO_IOMMU_UNMAP_DMA, unmap);
+    ret = container->io->dma_unmap(container, unmap, bitmap);  

The bitmap arg doesn't make a lot of sense here, its use doesn't come around until vfio_user_dma_unmap() is added, but even then, it's readily available at &unmap->data with validity determined by unmap->flags. The eventual sanity test in vfio_user_dma_unmap() really only seems to prove the arg is unnecessary. Thanks,

The same comment really applies to .dirty_bitmap() with respect to the range arg, but I think it's also important to note that both of these are all but deprecated interfaces for the kernel. The migration series[1] is pre-enabling these kernel interfaces to be removed by adding support to QEMU to dirty all pages when support isn't present, then we replace it with what we expect to be a longer term solution with device dirty tracking support[2]. All the more reason not to make these part of the internal API shared by kernel and user versions. Thanks,

Alex

[1]https://lore.kernel.org/all/[email protected]/ [2]https://lore.kernel.org/all/[email protected]/

#6 lore raw

PATCH v2 4/23 vfio-user: add region cache

In reply to John Johnson

cache VFIO_DEVICE_GET_REGION_INFO results to reduce memory alloc/free cycles and as prep work for vfio-user

---
 include/hw/vfio/vfio-common.h |  2 ++
 hw/vfio/ccw.c                 |  5 -----
 hw/vfio/common.c              | 41 +++++++++++++++++++++++++++++++++++------
 hw/vfio/igd.c                 | 23 +++++++++--------------
 hw/vfio/migration.c           |  2 --
 hw/vfio/pci-quirks.c          | 19 +++++--------------
 hw/vfio/pci.c                 |  8 --------
 7 files changed, 51 insertions(+), 49 deletions(-)
include/hw/vfio/vfio-common.h +2 -0
index 953bc0f..7779cc7 100644
@@ -145,6 +145,7 @@ typedef struct VFIODevice {
     VFIOMigration *migration;
     Error *migration_blocker;
     OnOffAuto pre_copy_dirty_page_tracking;
+    struct vfio_region_info **regions;
 } VFIODevice;
 
 struct VFIODeviceOps {
@@ -249,6 +250,7 @@ int vfio_get_region_info(VFIODevice *vbasedev, int index,
                          struct vfio_region_info **info);
 int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type,
                              uint32_t subtype, struct vfio_region_info **info);
+void vfio_get_all_regions(VFIODevice *vbasedev);
 bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type);
 struct vfio_info_cap_header *
 vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id);
hw/vfio/ccw.c +0 -5
index 0354737..06b588c 100644
@@ -517,7 +517,6 @@ static void vfio_ccw_get_region(VFIOCCWDevice *vcdev, Error **errp)
 
     vcdev->io_region_offset = info->offset;
     vcdev->io_region = g_malloc0(info->size);
-    g_free(info);
 
     /* check for the optional async command region */
     ret = vfio_get_dev_region_info(vdev, VFIO_REGION_TYPE_CCW,
@@ -530,7 +529,6 @@ static void vfio_ccw_get_region(VFIOCCWDevice *vcdev, Error **errp)
         }
         vcdev->async_cmd_region_offset = info->offset;
         vcdev->async_cmd_region = g_malloc0(info->size);
-        g_free(info);
     }
 
     ret = vfio_get_dev_region_info(vdev, VFIO_REGION_TYPE_CCW,
@@ -543,7 +541,6 @@ static void vfio_ccw_get_region(VFIOCCWDevice *vcdev, Error **errp)
         }
         vcdev->schib_region_offset = info->offset;
         vcdev->schib_region = g_malloc(info->size);
-        g_free(info);
     }
 
     ret = vfio_get_dev_region_info(vdev, VFIO_REGION_TYPE_CCW,
@@ -557,7 +554,6 @@ static void vfio_ccw_get_region(VFIOCCWDevice *vcdev, Error **errp)
         }
         vcdev->crw_region_offset = info->offset;
         vcdev->crw_region = g_malloc(info->size);
-        g_free(info);
     }
 
     return;
@@ -567,7 +563,6 @@ out_err:
     g_free(vcdev->schib_region);
     g_free(vcdev->async_cmd_region);
     g_free(vcdev->io_region);
-    g_free(info);
     return;
 }
 
hw/vfio/common.c +35 -6

95 lines not on this page · raw message

hw/vfio/igd.c +9 -14
index afe3fe7..22efa1a 100644
@@ -425,7 +425,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
     if ((ret || !rom->size) && !vdev->pdev.romfile) {
         error_report("IGD device %s has no ROM, legacy mode disabled",
                      vdev->vbasedev.name);
-        goto out;
+        return;
     }
 
     /*
@@ -436,7 +436,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
         error_report("IGD device %s hotplugged, ROM disabled, "
                      "legacy mode disabled", vdev->vbasedev.name);
         vdev->rom_read_failed = true;
-        goto out;
+        return;
     }
 
     /*
@@ -449,7 +449,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
     if (ret) {
         error_report("IGD device %s does not support OpRegion access,"
                      "legacy mode disabled", vdev->vbasedev.name);
-        goto out;
+        return;
     }
 
     ret = vfio_get_dev_region_info(&vdev->vbasedev,
@@ -458,7 +458,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
     if (ret) {
         error_report("IGD device %s does not support host bridge access,"
                      "legacy mode disabled", vdev->vbasedev.name);
-        goto out;
+        return;
     }
 
     ret = vfio_get_dev_region_info(&vdev->vbasedev,
@@ -467,7 +467,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
     if (ret) {
         error_report("IGD device %s does not support LPC bridge access,"
                      "legacy mode disabled", vdev->vbasedev.name);
-        goto out;
+        return;
     }
 
     gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, 4);
@@ -481,7 +481,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
         error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
         error_report("IGD device %s failed to enable VGA access, "
                      "legacy mode disabled", vdev->vbasedev.name);
-        goto out;
+        return;
     }
 
     /* Create our LPC/ISA bridge */
@@ -489,7 +489,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
     if (ret) {
         error_report("IGD device %s failed to create LPC bridge, "
                      "legacy mode disabled", vdev->vbasedev.name);
-        goto out;
+        return;
     }
 
     /* Stuff some host values into the VM PCI host bridge */
@@ -497,7 +497,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
     if (ret) {
         error_report("IGD device %s failed to modify host bridge, "
                      "legacy mode disabled", vdev->vbasedev.name);
-        goto out;
+        return;
     }
 
     /* Setup OpRegion access */
@@ -505,7 +505,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
     if (ret) {
         error_append_hint(&err, "IGD legacy mode disabled\n");
         error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
-        goto out;
+        return;
     }
 
     /* Setup our quirk to munge GTT addresses to the VM allocated buffer */
@@ -608,9 +608,4 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
 
     trace_vfio_pci_igd_bdsm_enabled(vdev->vbasedev.name, ggms_mb + gms_mb);
 
-out:
-    g_free(rom);
-    g_free(opregion);
-    g_free(host);
-    g_free(lpc);
 }
hw/vfio/migration.c +0 -2
index a6ad1f8..397be43 100644
@@ -879,13 +879,11 @@ int vfio_migration_probe(VFIODevice *vbasedev, Error **errp)
     }
 
     trace_vfio_migration_probe(vbasedev->name, info->index);
-    g_free(info);
     return 0;
 
 add_blocker:
     error_setg(&vbasedev->migration_blocker,
                "VFIO device doesn't support migration");
-    g_free(info);
 
     ret = migrate_add_blocker(vbasedev->migration_blocker, errp);
     if (ret < 0) {
hw/vfio/pci-quirks.c +5 -14
index f0147a0..c04ee19 100644
@@ -1585,16 +1585,14 @@ int vfio_pci_nvidia_v100_ram_init(VFIOPCIDevice *vdev, Error **errp)
 
     hdr = vfio_get_region_info_cap(nv2reg, VFIO_REGION_INFO_CAP_NVLINK2_SSATGT);
     if (!hdr) {
-        ret = -ENODEV;
-        goto free_exit;
+        return -ENODEV;
     }
     cap = (void *) hdr;
 
     p = mmap(NULL, nv2reg->size, PROT_READ | PROT_WRITE,
              MAP_SHARED, vdev->vbasedev.fd, nv2reg->offset);
     if (p == MAP_FAILED) {
-        ret = -errno;
-        goto free_exit;
+        return -errno;
     }
 
     quirk = vfio_quirk_alloc(1);
@@ -1607,8 +1605,6 @@ int vfio_pci_nvidia_v100_ram_init(VFIOPCIDevice *vdev, Error **errp)
                                    OBJ_PROP_FLAG_READ);
     trace_vfio_pci_nvidia_gpu_setup_quirk(vdev->vbasedev.name, cap->tgt,
                                           nv2reg->size);
-free_exit:
-    g_free(nv2reg);
 
     return ret;
 }
@@ -1635,16 +1631,14 @@ int vfio_pci_nvlink2_init(VFIOPCIDevice *vdev, Error **errp)
     hdr = vfio_get_region_info_cap(atsdreg,
                                    VFIO_REGION_INFO_CAP_NVLINK2_SSATGT);
     if (!hdr) {
-        ret = -ENODEV;
-        goto free_exit;
+        return -ENODEV;
     }
     captgt = (void *) hdr;
 
     hdr = vfio_get_region_info_cap(atsdreg,
                                    VFIO_REGION_INFO_CAP_NVLINK2_LNKSPD);
     if (!hdr) {
-        ret = -ENODEV;
-        goto free_exit;
+        return -ENODEV;
     }
     capspeed = (void *) hdr;
 
@@ -1653,8 +1647,7 @@ int vfio_pci_nvlink2_init(VFIOPCIDevice *vdev, Error **errp)
         p = mmap(NULL, atsdreg->size, PROT_READ | PROT_WRITE,
                  MAP_SHARED, vdev->vbasedev.fd, atsdreg->offset);
         if (p == MAP_FAILED) {
-            ret = -errno;
-            goto free_exit;
+            return -errno;
         }
 
         quirk = vfio_quirk_alloc(1);
@@ -1674,8 +1667,6 @@ int vfio_pci_nvlink2_init(VFIOPCIDevice *vdev, Error **errp)
                                    OBJ_PROP_FLAG_READ);
     trace_vfio_pci_nvlink2_setup_quirk_lnkspd(vdev->vbasedev.name,
                                               capspeed->link_speed);
-free_exit:
-    g_free(atsdreg);
 
     return ret;
 }
hw/vfio/pci.c +0 -8
index 9d70114..b214a93 100644
@@ -836,8 +836,6 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
     vdev->rom_size = size = reg_info->size;
     vdev->rom_offset = reg_info->offset;
 
-    g_free(reg_info);
-
     if (!vdev->rom_size) {
         vdev->rom_read_failed = true;
         error_report("vfio-pci: Cannot read device rom at "
@@ -2564,7 +2562,6 @@ int vfio_populate_vga(VFIOPCIDevice *vdev, Error **errp)
         error_setg(errp, "unexpected VGA info, flags 0x%lx, size 0x%lx",
                    (unsigned long)reg_info->flags,
                    (unsigned long)reg_info->size);
-        g_free(reg_info);
         return -EINVAL;
     }
 
@@ -2573,8 +2570,6 @@ int vfio_populate_vga(VFIOPCIDevice *vdev, Error **errp)
     vdev->vga->fd_offset = reg_info->offset;
     vdev->vga->fd = vdev->vbasedev.fd;
 
-    g_free(reg_info);
-
     vdev->vga->region[QEMU_PCI_VGA_MEM].offset = QEMU_PCI_VGA_MEM_BASE;
     vdev->vga->region[QEMU_PCI_VGA_MEM].nr = QEMU_PCI_VGA_MEM;
     QLIST_INIT(&vdev->vga->region[QEMU_PCI_VGA_MEM].quirks);
@@ -2669,8 +2664,6 @@ static void vfio_populate_device(VFIOPCIDevice *vdev, Error **errp)
     }
     vdev->config_offset = reg_info->offset;
 
-    g_free(reg_info);
-
     if (vdev->features & VFIO_FEATURE_ENABLE_VGA) {
         ret = vfio_populate_vga(vdev, errp);
         if (ret) {
@@ -3079,7 +3072,6 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
         }
 
         ret = vfio_pci_igd_opregion_init(vdev, opregion, errp);
-        g_free(opregion);
         if (ret) {
             goto out_teardown;
         }
Signature · 1 line
1.9.4
#7 lore raw

PATCH v2 5/23 vfio-user: add device IO ops vector

In reply to John Johnson

Used for communication with VFIO driver
(prep work for vfio-user, which will communicate over a socket)

---
 include/hw/vfio/vfio-common.h |  14 +++++
 hw/vfio/ap.c                  |   1 +
 hw/vfio/ccw.c                 |   1 +
 hw/vfio/common.c              |  85 +++++++++++++++++++++----
 hw/vfio/pci.c                 | 140 ++++++++++++++++++++++++++----------------
 hw/vfio/platform.c            |   1 +
 6 files changed, 178 insertions(+), 64 deletions(-)
include/hw/vfio/vfio-common.h +14 -0
index 7779cc7..c2ff9ea 100644
@@ -124,6 +124,7 @@ typedef struct VFIOHostDMAWindow {
 } VFIOHostDMAWindow;
 
 typedef struct VFIODeviceOps VFIODeviceOps;
+typedef struct VFIODeviceIO VFIODeviceIO;
 
 typedef struct VFIODevice {
     QLIST_ENTRY(VFIODevice) next;
@@ -139,6 +140,7 @@ typedef struct VFIODevice {
     bool ram_block_discard_allowed;
     bool enable_migration;
     VFIODeviceOps *ops;
+    VFIODeviceIO *io;
     unsigned int num_irqs;
     unsigned int num_regions;
     unsigned int flags;
@@ -165,6 +167,16 @@ struct VFIODeviceOps {
  * through ioctl() to the kernel VFIO driver, but vfio-user
  * can use a socket to a remote process.
  */
+struct VFIODeviceIO {
+    int (*get_region_info)(VFIODevice *vdev,
+                           struct vfio_region_info *info);
+    int (*get_irq_info)(VFIODevice *vdev, struct vfio_irq_info *irq);
+    int (*set_irqs)(VFIODevice *vdev, struct vfio_irq_set *irqs);
+    int (*region_read)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size,
+                       void *data);
+    int (*region_write)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size,
+                        void *data);
+};
 
 struct VFIOContainerIO {
     int (*dma_map)(VFIOContainer *container,
@@ -177,6 +189,8 @@ struct VFIOContainerIO {
                         struct vfio_iommu_type1_dirty_bitmap_get *range);
 };
 
+extern VFIODeviceIO vfio_dev_io_ioctl;
+
 #endif /* CONFIG_LINUX */
 
 typedef struct VFIOGroup {
hw/vfio/ap.c +1 -0
index e0dd561..c6638d5 100644
@@ -102,6 +102,7 @@ static void vfio_ap_realize(DeviceState *dev, Error **errp)
     mdevid = basename(vapdev->vdev.sysfsdev);
     vapdev->vdev.name = g_strdup_printf("%s", mdevid);
     vapdev->vdev.dev = dev;
+    vapdev->vdev.io = &vfio_dev_io_ioctl;
 
     /*
      * vfio-ap devices operate in a way compatible with discarding of
hw/vfio/ccw.c +1 -0
index 06b588c..e4d840d 100644
@@ -614,6 +614,7 @@ static void vfio_ccw_get_device(VFIOGroup *group, VFIOCCWDevice *vcdev,
     vcdev->vdev.type = VFIO_DEVICE_TYPE_CCW;
     vcdev->vdev.name = name;
     vcdev->vdev.dev = &vcdev->cdev.parent_obj.parent_obj;
+    vcdev->vdev.io = &vfio_dev_io_ioctl;
 
     return;
 
hw/vfio/common.c +74 -11

158 lines not on this page · raw message

hw/vfio/pci.c +87 -53

289 lines not on this page · raw message

hw/vfio/platform.c +1 -0
index 5af73f9..222405e 100644
@@ -621,6 +621,7 @@ static void vfio_platform_realize(DeviceState *dev, Error **errp)
     vbasedev->type = VFIO_DEVICE_TYPE_PLATFORM;
     vbasedev->dev = dev;
     vbasedev->ops = &vfio_platform_ops;
+    vbasedev->io = &vfio_dev_io_ioctl;
 
     qemu_mutex_init(&vdev->intp_mutex);
 
Signature · 1 line
1.9.4
#8 lore raw

PATCH v2 6/23 vfio-user: Define type vfio_user_pci_dev_info

In reply to John Johnson

New class for vfio-user with its class and instance constructors and destructors, and its pci ops.

---
 hw/vfio/pci.h       |   7 +++
 hw/vfio/pci.c       |  12 +++---
 hw/vfio/user-pci.c  | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 MAINTAINERS         |   3 ++
 hw/vfio/Kconfig     |  10 +++++
 hw/vfio/meson.build |   1 +
 6 files changed, 148 insertions(+), 6 deletions(-)
 create mode 100644 hw/vfio/user-pci.c
hw/vfio/pci.h +7 -0
index 7fb656c..50a1d07 100644
@@ -208,6 +208,13 @@ uint32_t vfio_pci_read_config(PCIDevice *pdev, uint32_t addr, int len);
 void vfio_pci_write_config(PCIDevice *pdev,
                            uint32_t addr, uint32_t val, int len);
 
+void vfio_intx_eoi(VFIODevice *vbasedev);
+Object *vfio_pci_get_object(VFIODevice *vbasedev);
+void vfio_pci_save_config(VFIODevice *vbasedev, QEMUFile *f);
+int vfio_pci_load_config(VFIODevice *vbasedev, QEMUFile *f);
+void vfio_put_device(VFIOPCIDevice *vdev);
+void vfio_instance_init(Object *obj);
+
 uint64_t vfio_vga_read(void *opaque, hwaddr addr, unsigned size);
 void vfio_vga_write(void *opaque, hwaddr addr, uint64_t data, unsigned size);
 
hw/vfio/pci.c +6 -6
index c3c2e76..a8bc0ea 100644
@@ -107,7 +107,7 @@ static void vfio_intx_interrupt(void *opaque)
     }
 }
 
-static void vfio_intx_eoi(VFIODevice *vbasedev)
+void vfio_intx_eoi(VFIODevice *vbasedev)
 {
     VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
 
@@ -2492,7 +2492,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);
 
@@ -2517,14 +2517,14 @@ const VMStateDescription vmstate_vfio_pci_config = {
     }
 };
 
-static void vfio_pci_save_config(VFIODevice *vbasedev, QEMUFile *f)
+void vfio_pci_save_config(VFIODevice *vbasedev, QEMUFile *f)
 {
     VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
 
     vmstate_save_state(f, &vmstate_vfio_pci_config, vdev, NULL);
 }
 
-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;
@@ -2719,7 +2719,7 @@ static void vfio_populate_device(VFIOPCIDevice *vdev, Error **errp)
     }
 }
 
-static void vfio_put_device(VFIOPCIDevice *vdev)
+void vfio_put_device(VFIOPCIDevice *vdev)
 {
     g_free(vdev->vbasedev.name);
     g_free(vdev->msix);
@@ -3271,7 +3271,7 @@ post_reset:
     vfio_pci_post_reset(vdev);
 }
 
-static void vfio_instance_init(Object *obj)
+void vfio_instance_init(Object *obj)
 {
     PCIDevice *pci_dev = PCI_DEVICE(obj);
     VFIOPCIDevice *vdev = VFIO_PCI_BASE(obj);
hw/vfio/user-pci.c new file +121 -0

121 lines not on this page · raw message

MAINTAINERS +3 -0
index 999340d..28fce0e 100644
@@ -1987,8 +1987,11 @@ L: [email protected]
 vfio-user
 M: John G Johnson <[email protected]>
 M: Thanos Makatos <[email protected]>
+M: Elena Ufimtseva <[email protected]>
+M: Jagannathan Raman <[email protected]>
 S: Supported
 F: docs/devel/vfio-user.rst
+F: hw/vfio/user*
 
 vhost
 M: Michael S. Tsirkin <[email protected]>
hw/vfio/Kconfig +10 -0
index 7cdba05..301894e 100644
@@ -2,6 +2,10 @@ config VFIO
     bool
     depends on LINUX
 
+config VFIO_USER
+    bool
+    depends on VFIO
+
 config VFIO_PCI
     bool
     default y
@@ -9,6 +13,12 @@ config VFIO_PCI
     select EDID
     depends on LINUX && PCI
 
+config VFIO_USER_PCI
+    bool
+    default y
+    select VFIO_USER
+    depends on VFIO_PCI
+
 config VFIO_CCW
     bool
     default y
hw/vfio/meson.build +1 -0
index da9af29..731c3c6 100644
@@ -9,6 +9,7 @@ vfio_ss.add(when: 'CONFIG_VFIO_PCI', if_true: files(
   'pci-quirks.c',
   'pci.c',
 ))
+vfio_ss.add(when: 'CONFIG_VFIO_USER_PCI', if_true: files('user-pci.c'))
 vfio_ss.add(when: 'CONFIG_VFIO_CCW', if_true: files('ccw.c'))
 vfio_ss.add(when: 'CONFIG_VFIO_PLATFORM', if_true: files('platform.c'))
 vfio_ss.add(when: 'CONFIG_VFIO_XGMAC', if_true: files('calxeda-xgmac.c'))
Signature · 1 line
1.9.4
#9 lore raw

PATCH v2 7/23 vfio-user: connect vfio proxy to remote server

In reply to John Johnson

add user.c & user.h files for vfio-user code add proxy struct to handle comms with remote server

---
 hw/vfio/user.h                |  78 +++++++++++++++++++
 include/hw/vfio/vfio-common.h |   2 +
 hw/vfio/user-pci.c            |  19 +++++
 hw/vfio/user.c                | 170 ++++++++++++++++++++++++++++++++++++++++++
 hw/vfio/meson.build           |   1 +
 5 files changed, 270 insertions(+)
 create mode 100644 hw/vfio/user.h
 create mode 100644 hw/vfio/user.c
hw/vfio/user.h new file +78 -0
new file mode 100644
index 0000000..ac7d15d
@@ -0,0 +1,78 @@
+#ifndef VFIO_USER_H
+#define VFIO_USER_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.
+ *
+ */
+
+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_H */
include/hw/vfio/vfio-common.h +2 -0
index c2ff9ea..e1ee0ac 100644
@@ -76,6 +76,7 @@ typedef struct VFIOAddressSpace {
 
 struct VFIOGroup;
 typedef struct VFIOContainerIO VFIOContainerIO;
+typedef struct VFIOUserProxy VFIOUserProxy;
 
 typedef struct VFIOContainer {
     VFIOAddressSpace *space;
@@ -147,6 +148,7 @@ typedef struct VFIODevice {
     VFIOMigration *migration;
     Error *migration_blocker;
     OnOffAuto pre_copy_dirty_page_tracking;
+    VFIOUserProxy *proxy;
     struct vfio_region_info **regions;
 } VFIODevice;
 
hw/vfio/user-pci.c +19 -0
index fc47a3e..a3fc36d 100644
@@ -32,6 +32,7 @@
 #include "qapi/error.h"
 #include "migration/blocker.h"
 #include "migration/qemu-file.h"
+#include "hw/vfio/user.h"
 
 #define TYPE_VFIO_USER_PCI "vfio-user-pci"
 OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI)
@@ -63,6 +64,9 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)
     VFIOUserPCIDevice *udev = VFIO_USER_PCI(pdev);
     VFIOPCIDevice *vdev = VFIO_PCI_BASE(pdev);
     VFIODevice *vbasedev = &vdev->vbasedev;
+    SocketAddress addr;
+    VFIOUserProxy *proxy;
+    Error *err = NULL;
 
     /*
      * TODO: make option parser understand SocketAddress
@@ -75,6 +79,16 @@ 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, &err);
+    if (!proxy) {
+        error_propagate(errp, err);
+        return;
+    }
+    vbasedev->proxy = proxy;
+
     vbasedev->name = g_strdup_printf("VFIO user <%s>", udev->sock_name);
     vbasedev->ops = &vfio_user_pci_ops;
     vbasedev->type = VFIO_DEVICE_TYPE_PCI;
@@ -85,8 +99,13 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)
 static void vfio_user_instance_finalize(Object *obj)
 {
     VFIOPCIDevice *vdev = VFIO_PCI_BASE(obj);
+    VFIODevice *vbasedev = &vdev->vbasedev;
 
     vfio_put_device(vdev);
+
+    if (vbasedev->proxy != NULL) {
+        vfio_user_disconnect(vbasedev->proxy);
+    }
 }
 
 static Property vfio_user_pci_dev_properties[] = {
hw/vfio/user.c new file +170 -0

170 lines not on this page · raw message

hw/vfio/meson.build +1 -0
index 731c3c6..f24a47d 100644
@@ -9,6 +9,7 @@ vfio_ss.add(when: 'CONFIG_VFIO_PCI', if_true: files(
   'pci-quirks.c',
   'pci.c',
 ))
+vfio_ss.add(when: 'CONFIG_VFIO_USER', if_true: files('user.c'))
 vfio_ss.add(when: 'CONFIG_VFIO_USER_PCI', if_true: files('user-pci.c'))
 vfio_ss.add(when: 'CONFIG_VFIO_CCW', if_true: files('ccw.c'))
 vfio_ss.add(when: 'CONFIG_VFIO_PLATFORM', if_true: files('platform.c'))
Signature · 1 line
1.9.4
#10 lore raw

PATCH v2 8/23 vfio-user: define socket receive functions

In reply to John Johnson

Add infrastructure needed to receive incoming messages

---
 hw/vfio/user-protocol.h |  54 +++++++
 hw/vfio/user.h          |   8 +
 hw/vfio/user-pci.c      |  11 ++
 hw/vfio/user.c          | 408 ++++++++++++++++++++++++++++++++++++++++++++++++
 hw/vfio/trace-events    |   5 +
 5 files changed, 486 insertions(+)
 create mode 100644 hw/vfio/user-protocol.h
hw/vfio/user-protocol.h new file +54 -0
new file mode 100644
index 0000000..d23877c
@@ -0,0 +1,54 @@
+#ifndef VFIO_USER_PROTOCOL_H
+#define VFIO_USER_PROTOCOL_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.
+ *
+ * Each message has a standard header that describes the command
+ * being sent, which is almost always a VFIO ioctl().
+ *
+ * The header may be followed by command-specific data, such as the
+ * region and offset info for read and write commands.
+ */
+
+typedef struct {
+    uint16_t id;
+    uint16_t command;
+    uint32_t size;
+    uint32_t flags;
+    uint32_t error_reply;
+} VFIOUserHdr;
+
+/* VFIOUserHdr commands */
+enum vfio_user_command {
+    VFIO_USER_VERSION                   = 1,
+    VFIO_USER_DMA_MAP                   = 2,
+    VFIO_USER_DMA_UNMAP                 = 3,
+    VFIO_USER_DEVICE_GET_INFO           = 4,
+    VFIO_USER_DEVICE_GET_REGION_INFO    = 5,
+    VFIO_USER_DEVICE_GET_REGION_IO_FDS  = 6,
+    VFIO_USER_DEVICE_GET_IRQ_INFO       = 7,
+    VFIO_USER_DEVICE_SET_IRQS           = 8,
+    VFIO_USER_REGION_READ               = 9,
+    VFIO_USER_REGION_WRITE              = 10,
+    VFIO_USER_DMA_READ                  = 11,
+    VFIO_USER_DMA_WRITE                 = 12,
+    VFIO_USER_DEVICE_RESET              = 13,
+    VFIO_USER_DIRTY_PAGES               = 14,
+    VFIO_USER_MAX,
+};
+
+/* VFIOUserHdr flags */
+#define VFIO_USER_REQUEST       0x0
+#define VFIO_USER_REPLY         0x1
+#define VFIO_USER_TYPE          0xF
+
+#define VFIO_USER_NO_REPLY      0x10
+#define VFIO_USER_ERROR         0x20
+
+#endif /* VFIO_USER_PROTOCOL_H */
hw/vfio/user.h +8 -0
index ac7d15d..5259a30 100644
@@ -11,6 +11,8 @@
  *
  */
 
+#include "user-protocol.h"
+
 typedef struct {
     int send_fds;
     int recv_fds;
@@ -27,6 +29,7 @@ enum msg_type {
 
 typedef struct VFIOUserMsg {
     QTAILQ_ENTRY(VFIOUserMsg) next;
+    VFIOUserHdr *hdr;
     VFIOUserFDs *fds;
     uint32_t rsize;
     uint32_t id;
@@ -66,6 +69,8 @@ typedef struct VFIOUserProxy {
     VFIOUserMsgQ incoming;
     VFIOUserMsgQ outgoing;
     VFIOUserMsg *last_nowait;
+    VFIOUserMsg *part_recv;
+    size_t recv_left;
     enum proxy_state state;
 } VFIOUserProxy;
 
@@ -74,5 +79,8 @@ typedef struct VFIOUserProxy {
 
 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_H */
hw/vfio/user-pci.c +11 -0
index a3fc36d..8b4e3ea 100644
@@ -43,6 +43,16 @@ struct VFIOUserPCIDevice {
 };
 
 /*
+ * 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
  */
 static void vfio_user_compute_needs_reset(VFIODevice *vbasedev)
@@ -88,6 +98,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);
     vbasedev->ops = &vfio_user_pci_ops;
hw/vfio/user.c +408 -0

453 lines not on this page · raw message

hw/vfio/trace-events +5 -0
index 73dffe9..73cc121 100644
@@ -166,3 +166,8 @@ vfio_load_state_device_data(const char *name, uint64_t data_offset, uint64_t dat
 vfio_load_cleanup(const char *name) " (%s)"
 vfio_get_dirty_bitmap(int fd, uint64_t iova, uint64_t size, uint64_t bitmap_size, uint64_t start) "container fd=%d, iova=0x%"PRIx64" size= 0x%"PRIx64" bitmap_size=0x%"PRIx64" start=0x%"PRIx64
 vfio_iommu_map_dirty_notify(uint64_t iova_start, uint64_t iova_end) "iommu dirty @ 0x%"PRIx64" - 0x%"PRIx64
+
+# user.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
1.9.4
#11 lore raw

PATCH v2 9/23 vfio-user: define socket send functions

In reply to John Johnson

Also negotiate protocol version with remote server

---
 hw/vfio/user-protocol.h |  62 ++++++
 hw/vfio/user.h          |   9 +
 hw/vfio/user-pci.c      |  16 ++
 hw/vfio/user.c          | 512 ++++++++++++++++++++++++++++++++++++++++++++++++
 hw/vfio/trace-events    |   2 +
 5 files changed, 601 insertions(+)
hw/vfio/user-protocol.h +62 -0
index d23877c..5de5b20 100644
@@ -51,4 +51,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.h +9 -0
index 5259a30..038e5e3 100644
@@ -35,6 +35,7 @@ typedef struct VFIOUserMsg {
     uint32_t id;
     QemuCond cv;
     bool complete;
+    bool pending;
     enum msg_type type;
 } VFIOUserMsg;
 
@@ -54,6 +55,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;
@@ -76,11 +83,13 @@ typedef struct VFIOUserProxy {
 
 /* VFIOProxy flags */
 #define VFIO_PROXY_CLIENT        0x1
+#define VFIO_PROXY_FORCE_QUEUED  0x4
 
 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);
+int vfio_user_validate_version(VFIOUserProxy *proxy, Error **errp);
 
 #endif /* VFIO_USER_H */
hw/vfio/user-pci.c +16 -0
index 8b4e3ea..0fe2593 100644
@@ -40,6 +40,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI)
 struct VFIOUserPCIDevice {
     VFIOPCIDevice device;
     char *sock_name;
+    bool send_queued;   /* all sends are queued */
 };
 
 /*
@@ -100,11 +101,25 @@ 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;
+    }
+
+    vfio_user_validate_version(proxy, &err);
+    if (err != NULL) {
+        error_propagate(errp, err);
+        goto error;
+    }
+
     vbasedev->name = g_strdup_printf("VFIO user <%s>", udev->sock_name);
     vbasedev->ops = &vfio_user_pci_ops;
     vbasedev->type = VFIO_DEVICE_TYPE_PCI;
     vbasedev->dev = DEVICE(vdev);
 
+    return;
+
+error:
+    error_prepend(errp, VFIO_MSG_PREFIX, vdev->vbasedev.name);
 }
 
 static void vfio_user_instance_finalize(Object *obj)
@@ -121,6 +136,7 @@ static void vfio_user_instance_finalize(Object *obj)
 
 static 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_END_OF_LIST(),
 };
 
hw/vfio/user.c +512 -0

566 lines not on this page · raw message

hw/vfio/trace-events +2 -0
index 73cc121..e3640bc 100644
@@ -171,3 +171,5 @@ vfio_iommu_map_dirty_notify(uint64_t iova_start, uint64_t iova_end) "iommu dirty
 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
1.9.4
#12 lore raw

PATCH v2 10/23 vfio-user: get device info

In reply to John Johnson
---
 hw/vfio/user-protocol.h       | 12 ++++++++++
 hw/vfio/user.h                |  1 +
 include/hw/vfio/vfio-common.h |  3 +++
 hw/vfio/common.c              | 23 +++++++++++-------
 hw/vfio/user-pci.c            |  6 +++++
 hw/vfio/user.c                | 55 +++++++++++++++++++++++++++++++++++++++++++
 hw/vfio/trace-events          |  1 +
 7 files changed, 93 insertions(+), 8 deletions(-)
hw/vfio/user-protocol.h +12 -0
index 5de5b20..5f9ef17 100644
@@ -113,4 +113,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.h +1 -0
index 038e5e3..d148661 100644
@@ -90,6 +90,7 @@ void vfio_user_disconnect(VFIOUserProxy *proxy);
 void vfio_user_set_handler(VFIODevice *vbasedev,
                            void (*handler)(void *opaque, VFIOUserMsg *msg),
                            void *reqarg);
+int vfio_user_get_device(VFIODevice *vbasedev, Error **errp);
 int vfio_user_validate_version(VFIOUserProxy *proxy, Error **errp);
 
 #endif /* VFIO_USER_H */
include/hw/vfio/vfio-common.h +3 -0
index e1ee0ac..0962e37 100644
@@ -254,6 +254,9 @@ void vfio_put_group(VFIOGroup *group);
 int vfio_get_device(VFIOGroup *group, const char *name,
                     VFIODevice *vbasedev, Error **errp);
 
+void vfio_init_device(VFIODevice *vbasedev, VFIOGroup *group,
+                      struct vfio_device_info *info);
+
 extern const MemoryRegionOps vfio_region_ops;
 typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList;
 extern VFIOGroupList vfio_group_list;
hw/vfio/common.c +15 -8
index 45b950a..792e247 100644
@@ -2369,6 +2369,20 @@ void vfio_get_all_regions(VFIODevice *vbasedev)
     }
 }
 
+void vfio_init_device(VFIODevice *vbasedev, VFIOGroup *group,
+                      struct vfio_device_info *info)
+{
+    vbasedev->group = group;
+    QLIST_INSERT_HEAD(&group->device_list, vbasedev, next);
+
+    vbasedev->num_irqs = info->num_irqs;
+    vbasedev->num_regions = info->num_regions;
+    vbasedev->flags = info->flags;
+    vbasedev->reset_works = !!(info->flags & VFIO_DEVICE_FLAGS_RESET);
+
+    vfio_get_all_regions(vbasedev);
+}
+
 int vfio_get_device(VFIOGroup *group, const char *name,
                     VFIODevice *vbasedev, Error **errp)
 {
@@ -2414,18 +2428,11 @@ int vfio_get_device(VFIOGroup *group, const char *name,
     }
 
     vbasedev->fd = fd;
-    vbasedev->group = group;
-    QLIST_INSERT_HEAD(&group->device_list, vbasedev, next);
-
-    vbasedev->num_irqs = dev_info.num_irqs;
-    vbasedev->num_regions = dev_info.num_regions;
-    vbasedev->flags = dev_info.flags;
+    vfio_init_device(vbasedev, group, &dev_info);
 
     trace_vfio_get_device(name, dev_info.flags, dev_info.num_regions,
                           dev_info.num_irqs);
 
-    vfio_get_all_regions(vbasedev);
-    vbasedev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET);
     return 0;
 }
 
hw/vfio/user-pci.c +6 -0
index 0fe2593..e5a9450 100644
@@ -77,6 +77,7 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)
     VFIODevice *vbasedev = &vdev->vbasedev;
     SocketAddress addr;
     VFIOUserProxy *proxy;
+    int ret;
     Error *err = NULL;
 
     /*
@@ -116,6 +117,11 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)
     vbasedev->type = VFIO_DEVICE_TYPE_PCI;
     vbasedev->dev = DEVICE(vdev);
 
+    ret = vfio_user_get_device(vbasedev, errp);
+    if (ret) {
+        goto error;
+    }
+
     return;
 
 error:
hw/vfio/user.c +55 -0

76 lines not on this page · raw message

hw/vfio/trace-events +1 -0
index e3640bc..ff903c0 100644
@@ -173,3 +173,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
1.9.4
#13 lore raw

PATCH v2 11/23 vfio-user: get region info

In reply to John Johnson

Add per-region FD to support mmap() of remote device regions

---
 hw/vfio/user-protocol.h       | 14 +++++++++
 hw/vfio/user.h                |  2 ++
 include/hw/vfio/vfio-common.h |  5 +++-
 hw/vfio/ap.c                  |  1 +
 hw/vfio/ccw.c                 |  1 +
 hw/vfio/common.c              | 31 ++++++++++++++++++--
 hw/vfio/pci.c                 |  1 +
 hw/vfio/platform.c            |  1 +
 hw/vfio/user-pci.c            |  2 ++
 hw/vfio/user.c                | 68 +++++++++++++++++++++++++++++++++++++++++++
 hw/vfio/trace-events          |  1 +
 11 files changed, 123 insertions(+), 4 deletions(-)
hw/vfio/user-protocol.h +14 -0
index 5f9ef17..6f70a48 100644
@@ -125,4 +125,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.h +2 -0
index d148661..e6485dc 100644
@@ -93,4 +93,6 @@ void vfio_user_set_handler(VFIODevice *vbasedev,
 int vfio_user_get_device(VFIODevice *vbasedev, Error **errp);
 int vfio_user_validate_version(VFIOUserProxy *proxy, Error **errp);
 
+extern VFIODeviceIO vfio_dev_io_sock;
+
 #endif /* VFIO_USER_H */
include/hw/vfio/vfio-common.h +4 -1
index 0962e37..9fb4c80 100644
@@ -56,6 +56,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;
 
 typedef struct VFIOMigration {
@@ -140,6 +141,7 @@ typedef struct VFIODevice {
     bool no_mmap;
     bool ram_block_discard_allowed;
     bool enable_migration;
+    bool use_regfds;
     VFIODeviceOps *ops;
     VFIODeviceIO *io;
     unsigned int num_irqs;
@@ -150,6 +152,7 @@ typedef struct VFIODevice {
     OnOffAuto pre_copy_dirty_page_tracking;
     VFIOUserProxy *proxy;
     struct vfio_region_info **regions;
+    int *regfds;
 } VFIODevice;
 
 struct VFIODeviceOps {
@@ -171,7 +174,7 @@ struct VFIODeviceOps {
  */
 struct VFIODeviceIO {
     int (*get_region_info)(VFIODevice *vdev,
-                           struct vfio_region_info *info);
+                           struct vfio_region_info *info, int *fd);
     int (*get_irq_info)(VFIODevice *vdev, struct vfio_irq_info *irq);
     int (*set_irqs)(VFIODevice *vdev, struct vfio_irq_set *irqs);
     int (*region_read)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size,
hw/vfio/ap.c +1 -0
index c6638d5..06d745f 100644
@@ -103,6 +103,7 @@ static void vfio_ap_realize(DeviceState *dev, Error **errp)
     vapdev->vdev.name = g_strdup_printf("%s", mdevid);
     vapdev->vdev.dev = dev;
     vapdev->vdev.io = &vfio_dev_io_ioctl;
+    vapdev->vdev.use_regfds = false;
 
     /*
      * vfio-ap devices operate in a way compatible with discarding of
hw/vfio/ccw.c +1 -0
index e4d840d..00605bd 100644
@@ -615,6 +615,7 @@ static void vfio_ccw_get_device(VFIOGroup *group, VFIOCCWDevice *vcdev,
     vcdev->vdev.name = name;
     vcdev->vdev.dev = &vcdev->cdev.parent_obj.parent_obj;
     vcdev->vdev.io = &vfio_dev_io_ioctl;
+    vcdev->vdev.use_regfds = false;
 
     return;
 
hw/vfio/common.c +28 -3
index 792e247..d26b325 100644
@@ -1584,6 +1584,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->regfds != NULL) {
+        region->fd = vbasedev->regfds[index];
+    } else {
+        region->fd = vbasedev->fd;
+    }
 
     if (region->size) {
         region->mem = g_new0(MemoryRegion, 1);
@@ -1635,7 +1640,7 @@ int vfio_region_mmap(VFIORegion *region)
 
     for (i = 0; i < region->nr_mmaps; i++) {
         region->mmaps[i].mmap = mmap(NULL, region->mmaps[i].size, prot,
-                                     MAP_SHARED, region->vbasedev->fd,
+                                     MAP_SHARED, region->fd,
                                      region->fd_offset +
                                      region->mmaps[i].offset);
         if (region->mmaps[i].mmap == MAP_FAILED) {
@@ -2442,10 +2447,17 @@ void vfio_put_base_device(VFIODevice *vbasedev)
         int i;
 
         for (i = 0; i < vbasedev->num_regions; i++) {
+            if (vbasedev->regfds != NULL && vbasedev->regfds[i] != -1) {
+                close(vbasedev->regfds[i]);
+            }
             g_free(vbasedev->regions[i]);
         }
         g_free(vbasedev->regions);
         vbasedev->regions = NULL;
+        if (vbasedev->regfds != NULL) {
+            g_free(vbasedev->regfds);
+            vbasedev->regfds = NULL;
+        }
     }
 
     if (!vbasedev->group) {
@@ -2461,12 +2473,16 @@ int vfio_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;
 
     /* create region cache */
     if (vbasedev->regions == NULL) {
         vbasedev->regions = g_new0(struct vfio_region_info *,
                                    vbasedev->num_regions);
+        if (vbasedev->use_regfds) {
+            vbasedev->regfds = g_new0(int, vbasedev->num_regions);
+        }
     }
     /* check cache */
     if (vbasedev->regions[index] != NULL) {
@@ -2480,7 +2496,7 @@ int vfio_get_region_info(VFIODevice *vbasedev, int index,
 retry:
     (*info)->argsz = argsz;
 
-    ret = vbasedev->io->get_region_info(vbasedev, *info);
+    ret = vbasedev->io->get_region_info(vbasedev, *info, &fd);
     if (ret != 0) {
         g_free(*info);
         *info = NULL;
@@ -2490,12 +2506,19 @@ retry:
     if ((*info)->argsz > argsz) {
         argsz = (*info)->argsz;
         *info = g_realloc(*info, argsz);
+        if (fd != -1) {
+            close(fd);
+            fd = -1;
+        }
 
         goto retry;
     }
 
     /* fill cache */
     vbasedev->regions[index] = *info;
+    if (vbasedev->regfds != NULL) {
+        vbasedev->regfds[index] = fd;
+    }
 
     return 0;
 }
@@ -2646,10 +2669,12 @@ int vfio_eeh_as_op(AddressSpace *as, uint32_t op)
  */
 
 static int vfio_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/pci.c +1 -0
index a8bc0ea..935d247 100644
@@ -2908,6 +2908,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
     vbasedev->type = VFIO_DEVICE_TYPE_PCI;
     vbasedev->dev = DEVICE(vdev);
     vbasedev->io = &vfio_dev_io_ioctl;
+    vbasedev->use_regfds = false;
 
     tmp = g_strdup_printf("%s/iommu_group", vbasedev->sysfsdev);
     len = readlink(tmp, group_path, sizeof(group_path));
hw/vfio/platform.c +1 -0
index 222405e..8ddfcca 100644
@@ -622,6 +622,7 @@ static void vfio_platform_realize(DeviceState *dev, Error **errp)
     vbasedev->dev = dev;
     vbasedev->ops = &vfio_platform_ops;
     vbasedev->io = &vfio_dev_io_ioctl;
+    vbasedev->use_regfds = false;
 
     qemu_mutex_init(&vdev->intp_mutex);
 
hw/vfio/user-pci.c +2 -0
index e5a9450..09c6c98 100644
@@ -116,6 +116,8 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)
     vbasedev->ops = &vfio_user_pci_ops;
     vbasedev->type = VFIO_DEVICE_TYPE_PCI;
     vbasedev->dev = DEVICE(vdev);
+    vbasedev->io = &vfio_dev_io_sock;
+    vbasedev->use_regfds = true;
 
     ret = vfio_user_get_device(vbasedev, errp);
     if (ret) {
hw/vfio/user.c +68 -0

71 lines not on this page · raw message

hw/vfio/trace-events +1 -0
index ff903c0..939113a 100644
@@ -174,3 +174,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
1.9.4
#14 lore raw

Re: [PATCH v2 11/23] vfio-user: get region info

Quoted 26 lines · On Wed, 1 Feb 2023 21:55:47 -0800 John Johnson <[email protected]>…
hw/vfio/common.c +6 -1
index 792e247..d26b325 100644
@@ -1584,6 +1584,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->regfds != NULL) {
+        region->fd = vbasedev->regfds[index];
+    } else {
+        region->fd = vbasedev->fd;
+    }
 
     if (region->size) {
         region->mem = g_new0(MemoryRegion, 1);
@@ -1635,7 +1640,7 @@ int vfio_region_mmap(VFIORegion *region)
 
     for (i = 0; i < region->nr_mmaps; i++) {
         region->mmaps[i].mmap = mmap(NULL, region->mmaps[i].size, prot,
-                                     MAP_SHARED, region->vbasedev->fd,
+                                     MAP_SHARED, region->fd,
                                      region->fd_offset +
                                      region->mmaps[i].offset);
         if (region->mmaps[i].mmap == MAP_FAILED) {

hw/vfio/common.c void vfio_put_base_device(VFIODevice *vbasedev) · line 2452

Quoted 6 lines
hw/vfio/common.c +3 -0
@@ -2442,10 +2447,17 @@ void vfio_put_base_device(VFIODevice *vbasedev)
         int i;
 
         for (i = 0; i < vbasedev->num_regions; i++) {
+            if (vbasedev->regfds != NULL && vbasedev->regfds[i] != -1) {
+                close(vbasedev->regfds[i]);
+            }

There's an exit in vfio_get_region_info() where regfds is allocated and the regfd[i] could still be zero initialized, ie. if .get_region_info() fails. vfio_get_all_regions() ignores the return value of vfio_get_region_info(). We could close(0) here. Thanks,

Alex

#15 lore raw

PATCH v2 12/23 vfio-user: region read/write

In reply to John Johnson

Add support for posted writes on remote devices

---
 hw/vfio/user-protocol.h       |  12 +++++
 hw/vfio/user.h                |   1 +
 include/hw/vfio/vfio-common.h |   3 +-
 hw/vfio/common.c              |  23 ++++++---
 hw/vfio/pci.c                 |   5 +-
 hw/vfio/user-pci.c            |   5 ++
 hw/vfio/user.c                | 112 ++++++++++++++++++++++++++++++++++++++++++
 hw/vfio/trace-events          |   1 +
 8 files changed, 154 insertions(+), 8 deletions(-)
hw/vfio/user-protocol.h +12 -0
index 6f70a48..6987435 100644
@@ -139,4 +139,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.h +1 -0
index e6485dc..3012a86 100644
@@ -84,6 +84,7 @@ typedef struct VFIOUserProxy {
 /* VFIOProxy flags */
 #define VFIO_PROXY_CLIENT        0x1
 #define VFIO_PROXY_FORCE_QUEUED  0x4
+#define VFIO_PROXY_NO_POST       0x8
 
 VFIOUserProxy *vfio_user_connect_dev(SocketAddress *addr, Error **errp);
 void vfio_user_disconnect(VFIOUserProxy *proxy);
include/hw/vfio/vfio-common.h +2 -1
index 9fb4c80..bbc4b15 100644
@@ -57,6 +57,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;
 
 typedef struct VFIOMigration {
@@ -180,7 +181,7 @@ struct VFIODeviceIO {
     int (*region_read)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size,
                        void *data);
     int (*region_write)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size,
-                        void *data);
+                        void *data, bool post);
 };
 
 struct VFIOContainerIO {
hw/vfio/common.c +17 -6
index d26b325..de64e53 100644
@@ -215,6 +215,7 @@ void vfio_region_write(void *opaque, hwaddr addr,
         uint32_t dword;
         uint64_t qword;
     } buf;
+    bool post = region->post_wr;
     int ret;
 
     switch (size) {
@@ -235,12 +236,19 @@ void vfio_region_write(void *opaque, hwaddr addr,
         break;
     }
 
-    ret = vbasedev->io->region_write(vbasedev, region->nr, addr, size, &buf);
+    /* read-after-write hazard if guest can directly access region */
+    if (region->nr_mmaps) {
+        post = false;
+    }
+    ret = vbasedev->io->region_write(vbasedev, region->nr, addr, size, &buf,
+                                     post);
     if (ret != size) {
+        const char *err = ret < 0 ? strerror(-ret) : "short write";
+
         error_report("%s(%s:region%d+0x%"HWADDR_PRIx", 0x%"PRIx64
-                     ",%d) failed: %m",
+                     ",%d) failed: %s",
                      __func__, vbasedev->name, region->nr,
-                     addr, data, size);
+                     addr, data, size, err);
     }
     trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size);
 
@@ -271,9 +279,11 @@ uint64_t vfio_region_read(void *opaque,
 
     ret = vbasedev->io->region_read(vbasedev, region->nr, addr, size, &buf);
     if (ret != size) {
-        error_report("%s(%s:region%d+0x%"HWADDR_PRIx", %d) failed: %m",
+        const char *err = ret < 0 ? strerror(-ret) : "short read";
+
+        error_report("%s(%s:region%d+0x%"HWADDR_PRIx", %d) failed: %s",
                      __func__, vbasedev->name, region->nr,
-                     addr, size);
+                     addr, size, err);
         return (uint64_t)-1;
     }
 
@@ -1584,6 +1594,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->regfds != NULL) {
         region->fd = vbasedev->regfds[index];
     } else {
@@ -2711,7 +2722,7 @@ static int vfio_io_region_read(VFIODevice *vbasedev, uint8_t index, off_t off,
 }
 
 static int vfio_io_region_write(VFIODevice *vbasedev, uint8_t index, off_t off,
-                                uint32_t size, void *data)
+                                uint32_t size, void *data, bool post)
 {
     struct vfio_region_info *info = vbasedev->regions[index];
     int ret;
hw/vfio/pci.c +4 -1
index 935d247..be714b7 100644
@@ -49,7 +49,7 @@
                                  (off), (size), (data)))
 #define VDEV_CONFIG_WRITE(vbasedev, off, size, data) \
     ((vbasedev)->io->region_write((vbasedev), VFIO_PCI_CONFIG_REGION_INDEX, \
-                                  (off), (size), (data)))
+                                  (off), (size), (data), false))
 
 #define TYPE_VFIO_PCI_NOHOTPLUG "vfio-pci-nohotplug"
 
@@ -1702,6 +1702,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/user-pci.c +5 -0
index 09c6c98..900ab5f 100644
@@ -41,6 +41,7 @@ struct VFIOUserPCIDevice {
     VFIOPCIDevice device;
     char *sock_name;
     bool send_queued;   /* all sends are queued */
+    bool no_post;       /* all regions write are sync */
 };
 
 /*
@@ -105,6 +106,9 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)
     if (udev->send_queued) {
         proxy->flags |= VFIO_PROXY_FORCE_QUEUED;
     }
+    if (udev->no_post) {
+        proxy->flags |= VFIO_PROXY_NO_POST;
+    }
 
     vfio_user_validate_version(proxy, &err);
     if (err != NULL) {
@@ -145,6 +149,7 @@ static void vfio_user_instance_finalize(Object *obj)
 static 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_BOOL("x-no-posted-writes", VFIOUserPCIDevice, no_post, false),
     DEFINE_PROP_END_OF_LIST(),
 };
 
hw/vfio/user.c +112 -0

136 lines not on this page · raw message

hw/vfio/trace-events +1 -0
index 939113a..1f3688f 100644
@@ -175,3 +175,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
1.9.4
#16 lore raw

Re: [PATCH v2 12/23] vfio-user: region read/write

Quoted 128 lines · On Wed, 1 Feb 2023 21:55:48 -0800 John Johnson <[email protected]>…

Add support for posted writes on remote devices

---
 hw/vfio/user-protocol.h       |  12 +++++
 hw/vfio/user.h                |   1 +
 include/hw/vfio/vfio-common.h |   3 +-
 hw/vfio/common.c              |  23 ++++++---
 hw/vfio/pci.c                 |   5 +-
 hw/vfio/user-pci.c            |   5 ++
 hw/vfio/user.c                | 112 ++++++++++++++++++++++++++++++++++++++++++
 hw/vfio/trace-events          |   1 +
 8 files changed, 154 insertions(+), 8 deletions(-)
hw/vfio/user-protocol.h +12 -0
index 6f70a48..6987435 100644
@@ -139,4 +139,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.h +1 -0
index e6485dc..3012a86 100644
@@ -84,6 +84,7 @@ typedef struct VFIOUserProxy {
 /* VFIOProxy flags */
 #define VFIO_PROXY_CLIENT        0x1
 #define VFIO_PROXY_FORCE_QUEUED  0x4
+#define VFIO_PROXY_NO_POST       0x8
 
 VFIOUserProxy *vfio_user_connect_dev(SocketAddress *addr, Error **errp);
 void vfio_user_disconnect(VFIOUserProxy *proxy);
include/hw/vfio/vfio-common.h +2 -1
index 9fb4c80..bbc4b15 100644
@@ -57,6 +57,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;
 
 typedef struct VFIOMigration {
@@ -180,7 +181,7 @@ struct VFIODeviceIO {
     int (*region_read)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size,
                        void *data);
     int (*region_write)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size,
-                        void *data);
+                        void *data, bool post);
 };
 
 struct VFIOContainerIO {
hw/vfio/common.c +16 -5
index d26b325..de64e53 100644
@@ -215,6 +215,7 @@ void vfio_region_write(void *opaque, hwaddr addr,
         uint32_t dword;
         uint64_t qword;
     } buf;
+    bool post = region->post_wr;
     int ret;
 
     switch (size) {
@@ -235,12 +236,19 @@ void vfio_region_write(void *opaque, hwaddr addr,
         break;
     }
 
-    ret = vbasedev->io->region_write(vbasedev, region->nr, addr, size, &buf);
+    /* read-after-write hazard if guest can directly access region */
+    if (region->nr_mmaps) {
+        post = false;
+    }
+    ret = vbasedev->io->region_write(vbasedev, region->nr, addr, size, &buf,
+                                     post);
     if (ret != size) {
+        const char *err = ret < 0 ? strerror(-ret) : "short write";
+
         error_report("%s(%s:region%d+0x%"HWADDR_PRIx", 0x%"PRIx64
-                     ",%d) failed: %m",
+                     ",%d) failed: %s",
                      __func__, vbasedev->name, region->nr,
-                     addr, data, size);
+                     addr, data, size, err);
     }
     trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size);
 
@@ -271,9 +279,11 @@ uint64_t vfio_region_read(void *opaque,
 
     ret = vbasedev->io->region_read(vbasedev, region->nr, addr, size, &buf);
     if (ret != size) {
-        error_report("%s(%s:region%d+0x%"HWADDR_PRIx", %d) failed: %m",
+        const char *err = ret < 0 ? strerror(-ret) : "short read";
+
+        error_report("%s(%s:region%d+0x%"HWADDR_PRIx", %d) failed: %s",
                      __func__, vbasedev->name, region->nr,
-                     addr, size);
+                     addr, size, err);
         return (uint64_t)-1;
     }
 
@@ -1584,6 +1594,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->regfds != NULL) {
         region->fd = vbasedev->regfds[index];
     } else {

hw/vfio/common.c static int vfio_io_region_read(VFIODevice *vbasedev, uint8_t index, off_t off, · line 2726

Quoted 6 lines
hw/vfio/common.c +1 -1
@@ -2711,7 +2722,7 @@ static int vfio_io_region_read(VFIODevice *vbasedev, uint8_t index, off_t off,
 }
 
 static int vfio_io_region_write(VFIODevice *vbasedev, uint8_t index, off_t off,
-                                uint32_t size, void *data)
+                                uint32_t size, void *data, bool post)
 {

This is all a bit confusing as a non-posted write on bare metal would require a follow-up read to flush the write, but in the kernel case we rely on both the bus protocols and the userspace driver to perform such actions to enforce coherency. The read-after-write hazard comment above suggests the issue is split access between mmap and read/write across the region, where the mmap access bypasses the socket protocol. But isn't this actually across the whole device? For example, PCI doesn't care which BAR a write targets, reads to another BAR cannot bypass the write, aiui. IOW, couldn't a write to a BAR that doesn't support mmap affect the behavior of a BAR that does support mmap, and therefore there should be no posted writes for the entire device if any region supports mmap access?

I wonder if a better approach wouldn't be to make all writes operations synchronous and avoid read-after-write hazard by performing writes through the mmap when available, ie. make use of the same bypass to avoid the hazard.

In any case, I think this deserves more comments, both why vfio-user needs it and why vfio-kernel doesn't, and ideally the initial read/write implementation would be entirely synchronous with posted write support added as a separate patch with full explanation and justification. Thanks,

Alex

#17 lore raw

Re: [PATCH v2 12/23] vfio-user: region read/write

Quoted 146 lines · Alex Williamson
Quoted 128 lines · On Wed, 1 Feb 2023 21:55:48 -0800 John Johnson <[email protected]>…

Add support for posted writes on remote devices

---

hw/vfio/user-protocol.h       |  12 +++++
hw/vfio/user.h                |   1 +
include/hw/vfio/vfio-common.h |   3 +-
hw/vfio/common.c              |  23 ++++++---
hw/vfio/pci.c                 |   5 +-
hw/vfio/user-pci.c            |   5 ++
hw/vfio/user.c                | 112 ++++++++++++++++++++++++++++++++++++++++++
hw/vfio/trace-events          |   1 +
8 files changed, 154 insertions(+), 8 deletions(-)
hw/vfio/user-protocol.h +12 -0
index 6f70a48..6987435 100644
@@ -139,4 +139,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.h +1 -0
index e6485dc..3012a86 100644
@@ -84,6 +84,7 @@ typedef struct VFIOUserProxy {
 /* VFIOProxy flags */
 #define VFIO_PROXY_CLIENT        0x1
 #define VFIO_PROXY_FORCE_QUEUED  0x4
+#define VFIO_PROXY_NO_POST       0x8
 
 VFIOUserProxy *vfio_user_connect_dev(SocketAddress *addr, Error **errp);
 void vfio_user_disconnect(VFIOUserProxy *proxy);
include/hw/vfio/vfio-common.h +2 -1
index 9fb4c80..bbc4b15 100644
@@ -57,6 +57,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;
 
 typedef struct VFIOMigration {
@@ -180,7 +181,7 @@ struct VFIODeviceIO {
    int (*region_read)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size,
                       void *data);
    int (*region_write)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size,
-                        void *data);
+                        void *data, bool post);
 };
 
 struct VFIOContainerIO {
hw/vfio/common.c +16 -5
index d26b325..de64e53 100644
@@ -215,6 +215,7 @@ void vfio_region_write(void *opaque, hwaddr addr,
        uint32_t dword;
        uint64_t qword;
    } buf;
+    bool post = region->post_wr;
    int ret;
 
    switch (size) {
@@ -235,12 +236,19 @@ void vfio_region_write(void *opaque, hwaddr addr,
        break;
    }
 
-    ret = vbasedev->io->region_write(vbasedev, region->nr, addr, size, &buf);
+    /* read-after-write hazard if guest can directly access region */
+    if (region->nr_mmaps) {
+        post = false;
+    }
+    ret = vbasedev->io->region_write(vbasedev, region->nr, addr, size, &buf,
+                                     post);
    if (ret != size) {
+        const char *err = ret < 0 ? strerror(-ret) : "short write";
+
        error_report("%s(%s:region%d+0x%"HWADDR_PRIx", 0x%"PRIx64
-                     ",%d) failed: %m",
+                     ",%d) failed: %s",
                     __func__, vbasedev->name, region->nr,
-                     addr, data, size);
+                     addr, data, size, err);
    }
    trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size);
 
@@ -271,9 +279,11 @@ uint64_t vfio_region_read(void *opaque,
 
    ret = vbasedev->io->region_read(vbasedev, region->nr, addr, size, &buf);
    if (ret != size) {
-        error_report("%s(%s:region%d+0x%"HWADDR_PRIx", %d) failed: %m",
+        const char *err = ret < 0 ? strerror(-ret) : "short read";
+
+        error_report("%s(%s:region%d+0x%"HWADDR_PRIx", %d) failed: %s",
                     __func__, vbasedev->name, region->nr,
-                     addr, size);
+                     addr, size, err);
        return (uint64_t)-1;
    }
 
@@ -1584,6 +1594,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->regfds != NULL) {
        region->fd = vbasedev->regfds[index];
    } else {

hw/vfio/common.c static int vfio_io_region_read(VFIODevice *vbasedev, uint8_t index, off_t off, · line 2726

Quoted 6 lines
hw/vfio/common.c +1 -1
@@ -2711,7 +2722,7 @@ static int vfio_io_region_read(VFIODevice *vbasedev, uint8_t index, off_t off,
 }
 
 static int vfio_io_region_write(VFIODevice *vbasedev, uint8_t index, off_t off,
-                                uint32_t size, void *data)
+                                uint32_t size, void *data, bool post)
 {

This is all a bit confusing as a non-posted write on bare metal would require a follow-up read to flush the write, but in the kernel case we rely on both the bus protocols and the userspace driver to perform such actions to enforce coherency. The read-after-write hazard comment above

Quoted 9 lines

suggests the issue is split access between mmap and read/write across the region, where the mmap access bypasses the socket protocol. But isn't this actually across the whole device? For example, PCI doesn't care which BAR a write targets, reads to another BAR cannot bypass the write, aiui. IOW, couldn't a write to a BAR that doesn't support mmap affect the behavior of a BAR that does support mmap, and therefore there should be no posted writes for the entire device if any region supports mmap access?

The protocol has sequential ordering, so issues arise only when
reads to mapped regions pass writes that were sent asynchronously. The code is designed to handle common driver ordering operations: reading config space (which can’t be mapped, so they’re sequentially ordered by the protocol) and reading a nearby register (in the same region). There is a ’no-post’ vfio-user option in case the driver relies on ordering reads to other (non-config) regions

I made this the default since of the the primary customers is an
NVME device server that polls a mapped doorbell region and wants posted writes.

Quoted 5 lines

I wonder if a better approach wouldn't be to make all writes operations synchronous and avoid read-after-write hazard by performing writes through the mmap when available, ie. make use of the same bypass to avoid the hazard.

I can flip the default to be strict PCI, and force the NVME
folks to use an option to enable performance.

Quoted 6 lines

In any case, I think this deserves more comments, both why vfio-user needs it and why vfio-kernel doesn't, and ideally the initial read/write implementation would be entirely synchronous with posted write support added as a separate patch with full explanation and justification. Thanks,

Sure. Ordering is always a pain to reason about.

JJ

#18 lore raw

Re: [PATCH v2 12/23] vfio-user: region read/write

Quoted 162 lines · On Wed, 8 Feb 2023 06:38:27 +0000 John Johnson <[email protected]>…
Quoted 144 lines · Alex Williamson
Quoted 128 lines · On Wed, 1 Feb 2023 21:55:48 -0800 John Johnson <[email protected]>…

Add support for posted writes on remote devices

---

hw/vfio/user-protocol.h       |  12 +++++
hw/vfio/user.h                |   1 +
include/hw/vfio/vfio-common.h |   3 +-
hw/vfio/common.c              |  23 ++++++---
hw/vfio/pci.c                 |   5 +-
hw/vfio/user-pci.c            |   5 ++
hw/vfio/user.c                | 112 ++++++++++++++++++++++++++++++++++++++++++
hw/vfio/trace-events          |   1 +
8 files changed, 154 insertions(+), 8 deletions(-)
hw/vfio/user-protocol.h +12 -0
index 6f70a48..6987435 100644
@@ -139,4 +139,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.h +1 -0
index e6485dc..3012a86 100644
@@ -84,6 +84,7 @@ typedef struct VFIOUserProxy {
 /* VFIOProxy flags */
 #define VFIO_PROXY_CLIENT        0x1
 #define VFIO_PROXY_FORCE_QUEUED  0x4
+#define VFIO_PROXY_NO_POST       0x8
 
 VFIOUserProxy *vfio_user_connect_dev(SocketAddress *addr, Error **errp);
 void vfio_user_disconnect(VFIOUserProxy *proxy);
include/hw/vfio/vfio-common.h +2 -1
index 9fb4c80..bbc4b15 100644
@@ -57,6 +57,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;
 
 typedef struct VFIOMigration {
@@ -180,7 +181,7 @@ struct VFIODeviceIO {
    int (*region_read)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size,
                       void *data);
    int (*region_write)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size,
-                        void *data);
+                        void *data, bool post);
 };
 
 struct VFIOContainerIO {
hw/vfio/common.c +16 -5
index d26b325..de64e53 100644
@@ -215,6 +215,7 @@ void vfio_region_write(void *opaque, hwaddr addr,
        uint32_t dword;
        uint64_t qword;
    } buf;
+    bool post = region->post_wr;
    int ret;
 
    switch (size) {
@@ -235,12 +236,19 @@ void vfio_region_write(void *opaque, hwaddr addr,
        break;
    }
 
-    ret = vbasedev->io->region_write(vbasedev, region->nr, addr, size, &buf);
+    /* read-after-write hazard if guest can directly access region */
+    if (region->nr_mmaps) {
+        post = false;
+    }
+    ret = vbasedev->io->region_write(vbasedev, region->nr, addr, size, &buf,
+                                     post);
    if (ret != size) {
+        const char *err = ret < 0 ? strerror(-ret) : "short write";
+
        error_report("%s(%s:region%d+0x%"HWADDR_PRIx", 0x%"PRIx64
-                     ",%d) failed: %m",
+                     ",%d) failed: %s",
                     __func__, vbasedev->name, region->nr,
-                     addr, data, size);
+                     addr, data, size, err);
    }
    trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size);
 
@@ -271,9 +279,11 @@ uint64_t vfio_region_read(void *opaque,
 
    ret = vbasedev->io->region_read(vbasedev, region->nr, addr, size, &buf);
    if (ret != size) {
-        error_report("%s(%s:region%d+0x%"HWADDR_PRIx", %d) failed: %m",
+        const char *err = ret < 0 ? strerror(-ret) : "short read";
+
+        error_report("%s(%s:region%d+0x%"HWADDR_PRIx", %d) failed: %s",
                     __func__, vbasedev->name, region->nr,
-                     addr, size);
+                     addr, size, err);
        return (uint64_t)-1;
    }
 
@@ -1584,6 +1594,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->regfds != NULL) {
        region->fd = vbasedev->regfds[index];
    } else {

hw/vfio/common.c static int vfio_io_region_read(VFIODevice *vbasedev, uint8_t index, off_t off, · line 2726

Quoted 6 lines
hw/vfio/common.c +1 -1
@@ -2711,7 +2722,7 @@ static int vfio_io_region_read(VFIODevice *vbasedev, uint8_t index, off_t off,
 }
 
 static int vfio_io_region_write(VFIODevice *vbasedev, uint8_t index, off_t off,
-                                uint32_t size, void *data)
+                                uint32_t size, void *data, bool post)
 {  

This is all a bit confusing as a non-posted write on bare metal would require a follow-up read to flush the write, but in the kernel case we rely on both the bus protocols and the userspace driver to perform such actions to enforce coherency. The read-after-write hazard comment above

Quoted 9 lines

suggests the issue is split access between mmap and read/write across the region, where the mmap access bypasses the socket protocol. But isn't this actually across the whole device? For example, PCI doesn't care which BAR a write targets, reads to another BAR cannot bypass the write, aiui. IOW, couldn't a write to a BAR that doesn't support mmap affect the behavior of a BAR that does support mmap, and therefore there should be no posted writes for the entire device if any region supports mmap access?

The protocol has sequential ordering, so issues arise only when
reads to mapped regions pass writes that were sent asynchronously. The code is designed to handle common driver ordering operations: reading config space (which can’t be mapped, so they’re sequentially ordered by the protocol) and reading a nearby register (in the same region). There is a ’no-post’ vfio-user option in case the driver relies on ordering reads to other (non-config) regions

Quoted 3 lines

I made this the default since of the the primary customers is an
NVME device server that polls a mapped doorbell region and wants posted writes.

This sounds like we're targeting a very specific use case then since the code explicitly disables posted writes when mmaps within the same region are available. Another device might have these spread across different regions, where the mmap test might not force the non-posted write behavior and the user write could return with the operation still in-flight through the socket protocol, allowing a read through the mmap to bypass.

Device options are great for debugging and isolating certain features, but users expect that devices "just work".

Quoted 8 lines
Quoted 5 lines

I wonder if a better approach wouldn't be to make all writes operations synchronous and avoid read-after-write hazard by performing writes through the mmap when available, ie. make use of the same bypass to avoid the hazard.

I can flip the default to be strict PCI, and force the NVME
folks to use an option to enable performance.

All the physical NVME devices I have around only have a single BAR, so the test to disable posted writes if mmaps are available within the same region, ie. BAR, would necessarily cause all writes through the trapped space to be non-posted. Is your NVME device already working with a non-standard layout to "enable performance"?

I'd rather see default behavior that focuses on creating a compatible interface to bare metal, which I think means we need to demote all writes to non-posted when there's a mechanism where reads can bypass writes. From that we can build quirks for known devices which can overlay memory regions that only perform posted writes. Perhaps a side-band interface modeled after sparse mmaps would allow standardization of such a device quirk to allow the device on the server side to direct areas where posted writes are always safe. Thanks,

Alex

#19 lore raw

Re: [PATCH v2 12/23] vfio-user: region read/write

Quoted 190 lines · Alex Williamson
Quoted 162 lines · On Wed, 8 Feb 2023 06:38:27 +0000 John Johnson <[email protected]>…
Quoted 144 lines · Alex Williamson
Quoted 128 lines · On Wed, 1 Feb 2023 21:55:48 -0800 John Johnson <[email protected]>…

Add support for posted writes on remote devices

---

hw/vfio/user-protocol.h       |  12 +++++
hw/vfio/user.h                |   1 +
include/hw/vfio/vfio-common.h |   3 +-
hw/vfio/common.c              |  23 ++++++---
hw/vfio/pci.c                 |   5 +-
hw/vfio/user-pci.c            |   5 ++
hw/vfio/user.c                | 112 ++++++++++++++++++++++++++++++++++++++++++
hw/vfio/trace-events          |   1 +
8 files changed, 154 insertions(+), 8 deletions(-)
hw/vfio/user-protocol.h +12 -0
index 6f70a48..6987435 100644
@@ -139,4 +139,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.h +1 -0
index e6485dc..3012a86 100644
@@ -84,6 +84,7 @@ typedef struct VFIOUserProxy {
 /* VFIOProxy flags */
 #define VFIO_PROXY_CLIENT        0x1
 #define VFIO_PROXY_FORCE_QUEUED  0x4
+#define VFIO_PROXY_NO_POST       0x8
 
 VFIOUserProxy *vfio_user_connect_dev(SocketAddress *addr, Error **errp);
 void vfio_user_disconnect(VFIOUserProxy *proxy);
include/hw/vfio/vfio-common.h +2 -1
index 9fb4c80..bbc4b15 100644
@@ -57,6 +57,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;
 
 typedef struct VFIOMigration {
@@ -180,7 +181,7 @@ struct VFIODeviceIO {
   int (*region_read)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size,
                      void *data);
   int (*region_write)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size,
-                        void *data);
+                        void *data, bool post);
 };
 
 struct VFIOContainerIO {
hw/vfio/common.c +16 -5
index d26b325..de64e53 100644
@@ -215,6 +215,7 @@ void vfio_region_write(void *opaque, hwaddr addr,
       uint32_t dword;
       uint64_t qword;
   } buf;
+    bool post = region->post_wr;
   int ret;
 
   switch (size) {
@@ -235,12 +236,19 @@ void vfio_region_write(void *opaque, hwaddr addr,
       break;
   }
 
-    ret = vbasedev->io->region_write(vbasedev, region->nr, addr, size, &buf);
+    /* read-after-write hazard if guest can directly access region */
+    if (region->nr_mmaps) {
+        post = false;
+    }
+    ret = vbasedev->io->region_write(vbasedev, region->nr, addr, size, &buf,
+                                     post);
   if (ret != size) {
+        const char *err = ret < 0 ? strerror(-ret) : "short write";
+
       error_report("%s(%s:region%d+0x%"HWADDR_PRIx", 0x%"PRIx64
-                     ",%d) failed: %m",
+                     ",%d) failed: %s",
                    __func__, vbasedev->name, region->nr,
-                     addr, data, size);
+                     addr, data, size, err);
   }
   trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size);
 
@@ -271,9 +279,11 @@ uint64_t vfio_region_read(void *opaque,
 
   ret = vbasedev->io->region_read(vbasedev, region->nr, addr, size, &buf);
   if (ret != size) {
-        error_report("%s(%s:region%d+0x%"HWADDR_PRIx", %d) failed: %m",
+        const char *err = ret < 0 ? strerror(-ret) : "short read";
+
+        error_report("%s(%s:region%d+0x%"HWADDR_PRIx", %d) failed: %s",
                    __func__, vbasedev->name, region->nr,
-                     addr, size);
+                     addr, size, err);
       return (uint64_t)-1;
   }
 
@@ -1584,6 +1594,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->regfds != NULL) {
       region->fd = vbasedev->regfds[index];
   } else {

hw/vfio/common.c static int vfio_io_region_read(VFIODevice *vbasedev, uint8_t index, off_t off, · line 2726

Quoted 6 lines
hw/vfio/common.c +1 -1
@@ -2711,7 +2722,7 @@ static int vfio_io_region_read(VFIODevice *vbasedev, uint8_t index, off_t off,
 }
 
 static int vfio_io_region_write(VFIODevice *vbasedev, uint8_t index, off_t off,
-                                uint32_t size, void *data)
+                                uint32_t size, void *data, bool post)
 {  

This is all a bit confusing as a non-posted write on bare metal would require a follow-up read to flush the write, but in the kernel case we rely on both the bus protocols and the userspace driver to perform such actions to enforce coherency. The read-after-write hazard comment above

Quoted 9 lines

suggests the issue is split access between mmap and read/write across the region, where the mmap access bypasses the socket protocol. But isn't this actually across the whole device? For example, PCI doesn't care which BAR a write targets, reads to another BAR cannot bypass the write, aiui. IOW, couldn't a write to a BAR that doesn't support mmap affect the behavior of a BAR that does support mmap, and therefore there should be no posted writes for the entire device if any region supports mmap access?

The protocol has sequential ordering, so issues arise only when
reads to mapped regions pass writes that were sent asynchronously. The code is designed to handle common driver ordering operations: reading config space (which can’t be mapped, so they’re sequentially ordered by the protocol) and reading a nearby register (in the same region). There is a ’no-post’ vfio-user option in case the driver relies on ordering reads to other (non-config) regions

Quoted 3 lines

I made this the default since of the the primary customers is an
NVME device server that polls a mapped doorbell region and wants posted writes.

This sounds like we're targeting a very specific use case then since the code explicitly disables posted writes when mmaps within the same region are available. Another device might have these spread across different regions, where the mmap test might not force the non-posted write behavior and the user write could return with the operation still in-flight through the socket protocol, allowing a read through the mmap to bypass.

Device options are great for debugging and isolating certain features, but users expect that devices "just work".

Quoted 6 lines
Quoted 5 lines

I wonder if a better approach wouldn't be to make all writes operations synchronous and avoid read-after-write hazard by performing writes through the mmap when available, ie. make use of the same bypass to avoid the hazard.

Quoted 9 lines
Quoted 2 lines

I can flip the default to be strict PCI, and force the NVME
folks to use an option to enable performance.

All the physical NVME devices I have around only have a single BAR, so the test to disable posted writes if mmaps are available within the same region, ie. BAR, would necessarily cause all writes through the trapped space to be non-posted. Is your NVME device already working with a non-standard layout to "enable performance"?

It looks like they have 1 BAR and use 2 different modes depending
on if they want to dedicate threads to polling the doorbells.

Quoted 10 lines

I'd rather see default behavior that focuses on creating a compatible interface to bare metal, which I think means we need to demote all writes to non-posted when there's a mechanism where reads can bypass writes. From that we can build quirks for known devices which can overlay memory regions that only perform posted writes. Perhaps a side-band interface modeled after sparse mmaps would allow standardization of such a device quirk to allow the device on the server side to direct areas where posted writes are always safe. Thanks,

ok - the default will be strict PCI (no posted writes if any part
of the device is mapped) If the NVME folks want a looser mode, I can add it as an option. I’m not sure this is a property of the HW, more of the driver using config space reads for ordering, and not depending on memory region access ordering.

JJ

#20 lore raw

PATCH v2 13/23 vfio-user: pci_user_realize PCI setup

In reply to John Johnson

PCI BARs read from remote device
PCI config reads/writes sent to remote server

---
 hw/vfio/pci.h      |   9 ++
 hw/vfio/pci.c      | 257 ++++++++++++++++++++++++++++++-----------------------
 hw/vfio/user-pci.c |  47 ++++++++++
 3 files changed, 203 insertions(+), 110 deletions(-)
hw/vfio/pci.h +9 -0
index 50a1d07..4f70664 100644
@@ -212,7 +212,16 @@ void vfio_intx_eoi(VFIODevice *vbasedev);
 Object *vfio_pci_get_object(VFIODevice *vbasedev);
 void vfio_pci_save_config(VFIODevice *vbasedev, QEMUFile *f);
 int vfio_pci_load_config(VFIODevice *vbasedev, QEMUFile *f);
+void vfio_populate_device(VFIOPCIDevice *vdev, Error **errp);
+void vfio_teardown_msi(VFIOPCIDevice *vdev);
+void vfio_bars_exit(VFIOPCIDevice *vdev);
+void vfio_bars_finalize(VFIOPCIDevice *vdev);
+int vfio_add_capabilities(VFIOPCIDevice *vdev, Error **errp);
 void vfio_put_device(VFIOPCIDevice *vdev);
+void vfio_register_err_notifier(VFIOPCIDevice *vdev);
+void vfio_register_req_notifier(VFIOPCIDevice *vdev);
+void vfio_pci_config_setup(VFIOPCIDevice *vdev, Error **errp);
+int vfio_interrupt_setup(VFIOPCIDevice *vdev, Error **errp);
 void vfio_instance_init(Object *obj);
 
 uint64_t vfio_vga_read(void *opaque, hwaddr addr, unsigned size);
hw/vfio/pci.c +147 -110

323 lines not on this page · raw message

hw/vfio/user-pci.c +47 -0

61 lines not on this page · raw message

Signature · 1 line
1.9.4
#21 lore raw

PATCH v2 14/23 vfio-user: get and set IRQs

In reply to John Johnson
---
 hw/vfio/user-protocol.h |  25 +++++++++
 hw/vfio/pci.c           |   3 +-
 hw/vfio/user.c          | 140 ++++++++++++++++++++++++++++++++++++++++++++++++
 hw/vfio/trace-events    |   2 +
 4 files changed, 169 insertions(+), 1 deletion(-)
hw/vfio/user-protocol.h +25 -0
index 6987435..48dd475 100644
@@ -140,6 +140,31 @@ typedef struct {
 } 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/pci.c +2 -1
index b8b5c34..42e7c82 100644
@@ -711,7 +711,8 @@ retry:
     ret = vfio_enable_vectors(vdev, false);
     if (ret) {
         if (ret < 0) {
-            error_report("vfio: Error: Failed to setup MSI fds: %m");
+            error_report("vfio: Error: Failed to setup MSI fds: %s",
+                         strerror(-ret));
         } else {
             error_report("vfio: Error: Failed to enable %d "
                          "MSI vectors, retry with %d", vdev->nr_vectors, ret);
hw/vfio/user.c +140 -0

158 lines not on this page · raw message

hw/vfio/trace-events +2 -0
index 1f3688f..01563cb 100644
@@ -176,3 +176,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
1.9.4
#22 lore raw

PATCH v2 15/23 vfio-user: forward msix BAR accesses to server

In reply to John Johnson

Server holds device current device pending state Use irq masking commands in socket case

---
 hw/vfio/pci.h                 |  1 +
 include/hw/vfio/vfio-common.h |  3 ++
 hw/vfio/ccw.c                 |  1 +
 hw/vfio/common.c              | 26 ++++++++++++++++++
 hw/vfio/pci.c                 | 23 +++++++++++++++-
 hw/vfio/platform.c            |  1 +
 hw/vfio/user-pci.c            | 64 +++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 118 insertions(+), 1 deletion(-)
hw/vfio/pci.h +1 -0
index 4f70664..d3e5d5f 100644
@@ -113,6 +113,7 @@ typedef struct VFIOMSIXInfo {
     uint32_t table_offset;
     uint32_t pba_offset;
     unsigned long *pending;
+    MemoryRegion *pba_region;
 } VFIOMSIXInfo;
 
 /*
include/hw/vfio/vfio-common.h +3 -0
index bbc4b15..2c58d7d 100644
@@ -143,6 +143,7 @@ typedef struct VFIODevice {
     bool ram_block_discard_allowed;
     bool enable_migration;
     bool use_regfds;
+    bool can_mask_irq;
     VFIODeviceOps *ops;
     VFIODeviceIO *io;
     unsigned int num_irqs;
@@ -239,6 +240,8 @@ void vfio_put_base_device(VFIODevice *vbasedev);
 void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
+void vfio_unmask_single_irq(VFIODevice *vbasedev, int index, int irq);
+void vfio_mask_single_irq(VFIODevice *vbasedev, int index, int irq);
 int vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex,
                            int action, int fd, Error **errp);
 void vfio_region_write(void *opaque, hwaddr addr,
hw/vfio/ccw.c +1 -0
index 00605bd..bf67670 100644
@@ -616,6 +616,7 @@ static void vfio_ccw_get_device(VFIOGroup *group, VFIOCCWDevice *vcdev,
     vcdev->vdev.dev = &vcdev->cdev.parent_obj.parent_obj;
     vcdev->vdev.io = &vfio_dev_io_ioctl;
     vcdev->vdev.use_regfds = false;
+    vcdev->vdev.can_mask_irq = false;
 
     return;
 
hw/vfio/common.c +26 -0
index de64e53..0c1cb21 100644
@@ -102,6 +102,32 @@ void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index)
     vbasedev->io->set_irqs(vbasedev, &irq_set);
 }
 
+void vfio_mask_single_irq(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->set_irqs(vbasedev, &irq_set);
+}
+
+void vfio_unmask_single_irq(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->set_irqs(vbasedev, &irq_set);
+}
+
 static inline const char *action_to_str(int action)
 {
     switch (action) {
hw/vfio/pci.c +22 -1
index 42e7c82..7b16f8f 100644
@@ -477,6 +477,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;
 
     trace_vfio_msix_vector_do_use(vdev->vbasedev.name, nr);
@@ -490,6 +491,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);
     }
 
@@ -516,6 +518,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;
         }
     }
 
@@ -523,6 +526,8 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
      * We don't want to have the host allocate all possible MSI vectors
      * for a device if they're not in use, so we shutdown and incrementally
      * increase them as needed.
+     * Otherwise, unmask the vector if the vector is already setup (and we can
+     * do so) or send the fd if not.
      */
     if (vdev->nr_vectors < nr + 1) {
         vdev->nr_vectors = nr + 1;
@@ -533,6 +538,8 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
                 error_report("vfio: failed to enable vectors, %d", ret);
             }
         }
+    } else if (vdev->vbasedev.can_mask_irq && !new_vec) {
+        vfio_unmask_single_irq(&vdev->vbasedev, VFIO_PCI_MSIX_IRQ_INDEX, nr);
     } else {
         Error *err = NULL;
         int32_t fd;
@@ -574,6 +581,12 @@ 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->vbasedev.can_mask_irq) {
+        vfio_mask_single_irq(&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
@@ -644,7 +657,7 @@ static void vfio_msix_enable(VFIOPCIDevice *vdev)
         if (ret) {
             error_report("vfio: failed to enable vectors, %d", ret);
         }
-    } else {
+    } else if (!vdev->vbasedev.can_mask_irq) {
         /*
          * Some communication channels between VF & PF or PF & fw rely on the
          * physical state of the device and expect that enabling MSI-X from the
@@ -660,6 +673,13 @@ static void vfio_msix_enable(VFIOPCIDevice *vdev)
          */
         vfio_msix_vector_do_use(&vdev->pdev, 0, NULL, NULL);
         vfio_msix_vector_release(&vdev->pdev, 0);
+    } else {
+        /*
+         * If we can use irq masking, send an invalid fd on vector 0
+         * to enable MSI-X without any vectors enabled.
+         */
+        vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_MSIX_IRQ_INDEX, 0,
+                               VFIO_IRQ_SET_ACTION_TRIGGER, -1, NULL);
     }
 
     trace_vfio_msix_enable(vdev->vbasedev.name);
@@ -3040,6 +3060,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
     vbasedev->dev = DEVICE(vdev);
     vbasedev->io = &vfio_dev_io_ioctl;
     vbasedev->use_regfds = false;
+    vbasedev->can_mask_irq = false;
 
     tmp = g_strdup_printf("%s/iommu_group", vbasedev->sysfsdev);
     len = readlink(tmp, group_path, sizeof(group_path));
hw/vfio/platform.c +1 -0
index 8ddfcca..3387ec4 100644
@@ -623,6 +623,7 @@ static void vfio_platform_realize(DeviceState *dev, Error **errp)
     vbasedev->ops = &vfio_platform_ops;
     vbasedev->io = &vfio_dev_io_ioctl;
     vbasedev->use_regfds = false;
+    vbasedev->can_mask_irq = false;
 
     qemu_mutex_init(&vdev->intp_mutex);
 
hw/vfio/user-pci.c +64 -0

88 lines not on this page · raw message

Signature · 1 line
1.9.4
#23 lore raw

Re: [PATCH v2 15/23] vfio-user: forward msix BAR accesses to server

Quoted 33 lines · On Wed, 1 Feb 2023 21:55:51 -0800 John Johnson <[email protected]>…

Server holds device current device pending state Use irq masking commands in socket case

---
 hw/vfio/pci.h                 |  1 +
 include/hw/vfio/vfio-common.h |  3 ++
 hw/vfio/ccw.c                 |  1 +
 hw/vfio/common.c              | 26 ++++++++++++++++++
 hw/vfio/pci.c                 | 23 +++++++++++++++-
 hw/vfio/platform.c            |  1 +
 hw/vfio/user-pci.c            | 64 +++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 118 insertions(+), 1 deletion(-)
hw/vfio/pci.h +1 -0
index 4f70664..d3e5d5f 100644
@@ -113,6 +113,7 @@ typedef struct VFIOMSIXInfo {
     uint32_t table_offset;
     uint32_t pba_offset;
     unsigned long *pending;
+    MemoryRegion *pba_region;
 } VFIOMSIXInfo;
 
 /*
include/hw/vfio/vfio-common.h +0 -0
index bbc4b15..2c58d7d 100644

include/hw/vfio/vfio-common.h typedef struct VFIODevice { · line 146

Quoted 4 lines
include/hw/vfio/vfio-common.h +1 -0
@@ -143,6 +143,7 @@ typedef struct VFIODevice {
     bool ram_block_discard_allowed;
     bool enable_migration;
     bool use_regfds;
+    bool can_mask_irq;

How can this be a device level property? vfio-pci (kernel) supports masking of INTx. It seems like there needs to be a per-index info or probe here to to support this.

Quoted 82 lines
include/hw/vfio/vfio-common.h +2 -0
     VFIODeviceOps *ops;
     VFIODeviceIO *io;
     unsigned int num_irqs;
@@ -239,6 +240,8 @@ void vfio_put_base_device(VFIODevice *vbasedev);
 void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
+void vfio_unmask_single_irq(VFIODevice *vbasedev, int index, int irq);
+void vfio_mask_single_irq(VFIODevice *vbasedev, int index, int irq);
 int vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex,
                            int action, int fd, Error **errp);
 void vfio_region_write(void *opaque, hwaddr addr,
hw/vfio/ccw.c +1 -0
index 00605bd..bf67670 100644
@@ -616,6 +616,7 @@ static void vfio_ccw_get_device(VFIOGroup *group, VFIOCCWDevice *vcdev,
     vcdev->vdev.dev = &vcdev->cdev.parent_obj.parent_obj;
     vcdev->vdev.io = &vfio_dev_io_ioctl;
     vcdev->vdev.use_regfds = false;
+    vcdev->vdev.can_mask_irq = false;
 
     return;
 
hw/vfio/common.c +26 -0
index de64e53..0c1cb21 100644
@@ -102,6 +102,32 @@ void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index)
     vbasedev->io->set_irqs(vbasedev, &irq_set);
 }
 
+void vfio_mask_single_irq(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->set_irqs(vbasedev, &irq_set);
+}
+
+void vfio_unmask_single_irq(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->set_irqs(vbasedev, &irq_set);
+}
+
 static inline const char *action_to_str(int action)
 {
     switch (action) {
hw/vfio/pci.c +2 -0
index 42e7c82..7b16f8f 100644
@@ -477,6 +477,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;
 
     trace_vfio_msix_vector_do_use(vdev->vbasedev.name, nr);
@@ -490,6 +491,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);
     }
 

hw/vfio/pci.c static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr, · line 521

Quoted 4 lines
hw/vfio/pci.c +1 -0
@@ -516,6 +518,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;

This looks wrong to me, can't we get here when a previously used vector is unmasked?

Quoted 13 lines
hw/vfio/pci.c +2 -0
         }
     }
 
@@ -523,6 +526,8 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
      * We don't want to have the host allocate all possible MSI vectors
      * for a device if they're not in use, so we shutdown and incrementally
      * increase them as needed.
+     * Otherwise, unmask the vector if the vector is already setup (and we can
+     * do so) or send the fd if not.
      */
     if (vdev->nr_vectors < nr + 1) {
         vdev->nr_vectors = nr + 1;

hw/vfio/pci.c static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr, · line 542

Quoted 5 lines
hw/vfio/pci.c +2 -0
@@ -533,6 +538,8 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
                 error_report("vfio: failed to enable vectors, %d", ret);
             }
         }
+    } else if (vdev->vbasedev.can_mask_irq && !new_vec) {
+        vfio_unmask_single_irq(&vdev->vbasedev, VFIO_PCI_MSIX_IRQ_INDEX, nr);

The correctness of @new_vec is doubtful here, but masking support must be in reference to a specific IRQ index. INTx masking is implicit, but we'd probably need something on the VFIOPCIDevice to indicate support for MSI and MSIX masking support, separately.

Quoted 26 lines
hw/vfio/pci.c +7 -1
     } else {
         Error *err = NULL;
         int32_t fd;
@@ -574,6 +581,12 @@ 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->vbasedev.can_mask_irq) {
+        vfio_mask_single_irq(&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
@@ -644,7 +657,7 @@ static void vfio_msix_enable(VFIOPCIDevice *vdev)
         if (ret) {
             error_report("vfio: failed to enable vectors, %d", ret);
         }
-    } else {
+    } else if (!vdev->vbasedev.can_mask_irq) {
         /*
          * Some communication channels between VF & PF or PF & fw rely on the
          * physical state of the device and expect that enabling MSI-X from the

hw/vfio/pci.c static void vfio_msix_enable(VFIOPCIDevice *vdev) · line 682

Quoted 10 lines
hw/vfio/pci.c +7 -0
@@ -660,6 +673,13 @@ static void vfio_msix_enable(VFIOPCIDevice *vdev)
          */
         vfio_msix_vector_do_use(&vdev->pdev, 0, NULL, NULL);
         vfio_msix_vector_release(&vdev->pdev, 0);
+    } else {
+        /*
+         * If we can use irq masking, send an invalid fd on vector 0
+         * to enable MSI-X without any vectors enabled.
+         */
+        vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_MSIX_IRQ_INDEX, 0,
+                               VFIO_IRQ_SET_ACTION_TRIGGER, -1, NULL);

What does this have to do with masking? I'm not entirely sure this doesn't also work on vfio-pci (kernel).

In general this feels like a special cased version of MSI/X masking support rather than actually reworking things to support underlying masking support for these indexes. Thanks,

Alex

#24 lore raw

Re: [PATCH v2 15/23] vfio-user: forward msix BAR accesses to server

Quoted 34 lines · Alex Williamson
Quoted 27 lines · On Wed, 1 Feb 2023 21:55:51 -0800 John Johnson <[email protected]>…

Server holds device current device pending state
Use irq masking commands in socket case

---

hw/vfio/pci.h                 |  1 +
include/hw/vfio/vfio-common.h |  3 ++
hw/vfio/ccw.c                 |  1 +
hw/vfio/common.c              | 26 ++++++++++++++++++
hw/vfio/pci.c                 | 23 +++++++++++++++-
hw/vfio/platform.c            |  1 +
hw/vfio/user-pci.c            | 64 +++++++++++++++++++++++++++++++++++++++++++
7 files changed, 118 insertions(+), 1 deletion(-)
hw/vfio/pci.h +1 -0
index 4f70664..d3e5d5f 100644
@@ -113,6 +113,7 @@ typedef struct VFIOMSIXInfo {
    uint32_t table_offset;
    uint32_t pba_offset;
    unsigned long *pending;
+    MemoryRegion *pba_region;
 } VFIOMSIXInfo;
 
Quoted 15 lines

include/hw/vfio/vfio-common.h typedef struct VFIODevice { · line 146

Quoted 10 lines
hw/vfio/pci.h +0 -0
 /*
include/hw/vfio/vfio-common.h +1 -0
index bbc4b15..2c58d7d 100644
@@ -143,6 +143,7 @@ typedef struct VFIODevice {
    bool ram_block_discard_allowed;
    bool enable_migration;
    bool use_regfds;
+    bool can_mask_irq;

How can this be a device level property? vfio-pci (kernel) supports masking of INTx. It seems like there needs to be a per-index info or probe here to to support this.

It is only used for MSIX masking. MSI masking is done with
config space stores, and vfio-kernel and vfio-user handle them the same by forwarding the config space writes to the server so it can mask interrupts.

MSIX is trickier because the mask bits are in a memory region.
vfio-kernel doesn’t support SET_IRQS on MSIX vectors, so if the host doesn’t allow client mapping of the MSIX table to do the masking, vfio switches a masked vector’s event FD destination from KVM to QEMU, then uses the QEMU PCI emulation to mask the vector.

vfio-user has to use messages to mask MSIX vectors, since there
is no host config space to map. I originally forwarded the MSIX table writes to the server to do the masking, but the feedback on the vfio-user server changes was to add SET_IRQS support.

I can make this a PCI MSIX-specific bool if that helps.

Quoted 82 lines
Quoted 82 lines
include/hw/vfio/vfio-common.h +2 -0
    VFIODeviceOps *ops;
    VFIODeviceIO *io;
    unsigned int num_irqs;
@@ -239,6 +240,8 @@ void vfio_put_base_device(VFIODevice *vbasedev);
 void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
+void vfio_unmask_single_irq(VFIODevice *vbasedev, int index, int irq);
+void vfio_mask_single_irq(VFIODevice *vbasedev, int index, int irq);
 int vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex,
                           int action, int fd, Error **errp);
 void vfio_region_write(void *opaque, hwaddr addr,
hw/vfio/ccw.c +1 -0
index 00605bd..bf67670 100644
@@ -616,6 +616,7 @@ static void vfio_ccw_get_device(VFIOGroup *group, VFIOCCWDevice *vcdev,
    vcdev->vdev.dev = &vcdev->cdev.parent_obj.parent_obj;
    vcdev->vdev.io = &vfio_dev_io_ioctl;
    vcdev->vdev.use_regfds = false;
+    vcdev->vdev.can_mask_irq = false;
 
    return;
 
hw/vfio/common.c +26 -0
index de64e53..0c1cb21 100644
@@ -102,6 +102,32 @@ void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index)
    vbasedev->io->set_irqs(vbasedev, &irq_set);
 }
 
+void vfio_mask_single_irq(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->set_irqs(vbasedev, &irq_set);
+}
+
+void vfio_unmask_single_irq(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->set_irqs(vbasedev, &irq_set);
+}
+
 static inline const char *action_to_str(int action)
 {
    switch (action) {
hw/vfio/pci.c +2 -0
index 42e7c82..7b16f8f 100644
@@ -477,6 +477,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;
 
    trace_vfio_msix_vector_do_use(vdev->vbasedev.name, nr);
@@ -490,6 +491,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);
    }
 
Quoted 9 lines

hw/vfio/pci.c static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr, · line 521

Quoted 5 lines
hw/vfio/pci.c +1 -0
@@ -516,6 +518,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;

This looks wrong to me, can't we get here when a previously used vector is unmasked?

It’s for the case where we upgrade the vector from using an event FD
that terminates in QEMU to one that terminates in KVM. In that case we want to send the FD again, not just unmask the current FD.

Quoted 15 lines
Quoted 15 lines
hw/vfio/pci.c +2 -0
        }
    }
 
@@ -523,6 +526,8 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
     * We don't want to have the host allocate all possible MSI vectors
     * for a device if they're not in use, so we shutdown and incrementally
     * increase them as needed.
+     * Otherwise, unmask the vector if the vector is already setup (and we can
+     * do so) or send the fd if not.
     */
    if (vdev->nr_vectors < nr + 1) {
        vdev->nr_vectors = nr + 1;
@@ -533,6 +538,8 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
                error_report("vfio: failed to enable vectors, %d", ret);
            }
Quoted 9 lines

hw/vfio/pci.c static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr, · line 542

Quoted 3 lines
hw/vfio/pci.c +2 -0
        }
+    } else if (vdev->vbasedev.can_mask_irq && !new_vec) {
+        vfio_unmask_single_irq(&vdev->vbasedev, VFIO_PCI_MSIX_IRQ_INDEX, nr);

The correctness of @new_vec is doubtful here, but masking support must be in reference to a specific IRQ index. INTx masking is implicit, but we'd probably need something on the VFIOPCIDevice to indicate support for MSI and MSIX masking support, separately.

MSI masking uses config writes, so this is only for MSIX.

Quoted 40 lines
Quoted 26 lines
hw/vfio/pci.c +7 -1
    } else {
        Error *err = NULL;
        int32_t fd;
@@ -574,6 +581,12 @@ 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->vbasedev.can_mask_irq) {
+        vfio_mask_single_irq(&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
@@ -644,7 +657,7 @@ static void vfio_msix_enable(VFIOPCIDevice *vdev)
        if (ret) {
            error_report("vfio: failed to enable vectors, %d", ret);
        }
-    } else {
+    } else if (!vdev->vbasedev.can_mask_irq) {
        /*
         * Some communication channels between VF & PF or PF & fw rely on the
         * physical state of the device and expect that enabling MSI-X from the

hw/vfio/pci.c static void vfio_msix_enable(VFIOPCIDevice *vdev) · line 682

Quoted 10 lines
hw/vfio/pci.c +7 -0
@@ -660,6 +673,13 @@ static void vfio_msix_enable(VFIOPCIDevice *vdev)
         */
        vfio_msix_vector_do_use(&vdev->pdev, 0, NULL, NULL);
        vfio_msix_vector_release(&vdev->pdev, 0);
+    } else {
+        /*
+         * If we can use irq masking, send an invalid fd on vector 0
+         * to enable MSI-X without any vectors enabled.
+         */
+        vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_MSIX_IRQ_INDEX, 0,
+                               VFIO_IRQ_SET_ACTION_TRIGGER, -1, NULL);

What does this have to do with masking? I'm not entirely sure this doesn't also work on vfio-pci (kernel).

Quoted 4 lines

In general this feels like a special cased version of MSI/X masking support rather than actually reworking things to support underlying masking support for these indexes. Thanks,

The comment in the !can_mask_irq clause sounded like vfio-kernel
needs a use/release cycle, so I didn’t want to change its behavior.

JJ

#25 lore raw

Re: [PATCH v2 15/23] vfio-user: forward msix BAR accesses to server

Quoted 48 lines · On Wed, 8 Feb 2023 06:38:30 +0000 John Johnson <[email protected]>…
Quoted 32 lines · Alex Williamson
Quoted 27 lines · On Wed, 1 Feb 2023 21:55:51 -0800 John Johnson <[email protected]>…

Server holds device current device pending state Use irq masking commands in socket case

---

hw/vfio/pci.h                 |  1 +
include/hw/vfio/vfio-common.h |  3 ++
hw/vfio/ccw.c                 |  1 +
hw/vfio/common.c              | 26 ++++++++++++++++++
hw/vfio/pci.c                 | 23 +++++++++++++++-
hw/vfio/platform.c            |  1 +
hw/vfio/user-pci.c            | 64 +++++++++++++++++++++++++++++++++++++++++++
7 files changed, 118 insertions(+), 1 deletion(-)
hw/vfio/pci.h +1 -0
index 4f70664..d3e5d5f 100644
@@ -113,6 +113,7 @@ typedef struct VFIOMSIXInfo {
    uint32_t table_offset;
    uint32_t pba_offset;
    unsigned long *pending;
+    MemoryRegion *pba_region;
 } VFIOMSIXInfo;
 
Quoted 15 lines

include/hw/vfio/vfio-common.h typedef struct VFIODevice { · line 146

Quoted 10 lines
hw/vfio/pci.h +0 -0
 /*
include/hw/vfio/vfio-common.h +1 -0
index bbc4b15..2c58d7d 100644
@@ -143,6 +143,7 @@ typedef struct VFIODevice {
    bool ram_block_discard_allowed;
    bool enable_migration;
    bool use_regfds;
+    bool can_mask_irq;  

How can this be a device level property? vfio-pci (kernel) supports masking of INTx. It seems like there needs to be a per-index info or probe here to to support this.

Quoted 4 lines

It is only used for MSIX masking. MSI masking is done with
config space stores, and vfio-kernel and vfio-user handle them the same by forwarding the config space writes to the server so it can mask interrupts.

I suppose this does slip through on vfio-kernel, though support via SET_IRQS would really be the uAPI mechanism we'd expect to use for masking, just as it is for enabling/disabling MSI. MSI is not well used enough to have flushed that out and it seems harmless, but is not the intended API.

Quoted 5 lines

MSIX is trickier because the mask bits are in a memory region.
vfio-kernel doesn’t support SET_IRQS on MSIX vectors, so if the host doesn’t allow client mapping of the MSIX table to do the masking, vfio switches a masked vector’s event FD destination from KVM to QEMU, then uses the QEMU PCI emulation to mask the vector.

Lack of support for MSIX ACTION_[UN]MASK is not an API limitation, it's an implementation issue in the kernel. Same for MSI. I believe this is resolved now, that there are mask/unmask APIs available within the kernel, as well as mechanisms to avoid the NORESIZE behavior now, so the intention would be to implement that support, along with a mechanism for the user to know the support is present. We already have that for NORESIZE via IRQ_INFO, so I suspect the right answer might be to add a new VFIO_IRQ_INFO_MSI_MASK to advertise masking support.

Quoted 4 lines

vfio-user has to use messages to mask MSIX vectors, since there
is no host config space to map. I originally forwarded the MSIX table writes to the server to do the masking, but the feedback on the vfio-user server changes was to add SET_IRQS support.

Which is what I'm describing above, QEMU should know via the VFIO uAPI whether MSI/X masking is supported and use that to configure the code to make use of it rather than some inferred value based on the interface type. Thanks,

Alex

#26 lore raw

Re: [PATCH v2 15/23] vfio-user: forward msix BAR accesses to server

Quoted 72 lines · Alex Williamson
Quoted 48 lines · On Wed, 8 Feb 2023 06:38:30 +0000 John Johnson <[email protected]>…
Quoted 32 lines · Alex Williamson
Quoted 27 lines · On Wed, 1 Feb 2023 21:55:51 -0800 John Johnson <[email protected]>…

Server holds device current device pending state Use irq masking commands in socket case

---

hw/vfio/pci.h                 |  1 +
include/hw/vfio/vfio-common.h |  3 ++
hw/vfio/ccw.c                 |  1 +
hw/vfio/common.c              | 26 ++++++++++++++++++
hw/vfio/pci.c                 | 23 +++++++++++++++-
hw/vfio/platform.c            |  1 +
hw/vfio/user-pci.c            | 64 +++++++++++++++++++++++++++++++++++++++++++
7 files changed, 118 insertions(+), 1 deletion(-)
hw/vfio/pci.h +1 -0
index 4f70664..d3e5d5f 100644
@@ -113,6 +113,7 @@ typedef struct VFIOMSIXInfo {
   uint32_t table_offset;
   uint32_t pba_offset;
   unsigned long *pending;
+    MemoryRegion *pba_region;
 } VFIOMSIXInfo;
 
Quoted 15 lines

include/hw/vfio/vfio-common.h typedef struct VFIODevice { · line 146

Quoted 10 lines
hw/vfio/pci.h +0 -0
 /*
include/hw/vfio/vfio-common.h +1 -0
index bbc4b15..2c58d7d 100644
@@ -143,6 +143,7 @@ typedef struct VFIODevice {
   bool ram_block_discard_allowed;
   bool enable_migration;
   bool use_regfds;
+    bool can_mask_irq;  

How can this be a device level property? vfio-pci (kernel) supports masking of INTx. It seems like there needs to be a per-index info or probe here to to support this.

Quoted 4 lines

It is only used for MSIX masking. MSI masking is done with
config space stores, and vfio-kernel and vfio-user handle them the same by forwarding the config space writes to the server so it can mask interrupts.

I suppose this does slip through on vfio-kernel, though support via SET_IRQS would really be the uAPI mechanism we'd expect to use for masking, just as it is for enabling/disabling MSI. MSI is not well used enough to have flushed that out and it seems harmless, but is not the intended API.

Quoted 5 lines

MSIX is trickier because the mask bits are in a memory region.
vfio-kernel doesn’t support SET_IRQS on MSIX vectors, so if the host doesn’t allow client mapping of the MSIX table to do the masking, vfio switches a masked vector’s event FD destination from KVM to QEMU, then uses the QEMU PCI emulation to mask the vector.

Quoted 9 lines

Lack of support for MSIX ACTION_[UN]MASK is not an API limitation, it's an implementation issue in the kernel. Same for MSI. I believe this is resolved now, that there are mask/unmask APIs available within the kernel, as well as mechanisms to avoid the NORESIZE behavior now, so the intention would be to implement that support, along with a mechanism for the user to know the support is present. We already have that for NORESIZE via IRQ_INFO, so I suspect the right answer might be to add a new VFIO_IRQ_INFO_MSI_MASK to advertise masking support.

I looked at the linux next vfio kernel driver, and it doesn't seem
to support MSI/X masking.

Quoted 10 lines
Quoted 4 lines

vfio-user has to use messages to mask MSIX vectors, since there
is no host config space to map. I originally forwarded the MSIX table writes to the server to do the masking, but the feedback on the vfio-user server changes was to add SET_IRQS support.

Which is what I'm describing above, QEMU should know via the VFIO uAPI whether MSI/X masking is supported and use that to configure the code to make use of it rather than some inferred value based on the interface type. Thanks,

The kernel doesn’t advertising it working either, so I think I can
key it off the presence of VFIO_IRQ_INFO_MASKABLE in irq_info

JJ

#27 lore raw

PATCH v2 16/23 vfio-user: proxy container connect/disconnect

In reply to John Johnson
---
 hw/vfio/user.h                |   6 +-
 include/hw/vfio/vfio-common.h |  10 +++
 hw/vfio/common.c              | 100 ++++++++++++++++-----------
 hw/vfio/user-pci.c            |  12 +++-
 hw/vfio/user.c                | 152 +++++++++++++++++++++++++++++++++++++++++-
 5 files changed, 237 insertions(+), 43 deletions(-)
hw/vfio/user.h +5 -1
index 3012a86..b89e5ca 100644
@@ -91,9 +91,13 @@ void vfio_user_disconnect(VFIOUserProxy *proxy);
 void vfio_user_set_handler(VFIODevice *vbasedev,
                            void (*handler)(void *opaque, VFIOUserMsg *msg),
                            void *reqarg);
-int vfio_user_get_device(VFIODevice *vbasedev, Error **errp);
+int vfio_user_get_device(VFIOGroup *group, VFIODevice *vbasedev, Error **errp);
+VFIOGroup *vfio_user_get_group(VFIOUserProxy *proxy, AddressSpace *as,
+                               Error **errp);
+void vfio_user_put_group(VFIOGroup *group);
 int vfio_user_validate_version(VFIOUserProxy *proxy, Error **errp);
 
 extern VFIODeviceIO vfio_dev_io_sock;
+extern VFIOContainerIO vfio_cont_io_sock;
 
 #endif /* VFIO_USER_H */
include/hw/vfio/vfio-common.h +10 -0
index 2c58d7d..b0c4453 100644
@@ -94,6 +94,7 @@ typedef struct VFIOContainer {
     uint64_t max_dirty_bitmap_size;
     unsigned long pgsizes;
     unsigned int dma_max_mappings;
+    VFIOUserProxy *proxy;
     QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
     QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list;
     QLIST_HEAD(, VFIOGroup) group_list;
@@ -236,6 +237,7 @@ typedef struct VFIODisplay {
     } dmabuf;
 } VFIODisplay;
 
+int vfio_ram_block_discard_disable(VFIOContainer *container, bool state);
 void vfio_put_base_device(VFIODevice *vbasedev);
 void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
@@ -244,6 +246,9 @@ void vfio_unmask_single_irq(VFIODevice *vbasedev, int index, int irq);
 void vfio_mask_single_irq(VFIODevice *vbasedev, int index, int irq);
 int vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex,
                            int action, int fd, Error **errp);
+void vfio_host_win_add(VFIOContainer *container, hwaddr min_iova,
+                       hwaddr max_iova, uint64_t iova_pgsizes);
+void vfio_listener_release(VFIOContainer *container);
 void vfio_region_write(void *opaque, hwaddr addr,
                            uint64_t data, unsigned size);
 uint64_t vfio_region_read(void *opaque,
@@ -256,11 +261,16 @@ void vfio_region_unmap(VFIORegion *region);
 void vfio_region_exit(VFIORegion *region);
 void vfio_region_finalize(VFIORegion *region);
 void vfio_reset_handler(void *opaque);
+VFIOAddressSpace *vfio_get_address_space(AddressSpace *as);
+void vfio_put_address_space(VFIOAddressSpace *space);
 VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp);
 void vfio_put_group(VFIOGroup *group);
 int vfio_get_device(VFIOGroup *group, const char *name,
                     VFIODevice *vbasedev, Error **errp);
 
+VFIOContainer *vfio_new_container(VFIOAddressSpace *space);
+void vfio_link_container(VFIOContainer *container, VFIOGroup *group);
+void vfio_unmap_container(VFIOContainer *container);
 void vfio_init_device(VFIODevice *vbasedev, VFIOGroup *group,
                       struct vfio_device_info *info);
 
hw/vfio/common.c +61 -39

173 lines not on this page · raw message

hw/vfio/user-pci.c +11 -1
index bc1d01a..a0aa320 100644
@@ -134,6 +134,7 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)
     VFIODevice *vbasedev = &vdev->vbasedev;
     SocketAddress addr;
     VFIOUserProxy *proxy;
+    VFIOGroup *group = NULL;
     int ret;
     Error *err = NULL;
 
@@ -180,8 +181,15 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)
     vbasedev->use_regfds = true;
     vbasedev->can_mask_irq = true;
 
-    ret = vfio_user_get_device(vbasedev, errp);
+    group = vfio_user_get_group(proxy, pci_device_iommu_address_space(pdev),
+                                errp);
+    if (!group) {
+        goto error;
+    }
+
+    ret = vfio_user_get_device(group, vbasedev, errp);
     if (ret) {
+        vfio_user_put_group(group);
         goto error;
     }
 
@@ -241,6 +249,7 @@ static void vfio_user_instance_finalize(Object *obj)
 {
     VFIOPCIDevice *vdev = VFIO_PCI_BASE(obj);
     VFIODevice *vbasedev = &vdev->vbasedev;
+    VFIOGroup *group = vbasedev->group;
 
     vfio_bars_finalize(vdev);
     g_free(vdev->emulated_config_bits);
@@ -251,6 +260,7 @@ static void vfio_user_instance_finalize(Object *obj)
     }
 
     vfio_put_device(vdev);
+    vfio_user_put_group(group);
 
     if (vbasedev->proxy != NULL) {
         vfio_user_disconnect(vbasedev->proxy);
hw/vfio/user.c +150 -2

181 lines not on this page · raw message

Signature · 1 line
1.9.4
#28 lore raw

PATCH v2 17/23 vfio-user: dma map/unmap operations

In reply to John Johnson

Add ability to do async operations during memory transactions

---
 hw/vfio/user-protocol.h       |  32 ++++++
 include/hw/vfio/vfio-common.h |   4 +-
 hw/vfio/common.c              |  64 +++++++++---
 hw/vfio/user.c                | 224 ++++++++++++++++++++++++++++++++++++++++++
 hw/vfio/trace-events          |   2 +
 5 files changed, 311 insertions(+), 15 deletions(-)
hw/vfio/user-protocol.h +32 -0
index 48dd475..109076d 100644
@@ -114,6 +114,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
  */
@@ -176,4 +201,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 */
include/hw/vfio/vfio-common.h +3 -1
index b0c4453..ee6ad8f 100644
@@ -90,6 +90,7 @@ typedef struct VFIOContainer {
     VFIOContainerIO *io;
     bool initialized;
     bool dirty_pages_supported;
+    bool async_ops;
     uint64_t dirty_pgsizes;
     uint64_t max_dirty_bitmap_size;
     unsigned long pgsizes;
@@ -187,7 +188,7 @@ struct VFIODeviceIO {
 };
 
 struct VFIOContainerIO {
-    int (*dma_map)(VFIOContainer *container,
+    int (*dma_map)(VFIOContainer *container, MemoryRegion *mr,
                    struct vfio_iommu_type1_dma_map *map);
     int (*dma_unmap)(VFIOContainer *container,
                      struct vfio_iommu_type1_dma_unmap *unmap,
@@ -195,6 +196,7 @@ struct VFIOContainerIO {
     int (*dirty_bitmap)(VFIOContainer *container,
                         struct vfio_iommu_type1_dirty_bitmap *bitmap,
                         struct vfio_iommu_type1_dirty_bitmap_get *range);
+    void (*wait_commit)(VFIOContainer *container);
 };
 
 extern VFIODeviceIO vfio_dev_io_ioctl;
hw/vfio/common.c +50 -14

158 lines not on this page · raw message

hw/vfio/user.c +224 -0

255 lines not on this page · raw message

hw/vfio/trace-events +2 -0
index 01563cb..a4e02ff 100644
@@ -178,3 +178,5 @@ 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_dma_map(uint64_t iova, uint64_t size, uint64_t off, uint32_t flags, bool will_commit) " iova 0x%"PRIx64" size 0x%"PRIx64" off 0x%"PRIx64" flags 0x%x will_commit %d"
+vfio_user_dma_unmap(uint64_t iova, uint64_t size, uint32_t flags, bool dirty, bool will_commit) " iova 0x%"PRIx64" size 0x%"PRIx64" flags 0x%x dirty %d will_commit %d"
Signature · 1 line
1.9.4
Acked-by Reviewed-by Michael S. Tsirkin on v2 17/23
#29 lore raw

Re: [PATCH v2 17/23] vfio-user: dma map/unmap operations

Quoted 12 lines · On Wed, 1 Feb 2023 21:55:53 -0800 John Johnson <[email protected]>…

Add ability to do async operations during memory transactions

---
 hw/vfio/user-protocol.h       |  32 ++++++
 include/hw/vfio/vfio-common.h |   4 +-
 hw/vfio/common.c              |  64 +++++++++---
 hw/vfio/user.c                | 224 ++++++++++++++++++++++++++++++++++++++++++
 hw/vfio/trace-events          |   2 +
 5 files changed, 311 insertions(+), 15 deletions(-)

...

Quoted 4 lines
hw/vfio/common.c +0 -0
index 6f99907..f04fd20 100644

...

Quoted 11 lines
hw/vfio/common.c +2 -1
@@ -587,7 +586,8 @@ static bool vfio_listener_skipped_section(MemoryRegionSection *section)
 
 /* Called with rcu_read_lock held.  */
 static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr,
-                               ram_addr_t *ram_addr, bool *read_only)
+                               ram_addr_t *ram_addr, bool *read_only,
+                               MemoryRegion **mrp)
 {
     MemoryRegion *mr;
     hwaddr xlat;

hw/vfio/common.c static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, · line 677

Quoted 10 lines
hw/vfio/common.c +4 -0
@@ -668,6 +668,10 @@ static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr,
         *read_only = !writable || mr->readonly;
     }
 
+    if (mrp != NULL) {
+        *mrp = mr;
+    }
+
     return true;
 }
 

This needs a rebase, the contents of vfio_get_xlat_addr() were largely moved to memory_get_xlat_addr() and the context is all wrong now. Thanks,

Alex

commit baa44bce87fe53ef5c95d39e634b3bace014d235

Author: Cindy Lu <[email protected]>
Date:   Mon Oct 31 11:10:19 2022 +0800

vfio: move implement of vfio_get_xlat_addr() to memory.c

- Move the implement vfio_get_xlat_addr to softmmu/memory.c, and change the name to memory_get_xlat_addr(). So we can use this function on other devices, such as vDPA device.
- Add a new function vfio_get_xlat_addr in vfio/common.c, and it will check whether the memory is backed by a discard manager. then device can have its own warning.

Message-Id: <[email protected]>

#30 lore raw

Re: [PATCH v2 17/23] vfio-user: dma map/unmap operations

In reply to John Johnson
Quoted 21 lines · On Wed, 1 Feb 2023 21:55:53 -0800 John Johnson <[email protected]>…
hw/vfio/common.c +12 -0
index 6f99907..f04fd20 100644
@@ -889,6 +894,29 @@ static bool vfio_known_safe_misalignment(MemoryRegionSection *section)
     return true;
 }
 
+static void vfio_listener_begin(MemoryListener *listener)
+{
+    VFIOContainer *container = container_of(listener, VFIOContainer, listener);
+
+    /*
+     * When DMA space is the physical address space,
+     * the region add/del listeners will fire during
+     * memory update transactions.  These depend on BQL
+     * being held, so do any resulting map/demap ops async
+     * while keeping BQL.
+     */
+    container->async_ops = true;

hw/vfio/common.c static bool vfio_known_safe_misalignment(MemoryRegionSection *section) · line 918

Quoted 10 lines
hw/vfio/common.c +10 -0
+}
+
+static void vfio_listener_commit(MemoryListener *listener)
+{
+    VFIOContainer *container = container_of(listener, VFIOContainer, listener);
+
+    /* wait here for any async requests sent during the transaction */
+    container->io->wait_commit(container);
+    container->async_ops = false;
+}

Since this ends up being entirely vfio-user related, what about embedding a user struct in VFIOContainer and make optional listener_begin and listener_commit callbacks in VFIOContainerIO, ie.

	if (container->io->listener_commit) {
	    container->io->listener_commit(container);
	}

Thanks,
Alex

#31 lore raw

PATCH v2 18/23 vfio-user: add dma_unmap_all

In reply to John Johnson
---
 include/hw/vfio/vfio-common.h |  1 +
 hw/vfio/common.c              | 45 ++++++++++++++++++++++++++++++++++---------
 hw/vfio/user.c                | 24 +++++++++++++++++++++++
 3 files changed, 61 insertions(+), 9 deletions(-)
include/hw/vfio/vfio-common.h +1 -0
index ee6ad8f..abef9b4 100644
@@ -193,6 +193,7 @@ struct VFIOContainerIO {
     int (*dma_unmap)(VFIOContainer *container,
                      struct vfio_iommu_type1_dma_unmap *unmap,
                      struct vfio_bitmap *bitmap);
+    int (*dma_unmap_all)(VFIOContainer *container, uint32_t flags);
     int (*dirty_bitmap)(VFIOContainer *container,
                         struct vfio_iommu_type1_dirty_bitmap *bitmap,
                         struct vfio_iommu_type1_dirty_bitmap_get *range);
hw/vfio/common.c +36 -9

71 lines not on this page · raw message

hw/vfio/user.c +24 -0
index 6dee775..fe6e476 100644
@@ -1825,6 +1825,28 @@ static int vfio_user_io_dma_unmap(VFIOContainer *container,
                                container->async_ops);
 }
 
+static int vfio_user_io_dma_unmap_all(VFIOContainer *container, uint32_t flags)
+{
+    struct vfio_iommu_type1_dma_unmap unmap = {
+        .argsz = sizeof(unmap),
+        .flags = flags | VFIO_DMA_UNMAP_FLAG_ALL,
+        .iova = 0,
+        .size = 0,
+    };
+
+    return vfio_user_dma_unmap(container->proxy, &unmap, NULL,
+                               container->async_ops);
+}
+
+static int vfio_user_io_dirty_bitmap(VFIOContainer *container,
+                        struct vfio_iommu_type1_dirty_bitmap *bitmap,
+                        struct vfio_iommu_type1_dirty_bitmap_get *range)
+{
+
+    /* vfio-user doesn't support migration */
+    return -EINVAL;
+}
+
 static void vfio_user_io_wait_commit(VFIOContainer *container)
 {
     vfio_user_wait_reqs(container->proxy);
@@ -1833,5 +1855,7 @@ static void vfio_user_io_wait_commit(VFIOContainer *container)
 VFIOContainerIO vfio_cont_io_sock = {
     .dma_map = vfio_user_io_dma_map,
     .dma_unmap = vfio_user_io_dma_unmap,
+    .dma_unmap_all = vfio_user_io_dma_unmap_all,
+    .dirty_bitmap = vfio_user_io_dirty_bitmap,
     .wait_commit = vfio_user_io_wait_commit,
 };
Signature · 1 line
1.9.4
#32 lore raw

Re: [PATCH v2 18/23] vfio-user: add dma_unmap_all

Quoted 8 lines · On Wed, 1 Feb 2023 21:55:54 -0800 John Johnson <[email protected]>…
---
 include/hw/vfio/vfio-common.h |  1 +
 hw/vfio/common.c              | 45 ++++++++++++++++++++++++++++++++++---------
 hw/vfio/user.c                | 24 +++++++++++++++++++++++
 3 files changed, 61 insertions(+), 9 deletions(-)

This is inventing a need for a kernel vs user callback op rather than probing support for VFIO_UNMAP_ALL and using VFIO_DMA_UNMAP_FLAG_VADDR consistently. The flags arg of the dma_unmap_all() op is unused and the kernel implementation introduces hard coded magic values. The vfio-user dirty_bitmap addition here is rather random. Please include some sort of description in the commit log for all patches. Thanks,

Alex

Quoted 132 lines
include/hw/vfio/vfio-common.h +1 -0
index ee6ad8f..abef9b4 100644
@@ -193,6 +193,7 @@ struct VFIOContainerIO {
     int (*dma_unmap)(VFIOContainer *container,
                      struct vfio_iommu_type1_dma_unmap *unmap,
                      struct vfio_bitmap *bitmap);
+    int (*dma_unmap_all)(VFIOContainer *container, uint32_t flags);
     int (*dirty_bitmap)(VFIOContainer *container,
                         struct vfio_iommu_type1_dirty_bitmap *bitmap,
                         struct vfio_iommu_type1_dirty_bitmap_get *range);
hw/vfio/common.c +36 -9
index f04fd20..8b55fbb 100644
@@ -508,6 +508,14 @@ static int vfio_dma_unmap(VFIOContainer *container,
     return container->io->dma_unmap(container, &unmap, NULL);
 }
 
+/*
+ * DMA - Mapping and unmapping for the "type1" IOMMU interface used on x86
+ */
+static int vfio_dma_unmap_all(VFIOContainer *container)
+{
+    return container->io->dma_unmap_all(container, VFIO_DMA_UNMAP_FLAG_ALL);
+}
+
 static int vfio_dma_map(VFIOContainer *container, MemoryRegion *mr, hwaddr iova,
                         ram_addr_t size, void *vaddr, bool readonly)
 {
@@ -1256,17 +1264,10 @@ static void vfio_listener_region_del(MemoryListener *listener,
 
     if (try_unmap) {
         if (int128_eq(llsize, int128_2_64())) {
-            /* The unmap ioctl doesn't accept a full 64-bit span. */
-            llsize = int128_rshift(llsize, 1);
+            ret = vfio_dma_unmap_all(container);
+        } else {
             ret = vfio_dma_unmap(container, iova, int128_get64(llsize), NULL);
-            if (ret) {
-                error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx", "
-                             "0x%"HWADDR_PRIx") = %d (%m)",
-                             container, iova, int128_get64(llsize), ret);
-            }
-            iova += int128_get64(llsize);
         }
-        ret = vfio_dma_unmap(container, iova, int128_get64(llsize), NULL);
         if (ret) {
             error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx", "
                          "0x%"HWADDR_PRIx") = %d (%m)",
@@ -2867,6 +2868,31 @@ static int vfio_io_dma_unmap(VFIOContainer *container,
     return 0;
 }
 
+static int vfio_io_dma_unmap_all(VFIOContainer *container, uint32_t flags)
+{
+    struct vfio_iommu_type1_dma_unmap unmap = {
+        .argsz = sizeof(unmap),
+        .flags = 0,
+        .size = 0x8000000000000000,
+    };
+    int ret;
+
+    /* The unmap ioctl doesn't accept a full 64-bit span. */
+    unmap.iova = 0;
+    ret = ioctl(container->fd, VFIO_IOMMU_UNMAP_DMA, &unmap);
+    if (ret) {
+        return -errno;
+    }
+
+    unmap.iova += unmap.size;
+    ret = ioctl(container->fd, VFIO_IOMMU_UNMAP_DMA, &unmap);
+    if (ret) {
+        return -errno;
+    }
+
+    return 0;
+}
+
 static int vfio_io_dirty_bitmap(VFIOContainer *container,
                                 struct vfio_iommu_type1_dirty_bitmap *bitmap,
                                 struct vfio_iommu_type1_dirty_bitmap_get *range)
@@ -2886,6 +2912,7 @@ static void vfio_io_wait_commit(VFIOContainer *container)
 static VFIOContainerIO vfio_cont_io_ioctl = {
     .dma_map = vfio_io_dma_map,
     .dma_unmap = vfio_io_dma_unmap,
+    .dma_unmap_all = vfio_io_dma_unmap_all,
     .dirty_bitmap = vfio_io_dirty_bitmap,
     .wait_commit = vfio_io_wait_commit,
 };
hw/vfio/user.c +24 -0
index 6dee775..fe6e476 100644
@@ -1825,6 +1825,28 @@ static int vfio_user_io_dma_unmap(VFIOContainer *container,
                                container->async_ops);
 }
 
+static int vfio_user_io_dma_unmap_all(VFIOContainer *container, uint32_t flags)
+{
+    struct vfio_iommu_type1_dma_unmap unmap = {
+        .argsz = sizeof(unmap),
+        .flags = flags | VFIO_DMA_UNMAP_FLAG_ALL,
+        .iova = 0,
+        .size = 0,
+    };
+
+    return vfio_user_dma_unmap(container->proxy, &unmap, NULL,
+                               container->async_ops);
+}
+
+static int vfio_user_io_dirty_bitmap(VFIOContainer *container,
+                        struct vfio_iommu_type1_dirty_bitmap *bitmap,
+                        struct vfio_iommu_type1_dirty_bitmap_get *range)
+{
+
+    /* vfio-user doesn't support migration */
+    return -EINVAL;
+}
+
 static void vfio_user_io_wait_commit(VFIOContainer *container)
 {
     vfio_user_wait_reqs(container->proxy);
@@ -1833,5 +1855,7 @@ static void vfio_user_io_wait_commit(VFIOContainer *container)
 VFIOContainerIO vfio_cont_io_sock = {
     .dma_map = vfio_user_io_dma_map,
     .dma_unmap = vfio_user_io_dma_unmap,
+    .dma_unmap_all = vfio_user_io_dma_unmap_all,
+    .dirty_bitmap = vfio_user_io_dirty_bitmap,
     .wait_commit = vfio_user_io_wait_commit,
 };
#33 lore raw

PATCH v2 19/23 vfio-user: no-mmap DMA support

In reply to John Johnson

Force remote process to use DMA r/w messages instead of directly mapping guest memory.

---
 hw/vfio/user.h     | 1 +
 hw/vfio/user-pci.c | 5 +++++
 hw/vfio/user.c     | 2 +-
 3 files changed, 7 insertions(+), 1 deletion(-)
hw/vfio/user.h +1 -0
index b89e5ca..fe0115b 100644
@@ -83,6 +83,7 @@ typedef struct VFIOUserProxy {
 
 /* VFIOProxy flags */
 #define VFIO_PROXY_CLIENT        0x1
+#define VFIO_PROXY_NO_MMAP       0x2
 #define VFIO_PROXY_FORCE_QUEUED  0x4
 #define VFIO_PROXY_NO_POST       0x8
 
hw/vfio/user-pci.c +5 -0
index a0aa320..bf84d7c 100644
@@ -40,6 +40,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI)
 struct VFIOUserPCIDevice {
     VFIOPCIDevice device;
     char *sock_name;
+    bool no_direct_dma; /* disable shared mem for DMA */
     bool send_queued;   /* all sends are queued */
     bool no_post;       /* all regions write are sync */
 };
@@ -160,6 +161,9 @@ 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->no_direct_dma) {
+        proxy->flags |= VFIO_PROXY_NO_MMAP;
+    }
     if (udev->send_queued) {
         proxy->flags |= VFIO_PROXY_FORCE_QUEUED;
     }
@@ -269,6 +273,7 @@ static void vfio_user_instance_finalize(Object *obj)
 
 static Property vfio_user_pci_dev_properties[] = {
     DEFINE_PROP_STRING("socket", VFIOUserPCIDevice, sock_name),
+    DEFINE_PROP_BOOL("no-direct-dma", VFIOUserPCIDevice, no_direct_dma, false),
     DEFINE_PROP_BOOL("x-send-queued", VFIOUserPCIDevice, send_queued, false),
     DEFINE_PROP_BOOL("x-no-posted-writes", VFIOUserPCIDevice, no_post, false),
     DEFINE_PROP_END_OF_LIST(),
hw/vfio/user.c +1 -1
index fe6e476..0a7b354 100644
@@ -1806,7 +1806,7 @@ static int vfio_user_io_dma_map(VFIOContainer *container, MemoryRegion *mr,
      * map->vaddr enters as a QEMU process address
      * make it either a file offset for mapped areas or 0
      */
-    if (fd != -1) {
+    if (fd != -1 && (container->proxy->flags & VFIO_PROXY_NO_MMAP) == 0) {
         void *addr = (void *)(uintptr_t)map->vaddr;
 
         map->vaddr = qemu_ram_block_host_offset(mr->ram_block, addr);
Signature · 1 line
1.9.4
#34 lore raw

PATCH v2 20/23 vfio-user: dma read/write operations

In reply to John Johnson

Messages from server to client that peform device DMA.

---
 hw/vfio/user-protocol.h |  11 +++++
 hw/vfio/user.h          |   3 ++
 hw/vfio/user-pci.c      | 110 ++++++++++++++++++++++++++++++++++++++++++++++++
 hw/vfio/user.c          |  57 +++++++++++++++++++++++++
 4 files changed, 181 insertions(+)
hw/vfio/user-protocol.h +11 -0
index 109076d..1a40cca 100644
@@ -201,6 +201,17 @@ typedef struct {
     char data[];
 } VFIOUserRegionRW;
 
+/*
+ * 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;
hw/vfio/user.h +3 -0
index fe0115b..ae7654f 100644
@@ -97,6 +97,9 @@ VFIOGroup *vfio_user_get_group(VFIOUserProxy *proxy, AddressSpace *as,
                                Error **errp);
 void vfio_user_put_group(VFIOGroup *group);
 int vfio_user_validate_version(VFIOUserProxy *proxy, Error **errp);
+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_putfds(VFIOUserMsg *msg);
 
 extern VFIODeviceIO vfio_dev_io_sock;
 extern VFIOContainerIO vfio_cont_io_sock;
hw/vfio/user-pci.c +110 -0

123 lines not on this page · raw message

hw/vfio/user.c +57 -0

69 lines not on this page · raw message

Signature · 1 line
1.9.4
#35 lore raw

PATCH v2 21/23 vfio-user: pci reset

In reply to John Johnson

Message to tell the server to reset the device.

---
 hw/vfio/pci.h      |  2 ++
 hw/vfio/user.h     |  1 +
 hw/vfio/pci.c      |  4 ++--
 hw/vfio/user-pci.c | 15 +++++++++++++++
 hw/vfio/user.c     | 12 ++++++++++++
 5 files changed, 32 insertions(+), 2 deletions(-)
hw/vfio/pci.h +2 -0
index d3e5d5f..3607c6e 100644
@@ -218,6 +218,8 @@ void vfio_teardown_msi(VFIOPCIDevice *vdev);
 void vfio_bars_exit(VFIOPCIDevice *vdev);
 void vfio_bars_finalize(VFIOPCIDevice *vdev);
 int vfio_add_capabilities(VFIOPCIDevice *vdev, Error **errp);
+void vfio_pci_pre_reset(VFIOPCIDevice *vdev);
+void vfio_pci_post_reset(VFIOPCIDevice *vdev);
 void vfio_put_device(VFIOPCIDevice *vdev);
 void vfio_register_err_notifier(VFIOPCIDevice *vdev);
 void vfio_register_req_notifier(VFIOPCIDevice *vdev);
hw/vfio/user.h +1 -0
index ae7654f..b7a9f57 100644
@@ -100,6 +100,7 @@ int vfio_user_validate_version(VFIOUserProxy *proxy, Error **errp);
 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_putfds(VFIOUserMsg *msg);
+void vfio_user_reset(VFIOUserProxy *proxy);
 
 extern VFIODeviceIO vfio_dev_io_sock;
 extern VFIOContainerIO vfio_cont_io_sock;
hw/vfio/pci.c +2 -2
index 7b16f8f..52fbfe6 100644
@@ -2225,7 +2225,7 @@ int vfio_add_capabilities(VFIOPCIDevice *vdev, Error **errp)
     return 0;
 }
 
-static void vfio_pci_pre_reset(VFIOPCIDevice *vdev)
+void vfio_pci_pre_reset(VFIOPCIDevice *vdev)
 {
     PCIDevice *pdev = &vdev->pdev;
     uint16_t cmd;
@@ -2262,7 +2262,7 @@ static void vfio_pci_pre_reset(VFIOPCIDevice *vdev)
     vfio_pci_write_config(pdev, PCI_COMMAND, cmd, 2);
 }
 
-static void vfio_pci_post_reset(VFIOPCIDevice *vdev)
+void vfio_pci_post_reset(VFIOPCIDevice *vdev)
 {
     VFIODevice *vbasedev = &vdev->vbasedev;
     Error *err = NULL;
hw/vfio/user-pci.c +15 -0
index 6465b1c..ee018db 100644
@@ -381,6 +381,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_reset(vbasedev->proxy);
+    }
+
+    vfio_pci_post_reset(vdev);
+}
+
 static Property vfio_user_pci_dev_properties[] = {
     DEFINE_PROP_STRING("socket", VFIOUserPCIDevice, sock_name),
     DEFINE_PROP_BOOL("no-direct-dma", VFIOUserPCIDevice, no_direct_dma, false),
@@ -394,6 +408,7 @@ static void vfio_user_pci_dev_class_init(ObjectClass *klass, void *data)
     DeviceClass *dc = DEVICE_CLASS(klass);
     PCIDeviceClass *pdc = PCI_DEVICE_CLASS(klass);
 
+    dc->reset = 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;
hw/vfio/user.c +12 -0
index 3aabf6b..9b51686 100644
@@ -1781,6 +1781,18 @@ static int vfio_user_region_write(VFIOUserProxy *proxy, uint8_t index,
     return ret;
 }
 
+void vfio_user_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, false);
+    if (msg.flags & VFIO_USER_ERROR) {
+        error_printf("reset reply error %d\n", msg.error_reply);
+    }
+}
+
 
 /*
  * Socket-based io_ops
Signature · 1 line
1.9.4
#36 lore raw

PATCH v2 22/23 vfio-user: add 'x-msg-timeout' option that specifies msg wait times

In reply to John Johnson
---
 hw/vfio/user.h     | 1 +
 hw/vfio/user-pci.c | 4 ++++
 hw/vfio/user.c     | 7 ++++---
 3 files changed, 9 insertions(+), 3 deletions(-)
hw/vfio/user.h +1 -0
index b7a9f57..52b3f89 100644
@@ -62,6 +62,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 +4 -0
index ee018db..5d0b224 100644
@@ -43,6 +43,7 @@ struct VFIOUserPCIDevice {
     bool no_direct_dma; /* disable shared mem for DMA */
     bool send_queued;   /* all sends are queued */
     bool no_post;       /* all regions write are sync */
+    uint32_t wait_time; /* timeout for message replies */
 };
 
 /*
@@ -280,6 +281,8 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)
     if (udev->no_post) {
         proxy->flags |= VFIO_PROXY_NO_POST;
     }
+    /* user specified or 5 sec default */
+    proxy->wait_time = udev->wait_time;
 
     vfio_user_validate_version(proxy, &err);
     if (err != NULL) {
@@ -400,6 +403,7 @@ static Property vfio_user_pci_dev_properties[] = {
     DEFINE_PROP_BOOL("no-direct-dma", VFIOUserPCIDevice, no_direct_dma, false),
     DEFINE_PROP_BOOL("x-send-queued", VFIOUserPCIDevice, send_queued, false),
     DEFINE_PROP_BOOL("x-no-posted-writes", VFIOUserPCIDevice, no_post, false),
+    DEFINE_PROP_UINT32("x-msg-timeout", VFIOUserPCIDevice, wait_time, 5000),
     DEFINE_PROP_END_OF_LIST(),
 };
 
hw/vfio/user.c +4 -3
index 9b51686..af5471b 100644
@@ -44,7 +44,6 @@
 #define VFIO_USER_MAX_REGIONS   100
 #define VFIO_USER_MAX_IRQS      50
 
-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);
@@ -735,7 +734,8 @@ static 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;
@@ -778,7 +778,8 @@ static 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
1.9.4
#37 lore raw

PATCH v2 23/23 vfio-user: add coalesced posted writes

In reply to John Johnson

Add new message to send multiple writes to server. 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.h          |   7 +++
 hw/vfio/user.c          | 130 +++++++++++++++++++++++++++++++++++++++++++++++-
 hw/vfio/trace-events    |   1 +
 4 files changed, 157 insertions(+), 2 deletions(-)
hw/vfio/user-protocol.h +21 -0
index 1a40cca..d09c29e 100644
@@ -40,6 +40,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,
 };
 
@@ -73,6 +74,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"
@@ -219,4 +221,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.h +7 -0
index 52b3f89..a5cf969 100644
@@ -79,6 +79,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;
 
@@ -87,6 +89,11 @@ typedef struct VFIOUserProxy {
 #define VFIO_PROXY_NO_MMAP       0x2
 #define VFIO_PROXY_FORCE_QUEUED  0x4
 #define VFIO_PROXY_NO_POST       0x8
+#define VFIO_PROXY_USE_MULTI     0x10
+
+/* coalescing high and low water marks for VFIOProxy num_outgoing */
+#define VFIO_USER_OUT_HIGH       1024
+#define VFIO_USER_OUT_LOW        128
 
 VFIOUserProxy *vfio_user_connect_dev(SocketAddress *addr, Error **errp);
 void vfio_user_disconnect(VFIOUserProxy *proxy);
hw/vfio/user.c +128 -2

207 lines not on this page · raw message

hw/vfio/trace-events +1 -0
index a4e02ff..e1e9681 100644
@@ -180,3 +180,4 @@ vfio_user_get_irq_info(uint32_t index, uint32_t flags, uint32_t count) " index %
 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_dma_map(uint64_t iova, uint64_t size, uint64_t off, uint32_t flags, bool will_commit) " iova 0x%"PRIx64" size 0x%"PRIx64" off 0x%"PRIx64" flags 0x%x will_commit %d"
 vfio_user_dma_unmap(uint64_t iova, uint64_t size, uint32_t flags, bool dirty, bool will_commit) " iova 0x%"PRIx64" size 0x%"PRIx64" flags 0x%x dirty %d will_commit %d"
+vfio_user_wrmulti(const char *s, uint64_t wr_cnt) " %s count 0x%"PRIx64
Signature · 1 line
1.9.4