intel_iommu: Enable PRQ support for passthrough device
cover intel_iommu: Enable PRQ support for passthrough device
Hi,
When svm=on,pasid=N,x-flst=on is configured for virtual vtd, we enable guest's support for vSVM. In this case, the host VTD may generate recoverable first stage page fault event, QEMU read the event and inject it to guest.
After guest handles the event, it sends a page group response, QEMU gets the response and pass it to host VTD.
This series adds QEMU support for receiving such host events through the FAULTQ interface and propagating them to the guest, then catching guest's responses and propagating them to host.
This patchset depends on pasid support:
https://lore.kernel.org/qemu-devel/[email protected]/#t
GIT branch: https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_prq
Tests:
Tested with DSA wq attached to an user APP which triggers IO page fault on process page table durging DMA.
Thanks
Zhenzhong
Zhenzhong Duan (5):
backends/iommufd: Introduce iommufd_backend_alloc_faultq backends/iommufd: Extend iommufd_backend_alloc_hwpt() with fault_id
intel_iommu_accel: Add PRQ injection for passthrough device
intel_iommu_accel: Accept PRQ response for passthrough device
intel_iommu_accel: teardown FAULTQ resources in bottom half
hw/i386/intel_iommu_accel.h | 16 +++ include/hw/i386/intel_iommu.h | 6 + include/system/iommufd.h | 7 +- backends/iommufd.c | 34 ++++- hw/arm/smmuv3-accel.c | 6 +- hw/i386/intel_iommu.c | 4 + hw/i386/intel_iommu_accel.c | 243 +++++++++++++++++++++++++++++++++- hw/vfio/iommufd.c | 2 +- backends/trace-events | 3 +- hw/i386/trace-events | 2 + 10 files changed, 308 insertions(+), 15 deletions(-)
Signature · 1 line
2.47.3
PATCH 1/5 backends/iommufd: Introduce iommufd_backend_alloc_faultq
Add a new helper for IOMMU_FAULT_QUEUE_ALLOC ioctl to allocate a fault handling object which will be used in hwpt allocation.
--- include/system/iommufd.h | 3 +++ backends/iommufd.c | 21 +++++++++++++++++++++ backends/trace-events | 1 + 3 files changed, 25 insertions(+)
include/system/iommufd.h +3 -0
@@ -100,6 +100,9 @@ bool iommufd_backend_alloc_veventq(IOMMUFDBackend *be, uint32_t viommu_id,uint32_t *out_veventq_id, uint32_t *out_veventq_fd, Error **errp); +bool iommufd_backend_alloc_faultq (IOMMUFDBackend *be,uint32_t *fault_id, +uint32_t *fault_fd,Error **errp); +bool iommufd_backend_set_dirty_tracking (IOMMUFDBackend *be,uint32_t hwpt_id,bool start,Error **errp); bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be, uint32_t hwpt_id,
backends/iommufd.c +21 -0
@@ -337,6 +337,27 @@ bool iommufd_backend_alloc_hwpt(IOMMUFDBackend *be, uint32_t dev_id,return true; } +bool iommufd_backend_alloc_faultq (IOMMUFDBackend *be,uint32_t *fault_id, +uint32_t *fault_fd,Error **errp) +{ +int ret, fd = be->fd; +struct iommu_fault_alloc cmd = { + .size =sizeof (cmd), + }; + + ret =ioctl (fd,IOMMU_FAULT_QUEUE_ALLOC , &cmd); +trace_iommufd_backend_alloc_faultq (fd, cmd.out_fault_id, cmd.out_fault_fd, + ret); +if (ret) { +error_setg_errno (errp, errno,"Failed to allocate fault queue" ); +return false; + } + + *fault_id = cmd.out_fault_id; + *fault_fd = cmd.out_fault_fd; +return true; +} +bool iommufd_backend_set_dirty_tracking (IOMMUFDBackend *be,uint32_t hwpt_id,bool start,Error **errp)
backends/trace-events +1 -0
@@ -17,6 +17,7 @@ iommufd_backend_unmap_dma_non_exist(int iommufd, uint32_t ioas, uint64_t iova, u 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_alloc_faultq(int iommufd, uint32_t fault_id, uint32_t fault_fd, int ret) " iommufd=%d fault_id=%d fault_fd=%d (%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)" iommufd_backend_get_dirty_bitmap(int iommufd, uint32_t hwpt_id, uint64_t iova, uint64_t size, uint64_t flags, uint64_t page_size, int ret) " iommufd=%d hwpt=%u iova=0x%"PRIx64" size=0x%"PRIx64" flags=0x%"PRIx64" page_size=0x%"PRIx64" (%d)"
Signature · 1 line
2.47.3
Re: [PATCH 1/5] backends/iommufd: Introduce iommufd_backend_alloc_faultq
Quoted 29 lines · Zhenzhong Duan
Add a new helper for
IOMMU_FAULT_QUEUE_ALLOCioctl to allocate a fault handling object which will be used in hwpt allocation.Signed-off-by Zhenzhong Duan <[email protected]>--- include/system/iommufd.h | 3 +++ backends/iommufd.c | 21 +++++++++++++++++++++ backends/trace-events | 1 + 3 files changed, 25 insertions(+)include/system/iommufd.h +3
-0@@ -100,6 +100,9 @@ bool iommufd_backend_alloc_veventq(IOMMUFDBackend *be, uint32_t viommu_id,uint32_t *out_veventq_id, uint32_t *out_veventq_fd, Error **errp); +bool iommufd_backend_alloc_faultq (IOMMUFDBackend *be,uint32_t *fault_id, +uint32_t *fault_fd,Error **errp); +bool iommufd_backend_set_dirty_tracking (IOMMUFDBackend *be,uint32_t hwpt_id,bool start,Error **errp); bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be, uint32_t hwpt_id,backends/iommufd.c +0
-0
backends/iommufd.c bool iommufd_backend_alloc_hwpt(IOMMUFDBackend *be, uint32_t dev_id, · line 341
Quoted 5 lines
backends/iommufd.c +2
-0@@ -337,6 +337,27 @@ bool iommufd_backend_alloc_hwpt(IOMMUFDBackend *be, uint32_t dev_id,return true; } +bool iommufd_backend_alloc_faultq (IOMMUFDBackend *be,uint32_t *fault_id, +uint32_t *fault_fd,Error **errp)
how about using int *fault_fd? I know the uapi returns __u32, but we anyhow need to have a conversion to int. If so, we may do it in this helper.
Quoted 34 lines
backends/iommufd.c +19
-0+{ +int ret, fd = be->fd; +struct iommu_fault_alloc cmd = { + .size =sizeof (cmd), + }; + + ret =ioctl (fd,IOMMU_FAULT_QUEUE_ALLOC , &cmd); +trace_iommufd_backend_alloc_faultq (fd, cmd.out_fault_id, cmd.out_fault_fd, + ret); +if (ret) { +error_setg_errno (errp, errno,"Failed to allocate fault queue" ); +return false; + } + + *fault_id = cmd.out_fault_id; + *fault_fd = cmd.out_fault_fd; +return true; +} +bool iommufd_backend_set_dirty_tracking (IOMMUFDBackend *be,uint32_t hwpt_id,bool start,Error **errp)backends/trace-events +1
-0@@ -17,6 +17,7 @@ iommufd_backend_unmap_dma_non_exist(int iommufd, uint32_t ioas, uint64_t iova, u 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_alloc_faultq(int iommufd, uint32_t fault_id, uint32_t fault_fd, int ret) " iommufd=%d fault_id=%d fault_fd=%d (%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)" iommufd_backend_get_dirty_bitmap(int iommufd, uint32_t hwpt_id, uint64_t iova, uint64_t size, uint64_t flags, uint64_t page_size, int ret) " iommufd=%d hwpt=%u iova=0x%"PRIx64" size=0x%"PRIx64" flags=0x%"PRIx64" page_size=0x%"PRIx64" (%d)"
RE: [PATCH 1/5] backends/iommufd: Introduce iommufd_backend_alloc_faultq
Quoted 45 lines
-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH 1/5] backends/iommufd: Introduceiommufd_backend_alloc_faultqinclude/system/iommufd.h bool
Quoted 15 lines · Zhenzhong Duan
Add a new helper for
IOMMU_FAULT_QUEUE_ALLOCioctl to allocate a fault handling object which will be used in hwpt allocation.Signed-off-by Zhenzhong Duan <[email protected]>--- include/system/iommufd.h | 3 +++ backends/iommufd.c | 21 +++++++++++++++++++++ backends/trace-events | 1 + 3 files changed, 25 insertions(+)include/system/iommufd.h +0
-0@@ -100,6 +100,9 @@ booliommufd_backend_alloc_veventq(IOMMUFDBackend *be,
uint32_tviommu_id,include/system/iommufd.h bool iommufd_backend_alloc_veventq(IOMMUFDBackend *be, uint32_t viommu_id,
Quoted 4 lines
include/system/iommufd.h +1
-0uint32_t *out_veventq_id, uint32_t *out_veventq_fd, Error **errp); +bool iommufd_backend_alloc_faultq(IOMMUFDBackend *be, uint32_t
*fault_id,include/system/iommufd.h bool iommufd_backend_alloc_veventq(IOMMUFDBackend *be, uint32_t viommu_id,
Quoted 3 lines
include/system/iommufd.h +2
-0+ uint32_t *fault_fd, Error **errp); + bool iommufd_backend_set_dirty_tracking(IOMMUFDBackend *be, uint32_t
hwpt_id,include/system/iommufd.h bool · line 108
Quoted 2 lines
include/system/iommufd.h +0
-0bool start, Error **errp); bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be, uint32_t
hwpt_id,backends/iommufd.c bool
Quoted 5 lines
backends/iommufd.c +0
-0@@ -337,6 +337,27 @@ booliommufd_backend_alloc_hwpt(IOMMUFDBackend *be,
uint32_tdev_id,Quoted 3 lines
backends/iommufd.c +0
-0return true; }
Quoted 7 lines
backends/iommufd.c bool · line 340
Quoted 1 line
backends/iommufd.c +1
-0+bool iommufd_backend_alloc_faultq(IOMMUFDBackend *be, uint32_t
*fault_id,backends/iommufd.c bool iommufd_backend_alloc_hwpt(IOMMUFDBackend *be, uint32_t dev_id, · line 341
Quoted 1 line
backends/iommufd.c +1
-0+ uint32_t *fault_fd, Error **errp)how about using int
*fault_fd? I know the uapi returns__u32, but we anyhow need to have a conversion to int. If so, we may do it in this helper.
Will do.
Thanks
Zhenzhong
PATCH 2/5 backends/iommufd: Extend iommufd_backend_alloc_hwpt() with fault_id
In reply to Zhenzhong DuanNo need to force caller to set IOMMU_HWPT_FAULT_ID_VALID, we take it by checking fault_id nonzero.
--- include/system/iommufd.h | 4 ++-- backends/iommufd.c | 13 +++++++++---- hw/arm/smmuv3-accel.c | 6 +++--- hw/i386/intel_iommu_accel.c | 2 +- hw/vfio/iommufd.c | 2 +- backends/trace-events | 2 +- 6 files changed, 17 insertions(+), 12 deletions(-)
include/system/iommufd.h +2 -2
@@ -85,8 +85,8 @@ bool iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,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); +void *data_ptr,uint32_t fault_id, +uint32_t *out_hwpt,Error **errp);bool iommufd_backend_alloc_viommu (IOMMUFDBackend *be,uint32_t dev_id,uint32_t viommu_type,uint32_t hwpt_id,uint32_t *out_hwpt,Error **errp);
backends/iommufd.c +9 -4
@@ -310,23 +310,28 @@ int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id,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) +void *data_ptr,uint32_t fault_id, +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, + .fault_id = fault_id, }; +if (fault_id) { + flags |=IOMMU_HWPT_FAULT_ID_VALID ; + } + alloc_hwpt.flags = flags; + 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, + data_len, (uintptr_t )data_ptr, fault_id, alloc_hwpt.out_hwpt_id, ret);if (ret) {error_setg_errno (errp, errno,"Failed to allocate hwpt" );
hw/arm/smmuv3-accel.c +3 -3
@@ -224,7 +224,7 @@ smmuv3_accel_dev_alloc_translate(SMMUv3AccelDevice *accel_dev, STE *ste, accel->viommu->viommu_id, flags, IOMMU_HWPT_DATA_ARM_SMMUV3, sizeof(nested_data), &nested_data, - &hwpt_id, errp)) { + 0, &hwpt_id, errp)) {return NULL ; }
@@ -558,14 +558,14 @@ smmuv3_accel_alloc_viommu(SMMUv3State *s, HostIOMMUDeviceIOMMUFD *idev,if (!iommufd_backend_alloc_hwpt (idev->iommufd, idev->devid, viommu_id,0 ,IOMMU_HWPT_DATA_ARM_SMMUV3 ,sizeof (abort_data), &abort_data, - &accel->abort_hwpt_id, errp)) { +0 , &accel->abort_hwpt_id, errp)) { goto free_viommu; }if (!iommufd_backend_alloc_hwpt (idev->iommufd, idev->devid, viommu_id,0 ,IOMMU_HWPT_DATA_ARM_SMMUV3 ,sizeof (bypass_data), &bypass_data, - &accel->bypass_hwpt_id, errp)) { +0 , &accel->bypass_hwpt_id, errp)) { goto free_abort_hwpt; }
hw/i386/intel_iommu_accel.c +1 -1
@@ -115,7 +115,7 @@ static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,return iommufd_backend_alloc_hwpt (idev->iommufd, idev->devid, idev->hwpt_id, flags,IOMMU_HWPT_DATA_VTD_S1 , -sizeof (vtd), &vtd, fs_hwpt_id, errp); +sizeof (vtd), &vtd,0 , fs_hwpt_id, errp); } static void vtd_destroy_old_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,
hw/vfio/iommufd.c +1 -1
@@ -444,7 +444,7 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,if (!iommufd_backend_alloc_hwpt (iommufd, vbasedev->devid, container->ioas_id, flags,IOMMU_HWPT_DATA_NONE ,0 ,NULL , - &hwpt_id, errp)) { +0 , &hwpt_id, errp)) {return false; }
backends/trace-events +1 -1
@@ -16,7 +16,7 @@ iommufd_backend_map_file_dma(int iommufd, uint32_t ioas, uint64_t iova, uint64_t 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_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 fault_id, 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" fault_id=%u out_hwpt=%u (%d)" iommufd_backend_alloc_faultq(int iommufd, uint32_t fault_id, uint32_t fault_fd, int ret) " iommufd=%d fault_id=%d fault_fd=%d (%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.47.3
Re: [PATCH 2/5] backends/iommufd: Extend iommufd_backend_alloc_hwpt() with fault_id
Quoted 2 lines · Zhenzhong Duan
No need to force caller to set
IOMMU_HWPT_FAULT_ID_VALID, we take it by checkingfault_idnonzero.
this is not reliable. There is no doc that claims fault_id should be non-zero.
Quoted 68 lines
Signed-off-by Zhenzhong Duan <[email protected]>--- include/system/iommufd.h | 4 ++-- backends/iommufd.c | 13 +++++++++---- hw/arm/smmuv3-accel.c | 6 +++--- hw/i386/intel_iommu_accel.c | 2 +- hw/vfio/iommufd.c | 2 +- backends/trace-events | 2 +- 6 files changed, 17 insertions(+), 12 deletions(-)include/system/iommufd.h +2
-2@@ -85,8 +85,8 @@ bool iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,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); +void *data_ptr,uint32_t fault_id, +uint32_t *out_hwpt,Error **errp);bool iommufd_backend_alloc_viommu (IOMMUFDBackend *be,uint32_t dev_id,uint32_t viommu_type,uint32_t hwpt_id,uint32_t *out_hwpt,Error **errp);backends/iommufd.c +9
-4@@ -310,23 +310,28 @@ int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id,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) +void *data_ptr,uint32_t fault_id, +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, + .fault_id = fault_id, }; +if (fault_id) { + flags |=IOMMU_HWPT_FAULT_ID_VALID ; + } + alloc_hwpt.flags = flags; + 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, + data_len, (uintptr_t )data_ptr, fault_id, alloc_hwpt.out_hwpt_id, ret);if (ret) {error_setg_errno (errp, errno,"Failed to allocate hwpt" );hw/arm/smmuv3-accel.c +0
-0
hw/arm/smmuv3-accel.c smmuv3_accel_dev_alloc_translate(SMMUv3AccelDevice *accel_dev, STE *ste, · line 227
Quoted 5 lines
hw/arm/smmuv3-accel.c +1
-1@@ -224,7 +224,7 @@ smmuv3_accel_dev_alloc_translate(SMMUv3AccelDevice *accel_dev, STE *ste, accel->viommu->viommu_id, flags,IOMMU_HWPT_DATA_ARM_SMMUV3 ,sizeof (nested_data), &nested_data, - &hwpt_id, errp)) { + 0, &hwpt_id, errp)) {
same remark. passing 0 is not reliable. :(
Quoted 59 lines
hw/arm/smmuv3-accel.c +2
-2return NULL ; }@@ -558,14 +558,14 @@ smmuv3_accel_alloc_viommu(SMMUv3State *s, HostIOMMUDeviceIOMMUFD *idev,if (!iommufd_backend_alloc_hwpt (idev->iommufd, idev->devid, viommu_id,0 ,IOMMU_HWPT_DATA_ARM_SMMUV3 ,sizeof (abort_data), &abort_data, - &accel->abort_hwpt_id, errp)) { +0 , &accel->abort_hwpt_id, errp)) { goto free_viommu; }if (!iommufd_backend_alloc_hwpt (idev->iommufd, idev->devid, viommu_id,0 ,IOMMU_HWPT_DATA_ARM_SMMUV3 ,sizeof (bypass_data), &bypass_data, - &accel->bypass_hwpt_id, errp)) { +0 , &accel->bypass_hwpt_id, errp)) { goto free_abort_hwpt; }hw/i386/intel_iommu_accel.c +1
-1@@ -115,7 +115,7 @@ static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,return iommufd_backend_alloc_hwpt (idev->iommufd, idev->devid, idev->hwpt_id, flags,IOMMU_HWPT_DATA_VTD_S1 , -sizeof (vtd), &vtd, fs_hwpt_id, errp); +sizeof (vtd), &vtd,0 , fs_hwpt_id, errp); } static void vtd_destroy_old_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,hw/vfio/iommufd.c +1
-1@@ -444,7 +444,7 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,if (!iommufd_backend_alloc_hwpt (iommufd, vbasedev->devid, container->ioas_id, flags,IOMMU_HWPT_DATA_NONE ,0 ,NULL , - &hwpt_id, errp)) { +0 , &hwpt_id, errp)) {return false; }backends/trace-events +1
-1@@ -16,7 +16,7 @@ iommufd_backend_map_file_dma(int iommufd, uint32_t ioas, uint64_t iova, uint64_t 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_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 fault_id, 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" fault_id=%u out_hwpt=%u (%d)" iommufd_backend_alloc_faultq(int iommufd, uint32_t fault_id, uint32_t fault_fd, int ret) " iommufd=%d fault_id=%d fault_fd=%d (%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)"
RE: [PATCH 2/5] backends/iommufd: Extend iommufd_backend_alloc_hwpt() with fault_id
Quoted 7 lines
-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH 2/5] backends/iommufd: Extendiommufd_backend_alloc_hwpt()withfault_id
Quoted 6 lines
Quoted 2 lines · Zhenzhong Duan
No need to force caller to set
IOMMU_HWPT_FAULT_ID_VALID, we take it by checkingfault_idnonzero.this is not reliable. There is no doc that claims
fault_idshould be non-zero.
Yes, from kernel code, 0 is never allocated as an object id but it's not documented clearly. Eric ever asked the same question before. https://lore.kernel.org/all/[email protected]/
@Nicolin, @JasonG, just want to ask if you have plan to doc it in kernel. If no, I'll update this patch following Yi's suggestion.
Thanks
Zhenzhong
Quoted 160 lines
include/system/iommufd.h bool
Quoted 17 lines
Signed-off-by Zhenzhong Duan <[email protected]>--- include/system/iommufd.h | 4 ++-- backends/iommufd.c | 13 +++++++++---- hw/arm/smmuv3-accel.c | 6 +++--- hw/i386/intel_iommu_accel.c | 2 +- hw/vfio/iommufd.c | 2 +- backends/trace-events | 2 +- 6 files changed, 17 insertions(+), 12 deletions(-)include/system/iommufd.h +0
-0@@ -85,8 +85,8 @@ booliommufd_backend_get_device_info(IOMMUFDBackend *be,
uint32_tdevid,include/system/iommufd.h bool iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,
Quoted 8 lines
include/system/iommufd.h +2
-2bool 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); +void *data_ptr,uint32_t fault_id, +uint32_t *out_hwpt,Error **errp); bool iommufd_backend_alloc_viommu(IOMMUFDBackend *be, uint32_t
dev_id,backends/iommufd.c int
Quoted 7 lines
include/system/iommufd.h +0
-0uint32_t viommu_type, uint32_t hwpt_id, uint32_t *out_hwpt, Error **errp);backends/iommufd.c +0
-0@@ -310,23 +310,28 @@ intiommufd_backend_unmap_dma(IOMMUFDBackend *be,
uint32_tioas_id,hw/arm/smmuv3-accel.c
Quoted 37 lines
backends/iommufd.c +9
-4bool 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) +void *data_ptr,uint32_t fault_id, +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, + .fault_id = fault_id, }; +if (fault_id) { + flags |=IOMMU_HWPT_FAULT_ID_VALID ; + } + alloc_hwpt.flags = flags; + 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, + data_len, (uintptr_t )data_ptr, fault_id, alloc_hwpt.out_hwpt_id, ret);if (ret) {error_setg_errno (errp, errno,"Failed to allocate hwpt" );hw/arm/smmuv3-accel.c +0
-0@@ -224,7 +224,7 @@smmuv3_accel_dev_alloc_translate(SMMUv3AccelDevice
*accel_dev, STE *ste,hw/arm/smmuv3-accel.c smmuv3_accel_dev_alloc_translate(SMMUv3AccelDevice *accel_dev, STE *ste, · line 227
Quoted 5 lines
hw/arm/smmuv3-accel.c +1
-1accel->viommu->viommu_id, flags,IOMMU_HWPT_DATA_ARM_SMMUV3 ,sizeof (nested_data), &nested_data, - &hwpt_id, errp)) { + 0, &hwpt_id, errp)) {same remark. passing 0 is not reliable. :(
hw/arm/smmuv3-accel.c smmuv3_accel_alloc_viommu(SMMUv3State *s,
Quoted 4 lines
hw/arm/smmuv3-accel.c +0
-0return NULL ; }@@ -558,14 +558,14 @@ smmuv3_accel_alloc_viommu(SMMUv3State *s,HostIOMMUDeviceIOMMUFD *idev,
hw/i386/intel_iommu_accel.c static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice
Quoted 21 lines
hw/arm/smmuv3-accel.c +2
-2if (!iommufd_backend_alloc_hwpt (idev->iommufd, idev->devid, viommu_id,0 ,IOMMU_HWPT_DATA_ARM_SMMUV3 ,sizeof (abort_data), &abort_data, - &accel->abort_hwpt_id, errp)) { +0 , &accel->abort_hwpt_id, errp)) { goto free_viommu; }if (!iommufd_backend_alloc_hwpt (idev->iommufd, idev->devid, viommu_id,0 ,IOMMU_HWPT_DATA_ARM_SMMUV3 ,sizeof (bypass_data), &bypass_data, - &accel->bypass_hwpt_id, errp)) { +0 , &accel->bypass_hwpt_id, errp)) { goto free_abort_hwpt; }hw/i386/intel_iommu_accel.c +0
-0@@ -115,7 +115,7 @@ static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice
*vtd_hiod,hw/vfio/iommufd.c static bool
Quoted 14 lines
hw/i386/intel_iommu_accel.c +1
-1return iommufd_backend_alloc_hwpt (idev->iommufd, idev->devid, idev- hwpt_id, flags,IOMMU_HWPT_DATA_VTD_S1 , -sizeof (vtd), &vtd, fs_hwpt_id, errp); +sizeof (vtd), &vtd,0 , fs_hwpt_id, errp); } static void vtd_destroy_old_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,hw/vfio/iommufd.c +0
-0@@ -444,7 +444,7 @@ static booliommufd_cdev_autodomains_get(VFIODevice *vbasedev,
backends/trace-events iommufd_backend_map_file_dma(int iommufd, uint32_t
Quoted 13 lines
hw/vfio/iommufd.c +1
-1if (!iommufd_backend_alloc_hwpt (iommufd, vbasedev->devid, container->ioas_id, flags,IOMMU_HWPT_DATA_NONE ,0 ,NULL , - &hwpt_id, errp)) { +0 , &hwpt_id, errp)) {return false; }backends/trace-events +0
-0@@ -16,7 +16,7 @@ iommufd_backend_map_file_dma(int iommufd, uint32_tioas,
uint64_tiova,uint64_tbackends/trace-events iommufd_backend_map_file_dma(int iommufd, uint32_t · line 16
Quoted 1 line
backends/trace-events +0
-0iommufd_backend_unmap_dma_non_exist(int iommufd, uint32_t ioas,
uint64_tiova,uint64_tsize, int ret) " Unmap nonexistent mapping: iommufd=%d ioas=%d iova=0x%"PRIx64" size=0x%"PRIx64" (%d)"backends/trace-events iommufd_backend_map_file_dma(int iommufd, uint32_t · line 17
Quoted 1 line
backends/trace-events +0
-0iommufd_backend_unmap_dma(int iommufd, uint32_t ioas, uint64_t iova,
uint64_tsize, int ret) " iommufd=%d ioas=%d iova=0x%"PRIx64" size=0x%"PRIx64" (%d)"backends/trace-events iommufd_backend_map_file_dma(int iommufd, uint32_t · line 18
Quoted 1 line
backends/trace-events +0
-0iommufd_backend_alloc_ioas(int iommufd, uint32_t ioas) " iommufd=%dioas=%d"
backends/trace-events iommufd_backend_map_file_dma(int iommufd, uint32_t · line 19
Quoted 1 line
backends/trace-events +0
-1-iommufd_backend_alloc_hwpt(int iommufd, uint32_t dev_id, uint32_t pt_id,
uint32_tflags,uint32_thwpt_type,uint32_tlen,uint64_tdata_ptr,uint32_tout_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)"backends/trace-events iommufd_backend_map_file_dma(int iommufd, uint32_t · line 19
Quoted 1 line
backends/trace-events +1
-0+iommufd_backend_alloc_hwpt(int iommufd, uint32_t dev_id, uint32_t pt_id,
uint32_tflags,uint32_thwpt_type,uint32_tlen,uint64_tdata_ptr,uint32_tfault_id,uint32_tout_hwpt_id, int ret) " iommufd=%d dev_id=%u pt_id=%u flags=0x%x hwpt_type=%u len=%u data_ptr=0x%"PRIx64" fault_id=%u out_hwpt=%u (%d)"backends/trace-events iommufd_backend_map_file_dma(int iommufd, uint32_t · line 20
Quoted 1 line
backends/trace-events +0
-0iommufd_backend_alloc_faultq(int iommufd, uint32_t fault_id, uint32_t
fault_fd, int ret) " iommufd=%d fault_id=%d fault_fd=%d (%d)"backends/trace-events iommufd_backend_map_file_dma(int iommufd, uint32_t · line 21
Quoted 1 line
backends/trace-events +0
-0iommufd_backend_free_id(int iommufd, uint32_t id, int ret) " iommufd=%did=%d (%d)"
backends/trace-events iommufd_backend_map_file_dma(int iommufd, uint32_t · line 22
Quoted 1 line
backends/trace-events +0
-0iommufd_backend_set_dirty(int iommufd, uint32_t hwpt_id, bool start, int ret)" iommufd=%d hwpt=%u enable=%d (%d)"
PATCH 3/5 intel_iommu_accel: Add PRQ injection for passthrough device
In reply to Zhenzhong DuanWhen the guest enables the PRQ in vIOMMU, allocate a FAULTQ object so that host-side recoverable fault events can be received and propagated back to the guest.
Install an event handler on the FAULTQ fd to read and propagate host generated recoverable fault events to the guest.
The handler runs in QEMU's main loop, using a non-blocking fd registered via qemu_set_fd_handler().
--- hw/i386/intel_iommu_accel.h | 2 + hw/i386/intel_iommu_accel.c | 151 +++++++++++++++++++++++++++++++++++- hw/i386/trace-events | 1 + 3 files changed, 150 insertions(+), 4 deletions(-)
hw/i386/intel_iommu_accel.h +2 -0
@@ -17,6 +17,8 @@ typedef struct VTDAccelPASIDCacheEntry {VTDPASIDEntry pasid_entry;uint32_t pasid;uint32_t fs_hwpt_id; +uint32_t fault_id; +int fault_fd;QLIST_ENTRY (VTDAccelPASIDCacheEntry) next; } VTDAccelPASIDCacheEntry;
hw/i386/intel_iommu_accel.c +147 -4
208 lines not on this page · raw message
hw/i386/trace-events +1 -0
@@ -77,6 +77,7 @@ vtd_reset_exit(void) "" vtd_device_attach_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int ret) "dev_id %d pasid %d hwpt_id %d, ret: %d" vtd_device_detach_hwpt(uint32_t dev_id, uint32_t pasid, int ret) "dev_id %d pasid %d ret: %d" vtd_device_reattach_def_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int ret) "dev_id %d pasid %d hwpt_id %d, ret: %d" +vtd_prq_read_fault(uint32_t fault_id, uint32_t fault_fd, ssize_t bytes) "fault_id %d fault_fd %d ret: %zd" # amd_iommu.c amdvi_evntlog_fail(uint64_t addr, uint32_t head) "error: fail to write at addr 0x%"PRIx64" + offset 0x%"PRIx32
Signature · 1 line
2.47.3
Re: [PATCH 3/5] intel_iommu_accel: Add PRQ injection for passthrough device
Quoted 66 lines · Zhenzhong Duan
When the guest enables the PRQ in vIOMMU, allocate a FAULTQ object so that host-side recoverable fault events can be received and propagated back to the guest.
Install an event handler on the FAULTQ fd to read and propagate host generated recoverable fault events to the guest.
The handler runs in QEMU's main loop, using a non-blocking fd registered via
qemu_set_fd_handler().Signed-off-by Zhenzhong Duan <[email protected]>--- hw/i386/intel_iommu_accel.h | 2 + hw/i386/intel_iommu_accel.c | 151 +++++++++++++++++++++++++++++++++++- hw/i386/trace-events | 1 + 3 files changed, 150 insertions(+), 4 deletions(-)hw/i386/intel_iommu_accel.h +2
-0@@ -17,6 +17,8 @@ typedef struct VTDAccelPASIDCacheEntry {VTDPASIDEntry pasid_entry;uint32_t pasid;uint32_t fs_hwpt_id; +uint32_t fault_id; +int fault_fd;QLIST_ENTRY (VTDAccelPASIDCacheEntry) next; } VTDAccelPASIDCacheEntry;hw/i386/intel_iommu_accel.c +12
-2@@ -9,6 +9,7 @@*/ #include "qemu/osdep.h" +#include "qemu/error-report.h" #include "system/iommufd.h" #include "intel_iommu_internal.h" #include "intel_iommu_accel.h" @@ -38,6 +39,8 @@ static inline int vtd_hiod_get_pe_from_pasid(VTDAccelPASIDCacheEntry *vtd_pce,return vtd_ce_get_pasid_entry (s, &ce, pe, pasid); } +static PCIIOMMUOps *accel_ops; +bool vtd_check_hiod_accel (IntelIOMMUState *s,VTDHostIOMMUDevice *vtd_hiod,Error **errp) {@@ -99,8 +102,137 @@ VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as)return NULL ; } -static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod, - VTDPASIDEntry *pe, uint32_t *fs_hwpt_id, +static void vtd_prq_report_fault (VTDAccelPASIDCacheEntry *vtd_pce, +struct iommu_hwpt_pgfault *fault,int cnt) +{ +VTDHostIOMMUDevice *vtd_hiod = vtd_pce->vtd_hiod; + +for (; cnt--; fault++) { +bool last_page = fault->flags &IOMMU_PGFAULT_FLAGS_LAST_PAGE ; + + accel_ops->pri_request_page(vtd_hiod->bus, vtd_hiod->iommu_state,
hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 123
Quoted 10 lines
hw/i386/intel_iommu_accel.c +10
-0+ vtd_hiod->devfn, vtd_pce->pasid, + fault->perm &IOMMU_PGFAULT_PERM_PRIV , + fault->perm &IOMMU_PGFAULT_PERM_EXEC , + fault->addr, last_page, fault->grpid, + fault->perm &IOMMU_PGFAULT_PERM_READ , + fault->perm & IOMMU_PGFAULT_PERM_WRITE); + } +} + +#define FAULTQ_BUF_SIZE 100/* Large enough in regular test */
hmmm. the comment is misleading, any evidence show this is large enough? Or modify the comment a bit like below:
/* Batch size per read(); remaining faults trigger another callback */
Quoted 44 lines
hw/i386/intel_iommu_accel.c +44
-0+ +static void vtd_prq_read_fault (void *opaque) +{ +VTDAccelPASIDCacheEntry *vtd_pce = opaque; +struct iommu_hwpt_pgfault fault[FAULTQ_BUF_SIZE ]; +uint32_t id = vtd_pce->fault_id, fd = vtd_pce->fault_fd; +ssize_t bytes, last_bytes; + + bytes =read (fd, fault,sizeof (fault)); +trace_vtd_prq_read_fault (id, fd, bytes); +if (bytes <0 ) { +if (errno !=EAGAIN && errno !=EINTR ) { +error_report_once ("FAULTQ(id %u): read failed (%m)" , id); + } +return ; + }else if (!bytes) { +error_report_once ("FAULTQ(id %u): fault group too big" , id); +return ; + } + + last_bytes = bytes %sizeof (fault[0 ]); +if (last_bytes) { +error_report_once ("FAULTQ(id %u): discard partial fault data: %zd/%zu" , + id, last_bytes,sizeof (fault)); + } + +vtd_prq_report_fault (vtd_pce, fault, bytes /sizeof (fault[0 ])); +} + +static void vtd_destroy_fs_faultq (VTDHostIOMMUDevice *vtd_hiod, +uint32_t fault_id,uint32_t fault_fd) +{ +HostIOMMUDeviceIOMMUFD *idev =HOST_IOMMU_DEVICE_IOMMUFD (vtd_hiod->hiod); + +if (!fault_id) { +return ; + } + +close (fault_fd); +iommufd_backend_free_id (idev->iommufd, fault_id); +} + +static bool vtd_create_fs_faultq(VTDHostIOMMUDevice *vtd_hiod, + uint32_t *fault_id_p, uint32_t *fault_fd_p,
hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 177
Quoted 10 lines
hw/i386/intel_iommu_accel.c +10
-0+ Error **errp) +{ +HostIOMMUDeviceIOMMUFD *idev =HOST_IOMMU_DEVICE_IOMMUFD (vtd_hiod->hiod); +IntelIOMMUState *s = vtd_hiod->iommu_state; +uint8_t bus_n =pci_bus_num (vtd_hiod->bus); +uint32_t fault_id, fault_fd; +VTDContextEntry ce; +int flags; + + if (!s->svm ||
s->svm is first referenced here, but no definition found.
Quoted 23 lines
hw/i386/intel_iommu_accel.c +23
-0+vtd_dev_to_context_entry (s, bus_n, vtd_hiod->devfn, &ce) || + !VTD_CE_GET_PRE(&ce)) { + *fault_id_p =0 ; +return true; + } + +if (!iommufd_backend_alloc_faultq (idev->iommufd, &fault_id, &fault_fd, + errp)) { +return false; + } + + flags =fcntl (fault_fd,F_GETFL ); +if (flags <0 ) { +error_setg_errno (errp, errno,"Failed to get flags for FAULTQ fd" ); + goto free_faultq; + } + +if (fcntl (fault_fd,F_SETFL , flags |O_NONBLOCK ) <0 ) { +error_setg_errno (errp, errno,"Failed to set O_NONBLOCK on FAULTQ fd" ); + goto free_faultq; + } + + *fault_id_p = fault_id;
hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 210
Quoted 10 lines
hw/i386/intel_iommu_accel.c +10
-0+ *fault_fd_p = fault_fd; +return true; + +free_faultq: +vtd_destroy_fs_faultq (vtd_hiod, fault_id, fault_fd); +return false; +} + +static void vtd_destroy_old_fs_faultq (VTDHostIOMMUDevice *vtd_hiod, +VTDAccelPASIDCacheEntry *vtd_pce)
vtd_pce records vtd_hiod? Also, the naming is strange. how about?
vtd_create_fs_faultq() -> vtd_alloc_fs_faultq() vtd_destroy_fs_faultq() -> vtd_free_fs_faultq() vtd_setup_fs_faultq() -> vtd_attach_fs_faultq() vtd_destroy_old_fs_faultq() -> vtd_destroy_release_fs_faultq()
Quoted 5 lines
hw/i386/intel_iommu_accel.c +5
-0+{ +if (!vtd_pce->fault_id) { +return ; + } +
hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 225
Quoted 10 lines
hw/i386/intel_iommu_accel.c +10
-0+qemu_set_fd_handler (vtd_pce->fault_fd,NULL ,NULL ,NULL ); +vtd_destroy_fs_faultq (vtd_hiod, vtd_pce->fault_id, vtd_pce->fault_fd); + vtd_pce->fault_id =0 ; + vtd_pce->fault_fd =-1 ; +} + +static void vtd_setup_fs_faultq (VTDAccelPASIDCacheEntry *vtd_pce, +uint32_t fault_id,uint32_t fault_fd) +{ +if (!fault_id) {
using zero to check fault_id is not reliable.
Quoted 85 lines
hw/i386/intel_iommu_accel.c +23
-2+return ; + } + + vtd_pce->fault_id = fault_id; + vtd_pce->fault_fd = fault_fd; +qemu_set_fd_handler (fault_fd, vtd_prq_read_fault,NULL , vtd_pce); +} + +static bool vtd_create_fs_hwpt (VTDHostIOMMUDevice *vtd_hiod,VTDPASIDEntry *pe, +uint32_t fault_id,uint32_t *fs_hwpt_id,Error **errp) {HostIOMMUDeviceIOMMUFD *idev =HOST_IOMMU_DEVICE_IOMMUFD (vtd_hiod->hiod);@@ -115,7 +247,8 @@ static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,return iommufd_backend_alloc_hwpt (idev->iommufd, idev->devid, idev->hwpt_id, flags,IOMMU_HWPT_DATA_VTD_S1 , -sizeof (vtd), &vtd,0 , fs_hwpt_id, errp); +sizeof (vtd), &vtd, fault_id, fs_hwpt_id, + errp); } static void vtd_destroy_old_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,@@ -137,6 +270,7 @@ static bool vtd_device_attach_iommufd(VTDAccelPASIDCacheEntry *vtd_pce,HostIOMMUDeviceIOMMUFD *idev =HOST_IOMMU_DEVICE_IOMMUFD (vtd_hiod->hiod);VTDPASIDEntry *pe = &vtd_pce->pasid_entry;uint32_t hwpt_id = idev->hwpt_id, pasid = vtd_pce->pasid; +uint32_t fault_id =0 , fault_fd;bool ret;/* @@ -151,7 +285,11 @@ static bool vtd_device_attach_iommufd(VTDAccelPASIDCacheEntry *vtd_pce, }if (vtd_pe_pgtt_is_fst (pe)) { -if (!vtd_create_fs_hwpt (vtd_hiod, pe, &hwpt_id, errp)) { +if (!vtd_create_fs_faultq (vtd_hiod, &fault_id, &fault_fd, errp)) { +return false; + } +if (!vtd_create_fs_hwpt (vtd_hiod, pe, fault_id, &hwpt_id, errp)) { +vtd_destroy_fs_faultq (vtd_hiod, fault_id, fault_fd);return false; } }@@ -161,11 +299,14 @@ static bool vtd_device_attach_iommufd(VTDAccelPASIDCacheEntry *vtd_pce,if (ret) {/* Destroy old fs_hwpt if it's a replacement */ vtd_destroy_old_fs_hwpt (vtd_hiod, vtd_pce); +vtd_destroy_old_fs_faultq (vtd_hiod, vtd_pce);if (vtd_pe_pgtt_is_fst (pe)) { vtd_pce->fs_hwpt_id = hwpt_id; +vtd_setup_fs_faultq (vtd_pce, fault_id, fault_fd); } }else if (vtd_pe_pgtt_is_fst (pe)) {iommufd_backend_free_id (idev->iommufd, hwpt_id); +vtd_destroy_fs_faultq (vtd_hiod, fault_id, fault_fd); }return ret;@@ -197,6 +338,7 @@ static bool vtd_device_detach_iommufd(VTDAccelPASIDCacheEntry *vtd_pce,if (ret) {vtd_destroy_old_fs_hwpt (vtd_hiod, vtd_pce); +vtd_destroy_old_fs_faultq (vtd_hiod, vtd_pce); }return ret;@@ -549,4 +691,5 @@ static uint64_t vtd_get_host_iommu_quirks(uint32_t type,void vtd_iommu_ops_update_accel (PCIIOMMUOps *ops) { ops->get_host_iommu_quirks = vtd_get_host_iommu_quirks; + accel_ops = ops; }hw/i386/trace-events +1
-0@@ -77,6 +77,7 @@ vtd_reset_exit(void) "" vtd_device_attach_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int ret) "dev_id %d pasid %d hwpt_id %d, ret: %d" vtd_device_detach_hwpt(uint32_t dev_id, uint32_t pasid, int ret) "dev_id %d pasid %d ret: %d" vtd_device_reattach_def_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int ret) "dev_id %d pasid %d hwpt_id %d, ret: %d" +vtd_prq_read_fault(uint32_t fault_id, uint32_t fault_fd, ssize_t bytes) "fault_id %d fault_fd %d ret: %zd" # amd_iommu.c amdvi_evntlog_fail(uint64_t addr, uint32_t head) "error: fail to write at addr 0x%"PRIx64" + offset 0x%"PRIx32
RE: [PATCH 3/5] intel_iommu_accel: Add PRQ injection for passthrough device
Quoted 82 lines
-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH 3/5]intel_iommu_accel: Add PRQ injection for passthrough deviceQuoted 7 lines · Zhenzhong Duan
When the guest enables the PRQ in vIOMMU, allocate a FAULTQ object so that host-side recoverable fault events can be received and propagated back to the guest.
Install an event handler on the FAULTQ fd to read and propagate host generated recoverable fault events to the guest.
Quoted 7 lines
The handler runs in QEMU's main loop, using a non-blocking fd registered via
qemu_set_fd_handler().Signed-off-by Zhenzhong Duan <[email protected]>--- hw/i386/intel_iommu_accel.h | 2 + hw/i386/intel_iommu_accel.c | 151+++++++++++++++++++++++++++++++++++-
hw/i386/intel_iommu_accel.c static inline int
Quoted 29 lines
hw/i386/trace-events | 1 + 3 files changed, 150 insertions(+), 4 deletions(-)hw/i386/intel_iommu_accel.h +2
-0@@ -17,6 +17,8 @@ typedef struct VTDAccelPASIDCacheEntry {VTDPASIDEntry pasid_entry;uint32_t pasid;uint32_t fs_hwpt_id; +uint32_t fault_id; +int fault_fd;QLIST_ENTRY (VTDAccelPASIDCacheEntry) next; } VTDAccelPASIDCacheEntry;hw/i386/intel_iommu_accel.c +1
-0@@ -9,6 +9,7 @@*/ #include "qemu/osdep.h" +#include "qemu/error-report.h" #include "system/iommufd.h" #include "intel_iommu_internal.h" #include "intel_iommu_accel.h" @@ -38,6 +39,8 @@ static inline intvtd_hiod_get_pe_from_pasid(VTDAccelPASIDCacheEntry
*vtd_pce,hw/i386/intel_iommu_accel.c static inline int vtd_hiod_get_pe_from_pasid(VTDAccelPASIDCacheEntry *vtd_pce,
Quoted 6 lines
hw/i386/intel_iommu_accel.c +2
-0return vtd_ce_get_pasid_entry (s, &ce, pe, pasid); } +static PCIIOMMUOps *accel_ops; + bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice
*vtd_hiod,hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice
Quoted 3 lines
hw/i386/intel_iommu_accel.c +0
-0Error **errp) {@@ -99,8 +102,137 @@ VTDHostIOMMUDevice*vtd_find_hiod_iommufd(VTDAddressSpace *as)
Quoted 18 lines
hw/i386/intel_iommu_accel.c +13
-2return NULL ; } -static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod, - VTDPASIDEntry *pe, uint32_t *fs_hwpt_id, +static void vtd_prq_report_fault (VTDAccelPASIDCacheEntry *vtd_pce, +struct iommu_hwpt_pgfault *fault,int cnt) +{ +VTDHostIOMMUDevice *vtd_hiod = vtd_pce->vtd_hiod; + +for (; cnt--; fault++) { +bool last_page = fault->flags &IOMMU_PGFAULT_FLAGS_LAST_PAGE ; + + accel_ops->pri_request_page(vtd_hiod->bus, vtd_hiod->iommu_state, + vtd_hiod->devfn, vtd_pce->pasid, + fault->perm & IOMMU_PGFAULT_PERM_PRIV, + fault->perm & IOMMU_PGFAULT_PERM_EXEC, + fault->addr, last_page, fault->grpid,
Quoted 8 lines
hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 123
Quoted 6 lines
hw/i386/intel_iommu_accel.c +6
-0+ fault->perm &IOMMU_PGFAULT_PERM_READ , + fault->perm & IOMMU_PGFAULT_PERM_WRITE); + } +} + +#define FAULTQ_BUF_SIZE 100/* Large enough in regular test */ hmmm. the comment is misleading, any evidence show this is large enough?
By running dsa_test app, I see the size is much less than 100.
Quoted 3 lines
Or modify the comment a bit like below:
/* Batch size per
read(); remaining faults trigger another callback */
Sounds good, will do.
Quoted 58 lines
Quoted 24 lines
hw/i386/intel_iommu_accel.c +23
-0+ +static void vtd_prq_read_fault (void *opaque) +{ +VTDAccelPASIDCacheEntry *vtd_pce = opaque; +struct iommu_hwpt_pgfault fault[FAULTQ_BUF_SIZE ]; +uint32_t id = vtd_pce->fault_id, fd = vtd_pce->fault_fd; +ssize_t bytes, last_bytes; + + bytes =read (fd, fault,sizeof (fault)); +trace_vtd_prq_read_fault (id, fd, bytes); +if (bytes <0 ) { +if (errno !=EAGAIN && errno !=EINTR ) { +error_report_once ("FAULTQ(id %u): read failed (%m)" , id); + } +return ; + }else if (!bytes) { +error_report_once ("FAULTQ(id %u): fault group too big" , id); +return ; + } + + last_bytes = bytes %sizeof (fault[0 ]); +if (last_bytes) { + error_report_once("FAULTQ(id %u): discard partial fault data: %zd/%zu",hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as)
Quoted 10 lines
hw/i386/intel_iommu_accel.c +10
-0+ id, last_bytes, sizeof(fault)); + } + +vtd_prq_report_fault (vtd_pce, fault, bytes /sizeof (fault[0 ])); +} + +static void vtd_destroy_fs_faultq (VTDHostIOMMUDevice *vtd_hiod, +uint32_t fault_id,uint32_t fault_fd) +{ + HostIOMMUDeviceIOMMUFD *idev =
HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);Quoted 4 lines
hw/i386/intel_iommu_accel.c +4
-0+ +if (!fault_id) { +return ; + }hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as)
Quoted 10 lines
hw/i386/intel_iommu_accel.c +10
-0+ +close (fault_fd); +iommufd_backend_free_id (idev->iommufd, fault_id); +} + +static bool vtd_create_fs_faultq (VTDHostIOMMUDevice *vtd_hiod, +uint32_t *fault_id_p,uint32_t *fault_fd_p, +Error **errp) +{ + HostIOMMUDeviceIOMMUFD *idev =
HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 177
Quoted 7 lines
hw/i386/intel_iommu_accel.c +7
-0+IntelIOMMUState *s = vtd_hiod->iommu_state; +uint8_t bus_n =pci_bus_num (vtd_hiod->bus); +uint32_t fault_id, fault_fd; +VTDContextEntry ce; +int flags; + + if (!s->svm ||
Quoted 1 line
s->svmis first referenced here, but no definition found.
It's a vtd property:
DEFINE_PROP_BOOL("svm", IntelIOMMUState, svm, false),
Quoted 28 lines
Quoted 28 lines
hw/i386/intel_iommu_accel.c +27
-0+vtd_dev_to_context_entry (s, bus_n, vtd_hiod->devfn, &ce) || + !VTD_CE_GET_PRE(&ce)) { + *fault_id_p =0 ; +return true; + } + +if (!iommufd_backend_alloc_faultq (idev->iommufd, &fault_id, &fault_fd, + errp)) { +return false; + } + + flags =fcntl (fault_fd,F_GETFL ); +if (flags <0 ) { +error_setg_errno (errp, errno,"Failed to get flags for FAULTQ fd" ); + goto free_faultq; + } + +if (fcntl (fault_fd,F_SETFL , flags |O_NONBLOCK ) <0 ) { +error_setg_errno (errp, errno,"Failed to set O_NONBLOCK on FAULTQ fd" ); + goto free_faultq; + } + + *fault_id_p = fault_id; + *fault_fd_p = fault_fd; +return true; + +free_faultq:
Quoted 8 lines
hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 210
Quoted 6 lines
hw/i386/intel_iommu_accel.c +6
-0+vtd_destroy_fs_faultq (vtd_hiod, fault_id, fault_fd); +return false; +} + +static void vtd_destroy_old_fs_faultq (VTDHostIOMMUDevice *vtd_hiod, +VTDAccelPASIDCacheEntry *vtd_pce)
vtd_pcerecordsvtd_hiod? Also, the naming is strange. how about?
Will delete parameter vtd_hiod.
Quoted 4 lines
vtd_create_fs_faultq()->vtd_alloc_fs_faultq()vtd_destroy_fs_faultq()->vtd_free_fs_faultq()
We had used create/destroy term for * _fs_hwpt()
Rename as alloc/free term for faultq would make code naming mixed.
See below code piece:
if (vtd_pe_pgtt_is_fst(pe)) {
if (!vtd_create_fs_faultq(vtd_hiod, &fault_id, &fault_fd, errp)) {
return false;
}
if (!vtd_create_fs_hwpt(vtd_hiod, pe, fault_id, &hwpt_id, errp)) {
vtd_destroy_fs_faultq(vtd_hiod, fault_id, fault_fd);
return false;
}
}
ret = host_iommu_device_iommufd_attach_hwpt(hiodi, pasid, hwpt_id, errp);
trace_vtd_device_attach_hwpt(hiodi->devid, pasid, hwpt_id, ret);
if (ret) {
/* Destroy old fs_hwpt if it's a replacement */
vtd_destroy_old_fs_hwpt(vtd_pce);
vtd_destroy_old_fs_faultq(vtd_pce);
if (vtd_pe_pgtt_is_fst(pe)) {
vtd_pce->fs_hwpt_id = hwpt_id;
vtd_setup_fs_faultq(vtd_pce, fault_id, fault_fd);
}
} else if (vtd_pe_pgtt_is_fst(pe)) {
iommufd_backend_free_id(hiodi->iommufd, hwpt_id);
vtd_destroy_fs_faultq(vtd_hiod, fault_id, fault_fd);
}Quoted 1 line
vtd_setup_fs_faultq()->vtd_attach_fs_faultq()
vtd_attach_fs_faultq() isn't an attachment op, so this renaming is confusing.
Quoted 1 line
vtd_destroy_old_fs_faultq()->vtd_destroy_release_fs_faultq()
vtd_free_old_fs_faultq() looks better, vtd_destroy_release_fs_faultq() doesn't show it's freeing old faultq.
Quoted 10 lines
Quoted 10 lines
hw/i386/intel_iommu_accel.c +9
-0+{ +if (!vtd_pce->fault_id) { +return ; + } + +qemu_set_fd_handler (vtd_pce->fault_fd,NULL ,NULL ,NULL ); +vtd_destroy_fs_faultq (vtd_hiod, vtd_pce->fault_id, vtd_pce->fault_fd); + vtd_pce->fault_id =0 ; + vtd_pce->fault_fd =-1 ;
Quoted 8 lines
hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 225
Quoted 6 lines
hw/i386/intel_iommu_accel.c +6
-0+} + +static void vtd_setup_fs_faultq (VTDAccelPASIDCacheEntry *vtd_pce, +uint32_t fault_id,uint32_t fault_fd) +{ +if (!fault_id) {using zero to check
fault_idis not reliable.
Yes, I'm waiting for JasonG and Nicolin's reply on this.
Thanks
Zhenzhong
Quoted 101 lines
hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as)
Quoted 10 lines
hw/i386/intel_iommu_accel.c +9
-0+return ; + } + + vtd_pce->fault_id = fault_id; + vtd_pce->fault_fd = fault_fd; +qemu_set_fd_handler (fault_fd, vtd_prq_read_fault,NULL , vtd_pce); +} + +static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,VTDPASIDEntry *pe,
hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as)
Quoted 4 lines
hw/i386/intel_iommu_accel.c +1
-0+ uint32_t fault_id, uint32_t *fs_hwpt_id, Error **errp) { HostIOMMUDeviceIOMMUFD *idev =
HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);hw/i386/intel_iommu_accel.c static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice
Quoted 1 line
hw/i386/intel_iommu_accel.c +0
-0@@ -115,7 +247,8 @@ static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice
*vtd_hiod,hw/i386/intel_iommu_accel.c static bool
Quoted 11 lines
hw/i386/intel_iommu_accel.c +2
-1return iommufd_backend_alloc_hwpt (idev->iommufd, idev->devid, idev- hwpt_id, flags,IOMMU_HWPT_DATA_VTD_S1 , -sizeof (vtd), &vtd,0 , fs_hwpt_id, errp); +sizeof (vtd), &vtd, fault_id, fs_hwpt_id, + errp); } static void vtd_destroy_old_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,@@ -137,6 +270,7 @@ static boolvtd_device_attach_iommufd(VTDAccelPASIDCacheEntry
*vtd_pce,hw/i386/intel_iommu_accel.c static bool · line 270
Quoted 1 line
hw/i386/intel_iommu_accel.c +0
-0HostIOMMUDeviceIOMMUFD *idev =
HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);hw/i386/intel_iommu_accel.c static bool
Quoted 7 lines
hw/i386/intel_iommu_accel.c +1
-0VTDPASIDEntry *pe = &vtd_pce->pasid_entry;uint32_t hwpt_id = idev->hwpt_id, pasid = vtd_pce->pasid; +uint32_t fault_id =0 , fault_fd;bool ret;/* @@ -151,7 +285,11 @@ static boolvtd_device_attach_iommufd(VTDAccelPASIDCacheEntry
*vtd_pce,hw/i386/intel_iommu_accel.c static bool
Quoted 13 lines
hw/i386/intel_iommu_accel.c +5
-1}if (vtd_pe_pgtt_is_fst (pe)) { -if (!vtd_create_fs_hwpt (vtd_hiod, pe, &hwpt_id, errp)) { +if (!vtd_create_fs_faultq (vtd_hiod, &fault_id, &fault_fd, errp)) { +return false; + } +if (!vtd_create_fs_hwpt (vtd_hiod, pe, fault_id, &hwpt_id, errp)) { +vtd_destroy_fs_faultq (vtd_hiod, fault_id, fault_fd);return false; } }@@ -161,11 +299,14 @@ static boolvtd_device_attach_iommufd(VTDAccelPASIDCacheEntry
*vtd_pce,hw/i386/intel_iommu_accel.c static bool
Quoted 15 lines
hw/i386/intel_iommu_accel.c +3
-0if (ret) {/* Destroy old fs_hwpt if it's a replacement */ vtd_destroy_old_fs_hwpt (vtd_hiod, vtd_pce); +vtd_destroy_old_fs_faultq (vtd_hiod, vtd_pce);if (vtd_pe_pgtt_is_fst (pe)) { vtd_pce->fs_hwpt_id = hwpt_id; +vtd_setup_fs_faultq (vtd_pce, fault_id, fault_fd); } }else if (vtd_pe_pgtt_is_fst (pe)) {iommufd_backend_free_id (idev->iommufd, hwpt_id); +vtd_destroy_fs_faultq (vtd_hiod, fault_id, fault_fd); }return ret;@@ -197,6 +338,7 @@ static boolvtd_device_detach_iommufd(VTDAccelPASIDCacheEntry
*vtd_pce,hw/i386/intel_iommu_accel.c static uint64_t vtd_get_host_iommu_quirks(uint32_t
Quoted 8 lines
hw/i386/intel_iommu_accel.c +1
-0if (ret) {vtd_destroy_old_fs_hwpt (vtd_hiod, vtd_pce); +vtd_destroy_old_fs_faultq (vtd_hiod, vtd_pce); }return ret;@@ -549,4 +691,5 @@ static uint64_t vtd_get_host_iommu_quirks(uint32_ttype,
hw/i386/trace-events vtd_reset_exit(void) "" · line 77
Quoted 11 lines
hw/i386/intel_iommu_accel.c +1
-0void vtd_iommu_ops_update_accel (PCIIOMMUOps *ops) { ops->get_host_iommu_quirks = vtd_get_host_iommu_quirks; + accel_ops = ops; }hw/i386/trace-events +0
-0@@ -77,6 +77,7 @@ vtd_reset_exit(void) "" vtd_device_attach_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, intret) "
dev_id%d pasid %dhwpt_id%d, ret: %d"hw/i386/trace-events vtd_reset_exit(void) "" · line 78
Quoted 1 line
hw/i386/trace-events +0
-0vtd_device_detach_hwpt(uint32_t dev_id, uint32_t pasid, int ret) "dev_id %dpasid %d ret: %d"
hw/i386/trace-events vtd_reset_exit(void) "" · line 79
Quoted 1 line
hw/i386/trace-events +0
-0vtd_device_reattach_def_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t
hwpt_id, int ret) "dev_id%d pasid %dhwpt_id%d, ret: %d"hw/i386/trace-events vtd_reset_exit(void) "" · line 80
Quoted 1 line
hw/i386/trace-events +1
-0+vtd_prq_read_fault(uint32_t fault_id, uint32_t fault_fd, ssize_t bytes)"
fault_id%dfault_fd%d ret: %zd"hw/i386/trace-events vtd_reset_exit(void) "" · line 82
Quoted 3 lines
hw/i386/trace-events +0
-0# amd_iommu.c amdvi_evntlog_fail(uint64_t addr, uint32_t head) "error: fail to write at addr0x%"PRIx64" + offset 0x%"PRIx32
PATCH 4/5 intel_iommu_accel: Accept PRQ response for passthrough device
In reply to Zhenzhong DuanPropagate guest's PRQ response to host by writing to fault_fd. Create a new VTDPRQEntry to cache cookie for each fault group, this cookie is used to mark the fault group on host side.
--- hw/i386/intel_iommu_accel.h | 14 ++++++++ include/hw/i386/intel_iommu.h | 6 ++++ hw/i386/intel_iommu.c | 4 +++ hw/i386/intel_iommu_accel.c | 65 +++++++++++++++++++++++++++++++++++ hw/i386/trace-events | 1 + 5 files changed, 90 insertions(+)
hw/i386/intel_iommu_accel.h +14 -0
@@ -19,6 +19,9 @@ typedef struct VTDAccelPASIDCacheEntry {uint32_t fs_hwpt_id;uint32_t fault_id;int fault_fd; + QLIST_HEAD(, VTDPRQEntry) vtd_prq_list; +IOMMUPRINotifier pri_notifier_entry; +IOMMUPRINotifier *pri_notifier;QLIST_ENTRY (VTDAccelPASIDCacheEntry) next; } VTDAccelPASIDCacheEntry;
@@ -31,6 +34,9 @@ void vtd_flush_host_piotlb_all_accel(IntelIOMMUState *s, uint16_t domain_id, uint64_t npages, bool ih);void vtd_pasid_cache_sync_accel (IntelIOMMUState *s,VTDPASIDCacheInfo *pc_info);void vtd_pasid_cache_reset_accel (IntelIOMMUState *s); +bool vtd_propagate_page_group_response_accel (IntelIOMMUState *s, +uint16_t rid,uint32_t pasid, +IOMMUPRIResponse *response);void vtd_iommu_ops_update_accel (PCIIOMMUOps *ops);#else static inline bool vtd_check_hiod_accel(IntelIOMMUState *s,
@@ -69,6 +75,14 @@ static inline void vtd_pasid_cache_reset_accel(IntelIOMMUState *s) { } +static inline +bool vtd_propagate_page_group_response_accel (IntelIOMMUState *s, +uint16_t rid,uint32_t pasid, +IOMMUPRIResponse *response) +{ +return false; +} +static inline void vtd_iommu_ops_update_accel (PCIIOMMUOps *ops) { }
include/hw/i386/intel_iommu.h +6 -0
@@ -100,6 +100,12 @@ typedef struct VTDPASIDCacheEntry {bool valid; } VTDPASIDCacheEntry; +typedef struct VTDPRQEntry { +uint32_t grpid; +uint32_t cookie; +QLIST_ENTRY (VTDPRQEntry) next; +}VTDPRQEntry ; +struct VTDAddressSpace {PCIBus *bus;uint8_t devfn;
hw/i386/intel_iommu.c +4 -0
@@ -3390,6 +3390,10 @@ static bool vtd_process_page_group_response_desc(IntelIOMMUState *s, response.response_code =IOMMU_PRI_RESP_FAILURE ; } +if (vtd_propagate_page_group_response_accel (s, rid, pasid, &response)) { +return true; + } +if (vtd_dev_as->pri_notifier) { vtd_dev_as->pri_notifier->notify (vtd_dev_as->pri_notifier, &response); }
hw/i386/intel_iommu_accel.c +65 -0
101 lines not on this page · raw message
hw/i386/trace-events +1 -0
@@ -78,6 +78,7 @@ vtd_device_attach_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int re vtd_device_detach_hwpt(uint32_t dev_id, uint32_t pasid, int ret) "dev_id %d pasid %d ret: %d" vtd_device_reattach_def_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int ret) "dev_id %d pasid %d hwpt_id %d, ret: %d" vtd_prq_read_fault(uint32_t fault_id, uint32_t fault_fd, ssize_t bytes) "fault_id %d fault_fd %d ret: %zd" +vtd_prq_response_notify(uint32_t fault_id, uint32_t fault_fd, uint32_t cookie, uint32_t code, ssize_t bytes) "fault_id %d fault_fd %d cookie %d code %d ret: %zd" # amd_iommu.c amdvi_evntlog_fail(uint64_t addr, uint32_t head) "error: fail to write at addr 0x%"PRIx64" + offset 0x%"PRIx32
Signature · 1 line
2.47.3
Re: [PATCH 4/5] intel_iommu_accel: Accept PRQ response for passthrough device
Quoted 59 lines · Zhenzhong Duan
Propagate guest's PRQ response to host by writing to
fault_fd. Create a new VTDPRQEntry to cache cookie for each fault group, this cookie is used to mark the fault group on host side.---
hw/i386/intel_iommu_accel.h | 14 ++++++++include/hw/i386/intel_iommu.h| 6 ++++ hw/i386/intel_iommu.c | 4 +++ hw/i386/intel_iommu_accel.c | 65 +++++++++++++++++++++++++++++++++++ hw/i386/trace-events | 1 +5 files changed, 90 insertions(+)hw/i386/intel_iommu_accel.h +3
-0@@ -19,6 +19,9 @@ typedef struct VTDAccelPASIDCacheEntry { uint32_t fs_hwpt_id; uint32_t fault_id; int fault_fd; + QLIST_HEAD(, VTDPRQEntry) vtd_prq_list; + IOMMUPRINotifier pri_notifier_entry; + IOMMUPRINotifier *pri_notifier; QLIST_ENTRY(VTDAccelPASIDCacheEntry) next; } VTDAccelPASIDCacheEntry;hw/i386/intel_iommu_accel.h +3
-0@@ -31,6 +34,9 @@ void vtd_flush_host_piotlb_all_accel(IntelIOMMUState *s, uint16_t domain_id, uint64_t npages, bool ih); void vtd_pasid_cache_sync_accel(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info); void vtd_pasid_cache_reset_accel(IntelIOMMUState *s); +bool vtd_propagate_page_group_response_accel (IntelIOMMUState *s, + uint16_t rid, uint32_t pasid, + IOMMUPRIResponse *response); void vtd_iommu_ops_update_accel(PCIIOMMUOps *ops); #else static inline bool vtd_check_hiod_accel(IntelIOMMUState *s,@@ -69,6 +75,14 @@ static inline void vtd_pasid_cache_reset_accel(IntelIOMMUState *s) { }hw/i386/intel_iommu_accel.h +8
-0+static inline +bool vtd_propagate_page_group_response_accel (IntelIOMMUState *s, + uint16_t rid, uint32_t pasid, + IOMMUPRIResponse *response) +{ + return false; +} +static inline void
vtd_iommu_ops_update_accel(PCIIOMMUOps *ops){
}include/hw/i386/intel_iommu.h +0
-0@@ -100,6 +100,12 @@ typedef struct VTDPASIDCacheEntry { bool valid; } VTDPASIDCacheEntry;
Quoted 3 lines
+typedef struct VTDPRQEntry {
+uint32_tgrpid;
Hi Zhenzhon,
Maybe silly question, but why don't we keep naming it prgi?
Quoted 145 lines
+
uint32_tcookie;
+QLIST_ENTRY(VTDPRQEntry)next;include/hw/i386/intel_iommu.h +2
-0+} VTDPRQEntry; +struct VTDAddressSpace {
PCIBus *bus;
uint8_tdevfn;hw/i386/intel_iommu.c +0
-0@@ -3390,6 +3390,10 @@ static bool vtd_process_page_group_response_desc(IntelIOMMUState *s, response.response_code = IOMMU_PRI_RESP_FAILURE; }
+ if (vtd_propagate_page_group_response_accel(s, rid, pasid, &response)) { + return true;
+ }
+
if (vtd_dev_as->pri_notifier) {vtd_dev_as->pri_notifier->notify(vtd_dev_as->pri_notifier, &response); }hw/i386/intel_iommu_accel.c +0
-0@@ -102,6 +102,30 @@ VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) return NULL; }hw/i386/intel_iommu_accel.c +24
-0+bool vtd_propagate_page_group_response_accel (IntelIOMMUState *s, + uint16_t rid, uint32_t pasid, + IOMMUPRIResponse *response) +{ + VTDAddressSpace *vtd_as = vtd_get_as_by_sid(s, rid); + VTDAccelPASIDCacheEntry *vtd_pce; + VTDHostIOMMUDevice *vtd_hiod = vtd_find_hiod_iommufd(vtd_as); + + if (!vtd_hiod) { + return false; + } + + QLIST_FOREACH(vtd_pce, &vtd_hiod->pasid_cache_list, next) { + if (vtd_pce->pasid == pasid) { + if (vtd_pce->pri_notifier) { + vtd_pce->pri_notifier->notify(vtd_pce->pri_notifier, response); + } + return true; + } + } + + return false; +} +static void
vtd_prq_report_fault(VTDAccelPASIDCacheEntry *vtd_pce, struct iommu_hwpt_pgfault *fault, int cnt){hw/i386/intel_iommu_accel.c +7
-0@@ -117,6 +141,13 @@ static void vtd_prq_report_fault(VTDAccelPASIDCacheEntry *vtd_pce, fault->addr, last_page, fault->grpid, fault->perm & IOMMU_PGFAULT_PERM_READ, fault->perm & IOMMU_PGFAULT_PERM_WRITE); + if (last_page) { + VTDPRQEntry *prqe = g_malloc0(sizeof(*prqe)); + + prqe->grpid = fault->grpid; + prqe->cookie = fault->cookie; + QLIST_INSERT_HEAD(&vtd_pce->vtd_prq_list, prqe, next); + } } }hw/i386/intel_iommu_accel.c +0
-0@@ -150,6 +181,36 @@ static void vtd_prq_read_fault(void *opaque) vtd_prq_report_fault(vtd_pce, fault, bytes / sizeof(fault[0])); }hw/i386/intel_iommu_accel.c +30
-0+static void vtd_prq_response_notify (struct IOMMUPRINotifier *notifier, + IOMMUPRIResponse *response) +{ + VTDAccelPASIDCacheEntry *vtd_pce = + container_of(notifier, VTDAccelPASIDCacheEntry, pri_notifier_entry); + uint32_t id = vtd_pce->fault_id, fd = vtd_pce->fault_fd; + struct iommu_hwpt_page_response resp; + VTDPRQEntry *prqe, *tmp; + ssize_t bytes; + + QLIST_FOREACH_SAFE(prqe, &vtd_pce->vtd_prq_list, next, tmp) { + if (prqe->grpid != response->prgi) { + continue; + } + + resp.cookie = prqe->cookie; + resp.code = response->response_code; + bytes = write(fd, &resp, sizeof(resp)); + trace_vtd_prq_response_notify(id, fd, resp.cookie, resp.code, bytes); + if (bytes < 0) { + error_report_once("FAULTQ(id %u): write failed " + "[cookie 0x%x code 0x%x] (%m)", + id, resp.cookie, resp.code); + } + + QLIST_REMOVE(prqe, next); + g_free(prqe); + } +} +static void
vtd_destroy_fs_faultq(VTDHostIOMMUDevice *vtd_hiod, uint32_t fault_id, uint32_t fault_fd){hw/i386/intel_iommu_accel.c +0
-0@@ -213,6 +274,7 @@ static void vtd_destroy_old_fs_faultq(VTDHostIOMMUDevice *vtd_hiod, return; }
+vtd_pce->pri_notifier= NULL;qemu_set_fd_handler(vtd_pce->fault_fd, NULL, NULL, NULL);vtd_destroy_fs_faultq(vtd_hiod, vtd_pce->fault_id, vtd_pce->fault_fd);vtd_pce->fault_id= 0;hw/i386/intel_iommu_accel.c +0
-0@@ -228,6 +290,8 @@ static void vtd_setup_fs_faultq(VTDAccelPASIDCacheEntry *vtd_pce,
vtd_pce->fault_id=fault_id;
vtd_pce->fault_fd=fault_fd;
+vtd_pce->pri_notifier_entry.notify=vtd_prq_response_notify; +vtd_pce->pri_notifier=&vtd_pce->pri_notifier_entry;qemu_set_fd_handler(fault_fd, vtd_prq_read_fault, NULL, vtd_pce); }hw/i386/intel_iommu_accel.c +1
-0@@ -492,6 +556,7 @@ static void vtd_accel_fill_pc(VTDHostIOMMUDevice *vtd_hiod, uint32_t pasid, vtd_pce->vtd_hiod = vtd_hiod; vtd_pce->pasid = pasid; vtd_pce->pasid_entry = *pe; + QLIST_INIT(&vtd_pce->vtd_prq_list); QLIST_INSERT_HEAD(&vtd_hiod->pasid_cache_list, vtd_pce, next);
if (!vtd_device_attach_iommufd(vtd_pce,&local_err)) {hw/i386/trace-events +0
-0@@ -78,6 +78,7 @@ vtd_device_attach_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int revtd_device_detach_hwpt(uint32_t
dev_id,uint32_tpasid, int ret) "dev_id%d pasid %d ret: %d"
vtd_device_reattach_def_hwpt(uint32_tdev_id,uint32_tpasid,uint32_thwpt_id, int ret) "dev_id%d pasid %dhwpt_id%d, ret: %d"vtd_prq_read_fault(uint32_t fault_id, uint32_t fault_fd, ssize_t bytes)"fault_id%dfault_fd%d ret: %zd" +vtd_prq_response_notify(uint32_tfault_id,uint32_tfault_fd,uint32_tcookie,uint32_tcode,ssize_tbytes) "fault_id%dfault_fd%d cookie %d code %d ret: %zd"# amd_iommu.c amdvi_evntlog_fail(uint64_t addr, uint32_t head) "error: fail to write at addr 0x%"PRIx64" + offset 0x%"PRIx32
RE: [PATCH 4/5] intel_iommu_accel: Accept PRQ response for passthrough device
Hi Clement,
Quoted 72 lines
-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH 4/5]intel_iommu_accel: Accept PRQ response for passthrough deviceQuoted 5 lines · Zhenzhong Duan
Propagate guest's PRQ response to host by writing to
fault_fd. Create a new VTDPRQEntry to cache cookie for each fault group, this cookie is used to mark the fault group on host side.Signed-off-by Zhenzhong Duan<[[email protected]](mailto:[email protected])>
hw/i386/intel_iommu_accel.h void vtd_flush_host_piotlb_all_accel(IntelIOMMUState *s,
Quoted 23 lines
---
hw/i386/intel_iommu_accel.h | 14 ++++++++include/hw/i386/intel_iommu.h| 6 ++++ hw/i386/intel_iommu.c | 4 +++ hw/i386/intel_iommu_accel.c | 65 +++++++++++++++++++++++++++++++++++ hw/i386/trace-events | 1 +5 files changed, 90 insertions(+)hw/i386/intel_iommu_accel.h +3
-0@@ -19,6 +19,9 @@ typedef struct VTDAccelPASIDCacheEntry { uint32_t fs_hwpt_id; uint32_t fault_id; int fault_fd; + QLIST_HEAD(, VTDPRQEntry) vtd_prq_list; + IOMMUPRINotifier pri_notifier_entry; + IOMMUPRINotifier *pri_notifier; QLIST_ENTRY(VTDAccelPASIDCacheEntry) next; } VTDAccelPASIDCacheEntry;@@ -31,6 +34,9 @@ void vtd_flush_host_piotlb_all_accel(IntelIOMMUState *s,
uint16_tdomain_id,Quoted 2 lines
hw/i386/intel_iommu_accel.h +0
-0uint64_t npages, bool ih);void vtd_pasid_cache_sync_accel(IntelIOMMUState *s, VTDPASIDCacheInfo
*pc_info);hw/i386/intel_iommu_accel.h static inline void
Quoted 8 lines
void
vtd_pasid_cache_reset_accel(IntelIOMMUState *s);
+bool vtd_propagate_page_group_response_accel(IntelIOMMUState *s, +uint16_trid,uint32_tpasid, + IOMMUPRIResponse *response); voidvtd_iommu_ops_update_accel(PCIIOMMUOps *ops);
#else
static inline bool vtd_check_hiod_accel(IntelIOMMUState *s,hw/i386/intel_iommu_accel.h +0
-0@@ -69,6 +75,14 @@ static inline void
vtd_pasid_cache_reset_accel(IntelIOMMUState *s)Quoted 22 lines
hw/i386/intel_iommu_accel.h +8
-0{ } +static inline +bool vtd_propagate_page_group_response_accel (IntelIOMMUState *s, + uint16_t rid, uint32_t pasid, + IOMMUPRIResponse *response) +{ + return false; +} + static inline void vtd_iommu_ops_update_accel(PCIIOMMUOps *ops) { }include/hw/i386/intel_iommu.h +0
-0@@ -100,6 +100,12 @@ typedef struct VTDPASIDCacheEntry { bool valid; } VTDPASIDCacheEntry;
Quoted 6 lines
include/hw/i386/intel_iommu.h typedef struct VTDPASIDCacheEntry { · line 104
Quoted 2 lines
include/hw/i386/intel_iommu.h +2
-0+typedef struct VTDPRQEntry { + uint32_t grpid;Hi Zhenzhon,
Maybe silly question, but why don't we keep naming it prgi?
Good question, the naming is picked from uAPI, see below:
struct iommu_hwpt_pgfault {
__u32 flags;
__u32 dev_id;
__u32 pasid;
__u32 grpid;
__u32 perm;
__u32 __reserved;
__aligned_u64 addr;
__u32 length;
__u32 cookie;
};Because VTDPRQEntry caches page group id and cookie from kernel uAPI, so align the name with kernel.
Thanks
Zhenzhong
CLEMENT MATHIEU--DRIF
Reviewed-byRe: [PATCH 4/5] intel_iommu_accel: Accept PRQ response for passthrough device
Ok thanks for the clarification
Quoted 112 lines · Zhenzhong
Hi Clement,
Quoted 81 lines
-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[[email protected]](mailto:[email protected])>
Subject: Re: [PATCH 4/5]intel_iommu_accel: Accept PRQ response for
passthrough deviceQuoted 5 lines · Zhenzhong Duan
Propagate guest's PRQ response to host by writing to
fault_fd. Create a new VTDPRQEntry to cache cookie for each fault group, this cookie is used to mark the fault group on host side.Signed-off-by Zhenzhong Duan<[[[email protected]](mailto:[email protected])](mailto:[[email protected]](mailto:[email protected]))>
hw/i386/intel_iommu_accel.h void vtd_flush_host_piotlb_all_accel(IntelIOMMUState *s,
Quoted 23 lines
---
hw/i386/intel_iommu_accel.h | 14 ++++++++include/hw/i386/intel_iommu.h| 6 ++++ hw/i386/intel_iommu.c | 4 +++ hw/i386/intel_iommu_accel.c | 65 +++++++++++++++++++++++++++++++++++ hw/i386/trace-events | 1 +5 files changed, 90 insertions(+)hw/i386/intel_iommu_accel.h +3
-0@@ -19,6 +19,9 @@ typedef struct VTDAccelPASIDCacheEntry { uint32_t fs_hwpt_id; uint32_t fault_id; int fault_fd; + QLIST_HEAD(, VTDPRQEntry) vtd_prq_list; + IOMMUPRINotifier pri_notifier_entry; + IOMMUPRINotifier *pri_notifier; QLIST_ENTRY(VTDAccelPASIDCacheEntry) next; } VTDAccelPASIDCacheEntry;@@ -31,6 +34,9 @@ void vtd_flush_host_piotlb_all_accel(IntelIOMMUState *s,
uint16_tdomain_id,Quoted 2 lines
hw/i386/intel_iommu_accel.h +0
-0uint64_t npages, bool ih);void vtd_pasid_cache_sync_accel(IntelIOMMUState *s, VTDPASIDCacheInfo
*pc_info);hw/i386/intel_iommu_accel.h static inline void
Quoted 8 lines
void
vtd_pasid_cache_reset_accel(IntelIOMMUState *s);
+bool vtd_propagate_page_group_response_accel(IntelIOMMUState *s, +uint16_trid,uint32_tpasid, + IOMMUPRIResponse *response); voidvtd_iommu_ops_update_accel(PCIIOMMUOps *ops);
#else
static inline bool vtd_check_hiod_accel(IntelIOMMUState *s,hw/i386/intel_iommu_accel.h +0
-0@@ -69,6 +75,14 @@ static inline void
vtd_pasid_cache_reset_accel(IntelIOMMUState *s)Quoted 22 lines
hw/i386/intel_iommu_accel.h +8
-0{ } +static inline +bool vtd_propagate_page_group_response_accel (IntelIOMMUState *s, + uint16_t rid, uint32_t pasid, + IOMMUPRIResponse *response) +{ + return false; +} + static inline void vtd_iommu_ops_update_accel(PCIIOMMUOps *ops) { }include/hw/i386/intel_iommu.h +0
-0@@ -100,6 +100,12 @@ typedef struct VTDPASIDCacheEntry { bool valid; } VTDPASIDCacheEntry;Quoted 7 lines
include/hw/i386/intel_iommu.h typedef struct VTDPASIDCacheEntry { · line 104
Quoted 2 lines
include/hw/i386/intel_iommu.h +2
-0+typedef struct VTDPRQEntry { + uint32_t grpid;Hi Zhenzhon,
Maybe silly question, but why don't we keep naming it prgi?
Good question, the naming is picked from uAPI, see below:
struct
iommu_hwpt_pgfault{
__u32flags;
__u32dev_id;
__u32pasid;
__u32grpid;
__u32perm;
__u32__reserved;
__aligned_u64addr;
__u32length;
__u32cookie;
};Because VTDPRQEntry caches page group id and cookie from kernel uAPI, so align the name with kernel.
Thanks
Zhenzhong
Re: [PATCH 4/5] intel_iommu_accel: Accept PRQ response for passthrough device
In reply to Zhenzhong DuanQuoted 89 lines · Zhenzhong Duan
Propagate guest's PRQ response to host by writing to
fault_fd. Create a new VTDPRQEntry to cache cookie for each fault group, this cookie is used to mark the fault group on host side.Signed-off-by Zhenzhong Duan <[email protected]>--- hw/i386/intel_iommu_accel.h | 14 ++++++++ include/hw/i386/intel_iommu.h | 6 ++++ hw/i386/intel_iommu.c | 4 +++ hw/i386/intel_iommu_accel.c | 65 +++++++++++++++++++++++++++++++++++ hw/i386/trace-events | 1 + 5 files changed, 90 insertions(+)hw/i386/intel_iommu_accel.h +14
-0@@ -19,6 +19,9 @@ typedef struct VTDAccelPASIDCacheEntry {uint32_t fs_hwpt_id;uint32_t fault_id;int fault_fd; + QLIST_HEAD(, VTDPRQEntry) vtd_prq_list; +IOMMUPRINotifier pri_notifier_entry; +IOMMUPRINotifier *pri_notifier;QLIST_ENTRY (VTDAccelPASIDCacheEntry) next; } VTDAccelPASIDCacheEntry;@@ -31,6 +34,9 @@ void vtd_flush_host_piotlb_all_accel(IntelIOMMUState *s, uint16_t domain_id, uint64_t npages, bool ih);void vtd_pasid_cache_sync_accel (IntelIOMMUState *s,VTDPASIDCacheInfo *pc_info);void vtd_pasid_cache_reset_accel (IntelIOMMUState *s); +bool vtd_propagate_page_group_response_accel (IntelIOMMUState *s, +uint16_t rid,uint32_t pasid, +IOMMUPRIResponse *response);void vtd_iommu_ops_update_accel (PCIIOMMUOps *ops);#else static inline bool vtd_check_hiod_accel(IntelIOMMUState *s,@@ -69,6 +75,14 @@ static inline void vtd_pasid_cache_reset_accel(IntelIOMMUState *s) { } +static inline +bool vtd_propagate_page_group_response_accel (IntelIOMMUState *s, +uint16_t rid,uint32_t pasid, +IOMMUPRIResponse *response) +{ +return false; +} +static inline void vtd_iommu_ops_update_accel (PCIIOMMUOps *ops) { }include/hw/i386/intel_iommu.h +6
-0@@ -100,6 +100,12 @@ typedef struct VTDPASIDCacheEntry {bool valid; } VTDPASIDCacheEntry; +typedef struct VTDPRQEntry { +uint32_t grpid; +uint32_t cookie; +QLIST_ENTRY (VTDPRQEntry) next; +}VTDPRQEntry ; +struct VTDAddressSpace {PCIBus *bus;uint8_t devfn;hw/i386/intel_iommu.c +4
-0@@ -3390,6 +3390,10 @@ static bool vtd_process_page_group_response_desc(IntelIOMMUState *s, response.response_code =IOMMU_PRI_RESP_FAILURE ; } +if (vtd_propagate_page_group_response_accel (s, rid, pasid, &response)) { +return true; + } +if (vtd_dev_as->pri_notifier) { vtd_dev_as->pri_notifier->notify (vtd_dev_as->pri_notifier, &response); }hw/i386/intel_iommu_accel.c +0
-0
hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 107
Quoted 6 lines
hw/i386/intel_iommu_accel.c +3
-0@@ -102,6 +102,30 @@ VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as)return NULL ; } +bool vtd_propagate_page_group_response_accel (IntelIOMMUState *s, +uint16_t rid,uint32_t pasid, +IOMMUPRIResponse *response)
s/vtd_propagate_page_group_response_accel/vtd_accel_propagate_page_group_response/
hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 120
Quoted 13 lines
hw/i386/intel_iommu_accel.c +13
-0+{ +VTDAddressSpace *vtd_as =vtd_get_as_by_sid (s, rid); +VTDAccelPASIDCacheEntry *vtd_pce; +VTDHostIOMMUDevice *vtd_hiod =vtd_find_hiod_iommufd (vtd_as); + +if (!vtd_hiod) { +return false; + } + + QLIST_FOREACH(vtd_pce, &vtd_hiod->pasid_cache_list, next) { +if (vtd_pce->pasid == pasid) { +if (vtd_pce->pri_notifier) { + vtd_pce->pri_notifier->notify (vtd_pce->pri_notifier, response);
is the notify mechanism really needed? You already have vtd_pce here which is the main input that needed by vtd_prq_response_notify(). You might call vtd_prq_response_notify() directly here since it's
internal helper?
Quoted 99 lines
hw/i386/intel_iommu_accel.c +49
-0+ } +return true; + } + } + +return false; +} +static void vtd_prq_report_fault (VTDAccelPASIDCacheEntry *vtd_pce,struct iommu_hwpt_pgfault *fault,int cnt) {@@ -117,6 +141,13 @@ static void vtd_prq_report_fault(VTDAccelPASIDCacheEntry *vtd_pce, fault->addr, last_page, fault->grpid, fault->perm &IOMMU_PGFAULT_PERM_READ , fault->perm & IOMMU_PGFAULT_PERM_WRITE); +if (last_page) { +VTDPRQEntry *prqe =g_malloc0 (sizeof (*prqe)); + + prqe->grpid = fault->grpid; + prqe->cookie = fault->cookie; +QLIST_INSERT_HEAD (&vtd_pce->vtd_prq_list, prqe, next); + } } }@@ -150,6 +181,36 @@ static void vtd_prq_read_fault(void *opaque)vtd_prq_report_fault (vtd_pce, fault, bytes /sizeof (fault[0 ])); } +static void vtd_prq_response_notify (struct IOMMUPRINotifier *notifier, +IOMMUPRIResponse *response) +{ +VTDAccelPASIDCacheEntry *vtd_pce = +container_of (notifier, VTDAccelPASIDCacheEntry, pri_notifier_entry); +uint32_t id = vtd_pce->fault_id, fd = vtd_pce->fault_fd; +struct iommu_hwpt_page_response resp; +VTDPRQEntry *prqe, *tmp; +ssize_t bytes; + + QLIST_FOREACH_SAFE(prqe, &vtd_pce->vtd_prq_list, next, tmp) { +if (prqe->grpid != response->prgi) { +continue ; + } + + resp.cookie = prqe->cookie; + resp.code = response->response_code; + bytes =write (fd, &resp,sizeof (resp)); +trace_vtd_prq_response_notify (id, fd, resp.cookie, resp.code, bytes); +if (bytes <0 ) { +error_report_once ("FAULTQ(id %u): write failed " +"[cookie 0x%x code 0x%x] (%m)" , + id, resp.cookie, resp.code); + } + +QLIST_REMOVE (prqe, next); +g_free (prqe); + } +} +static void vtd_destroy_fs_faultq (VTDHostIOMMUDevice *vtd_hiod,uint32_t fault_id,uint32_t fault_fd) {@@ -213,6 +274,7 @@ static void vtd_destroy_old_fs_faultq(VTDHostIOMMUDevice *vtd_hiod,return ; } + vtd_pce->pri_notifier =NULL ;qemu_set_fd_handler (vtd_pce->fault_fd,NULL ,NULL ,NULL );vtd_destroy_fs_faultq (vtd_hiod, vtd_pce->fault_id, vtd_pce->fault_fd); vtd_pce->fault_id =0 ;@@ -228,6 +290,8 @@ static void vtd_setup_fs_faultq(VTDAccelPASIDCacheEntry *vtd_pce, vtd_pce->fault_id = fault_id; vtd_pce->fault_fd = fault_fd; + vtd_pce->pri_notifier_entry.notify = vtd_prq_response_notify; + vtd_pce->pri_notifier = &vtd_pce->pri_notifier_entry;qemu_set_fd_handler (fault_fd, vtd_prq_read_fault,NULL , vtd_pce); }@@ -492,6 +556,7 @@ static void vtd_accel_fill_pc(VTDHostIOMMUDevice *vtd_hiod, uint32_t pasid, vtd_pce->vtd_hiod = vtd_hiod; vtd_pce->pasid = pasid; vtd_pce->pasid_entry = *pe; +QLIST_INIT (&vtd_pce->vtd_prq_list);QLIST_INSERT_HEAD (&vtd_hiod->pasid_cache_list, vtd_pce, next);if (!vtd_device_attach_iommufd (vtd_pce, &local_err)) {hw/i386/trace-events +1
-0@@ -78,6 +78,7 @@ vtd_device_attach_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int re vtd_device_detach_hwpt(uint32_t dev_id, uint32_t pasid, int ret) "dev_id %d pasid %d ret: %d" vtd_device_reattach_def_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int ret) "dev_id %d pasid %d hwpt_id %d, ret: %d" vtd_prq_read_fault(uint32_t fault_id, uint32_t fault_fd, ssize_t bytes) "fault_id %d fault_fd %d ret: %zd" +vtd_prq_response_notify(uint32_t fault_id, uint32_t fault_fd, uint32_t cookie, uint32_t code, ssize_t bytes) "fault_id %d fault_fd %d cookie %d code %d ret: %zd" # amd_iommu.c amdvi_evntlog_fail(uint64_t addr, uint32_t head) "error: fail to write at addr 0x%"PRIx64" + offset 0x%"PRIx32
RE: [PATCH 4/5] intel_iommu_accel: Accept PRQ response for passthrough device
Quoted 106 lines
-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH 4/5]intel_iommu_accel: Accept PRQ response for passthrough deviceQuoted 10 lines · Zhenzhong Duan
Propagate guest's PRQ response to host by writing to
fault_fd. Create a new VTDPRQEntry to cache cookie for each fault group, this cookie is used to mark the fault group on host side.Signed-off-by Zhenzhong Duan <[email protected]>--- hw/i386/intel_iommu_accel.h | 14 ++++++++ include/hw/i386/intel_iommu.h | 6 ++++ hw/i386/intel_iommu.c | 4 +++ hw/i386/intel_iommu_accel.c | 65+++++++++++++++++++++++++++++++++++
hw/i386/intel_iommu_accel.h void vtd_flush_host_piotlb_all_accel(IntelIOMMUState *s,
Quoted 18 lines
hw/i386/trace-events | 1 + 5 files changed, 90 insertions(+)hw/i386/intel_iommu_accel.h +3
-0@@ -19,6 +19,9 @@ typedef struct VTDAccelPASIDCacheEntry {uint32_t fs_hwpt_id;uint32_t fault_id;int fault_fd; + QLIST_HEAD(, VTDPRQEntry) vtd_prq_list; +IOMMUPRINotifier pri_notifier_entry; +IOMMUPRINotifier *pri_notifier;QLIST_ENTRY (VTDAccelPASIDCacheEntry) next; } VTDAccelPASIDCacheEntry;@@ -31,6 +34,9 @@ void vtd_flush_host_piotlb_all_accel(IntelIOMMUState *s,
uint16_tdomain_id,hw/i386/intel_iommu_accel.h void vtd_flush_host_piotlb_all_accel(IntelIOMMUState *s, · line 35
Quoted 2 lines
hw/i386/intel_iommu_accel.h +0
-0uint64_t npages, bool ih); void vtd_pasid_cache_sync_accel(IntelIOMMUState *s, VTDPASIDCacheInfo
*pc_info);hw/i386/intel_iommu_accel.h static inline void
Quoted 8 lines
hw/i386/intel_iommu_accel.h +3
-0void vtd_pasid_cache_reset_accel (IntelIOMMUState *s); +bool vtd_propagate_page_group_response_accel (IntelIOMMUState *s, +uint16_t rid,uint32_t pasid, +IOMMUPRIResponse *response);void vtd_iommu_ops_update_accel (PCIIOMMUOps *ops);#else static inline bool vtd_check_hiod_accel(IntelIOMMUState *s,@@ -69,6 +75,14 @@ static inline void
vtd_pasid_cache_reset_accel(IntelIOMMUState *s)hw/i386/intel_iommu.c static bool
Quoted 36 lines
hw/i386/intel_iommu_accel.h +8
-0{ } +static inline +bool vtd_propagate_page_group_response_accel (IntelIOMMUState *s, +uint16_t rid,uint32_t pasid, +IOMMUPRIResponse *response) +{ +return false; +} +static inline void vtd_iommu_ops_update_accel (PCIIOMMUOps *ops) { }include/hw/i386/intel_iommu.h +6
-0@@ -100,6 +100,12 @@ typedef struct VTDPASIDCacheEntry {bool valid; } VTDPASIDCacheEntry; +typedef struct VTDPRQEntry { +uint32_t grpid; +uint32_t cookie; +QLIST_ENTRY (VTDPRQEntry) next; +}VTDPRQEntry ; +struct VTDAddressSpace {PCIBus *bus;uint8_t devfn;hw/i386/intel_iommu.c +0
-0@@ -3390,6 +3390,10 @@ static boolvtd_process_page_group_response_desc(IntelIOMMUState *s,
hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice
Quoted 15 lines
hw/i386/intel_iommu.c +4
-0response.response_code =IOMMU_PRI_RESP_FAILURE ; } +if (vtd_propagate_page_group_response_accel (s, rid, pasid, &response)) { +return true; + } +if (vtd_dev_as->pri_notifier) { vtd_dev_as->pri_notifier->notify (vtd_dev_as->pri_notifier, &response); }hw/i386/intel_iommu_accel.c +0
-0@@ -102,6 +102,30 @@ VTDHostIOMMUDevice*vtd_find_hiod_iommufd(VTDAddressSpace *as)
Quoted 3 lines
hw/i386/intel_iommu_accel.c +0
-0return NULL ; }
Quoted 6 lines
hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice · line 107
Quoted 3 lines
hw/i386/intel_iommu_accel.c +3
-0+bool vtd_propagate_page_group_response_accel (IntelIOMMUState *s, +uint16_t rid,uint32_t pasid, +IOMMUPRIResponse *response)s/vtd_propagate_page_group_response_accel/vtd_accel_propagate_page_group_ response/
Will do.
Quoted 11 lines
Quoted 11 lines
hw/i386/intel_iommu_accel.c +10
-0+{ +VTDAddressSpace *vtd_as =vtd_get_as_by_sid (s, rid); +VTDAccelPASIDCacheEntry *vtd_pce; +VTDHostIOMMUDevice *vtd_hiod =vtd_find_hiod_iommufd (vtd_as); + +if (!vtd_hiod) { +return false; + } + + QLIST_FOREACH(vtd_pce, &vtd_hiod->pasid_cache_list, next) {
Quoted 8 lines
hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 120
Quoted 3 lines
hw/i386/intel_iommu_accel.c +3
-0+if (vtd_pce->pasid == pasid) { +if (vtd_pce->pri_notifier) { + vtd_pce->pri_notifier->notify (vtd_pce->pri_notifier, response);is the notify mechanism really needed? You already have
vtd_pcehere which is the main input that needed byvtd_prq_response_notify(). You might callvtd_prq_response_notify()directly here since it's internal helper?
Good suggestion! Will do.
Thanks
Zhenzhong
PATCH 5/5 intel_iommu_accel: teardown FAULTQ resources in bottom half
In reply to Zhenzhong DuanWhen a pasid entry becomes invalid, we need to release all resources allocated for that entry including FAULTQ object and fault_fd.
We call qemu_set_fd_handler() to detach fault_fd's io_read handler and wakes up main thread from poll(), but there could still be a small window we call iommufd_backend_free_id(fault_id) before poll() exit and release fault_id file reference. In this rare case, FAULTQ object free return -EBUSY because opened fault_id file keeps reference of FAULTQ object.
Teardown FAULTQ resources in bottom half to ensure poll() has released fault_id file reference.
--- hw/i386/intel_iommu_accel.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-)
hw/i386/intel_iommu_accel.c +29 -2
@@ -267,16 +267,43 @@ free_faultq:return false; } +typedef struct IOMMUFaultQueue { +IOMMUFDBackend *iommufd; +uint32_t id; +int fd; +}IOMMUFaultQueue ; + +static void faultq_teardown_bh (void *opaque) +{ +IOMMUFaultQueue *fq = opaque; + +qemu_set_fd_handler (fq->fd,NULL ,NULL ,NULL ); +close (fq->fd); +iommufd_backend_free_id (fq->iommufd, fq->id); + +g_free (fq); +} + +static void vtd_destroy_old_fs_faultq (VTDHostIOMMUDevice *vtd_hiod,VTDAccelPASIDCacheEntry *vtd_pce) { +HostIOMMUDeviceIOMMUFD *idev =HOST_IOMMU_DEVICE_IOMMUFD (vtd_hiod->hiod); +if (!vtd_pce->fault_id) {return ; } vtd_pce->pri_notifier =NULL ; -qemu_set_fd_handler (vtd_pce->fault_fd,NULL ,NULL ,NULL ); -vtd_destroy_fs_faultq (vtd_hiod, vtd_pce->fault_id, vtd_pce->fault_fd); + +IOMMUFaultQueue *fq =g_malloc (sizeof (IOMMUFaultQueue)); + fq->iommufd = idev->iommufd; + fq->fd = vtd_pce->fault_fd; + fq->id = vtd_pce->fault_id; + +aio_bh_schedule_oneshot (iohandler_get_aio_context (), + faultq_teardown_bh, fq); + vtd_pce->fault_id =0 ; vtd_pce->fault_fd =-1 ; }
Signature · 1 line
2.47.3
Quoted 64 lines
-----Original Message-----
From: Duan, Zhenzhong <[email protected]>
Sent: Friday, March 27, 2026 10:52 AM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Tian, Kevin <[email protected]>; Liu, Yi L <[email protected]>; Hao, Xudong <[email protected]>; Duan, Zhenzhong <[email protected]>
Subject: [PATCH 0/5]intel_iommu: Enable PRQ support for passthrough deviceHi,
When svm=on,pasid=N,x-flst=on is configured for virtual vtd, we enable guest's support for vSVM. In this case, the host VTD may generate recoverable first stage page fault event, QEMU read the event and inject it to guest.
After guest handles the event, it sends a page group response, QEMU gets the response and pass it to host VTD.
This series adds QEMU support for receiving such host events through the FAULTQ interface and propagating them to the guest, then catching guest's responses and propagating them to host.
This patchset depends on pasid support:
https://lore.kernel.org/qemu-devel/20260326091130.321483-1-
[email protected]/#tGIT branch:
https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_prqTests:
Tested with DSA wq attached to an user APP which triggers IO page fault on process page table durging DMA.Thanks
ZhenzhongZhenzhong Duan (5):
backends/iommufd: Introduceiommufd_backend_alloc_faultqbackends/iommufd: Extendiommufd_backend_alloc_hwpt()withfault_id
intel_iommu_accel: Add PRQ injection for passthrough device
intel_iommu_accel: Accept PRQ response for passthrough device
intel_iommu_accel: teardown FAULTQ resources in bottom halfhw/i386/intel_iommu_accel.h | 16 +++ include/hw/i386/intel_iommu.h | 6 + include/system/iommufd.h | 7 +- backends/iommufd.c | 34 ++++- hw/arm/smmuv3-accel.c | 6 +- hw/i386/intel_iommu.c | 4 + hw/i386/intel_iommu_accel.c | 243 +++++++++++++++++++++++++++++++++- hw/vfio/iommufd.c | 2 +- backends/trace-events | 3 +- hw/i386/trace-events | 2 + 10 files changed, 308 insertions(+), 15 deletions(-)
Quoted 2 lines
--
2.47.3
DSA PF assignment to VM with vIOMMU scalable mode, userspace test tool dsa_test passed in VM. https://github.com/intel/idxd-config