vfio/pci: Extract mdev check into an helper

In preparation to skip initialization of the HostIOMMUDevice for mdev,
extract the checks that validate if a device is an mdev into helpers.

A vfio_device_is_mdev() is created, and subsystems consult VFIODevice::mdev
to check if it's mdev or not.

Signed-off-by: Joao Martins <joao.m.martins@oracle.com>
Reviewed-by: Cédric Le Goater <clg@redhat.com>
Reviewed-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
Reviewed-by: Eric Auger <eric.auger@redhat.com>
This commit is contained in:
Joao Martins 2024-07-19 13:04:49 +01:00 committed by Cédric Le Goater
parent 07321a6d08
commit 13e522f644
3 changed files with 19 additions and 9 deletions

View file

@ -675,3 +675,17 @@ int vfio_device_get_aw_bits(VFIODevice *vdev)
return HOST_IOMMU_DEVICE_CAP_AW_BITS_MAX; return HOST_IOMMU_DEVICE_CAP_AW_BITS_MAX;
} }
bool vfio_device_is_mdev(VFIODevice *vbasedev)
{
g_autofree char *subsys = NULL;
g_autofree char *tmp = NULL;
if (!vbasedev->sysfsdev) {
return false;
}
tmp = g_strdup_printf("%s/subsystem", vbasedev->sysfsdev);
subsys = realpath(tmp, NULL);
return subsys && (strcmp(subsys, "/sys/bus/mdev") == 0);
}

View file

@ -2963,12 +2963,9 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
ERRP_GUARD(); ERRP_GUARD();
VFIOPCIDevice *vdev = VFIO_PCI(pdev); VFIOPCIDevice *vdev = VFIO_PCI(pdev);
VFIODevice *vbasedev = &vdev->vbasedev; VFIODevice *vbasedev = &vdev->vbasedev;
char *subsys;
int i, ret; int i, ret;
bool is_mdev;
char uuid[UUID_STR_LEN]; char uuid[UUID_STR_LEN];
g_autofree char *name = NULL; g_autofree char *name = NULL;
g_autofree char *tmp = NULL;
if (vbasedev->fd < 0 && !vbasedev->sysfsdev) { if (vbasedev->fd < 0 && !vbasedev->sysfsdev) {
if (!(~vdev->host.domain || ~vdev->host.bus || if (!(~vdev->host.domain || ~vdev->host.bus ||
@ -2997,14 +2994,11 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
* stays in sync with the active working set of the guest driver. Prevent * stays in sync with the active working set of the guest driver. Prevent
* the x-balloon-allowed option unless this is minimally an mdev device. * the x-balloon-allowed option unless this is minimally an mdev device.
*/ */
tmp = g_strdup_printf("%s/subsystem", vbasedev->sysfsdev); vbasedev->mdev = vfio_device_is_mdev(vbasedev);
subsys = realpath(tmp, NULL);
is_mdev = subsys && (strcmp(subsys, "/sys/bus/mdev") == 0);
free(subsys);
trace_vfio_mdev(vbasedev->name, is_mdev); trace_vfio_mdev(vbasedev->name, vbasedev->mdev);
if (vbasedev->ram_block_discard_allowed && !is_mdev) { if (vbasedev->ram_block_discard_allowed && !vbasedev->mdev) {
error_setg(errp, "x-balloon-allowed only potentially compatible " error_setg(errp, "x-balloon-allowed only potentially compatible "
"with mdev devices"); "with mdev devices");
goto error; goto error;

View file

@ -116,6 +116,7 @@ typedef struct VFIODevice {
DeviceState *dev; DeviceState *dev;
int fd; int fd;
int type; int type;
bool mdev;
bool reset_works; bool reset_works;
bool needs_reset; bool needs_reset;
bool no_mmap; bool no_mmap;
@ -231,6 +232,7 @@ void vfio_region_exit(VFIORegion *region);
void vfio_region_finalize(VFIORegion *region); void vfio_region_finalize(VFIORegion *region);
void vfio_reset_handler(void *opaque); void vfio_reset_handler(void *opaque);
struct vfio_device_info *vfio_get_device_info(int fd); struct vfio_device_info *vfio_get_device_info(int fd);
bool vfio_device_is_mdev(VFIODevice *vbasedev);
bool vfio_attach_device(char *name, VFIODevice *vbasedev, bool vfio_attach_device(char *name, VFIODevice *vbasedev,
AddressSpace *as, Error **errp); AddressSpace *as, Error **errp);
void vfio_detach_device(VFIODevice *vbasedev); void vfio_detach_device(VFIODevice *vbasedev);