vfio queue
[PULL 01/16] hw/vfio/container: Fix SIGSEV on vfio_container_instance_finalize()
In reply to Cédric Le GoaterFrom: Eric Auger <[email protected]>
In vfio_connect_container's error path, the base container is removed twice form the VFIOAddressSpace QLIST: first on the listener_release_exit label and second, on free_container_exit label, through object_unref(container), which calls vfio_container_instance_finalize().
Let's remove the first instance.
--- hw/vfio/container.c | 1 - 1 file changed, 1 deletion(-)
hw/vfio/container.c +0 -1
@@ -656,7 +656,6 @@ static bool vfio_connect_container(VFIOGroup *group, AddressSpace *as,return true; listener_release_exit:QLIST_REMOVE (group, container_next); -QLIST_REMOVE (bcontainer, next);vfio_kvm_device_del_group (group);memory_listener_unregister (&bcontainer->listener);if (vioc->release) {
Signature · 1 line
2.45.2
[PULL 02/16] vfio/pci: Extract mdev check into an helper
In reply to Cédric Le GoaterFrom: Joao Martins <[email protected]>
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.
--- include/hw/vfio/vfio-common.h | 2 ++ hw/vfio/helpers.c | 14 ++++++++++++++ hw/vfio/pci.c | 12 +++--------- 3 files changed, 19 insertions(+), 9 deletions(-)
include/hw/vfio/vfio-common.h +2 -0
@@ -116,6 +116,7 @@ typedef struct VFIODevice {DeviceState *dev;int fd;int type; +bool mdev;bool reset_works;bool needs_reset;bool no_mmap;
@@ -231,6 +232,7 @@ void vfio_region_exit(VFIORegion *region);void vfio_region_finalize (VFIORegion *region);void vfio_reset_handler (void *opaque);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,AddressSpace *as,Error **errp);void vfio_detach_device (VFIODevice *vbasedev);
hw/vfio/helpers.c +14 -0
@@ -675,3 +675,17 @@ int vfio_device_get_aw_bits(VFIODevice *vdev)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 ); +}
hw/vfio/pci.c +3 -9
@@ -2963,12 +2963,9 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)ERRP_GUARD ();VFIOPCIDevice *vdev =VFIO_PCI (pdev);VFIODevice *vbasedev = &vdev->vbasedev; -char *subsys;int i, ret; -bool is_mdev;char uuid[UUID_STR_LEN ]; g_autofree char *name = NULL; - g_autofree char *tmp = NULL;if (vbasedev->fd <0 && !vbasedev->sysfsdev) { 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 * the x-balloon-allowed option unless this is minimally an mdev device. */ - tmp =g_strdup_printf ("%s/subsystem" , vbasedev->sysfsdev); - subsys =realpath (tmp,NULL ); - is_mdev = subsys && (strcmp (subsys,"/sys/bus/mdev" ) ==0 ); -free (subsys); + vbasedev->mdev =vfio_device_is_mdev (vbasedev); -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 " "with mdev devices" ); goto error;
Signature · 1 line
2.45.2
[PULL 04/16] backends/iommufd: Extend iommufd_backend_get_device_info() to fetch HW capabilities
In reply to Cédric Le GoaterFrom: Joao Martins <[email protected]>
The helper will be able to fetch vendor agnostic IOMMU capabilities supported both by hardware and software. Right now it is only iommu dirty tracking.
--- include/sysemu/iommufd.h | 2 +- backends/iommufd.c | 4 +++- hw/vfio/iommufd.c | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-)
include/sysemu/iommufd.h +1 -1
@@ -49,7 +49,7 @@ int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id, hwaddr iova, ram_addr_t size);bool iommufd_backend_get_device_info (IOMMUFDBackend *be,uint32_t devid,uint32_t *type,void *data,uint32_t len, -Error **errp); +uint64_t *caps,Error **errp);#define TYPE_HOST_IOMMU_DEVICE_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd"#endif
backends/iommufd.c +3 -1
@@ -209,7 +209,7 @@ int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id,bool iommufd_backend_get_device_info (IOMMUFDBackend *be,uint32_t devid,uint32_t *type,void *data,uint32_t len, -Error **errp) +uint64_t *caps,Error **errp) {struct iommu_hw_info info = { .size =sizeof (info),
@@ -225,6 +225,8 @@ bool iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,g_assert (type); *type = info.out_data_type; +g_assert (caps); + *caps = info.out_capabilities;return true; }
hw/vfio/iommufd.c +3 -1
@@ -628,11 +628,13 @@ static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,union {struct iommu_hw_info_vtd vtd; } data; +uint64_t hw_caps; hiod->agent = opaque;if (!iommufd_backend_get_device_info (vdev->iommufd, vdev->devid, - &type, &data,sizeof (data), errp)) { + &type, &data,sizeof (data), + &hw_caps, errp)) {return false; }
Signature · 1 line
2.45.2
[PULL 05/16] vfio/iommufd: Return errno in iommufd_cdev_attach_ioas_hwpt()
In reply to Cédric Le GoaterFrom: Joao Martins <[email protected]>
In preparation to implement auto domains have the attach function return the errno it got during domain attach instead of a bool.
-EINVAL is tracked to track domain incompatibilities, and decide whether to create a new IOMMU domain.
--- hw/vfio/iommufd.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
hw/vfio/iommufd.c +4 -4
@@ -172,7 +172,7 @@ out:return ret; } -static bool iommufd_cdev_attach_ioas_hwpt (VFIODevice *vbasedev,uint32_t id, +static int iommufd_cdev_attach_ioas_hwpt (VFIODevice *vbasedev,uint32_t id,Error **errp) {int iommufd = vbasedev->iommufd->fd;
@@ -187,12 +187,12 @@ static bool iommufd_cdev_attach_ioas_hwpt(VFIODevice *vbasedev, uint32_t id,error_setg_errno (errp, errno,"[iommufd=%d] error attach %s (%d) to id=%d" , iommufd, vbasedev->name, vbasedev->fd, id); -return false; +return -errno; }trace_iommufd_cdev_attach_ioas_hwpt (iommufd, vbasedev->name, vbasedev->fd, id); -return true; +return 0 ; }static bool iommufd_cdev_detach_ioas_hwpt (VFIODevice *vbasedev,Error **errp)
@@ -216,7 +216,7 @@ static bool iommufd_cdev_attach_container(VFIODevice *vbasedev, VFIOIOMMUFDContainer *container, Error **errp) { -return iommufd_cdev_attach_ioas_hwpt (vbasedev, container->ioas_id, errp); +return !iommufd_cdev_attach_ioas_hwpt (vbasedev, container->ioas_id, errp); } static void iommufd_cdev_detach_container(VFIODevice *vbasedev,
Signature · 1 line
2.45.2
[PULL 08/16] vfio/iommufd: Introduce auto domain creation
In reply to Cédric Le GoaterFrom: Joao Martins <[email protected]>
There's generally two modes of operation for IOMMUFD:
1) The simple user API which intends to perform relatively simple things with IOMMUs e.g. DPDK. The process generally creates an IOAS and attaches to VFIO and mainly performs IOAS_MAP and UNMAP.
2) The native IOMMUFD API where you have fine grained control of the IOMMU domain and model it accordingly. This is where most new feature are being steered to.
For dirty tracking 2) is required, as it needs to ensure that the stage-2/parent IOMMU domain will only attach devices that support dirty tracking (so far it is all homogeneous in x86, likely not the case for smmuv3). Such invariant on dirty tracking provides a useful guarantee to VMMs that will refuse incompatible device attachments for IOMMU domains.
Dirty tracking insurance is enforced via HWPT_ALLOC, which is responsible for creating an IOMMU domain. This is contrast to the 'simple API' where the IOMMU domain is created by IOMMUFD automatically when it attaches to VFIO (usually referred as autodomains) but it has the needed handling for mdevs.
To support dirty tracking with the advanced IOMMUFD API, it needs similar logic, where IOMMU domains are created and devices attached to compatible domains. Essentially mimicking kernel iommufd_device_auto_get_domain(). With mdevs given there's no IOMMU domain it falls back to IOAS attach.
The auto domain logic allows different IOMMU domains to be created when DMA dirty tracking is not desired (and VF can provide it), and others where it is. Here it is not used in this way given how VFIODevice migration state is initialized after the device attachment. But such mixed mode of IOMMU dirty tracking + device dirty tracking is an improvement that can be added on. Keep the 'all of nothing' of type1 approach that we have been using so far between container vs device dirty tracking.
--- include/hw/vfio/vfio-common.h | 9 ++++ include/sysemu/iommufd.h | 5 +++ backends/iommufd.c | 30 +++++++++++++ hw/vfio/iommufd.c | 85 +++++++++++++++++++++++++++++++++++ backends/trace-events | 1 + 5 files changed, 130 insertions(+)
include/hw/vfio/vfio-common.h +9 -0
@@ -95,10 +95,17 @@ typedef struct VFIOHostDMAWindow {typedef struct IOMMUFDBackend IOMMUFDBackend ; +typedef struct VFIOIOASHwpt { +uint32_t hwpt_id; + QLIST_HEAD(, VFIODevice) device_list; +QLIST_ENTRY (VFIOIOASHwpt) next; +}VFIOIOASHwpt ; +typedef struct VFIOIOMMUFDContainer {VFIOContainerBase bcontainer;IOMMUFDBackend *be;uint32_t ioas_id; + QLIST_HEAD(, VFIOIOASHwpt) hwpt_list; }VFIOIOMMUFDContainer ;OBJECT_DECLARE_SIMPLE_TYPE (VFIOIOMMUFDContainer,VFIO_IOMMU_IOMMUFD );
@@ -135,6 +142,8 @@ typedef struct VFIODevice {HostIOMMUDevice *hiod;int devid;IOMMUFDBackend *iommufd; +VFIOIOASHwpt *hwpt; +QLIST_ENTRY (VFIODevice) hwpt_next; } VFIODevice;struct VFIODeviceOps {
include/sysemu/iommufd.h +5 -0
@@ -50,6 +50,11 @@ int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id,bool iommufd_backend_get_device_info (IOMMUFDBackend *be,uint32_t devid,uint32_t *type,void *data,uint32_t len,uint64_t *caps,Error **errp); +bool iommufd_backend_alloc_hwpt (IOMMUFDBackend *be,uint32_t dev_id, +uint32_t pt_id,uint32_t flags, +uint32_t data_type,uint32_t data_len, +void *data_ptr,uint32_t *out_hwpt, +Error **errp);#define TYPE_HOST_IOMMU_DEVICE_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd"#endif
backends/iommufd.c +30 -0
@@ -207,6 +207,36 @@ int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id,return ret; } +bool iommufd_backend_alloc_hwpt (IOMMUFDBackend *be,uint32_t dev_id, +uint32_t pt_id,uint32_t flags, +uint32_t data_type,uint32_t data_len, +void *data_ptr,uint32_t *out_hwpt, +Error **errp) +{ +int ret, fd = be->fd; +struct iommu_hwpt_alloc alloc_hwpt = { + .size =sizeof (struct iommu_hwpt_alloc ), + .flags = flags, + .dev_id = dev_id, + .pt_id = pt_id, + .data_type = data_type, + .data_len = data_len, + .data_uptr = (uintptr_t )data_ptr, + }; + + ret =ioctl (fd,IOMMU_HWPT_ALLOC , &alloc_hwpt); +trace_iommufd_backend_alloc_hwpt (fd, dev_id, pt_id, flags, data_type, + data_len, (uintptr_t )data_ptr, + alloc_hwpt.out_hwpt_id, ret); +if (ret) { +error_setg_errno (errp, errno,"Failed to allocate hwpt" ); +return false; + } + + *out_hwpt = alloc_hwpt.out_hwpt_id; +return true; +} +bool iommufd_backend_get_device_info (IOMMUFDBackend *be,uint32_t devid,uint32_t *type,void *data,uint32_t len,uint64_t *caps,Error **errp)
hw/vfio/iommufd.c +85 -0
107 lines not on this page · raw message
backends/trace-events +1 -0
@@ -14,4 +14,5 @@ iommufd_backend_map_dma(int iommufd, uint32_t ioas, uint64_t iova, uint64_t size iommufd_backend_unmap_dma_non_exist(int iommufd, uint32_t ioas, uint64_t iova, uint64_t size, int ret) " Unmap nonexistent mapping: iommufd=%d ioas=%d iova=0x%"PRIx64" size=0x%"PRIx64" (%d)" iommufd_backend_unmap_dma(int iommufd, uint32_t ioas, uint64_t iova, uint64_t size, int ret) " iommufd=%d ioas=%d iova=0x%"PRIx64" size=0x%"PRIx64" (%d)" iommufd_backend_alloc_ioas(int iommufd, uint32_t ioas) " iommufd=%d ioas=%d" +iommufd_backend_alloc_hwpt(int iommufd, uint32_t dev_id, uint32_t pt_id, uint32_t flags, uint32_t hwpt_type, uint32_t len, uint64_t data_ptr, uint32_t out_hwpt_id, int ret) " iommufd=%d dev_id=%u pt_id=%u flags=0x%x hwpt_type=%u len=%u data_ptr=0x%"PRIx64" out_hwpt=%u (%d)" iommufd_backend_free_id(int iommufd, uint32_t id, int ret) " iommufd=%d id=%d (%d)"
Signature · 1 line
2.45.2
The following changes since commit 6af69d02706c821797802cfd56acdac13a7c9422:
Merge tag 'nvme-next-pull-request' of https://gitlab.com/birkelund/qemu into staging (2024-07-23 13:55:45 +1000)
are available in the Git repository at:
https://github.com/legoater/qemu/ tags/pull-vfio-20240723
for you to fetch changes up to 6ac9efe6805af60de14481fdde7d340080d38324:
vfio/common: Allow disabling device dirty page tracking (2024-07-23 11:10:10 +0200)
----------------------------------------------------------------
vfio queue:
* IOMMUFD Dirty Tracking support
* Fix for a possible SEGV in IOMMU type1 container
* Dropped initialization of host IOMMU device with mdev devices
----------------------------------------------------------------
Eric Auger (1):
hw/vfio/container: Fix SIGSEV on vfio_container_instance_finalize()
Joao Martins (13):
vfio/pci: Extract mdev check into an helper
vfio/iommufd: Don't initialize nor set a HOST_IOMMU_DEVICE with mdev backends/iommufd: Extend iommufd_backend_get_device_info() to fetch HW capabilities vfio/iommufd: Return errno in iommufd_cdev_attach_ioas_hwpt() vfio/iommufd: Introduce auto domain creation vfio/{iommufd,container}: Remove caps::aw_bits
vfio/iommufd: Add hw_caps field to HostIOMMUDeviceCaps
vfio/{iommufd, container}: Invoke HostIOMMUDevice::realize() during attach_device() vfio/iommufd: Probe and request hwpt dirty tracking capability vfio/iommufd: Implement VFIOIOMMUClass::set_dirty_tracking support vfio/iommufd: Implement VFIOIOMMUClass::query_dirty_bitmap support vfio/migration: Don't block migration device dirty tracking is unsupported vfio/common: Allow disabling device dirty page tracking
Zhenzhong Duan (2):
vfio/ap: Don't initialize HOST_IOMMU_DEVICE with mdev vfio/ccw: Don't initialize HOST_IOMMU_DEVICE with mdev
include/hw/vfio/vfio-common.h | 15 +++ include/sysemu/host_iommu_device.h | 5 +- include/sysemu/iommufd.h | 13 ++- backends/iommufd.c | 89 ++++++++++++++++- hw/vfio/ap.c | 3 + hw/vfio/ccw.c | 3 + hw/vfio/common.c | 17 ++-- hw/vfio/container.c | 10 +- hw/vfio/helpers.c | 25 +++++ hw/vfio/iommufd.c | 196 +++++++++++++++++++++++++++++++++++-- hw/vfio/migration.c | 12 ++- hw/vfio/pci.c | 26 ++--- backends/trace-events | 3 + 13 files changed, 377 insertions(+), 40 deletions(-)
[PULL 13/16] vfio/iommufd: Implement VFIOIOMMUClass::set_dirty_tracking support
From: Joao Martins <[email protected]>
ioctl(iommufd, IOMMU_HWPT_SET_DIRTY_TRACKING, arg) is the UAPI that enables or disables dirty page tracking. The ioctl is used if the hwpt has been created with dirty tracking supported domain (stored in hwpt::flags) and it is called on the whole list of iommu domains.
--- include/sysemu/iommufd.h | 2 ++ backends/iommufd.c | 23 +++++++++++++++++++++++ hw/vfio/iommufd.c | 32 ++++++++++++++++++++++++++++++++ backends/trace-events | 1 + 4 files changed, 58 insertions(+)
include/sysemu/iommufd.h +2 -0
@@ -55,6 +55,8 @@ bool iommufd_backend_alloc_hwpt(IOMMUFDBackend *be, uint32_t dev_id, uint32_t data_type, uint32_t data_len, void *data_ptr, uint32_t *out_hwpt, Error **errp); +bool iommufd_backend_set_dirty_tracking (IOMMUFDBackend *be,uint32_t hwpt_id, +bool start,Error **errp);#define TYPE_HOST_IOMMU_DEVICE_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd"#endif
backends/iommufd.c +23 -0
@@ -238,6 +238,29 @@ bool iommufd_backend_alloc_hwpt(IOMMUFDBackend *be, uint32_t dev_id,return true; } +bool iommufd_backend_set_dirty_tracking (IOMMUFDBackend *be, +uint32_t hwpt_id,bool start, +Error **errp) +{ +int ret; +struct iommu_hwpt_set_dirty_tracking set_dirty = { + .size =sizeof (set_dirty), + .hwpt_id = hwpt_id, + .flags = start ?IOMMU_HWPT_DIRTY_TRACKING_ENABLE :0 , + }; + + ret =ioctl (be->fd,IOMMU_HWPT_SET_DIRTY_TRACKING , &set_dirty); +trace_iommufd_backend_set_dirty (be->fd, hwpt_id, start, ret ? errno :0 ); +if (ret) { +error_setg_errno (errp, errno, +"IOMMU_HWPT_SET_DIRTY_TRACKING(hwpt_id %u) failed" , + hwpt_id); +return false; + } + +return true; +} +bool iommufd_backend_get_device_info (IOMMUFDBackend *be,uint32_t devid,uint32_t *type,void *data,uint32_t len,uint64_t *caps,Error **errp)
hw/vfio/iommufd.c +32 -0
@@ -115,6 +115,37 @@ static bool iommufd_hwpt_dirty_tracking(VFIOIOASHwpt *hwpt)return hwpt && hwpt->hwpt_flags &IOMMU_HWPT_ALLOC_DIRTY_TRACKING ; } +static int iommufd_set_dirty_page_tracking (const VFIOContainerBase *bcontainer, +bool start,Error **errp) +{ +const VFIOIOMMUFDContainer *container = +container_of (bcontainer, VFIOIOMMUFDContainer, bcontainer); +VFIOIOASHwpt *hwpt; + + QLIST_FOREACH(hwpt, &container->hwpt_list, next) { +if (!iommufd_hwpt_dirty_tracking (hwpt)) { +continue ; + } + +if (!iommufd_backend_set_dirty_tracking (container->be, + hwpt->hwpt_id, start, errp)) { + goto err; + } + } + +return 0 ; + +err: + QLIST_FOREACH(hwpt, &container->hwpt_list, next) { +if (!iommufd_hwpt_dirty_tracking (hwpt)) { +continue ; + } +iommufd_backend_set_dirty_tracking (container->be, + hwpt->hwpt_id, !start,NULL ); + } +return -EINVAL ; +} +static int iommufd_cdev_getfd (const char *sysfs_path,Error **errp) {ERRP_GUARD ();
@@ -739,6 +770,7 @@ static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data) vioc->attach_device = iommufd_cdev_attach; vioc->detach_device = iommufd_cdev_detach; vioc->pci_hot_reset = iommufd_cdev_pci_hot_reset; + vioc->set_dirty_page_tracking = iommufd_set_dirty_page_tracking; }; static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,
backends/trace-events +1 -0
@@ -16,3 +16,4 @@ iommufd_backend_unmap_dma(int iommufd, uint32_t ioas, uint64_t iova, uint64_t si iommufd_backend_alloc_ioas(int iommufd, uint32_t ioas) " iommufd=%d ioas=%d" iommufd_backend_alloc_hwpt(int iommufd, uint32_t dev_id, uint32_t pt_id, uint32_t flags, uint32_t hwpt_type, uint32_t len, uint64_t data_ptr, uint32_t out_hwpt_id, int ret) " iommufd=%d dev_id=%u pt_id=%u flags=0x%x hwpt_type=%u len=%u data_ptr=0x%"PRIx64" out_hwpt=%u (%d)" iommufd_backend_free_id(int iommufd, uint32_t id, int ret) " iommufd=%d id=%d (%d)" +iommufd_backend_set_dirty(int iommufd, uint32_t hwpt_id, bool start, int ret) " iommufd=%d hwpt=%u enable=%d (%d)"
Signature · 1 line
2.45.2
[PULL 11/16] vfio/{iommufd, container}: Invoke HostIOMMUDevice::realize() during attach_device()
In reply to Cédric Le GoaterFrom: Joao Martins <[email protected]>
Move the HostIOMMUDevice::realize() to be invoked during the attach of the device before we allocate IOMMUFD hardware pagetable objects (HWPT). This allows the use of the hw_caps obtained by IOMMU_GET_HW_INFO that essentially tell if the IOMMU behind the device supports dirty tracking.
Note: The HostIOMMUDevice data from legacy backend is static and doesn't need any information from the (type1-iommu) backend to be initialized. In contrast however, the IOMMUFD HostIOMMUDevice data requires the iommufd FD to be connected and having a devid to be able to successfully GET_HW_INFO. This means vfio_device_hiod_realize() is called in different places within the backend .attach_device() implementation.
--- include/hw/vfio/vfio-common.h | 1 + hw/vfio/common.c | 16 ++++++---------- hw/vfio/container.c | 4 ++++ hw/vfio/helpers.c | 11 +++++++++++ hw/vfio/iommufd.c | 11 +++++++++++ 5 files changed, 33 insertions(+), 10 deletions(-)
include/hw/vfio/vfio-common.h +1 -0
@@ -242,6 +242,7 @@ void vfio_region_finalize(VFIORegion *region);void vfio_reset_handler (void *opaque);struct vfio_device_info *vfio_get_device_info (int fd);bool vfio_device_is_mdev (VFIODevice *vbasedev); +bool vfio_device_hiod_realize (VFIODevice *vbasedev,Error **errp);bool vfio_attach_device (char *name,VFIODevice *vbasedev,AddressSpace *as,Error **errp);void vfio_detach_device (VFIODevice *vbasedev);
hw/vfio/common.c +6 -10
@@ -1536,7 +1536,7 @@ bool vfio_attach_device(char *name, VFIODevice *vbasedev, {const VFIOIOMMUClass *ops =VFIO_IOMMU_CLASS (object_class_by_name (TYPE_VFIO_IOMMU_LEGACY )); -HostIOMMUDevice *hiod; +HostIOMMUDevice *hiod =NULL ;if (vbasedev->iommufd) { ops =VFIO_IOMMU_CLASS (object_class_by_name (TYPE_VFIO_IOMMU_IOMMUFD ));
@@ -1544,21 +1544,17 @@ bool vfio_attach_device(char *name, VFIODevice *vbasedev,assert (ops); -if (!ops->attach_device (name, vbasedev, as, errp)) { -return false; - } -if (vbasedev->mdev) { -return true; +if (!vbasedev->mdev) { + hiod =HOST_IOMMU_DEVICE (object_new (ops->hiod_typename)); + vbasedev->hiod = hiod; } - hiod =HOST_IOMMU_DEVICE (object_new (ops->hiod_typename)); -if (!HOST_IOMMU_DEVICE_GET_CLASS (hiod)->realize (hiod, vbasedev, errp)) { +if (!ops->attach_device (name, vbasedev, as, errp)) {object_unref (hiod); - ops->detach_device (vbasedev); + vbasedev->hiod =NULL ;return false; } - vbasedev->hiod = hiod;return true; }
hw/vfio/container.c +4 -0
@@ -914,6 +914,10 @@ static bool vfio_legacy_attach_device(const char *name, VFIODevice *vbasedev,trace_vfio_attach_device (vbasedev->name, groupid); +if (!vfio_device_hiod_realize (vbasedev, errp)) { +return false; + } + group =vfio_get_group (groupid, as, errp);if (!group) {return false;
hw/vfio/helpers.c +11 -0
@@ -689,3 +689,14 @@ bool vfio_device_is_mdev(VFIODevice *vbasedev) subsys =realpath (tmp,NULL );return subsys && (strcmp (subsys,"/sys/bus/mdev" ) ==0 ); } + +bool vfio_device_hiod_realize (VFIODevice *vbasedev,Error **errp) +{ +HostIOMMUDevice *hiod = vbasedev->hiod; + +if (!hiod) { +return true; + } + +return HOST_IOMMU_DEVICE_GET_CLASS (hiod)->realize (hiod, vbasedev, errp); +}
hw/vfio/iommufd.c +11 -0
@@ -404,6 +404,17 @@ static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev, space =vfio_get_address_space (as); +/* +* The HostIOMMUDevice data from legacy backend is static and doesn't need +* any information from the (type1-iommu) backend to be initialized. In +* contrast however, the IOMMUFD HostIOMMUDevice data requires the iommufd +* FD to be connected and having a devid to be able to successfully call +* iommufd_backend_get_device_info(). +*/ +if (!vfio_device_hiod_realize (vbasedev, errp)) { + goto err_alloc_ioas; + } +/* try to attach to an existing container in this space */ QLIST_FOREACH(bcontainer, &space->containers, next) { container =container_of (bcontainer, VFIOIOMMUFDContainer, bcontainer);
Signature · 1 line
2.45.2
[PULL 06/16] vfio/ap: Don't initialize HOST_IOMMU_DEVICE with mdev
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
mdevs aren't "physical" devices and when asking for backing IOMMU info, it fails the entire provisioning of the guest. Fix that by setting vbasedev->mdev true so skipping HostIOMMUDevice initialization in the presence of mdevs.
--- hw/vfio/ap.c | 3 +++ 1 file changed, 3 insertions(+)
hw/vfio/ap.c +3 -0
@@ -230,6 +230,9 @@ static void vfio_ap_instance_init(Object *obj)*/ vfio_device_init (vbasedev,VFIO_DEVICE_TYPE_AP , &vfio_ap_ops,DEVICE (vapdev), true); + +/* AP device is mdev type device */ + vbasedev->mdev = true; }#ifdef CONFIG_IOMMUFD
Signature · 1 line
2.45.2
[PULL 09/16] vfio/{iommufd,container}: Remove caps::aw_bits
In reply to Cédric Le GoaterFrom: Joao Martins <[email protected]>
Remove caps::aw_bits which requires the bcontainer::iova_ranges being initialized after device is actually attached. Instead defer that to .get_cap() and call vfio_device_get_aw_bits() directly.
This is in preparation for HostIOMMUDevice::realize() being called early during attach_device().
--- include/sysemu/host_iommu_device.h | 3 --- backends/iommufd.c | 3 ++- hw/vfio/container.c | 5 +---- hw/vfio/iommufd.c | 1 - 4 files changed, 3 insertions(+), 9 deletions(-)
include/sysemu/host_iommu_device.h +0 -3
@@ -19,12 +19,9 @@* struct HostIOMMUDeviceCaps - Define host IOMMU device capabilities. * * @type: host platform IOMMU type. -* -* @aw_bits: host IOMMU address width. 0xff if no limitation. */ typedef struct HostIOMMUDeviceCaps {uint32_t type; -uint8_t aw_bits; }HostIOMMUDeviceCaps ;#define TYPE_HOST_IOMMU_DEVICE "host-iommu-device"
backends/iommufd.c +2 -1
@@ -18,6 +18,7 @@#include "qemu/error-report.h" #include "monitor/monitor.h" #include "trace.h" +#include "hw/vfio/vfio-common.h" #include <sys/ioctl.h> #include <linux/iommufd.h>
@@ -269,7 +270,7 @@ static int hiod_iommufd_get_cap(HostIOMMUDevice *hiod, int cap, Error **errp)case HOST_IOMMU_DEVICE_CAP_IOMMU_TYPE :return caps->type;case HOST_IOMMU_DEVICE_CAP_AW_BITS : -return caps->aw_bits; +return vfio_device_get_aw_bits (hiod->agent);default :error_setg (errp,"%s: unsupported capability %x" , hiod->name, cap);return -EINVAL ;
hw/vfio/container.c +1 -4
@@ -1141,7 +1141,6 @@ static bool hiod_legacy_vfio_realize(HostIOMMUDevice *hiod, void *opaque,VFIODevice *vdev = opaque; hiod->name =g_strdup (vdev->name); - hiod->caps.aw_bits =vfio_device_get_aw_bits (vdev); hiod->agent = opaque;return true;
@@ -1150,11 +1149,9 @@ static bool hiod_legacy_vfio_realize(HostIOMMUDevice *hiod, void *opaque,static int hiod_legacy_vfio_get_cap (HostIOMMUDevice *hiod,int cap,Error **errp) { -HostIOMMUDeviceCaps *caps = &hiod->caps; -switch (cap) {case HOST_IOMMU_DEVICE_CAP_AW_BITS : -return caps->aw_bits; +return vfio_device_get_aw_bits (hiod->agent);default :error_setg (errp,"%s: unsupported capability %x" , hiod->name, cap);return -EINVAL ;
hw/vfio/iommufd.c +0 -1
@@ -725,7 +725,6 @@ static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque, hiod->name =g_strdup (vdev->name); caps->type = type; - caps->aw_bits =vfio_device_get_aw_bits (vdev);return true; }
Signature · 1 line
2.45.2
[PULL 10/16] vfio/iommufd: Add hw_caps field to HostIOMMUDeviceCaps
In reply to Cédric Le GoaterFrom: Joao Martins <[email protected]>
Store the value of @caps returned by iommufd_backend_get_device_info() in a new field HostIOMMUDeviceCaps::hw_caps. Right now the only value is whether device IOMMU supports dirty tracking (IOMMU_HW_CAP_DIRTY_TRACKING).
This is in preparation for HostIOMMUDevice::realize() being called early during attach_device().
--- include/sysemu/host_iommu_device.h | 4 ++++ hw/vfio/iommufd.c | 1 + 2 files changed, 5 insertions(+)
include/sysemu/host_iommu_device.h +4 -0
@@ -19,9 +19,13 @@* struct HostIOMMUDeviceCaps - Define host IOMMU device capabilities. * * @type: host platform IOMMU type. +* +* @hw_caps: host platform IOMMU capabilities (e.g. on IOMMUFD this represents +* the @out_capabilities value returned from IOMMU_GET_HW_INFO ioctl) */ typedef struct HostIOMMUDeviceCaps {uint32_t type; +uint64_t hw_caps; }HostIOMMUDeviceCaps ;#define TYPE_HOST_IOMMU_DEVICE "host-iommu-device"
hw/vfio/iommufd.c +1 -0
@@ -725,6 +725,7 @@ static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque, hiod->name =g_strdup (vdev->name); caps->type = type; + caps->hw_caps = hw_caps;return true; }
Signature · 1 line
2.45.2
[PULL 07/16] vfio/ccw: Don't initialize HOST_IOMMU_DEVICE with mdev
In reply to Cédric Le GoaterFrom: Zhenzhong Duan <[email protected]>
mdevs aren't "physical" devices and when asking for backing IOMMU info, it fails the entire provisioning of the guest. Fix that by setting vbasedev->mdev true so skipping HostIOMMUDevice initialization in the presence of mdevs.
--- hw/vfio/ccw.c | 3 +++ 1 file changed, 3 insertions(+)
hw/vfio/ccw.c +3 -0
@@ -675,6 +675,9 @@ static void vfio_ccw_instance_init(Object *obj)VFIOCCWDevice *vcdev =VFIO_CCW (obj);VFIODevice *vbasedev = &vcdev->vdev; +/* CCW device is mdev type device */ + vbasedev->mdev = true; +/* * All vfio-ccw devices are believed to operate in a way compatible with * discarding of memory in RAM blocks, ie. pages pinned in the host are
Signature · 1 line
2.45.2
[PULL 12/16] vfio/iommufd: Probe and request hwpt dirty tracking capability
In reply to Cédric Le GoaterFrom: Joao Martins <[email protected]>
In preparation to using the dirty tracking UAPI, probe whether the IOMMU supports dirty tracking. This is done via the data stored in hiod::caps::hw_caps initialized from GET_HW_INFO.
Qemu doesn't know if VF dirty tracking is supported when allocating hardware pagetable in iommufd_cdev_autodomains_get(). This is because VFIODevice migration state hasn't been initialized *yet* hence it can't pick between VF dirty tracking vs IOMMU dirty tracking. So, if IOMMU supports dirty tracking it always creates HWPTs with IOMMU_HWPT_ALLOC_DIRTY_TRACKING even if later on VFIOMigration decides to use VF dirty tracking instead.
[ clg: - Fixed vbasedev->iommu_dirty_tracking assignment in
iommufd_cdev_autodomains_get()
- Added warning for heterogeneous dirty page tracking support in iommufd_cdev_autodomains_get() ]
--- include/hw/vfio/vfio-common.h | 2 ++ hw/vfio/iommufd.c | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+)
include/hw/vfio/vfio-common.h +2 -0
@@ -97,6 +97,7 @@ typedef struct IOMMUFDBackend IOMMUFDBackend;typedef struct VFIOIOASHwpt {uint32_t hwpt_id; +uint32_t hwpt_flags; QLIST_HEAD(, VFIODevice) device_list;QLIST_ENTRY (VFIOIOASHwpt) next; }VFIOIOASHwpt ;
@@ -139,6 +140,7 @@ typedef struct VFIODevice {OnOffAuto pre_copy_dirty_page_tracking;bool dirty_pages_supported;bool dirty_tracking; +bool iommu_dirty_tracking;HostIOMMUDevice *hiod;int devid;IOMMUFDBackend *iommufd;
hw/vfio/iommufd.c +26 -0
@@ -110,6 +110,11 @@ static void iommufd_cdev_unbind_and_disconnect(VFIODevice *vbasedev)iommufd_backend_disconnect (vbasedev->iommufd); } +static bool iommufd_hwpt_dirty_tracking (VFIOIOASHwpt *hwpt) +{ +return hwpt && hwpt->hwpt_flags &IOMMU_HWPT_ALLOC_DIRTY_TRACKING ; +} +static int iommufd_cdev_getfd (const char *sysfs_path,Error **errp) {ERRP_GUARD ();
@@ -243,10 +248,22 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev, } else { vbasedev->hwpt = hwpt;QLIST_INSERT_HEAD (&hwpt->device_list, vbasedev, hwpt_next); + vbasedev->iommu_dirty_tracking =iommufd_hwpt_dirty_tracking (hwpt);return true; } } +/* +* This is quite early and VFIO Migration state isn't yet fully +* initialized, thus rely only on IOMMU hardware capabilities as to +* whether IOMMU dirty tracking is going to be requested. Later +* vfio_migration_realize() may decide to use VF dirty tracking +* instead. +*/ +if (vbasedev->hiod->caps.hw_caps &IOMMU_HW_CAP_DIRTY_TRACKING ) { + flags =IOMMU_HWPT_ALLOC_DIRTY_TRACKING ; + } + if (!iommufd_backend_alloc_hwpt(iommufd, vbasedev->devid, container->ioas_id, flags, IOMMU_HWPT_DATA_NONE, 0, NULL,
@@ -256,6 +273,7 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev, hwpt =g_malloc0 (sizeof (*hwpt)); hwpt->hwpt_id = hwpt_id; + hwpt->hwpt_flags = flags;QLIST_INIT (&hwpt->device_list); ret =iommufd_cdev_attach_ioas_hwpt (vbasedev, hwpt->hwpt_id, errp);
@@ -266,8 +284,16 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev, } vbasedev->hwpt = hwpt; + vbasedev->iommu_dirty_tracking =iommufd_hwpt_dirty_tracking (hwpt);QLIST_INSERT_HEAD (&hwpt->device_list, vbasedev, hwpt_next);QLIST_INSERT_HEAD (&container->hwpt_list, hwpt, next); + container->bcontainer.dirty_pages_supported |= + vbasedev->iommu_dirty_tracking; +if (container->bcontainer.dirty_pages_supported && + !vbasedev->iommu_dirty_tracking) { +warn_report ("IOMMU instance for device %s doesn't support dirty tracking" , + vbasedev->name); + }return true; }
Signature · 1 line
2.45.2
[PULL 03/16] vfio/iommufd: Don't initialize nor set a HOST_IOMMU_DEVICE with mdev
In reply to Cédric Le GoaterFrom: Joao Martins <[email protected]>
mdevs aren't "physical" devices and when asking for backing IOMMU info, it fails the entire provisioning of the guest. Fix that by skipping HostIOMMUDevice initialization in the presence of mdevs, and skip setting an iommu device when it is known to be an mdev.
--- hw/vfio/common.c | 4 ++++ hw/vfio/pci.c | 11 ++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-)
hw/vfio/common.c +4 -0
@@ -1548,6 +1548,10 @@ bool vfio_attach_device(char *name, VFIODevice *vbasedev,return false; } +if (vbasedev->mdev) { +return true; + } + hiod =HOST_IOMMU_DEVICE (object_new (ops->hiod_typename));if (!HOST_IOMMU_DEVICE_GET_CLASS (hiod)->realize (hiod, vbasedev, errp)) {object_unref (hiod);
hw/vfio/pci.c +8 -3
@@ -3115,7 +3115,8 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)vfio_bars_register (vdev); -if (!pci_device_set_iommu_device (pdev, vbasedev->hiod, errp)) { +if (!vbasedev->mdev && + !pci_device_set_iommu_device (pdev, vbasedev->hiod, errp)) {error_prepend (errp,"Failed to set iommu_device: " ); goto out_teardown; }
@@ -3238,7 +3239,9 @@ out_deregister:timer_free (vdev->intx.mmap_timer); } out_unset_idev: -pci_device_unset_iommu_device (pdev); +if (!vbasedev->mdev) { +pci_device_unset_iommu_device (pdev); + } out_teardown:vfio_teardown_msi (vdev);vfio_bars_exit (vdev);
@@ -3283,7 +3286,9 @@ static void vfio_exitfn(PCIDevice *pdev)vfio_pci_disable_rp_atomics (vdev);vfio_bars_exit (vdev);vfio_migration_exit (vbasedev); -pci_device_unset_iommu_device (pdev); +if (!vbasedev->mdev) { +pci_device_unset_iommu_device (pdev); + } }static void vfio_pci_reset (DeviceState *dev)
Signature · 1 line
2.45.2
Cédric Le Goater
Reviewed-byQuoted 10 lines · Cédric Le Goater
The following changes since commit
6af69d02706c821797802cfd56acdac13a7c9422:Merge tag 'nvme-next-pull-request' of https://gitlab.com/birkelund/qemu into staging (2024-07-23 13:55:45 +1000)are available in the Git repository at:
https://github.com/legoater/qemu/ tags/pull-vfio-20240723for you to fetch changes up to
6ac9efe6805af60de14481fdde7d340080d38324:
Quoted 8 lines
vfio/common: Allow disabling device dirty page tracking (2024-07-23 11:10:10 +0200)
----------------------------------------------------------------
vfio queue:* IOMMUFD Dirty Tracking support
* Fix for a possible SEGV in IOMMU type1 container
* Dropped initialization of host IOMMU device with mdev devices
There is a problem with an email address in patch 13 :
I will repush an resend.
Thanks,
C.