Instead of iterating over all QOM devices to find the VFIODevice associated with a memory region, it is faster to just use the vfio_device_list to lookup the VFIODevice.
Cc: Alex Williamson <[email protected]> Cc: Cédric Le Goater <[email protected]> Signed-off-by: Vivek Kasireddy <[email protected]> --- hw/vfio/device.c | 12 ++++++++++++ include/hw/vfio/vfio-device.h | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/hw/vfio/device.c b/hw/vfio/device.c index 76869828fc..9ff73f9941 100644 --- a/hw/vfio/device.c +++ b/hw/vfio/device.c @@ -603,3 +603,15 @@ static VFIODeviceIOOps vfio_device_io_ops_ioctl = { .region_read = vfio_device_io_region_read, .region_write = vfio_device_io_region_write, }; + +VFIODevice *vfio_device_lookup(MemoryRegion *mr) +{ + VFIODevice *vbasedev; + + QLIST_FOREACH(vbasedev, &vfio_device_list, next) { + if (vbasedev->dev == mr->dev) { + return vbasedev; + } + } + return NULL; +} diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h index 44cacd3728..2f8087f133 100644 --- a/include/hw/vfio/vfio-device.h +++ b/include/hw/vfio/vfio-device.h @@ -300,6 +300,15 @@ int vfio_device_get_irq_info(VFIODevice *vbasedev, int index, * Returns the region index or -1 on error. */ int vfio_get_region_index_from_mr(MemoryRegion *mr); + +/** + * Return the VFIO device associated with a given MemoryRegion. + * + * @mr: MemoryRegion to use + * + * Returns the VFIO device if found or NULL. + */ +VFIODevice *vfio_device_lookup(MemoryRegion *mr); #endif /* Returns 0 on success, or a negative errno. */ -- 2.50.1
