Skip to contentSkip to conversation

vfio: Add VFIO I/O backend capability flags for feature support

PATCH Applied Cédric Le Goater 2 participants 3 messages Apr 9 – Apr 10

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vfio: Add VFIO I/O backend capability flags for feature support

Different VFIO I/O backends support different features. For example, the kernel VFIO backend supports DMA-BUF creation, while vfio-user does not. Currently, this is handled by attempting the operation and checking for -ENOTTY, which can lead to misleading warnings when a feature is simply not supported by a particular backend.

Introduce a capability flags mechanism in VFIODeviceIOOps that allows backends to explicitly advertise which features they support. Callers can check these capabilities before attempting operations, avoiding spurious errors and warnings.

---
 include/hw/vfio/vfio-device.h | 13 +++++++++++++
 hw/vfio/device.c              |  2 ++
 hw/vfio/region.c              |  5 +++++
 3 files changed, 20 insertions(+)
include/hw/vfio/vfio-device.h +13 -0
index a95c5bf50302a7394582dcc493d96aef00b47dc8..380a55d6e5ea280c8f44016c7a8a1dcb8f4c00fe 100644
@@ -172,12 +172,25 @@ typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList;
 extern VFIODeviceList vfio_device_list;
 
 #ifdef CONFIG_LINUX
+/*
+ * VFIO backend I/O operation capabilities
+ */
+#define VFIO_IO_CAP_DMA_BUF        (1ULL << 0)
+
 /*
  * How devices 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 VFIODeviceIOOps {
+    /**
+     * @capabilities
+     *
+     * Bitmask of VFIO_IO_CAP_* flags indicating which features this
+     * backend supports.
+     */
+    uint64_t capabilities;
+
     /**
      * @device_feature
      *
hw/vfio/device.c +2 -0
index 8f7ae919a55dd9e08bdd072eee33f5b46a91aa2a..3ffd69a579b8ac135ef729d3d836c8545e36786b 100644
@@ -648,6 +648,8 @@ static int vfio_device_io_region_write(VFIODevice *vbasedev, uint8_t index,
 }
 
 static VFIODeviceIOOps vfio_device_io_ops_ioctl = {
+    .capabilities = VFIO_IO_CAP_DMA_BUF,
+
     .device_feature = vfio_device_io_device_feature,
     .get_region_info = vfio_device_io_get_region_info,
     .get_irq_info = vfio_device_io_get_irq_info,
hw/vfio/region.c +5 -0
index 47fdc2df349b65c6be6c9605b7a38a4e367f0475..0342ca712acd0ad846685115c29432c15aa966a0 100644
@@ -293,6 +293,11 @@ static bool vfio_region_create_dma_buf(VFIORegion *region, Error **errp)
     size_t total_size;
     int i, ret;
 
+    /* Check if backend supports DMA-BUF creation */
+    if (!(vbasedev->io_ops->capabilities & VFIO_IO_CAP_DMA_BUF)) {
+        return true;
+    }
+
     total_size = sizeof(*feature) + sizeof(*dma_buf) +
                  sizeof(struct vfio_region_dma_range) * region->nr_mmaps;
     feature = g_malloc0(total_size);
Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Cédric Le Goater

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

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

Different VFIO I/O backends support different features. For example, the kernel VFIO backend supports DMA-BUF creation, while vfio-user does not. Currently, this is handled by attempting the operation and checking for -ENOTTY, which can lead to misleading warnings when a feature is simply not supported by a particular backend.

Quoted 7 lines

Introduce a capability flags mechanism in VFIODeviceIOOps that allows backends to explicitly advertise which features they support. Callers can check these capabilities before attempting operations, avoiding spurious errors and warnings.

Thanks!

regards
john

author Applied
#2 lore raw
In reply to Cédric Le Goater
Quoted 68 lines · Cédric Le Goater

Different VFIO I/O backends support different features. For example, the kernel VFIO backend supports DMA-BUF creation, while vfio-user does not. Currently, this is handled by attempting the operation and checking for -ENOTTY, which can lead to misleading warnings when a feature is simply not supported by a particular backend.

Introduce a capability flags mechanism in VFIODeviceIOOps that allows backends to explicitly advertise which features they support. Callers can check these capabilities before attempting operations, avoiding spurious errors and warnings.

---
  include/hw/vfio/vfio-device.h | 13 +++++++++++++
  hw/vfio/device.c              |  2 ++
  hw/vfio/region.c              |  5 +++++
  3 files changed, 20 insertions(+)
include/hw/vfio/vfio-device.h +13 -0
index a95c5bf50302a7394582dcc493d96aef00b47dc8..380a55d6e5ea280c8f44016c7a8a1dcb8f4c00fe 100644
@@ -172,12 +172,25 @@ typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList;
  extern VFIODeviceList vfio_device_list;
 
  #ifdef CONFIG_LINUX
+/*
+ * VFIO backend I/O operation capabilities
+ */
+#define VFIO_IO_CAP_DMA_BUF        (1ULL << 0)
+
  /*
   * How devices 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 VFIODeviceIOOps {
+    /**
+     * @capabilities
+     *
+     * Bitmask of VFIO_IO_CAP_* flags indicating which features this
+     * backend supports.
+     */
+    uint64_t capabilities;
+
      /**
       * @device_feature
       *
hw/vfio/device.c +2 -0
index 8f7ae919a55dd9e08bdd072eee33f5b46a91aa2a..3ffd69a579b8ac135ef729d3d836c8545e36786b 100644
@@ -648,6 +648,8 @@ static int vfio_device_io_region_write(VFIODevice *vbasedev, uint8_t index,
  }
 
  static VFIODeviceIOOps vfio_device_io_ops_ioctl = {
+    .capabilities = VFIO_IO_CAP_DMA_BUF,
+
      .device_feature = vfio_device_io_device_feature,
      .get_region_info = vfio_device_io_get_region_info,
      .get_irq_info = vfio_device_io_get_irq_info,
hw/vfio/region.c +0 -0
index 47fdc2df349b65c6be6c9605b7a38a4e367f0475..0342ca712acd0ad846685115c29432c15aa966a0 100644
@@ -293,6 +293,11 @@ static bool vfio_region_create_dma_buf(VFIORegion *region, Error **errp)
      size_t total_size;

hw/vfio/region.c static bool vfio_region_create_dma_buf(VFIORegion *region, Error **errp) · line 303

Quoted 10 lines
hw/vfio/region.c +5 -0
      int i, ret;
 
+    /* Check if backend supports DMA-BUF creation */
+    if (!(vbasedev->io_ops->capabilities & VFIO_IO_CAP_DMA_BUF)) {
+        return true;
+    }
+
      total_size = sizeof(*feature) + sizeof(*dma_buf) +
                   sizeof(struct vfio_region_dma_range) * region->nr_mmaps;
      feature = g_malloc0(total_size);

Applied to vfio-next.

Thanks,

C.