@samitouri / QOSamiQemu / commits / f2ac20026e

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. Cc: John Levon <john.levon@nutanix.com> Reviewed-by: John Levon <john.levon@nutanix.com> Link: https://lore.kernel.org/qemu-devel/20260409114312.1704062-1-clg@redhat.com Signed-off-by: Cédric Le Goater <clg@redhat.com>

Cédric Le Goater committed Apr 9, 2026 at 13:43 UTC f2ac20026e9b0d737f66241328d0be2cd5fea61b
3 files changed +20
hw/vfio/device.c
+2
@@ -648,6 +648,8 @@ static int vfio_device_io_region_write(VFIODevice *vbasedev, uint8_t index,
648 }
649
650 static VFIODeviceIOOps vfio_device_io_ops_ioctl = {
651 + .capabilities = VFIO_IO_CAP_DMA_BUF,
652 +
653 .device_feature = vfio_device_io_device_feature,
654 .get_region_info = vfio_device_io_get_region_info,
655 .get_irq_info = vfio_device_io_get_irq_info,
hw/vfio/region.c
+5
@@ -293,6 +293,11 @@ static bool vfio_region_create_dma_buf(VFIORegion *region, Error **errp)
293 size_t total_size;
294 int i, ret;
295
296 + /* Check if backend supports DMA-BUF creation */
297 + if (!(vbasedev->io_ops->capabilities & VFIO_IO_CAP_DMA_BUF)) {
298 + return true;
299 + }
300 +
301 total_size = sizeof(*feature) + sizeof(*dma_buf) +
302 sizeof(struct vfio_region_dma_range) * region->nr_mmaps;
303 feature = g_malloc0(total_size);
include/hw/vfio/vfio-device.h
+13
@@ -172,12 +172,25 @@ typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList;
172 extern VFIODeviceList vfio_device_list;
173
174 #ifdef CONFIG_LINUX
175 +/*
176 + * VFIO backend I/O operation capabilities
177 + */
178 +#define VFIO_IO_CAP_DMA_BUF (1ULL << 0)
179 +
180 /*
181 * How devices communicate with the server. The default option is through
182 * ioctl() to the kernel VFIO driver, but vfio-user can use a socket to a remote
183 * process.
184 */
185 struct VFIODeviceIOOps {
186 + /**
187 + * @capabilities
188 + *
189 + * Bitmask of VFIO_IO_CAP_* flags indicating which features this
190 + * backend supports.
191 + */
192 + uint64_t capabilities;
193 +
194 /**
195 * @device_feature
196 *