Skip to contentSkip to conversation

intel_iommu: Enable PRQ support for passthrough device

PATCH Superseded Zhenzhong Duan 3 participants 17 messages Aug 31 – Sep 8

v5 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v5 cover intel_iommu: Enable PRQ support for passthrough device

Hi,

When svm=on,pasid-bits=N,fsts=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.

GIT branch: https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_prq.v5

Tests:
Tested with DSA wq attached to an user APP which triggers IO page fault on process page table durging DMA.
Build test with window cross-build.

Thanks
Zhenzhong

Changelog:
v5:
- add comment "Accept the PCI PASID as input" to docstring of function vtd_pri_request_page (Clement)
- change vtd_prq_report_fault() parameter type "int cnt" -> "unsigned cnt" (Clement)
- check return value of vtd_pri_request_page() and send response (Clement)
- free all vtd_prq_list entries when detaching (Clement)

v4:
- add the comment to the docstring of the function vtd_prq_report_fault itself (Clement)
- add "out_" prefix for fault_id & fault_fd output parameter (Clement)

v3:
- convert to PCI pasid before passing to vtd_pri_request_page (Clement)
- expose vtd_pri_request_page and drop accel_ops pointer (Clement)
- add a cleanup patch "intel_iommu_accel: Guard VTDAccelPASIDCacheEntry definition" (Philippe)

v2:
- define fault_fd int type (Liuyi)
- update comments on FAULTQ_BUF_SIZE (Liuyi)
- drop param vtd_hiod in vtd_destroy_old_fs_faultq() (Liuyi)
- s/vtd_propagate_page_group_response_accel/vtd_accel_propagate_page_group_response (Liuyi)
- call vtd_prq_response_notify() directly instead of through pri_notifier (Liuyi)
- replace "fault_id != 0" check with "fault_fd < 0" check (Liuyi)

Zhenzhong Duan (6):
intel_iommu_accel: Guard VTDAccelPASIDCacheEntry definition 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 +-
 hw/i386/intel_iommu_internal.h |   3 +
 include/hw/i386/intel_iommu.h  |   6 +
 include/system/iommufd.h       |   7 +-
 backends/iommufd.c             |  28 +++-
 hw/arm/smmuv3-accel.c          |   6 +-
 hw/i386/intel_iommu.c          |  13 +-
 hw/i386/intel_iommu_accel.c    | 278 ++++++++++++++++++++++++++++++++-
 hw/vfio/iommufd.c              |   2 +-
 backends/trace-events          |   3 +-
 hw/i386/trace-events           |   2 +
 11 files changed, 342 insertions(+), 22 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

PATCH v5 1/6 intel_iommu_accel: Guard VTDAccelPASIDCacheEntry definition

Move the VTDAccelPASIDCacheEntry typedef structure inside the CONFIG_VTD_ACCEL ifdef block.

When CONFIG_VTD_ACCEL is disabled, this structure is entirely unused throughout the code. Moving the layout under the existing preprocessor guard prevents exposing dead type definitions and ensures the header remains clean when building without VTD acceleration support.

---
 hw/i386/intel_iommu_accel.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/i386/intel_iommu_accel.h +1 -1
index 4a9003c92d..46c1a29409 100644
@@ -12,6 +12,7 @@
 #define HW_I386_INTEL_IOMMU_ACCEL_H
 #include CONFIG_DEVICES
 
+#ifdef CONFIG_VTD_ACCEL
 typedef struct VTDAccelPASIDCacheEntry {
     VTDHostIOMMUDevice *vtd_hiod;
     VTDPASIDEntry pasid_entry;
@@ -20,7 +21,6 @@ typedef struct VTDAccelPASIDCacheEntry {
     QLIST_ENTRY(VTDAccelPASIDCacheEntry) next;
 } VTDAccelPASIDCacheEntry;
 
-#ifdef CONFIG_VTD_ACCEL
 bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,
                           Error **errp);
 VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as);
Signature · 1 line
2.52.0
Reviewed-by on v5 1/6
#2 lore raw

Re: [PATCH v5 1/6] intel_iommu_accel: Guard VTDAccelPASIDCacheEntry definition

Quoted 28 lines · Zhenzhong Duan

Move the VTDAccelPASIDCacheEntry typedef structure inside the CONFIG_VTD_ACCEL ifdef block.

When CONFIG_VTD_ACCEL is disabled, this structure is entirely unused throughout the code. Moving the layout under the existing preprocessor guard prevents exposing dead type definitions and ensures the header remains clean when building without VTD acceleration support.

---
  hw/i386/intel_iommu_accel.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/i386/intel_iommu_accel.h +1 -0
index 4a9003c92d..46c1a29409 100644
@@ -12,6 +12,7 @@
  #define HW_I386_INTEL_IOMMU_ACCEL_H
  #include CONFIG_DEVICES
 
+#ifdef CONFIG_VTD_ACCEL
  typedef struct VTDAccelPASIDCacheEntry {
      VTDHostIOMMUDevice *vtd_hiod;
      VTDPASIDEntry pasid_entry;

hw/i386/intel_iommu_accel.h typedef struct VTDAccelPASIDCacheEntry { · line 26

Quoted 7 lines
hw/i386/intel_iommu_accel.h +0 -1
@@ -20,7 +21,6 @@ typedef struct VTDAccelPASIDCacheEntry {
      QLIST_ENTRY(VTDAccelPASIDCacheEntry) next;
  } VTDAccelPASIDCacheEntry;
 
-#ifdef CONFIG_VTD_ACCEL
  bool vtd_check_hiod_accel(IntelIOMMUState *s, VTDHostIOMMUDevice *vtd_hiod,
                            Error **errp);
  VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as);

LGTM.

#3 lore raw

PATCH v5 2/6 backends/iommufd: Introduce iommufd_backend_alloc_faultq

In reply to Zhenzhong Duan

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
index 17bf1a3727..36b34b4899 100644
@@ -106,6 +106,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 *out_fault_id,
+                                  uint32_t *out_fault_fd, Error **errp);
+
 bool iommufd_backend_alloc_hw_queue(IOMMUFDBackend *be, uint32_t viommu_id,
                                     uint32_t queue_type, uint32_t index,
                                     uint64_t addr, uint64_t length,
backends/iommufd.c +21 -0
index 15f2a51350..2775eef741 100644
@@ -554,6 +554,27 @@ bool iommufd_backend_alloc_veventq(IOMMUFDBackend *be, uint32_t viommu_id,
     return true;
 }
 
+bool iommufd_backend_alloc_faultq(IOMMUFDBackend *be, uint32_t *out_fault_id,
+                                  uint32_t *out_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;
+    }
+
+    *out_fault_id = cmd.out_fault_id;
+    *out_fault_fd = cmd.out_fault_fd;
+    return true;
+}
+
 bool iommufd_backend_alloc_hw_queue(IOMMUFDBackend *be, uint32_t viommu_id,
                                     uint32_t queue_type, uint32_t index,
                                     uint64_t addr, uint64_t length,
backends/trace-events +1 -0
index 9b7b9b7d68..b1c1d89dd8 100644
@@ -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=%u fault_fd=%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)"
 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.52.0
Reviewed-by on v5 2/6
#4 lore raw

Re: [PATCH v5 2/6] backends/iommufd: Introduce iommufd_backend_alloc_faultq

Quoted 63 lines · Zhenzhong Duan

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
index 17bf1a3727..36b34b4899 100644
@@ -106,6 +106,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 *out_fault_id,
+                                  uint32_t *out_fault_fd, Error **errp);
+
  bool iommufd_backend_alloc_hw_queue(IOMMUFDBackend *be, uint32_t viommu_id,
                                      uint32_t queue_type, uint32_t index,
                                      uint64_t addr, uint64_t length,
backends/iommufd.c +21 -0
index 15f2a51350..2775eef741 100644
@@ -554,6 +554,27 @@ bool iommufd_backend_alloc_veventq(IOMMUFDBackend *be, uint32_t viommu_id,
      return true;
  }
 
+bool iommufd_backend_alloc_faultq(IOMMUFDBackend *be, uint32_t *out_fault_id,
+                                  uint32_t *out_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;
+    }
+
+    *out_fault_id = cmd.out_fault_id;
+    *out_fault_fd = cmd.out_fault_fd;
+    return true;
+}
+
  bool iommufd_backend_alloc_hw_queue(IOMMUFDBackend *be, uint32_t viommu_id,
                                      uint32_t queue_type, uint32_t index,
                                      uint64_t addr, uint64_t length,
backends/trace-events +0 -0
index 9b7b9b7d68..b1c1d89dd8 100644

backends/trace-events iommufd_backend_unmap_dma_non_exist(int iommufd, uint32_t ioas, uint64_t iova, u · line 23

Quoted 7 lines
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=%u fault_fd=%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)"
  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)"

LGTM.

#5 lore raw

PATCH v5 3/6 backends/iommufd: Extend iommufd_backend_alloc_hwpt() with fault_id

In reply to Zhenzhong Duan

We should set flags IOMMU_HWPT_FAULT_ID_VALID when pass in a fault object id in the caller.

---
 include/system/iommufd.h    | 4 ++--
 backends/iommufd.c          | 7 ++++---
 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, 12 insertions(+), 11 deletions(-)
include/system/iommufd.h +2 -2
index 36b34b4899..79b8dabfb6 100644
@@ -90,8 +90,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,
                                   void *data_ptr, uint32_t data_len,
backends/iommufd.c +4 -3
index 2775eef741..f46b3bfa7e 100644
@@ -315,8 +315,8 @@ 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 = {
@@ -327,11 +327,12 @@ bool iommufd_backend_alloc_hwpt(IOMMUFDBackend *be, uint32_t dev_id,
         .data_type = data_type,
         .data_len = data_len,
         .data_uptr = (uintptr_t)data_ptr,
+        .fault_id = fault_id,
     };
 
     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
index 91aca1aa14..66e6a5011a 100644
@@ -268,7 +268,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;
     }
 
@@ -632,14 +632,14 @@ smmuv3_accel_alloc_viommu(SMMUv3State *s, HostIOMMUDeviceIOMMUFD *hiodi,
     if (!iommufd_backend_alloc_hwpt(hiodi->iommufd, hiodi->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(hiodi->iommufd, hiodi->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
index c03435f8a2..d2f41f18f1 100644
@@ -94,7 +94,7 @@ static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,
     return iommufd_backend_alloc_hwpt(hiodi->iommufd, hiodi->devid,
                                       hiodi->hwpt_id, flags,
                                       IOMMU_HWPT_DATA_VTD_S1, sizeof(vtd), &vtd,
-                                      fs_hwpt_id, errp);
+                                      0, fs_hwpt_id, errp);
 }
 
 static void vtd_destroy_old_fs_hwpt(VTDAccelPASIDCacheEntry *vtd_pce)
hw/vfio/iommufd.c +1 -1
index 242644aa00..74a5d77dcc 100644
@@ -476,7 +476,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
index b1c1d89dd8..2f242feb78 100644
@@ -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=%u fault_fd=%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.52.0
Reviewed-by on v5 3/6
#6 lore raw

Re: [PATCH v5 3/6] backends/iommufd: Extend iommufd_backend_alloc_hwpt() with fault_id

Quoted 6 lines · Zhenzhong Duan

We should set flags IOMMU_HWPT_FAULT_ID_VALID when pass in a fault object id in the caller.

Quoted 8 lines
---
  include/system/iommufd.h    | 4 ++--
  backends/iommufd.c          | 7 ++++---
  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, 12 insertions(+), 11 deletions(-)
Quoted 113 lines
include/system/iommufd.h +2 -2
index 36b34b4899..79b8dabfb6 100644
@@ -90,8 +90,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,
                                    void *data_ptr, uint32_t data_len,
backends/iommufd.c +4 -3
index 2775eef741..f46b3bfa7e 100644
@@ -315,8 +315,8 @@ 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 = {
@@ -327,11 +327,12 @@ bool iommufd_backend_alloc_hwpt(IOMMUFDBackend *be, uint32_t dev_id,
          .data_type = data_type,
          .data_len = data_len,
          .data_uptr = (uintptr_t)data_ptr,
+        .fault_id = fault_id,
      };
 
      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
index 91aca1aa14..66e6a5011a 100644
@@ -268,7 +268,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;
      }
 
@@ -632,14 +632,14 @@ smmuv3_accel_alloc_viommu(SMMUv3State *s, HostIOMMUDeviceIOMMUFD *hiodi,
      if (!iommufd_backend_alloc_hwpt(hiodi->iommufd, hiodi->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(hiodi->iommufd, hiodi->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
index c03435f8a2..d2f41f18f1 100644
@@ -94,7 +94,7 @@ static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,
      return iommufd_backend_alloc_hwpt(hiodi->iommufd, hiodi->devid,
                                        hiodi->hwpt_id, flags,
                                        IOMMU_HWPT_DATA_VTD_S1, sizeof(vtd), &vtd,
-                                      fs_hwpt_id, errp);
+                                      0, fs_hwpt_id, errp);
  }
 
  static void vtd_destroy_old_fs_hwpt(VTDAccelPASIDCacheEntry *vtd_pce)
hw/vfio/iommufd.c +1 -1
index 242644aa00..74a5d77dcc 100644
@@ -476,7 +476,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
index b1c1d89dd8..2f242feb78 100644
@@ -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=%u fault_fd=%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)"
#7 lore raw

PATCH v5 4/6 intel_iommu_accel: Add PRQ injection for passthrough device

In reply to 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().

---
 hw/i386/intel_iommu_accel.h    |   2 +
 hw/i386/intel_iommu_internal.h |   3 +
 hw/i386/intel_iommu.c          |   9 +-
 hw/i386/intel_iommu_accel.c    | 160 +++++++++++++++++++++++++++++++--
 hw/i386/trace-events           |   1 +
 5 files changed, 164 insertions(+), 11 deletions(-)
hw/i386/intel_iommu_accel.h +2 -0
index 46c1a29409..e0319749a5 100644
@@ -18,6 +18,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_internal.h +3 -0
index 924e91cb8a..5ebc204e9a 100644
@@ -786,4 +786,7 @@ int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
 VTDAddressSpace *vtd_get_as_by_sid(IntelIOMMUState *s, uint16_t sid);
 int vtd_dev_get_pe_from_pasid(IntelIOMMUState *s, PCIBus *bus, uint8_t devfn,
                               uint32_t pasid, VTDPASIDEntry *pe);
+int vtd_pri_request_page(PCIBus *bus, void *opaque, int devfn, uint32_t pasid,
+                         bool priv_req, bool exec_req, hwaddr addr, bool lpig,
+                         uint16_t prgi, bool is_read, bool is_write);
 #endif
hw/i386/intel_iommu.c +4 -5
index 82c3c3b2c3..dcf23924ef 100644
@@ -5369,11 +5369,10 @@ static int vtd_pri_perform_implicit_invalidation(VTDAddressSpace *vtd_as,
     return ret;
 }
 
-/* Page Request Descriptor : 7.4.1.1 */
-static int vtd_pri_request_page(PCIBus *bus, void *opaque, int devfn,
-                                uint32_t pasid, bool priv_req, bool exec_req,
-                                hwaddr addr, bool lpig, uint16_t prgi,
-                                bool is_read, bool is_write)
+/* Accept the PCI PASID as input. Page Request Descriptor : 7.4.1.1 */
+int vtd_pri_request_page(PCIBus *bus, void *opaque, int devfn, uint32_t pasid,
+                         bool priv_req, bool exec_req, hwaddr addr, bool lpig,
+                         uint16_t prgi, bool is_read, bool is_write)
 {
     IntelIOMMUState *s = opaque;
     VTDAddressSpace *vtd_as;
hw/i386/intel_iommu_accel.c +154 -6

218 lines not on this page · raw message

hw/i386/trace-events +1 -0
index a1a50d0910..dca726c9b8 100644
@@ -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.52.0
#8 lore raw

Re: [PATCH v5 4/6] intel_iommu_accel: Add PRQ injection for passthrough device

Quoted 3 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.

I thought FAULTQ object is allocated at the time when PRE bit is set. :) How about below?

When PRQ is enabled in guest, vIOMMU side allocates FAULTQ object for each s1_hwpt, so that host-side recoverable fault events can be received and propagated to guest.

Quoted 46 lines

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_internal.h |   3 +
  hw/i386/intel_iommu.c          |   9 +-
  hw/i386/intel_iommu_accel.c    | 160 +++++++++++++++++++++++++++++++--
  hw/i386/trace-events           |   1 +
  5 files changed, 164 insertions(+), 11 deletions(-)
hw/i386/intel_iommu_accel.h +2 -0
index 46c1a29409..e0319749a5 100644
@@ -18,6 +18,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_internal.h +3 -0
index 924e91cb8a..5ebc204e9a 100644
@@ -786,4 +786,7 @@ int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
  VTDAddressSpace *vtd_get_as_by_sid(IntelIOMMUState *s, uint16_t sid);
  int vtd_dev_get_pe_from_pasid(IntelIOMMUState *s, PCIBus *bus, uint8_t devfn,
                                uint32_t pasid, VTDPASIDEntry *pe);
+int vtd_pri_request_page(PCIBus *bus, void *opaque, int devfn, uint32_t pasid,
+                         bool priv_req, bool exec_req, hwaddr addr, bool lpig,
+                         uint16_t prgi, bool is_read, bool is_write);
  #endif
hw/i386/intel_iommu.c +0 -0
index 82c3c3b2c3..dcf23924ef 100644

hw/i386/intel_iommu.c static int vtd_pri_perform_implicit_invalidation(VTDAddressSpace *vtd_as, · line 5372

Quoted 9 lines
hw/i386/intel_iommu.c +1 -5
@@ -5369,11 +5369,10 @@ static int vtd_pri_perform_implicit_invalidation(VTDAddressSpace *vtd_as,
      return ret;
  }
 
-/* Page Request Descriptor : 7.4.1.1 */
-static int vtd_pri_request_page(PCIBus *bus, void *opaque, int devfn,
-                                uint32_t pasid, bool priv_req, bool exec_req,
-                                hwaddr addr, bool lpig, uint16_t prgi,
-                                bool is_read, bool is_write)
+/* Accept the PCI PASID as input. Page Request Descriptor : 7.4.1.1 */

can this "Accept the PCI PASID" be more accurate? Because it is facing the emulated device models, so this should be PCI PASID instead of the pasids used within vIOMMU.

Quoted 19 lines
hw/i386/intel_iommu.c +3 -0
+int vtd_pri_request_page(PCIBus *bus, void *opaque, int devfn, uint32_t pasid,
+                         bool priv_req, bool exec_req, hwaddr addr, bool lpig,
+                         uint16_t prgi, bool is_read, bool is_write)
  {
      IntelIOMMUState *s = opaque;
      VTDAddressSpace *vtd_as;
hw/i386/intel_iommu_accel.c +1 -0
index d2f41f18f1..3808112f79 100644
@@ -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"

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 82

Quoted 8 lines
hw/i386/intel_iommu_accel.c +2 -3
@@ -77,14 +78,150 @@ 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,
-                               Error **errp)
+static void vtd_prq_report_fault(VTDAccelPASIDCacheEntry *vtd_pce,
+                                 struct iommu_hwpt_pgfault *fault, unsigned cnt)

vtd_prq_report_fault is a bit confusing. seems like reporting a fault of PRQ. how about naming vtd_recoverable_fault_report? or any other name?

Quoted 11 lines
hw/i386/intel_iommu_accel.c +11 -0
+{
+    VTDHostIOMMUDevice *vtd_hiod = vtd_pce->vtd_hiod;
+    uint32_t pasid =
+        vtd_pce->pasid == IOMMU_NO_PASID ? PCI_NO_PASID : vtd_pce->pasid;
+
+    for (; cnt--; fault++) {
+        bool last_page = fault->flags & IOMMU_PGFAULT_FLAGS_LAST_PAGE;
+
+        vtd_pri_request_page(vtd_hiod->bus, vtd_hiod->iommu_state,
+                             vtd_hiod->devfn, pasid,
+                             fault->perm & IOMMU_PGFAULT_PERM_PRIV,

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 103

Quoted 10 lines
hw/i386/intel_iommu_accel.c +10 -0
+                             fault->perm & IOMMU_PGFAULT_PERM_EXEC,
+                             fault->addr, last_page, fault->grpid,
+                             fault->perm & IOMMU_PGFAULT_PERM_READ,
+                             fault->perm & IOMMU_PGFAULT_PERM_WRITE);
+    }
+}
+
+/* Batch size per read(); remaining faults trigger another callback */
+#define FAULTQ_BUF_SIZE 100
+

anyhow, we won't miss any fault. right?

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 105

Quoted 2 lines
hw/i386/intel_iommu_accel.c +2 -0
+static void vtd_prq_read_fault(void *opaque)
+{

perhapss s/vtd_prq_read_fault/vtd_fs_faultq_read/, then aligned with the below create/destroy/setup helpers.

Quoted 49 lines
hw/i386/intel_iommu_accel.c +49 -0
+    VTDAccelPASIDCacheEntry *vtd_pce = opaque;
+    struct iommu_hwpt_pgfault fault[FAULTQ_BUF_SIZE];
+    uint32_t id = vtd_pce->fault_id;
+    int 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, int fault_fd)
+{
+    HostIOMMUDeviceIOMMUFD *hiodi = HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
+
+    if (fault_fd < 0) {
+        return;
+    }
+
+    close(fault_fd);
+    iommufd_backend_free_id(hiodi->iommufd, fault_id);
+}
+
+static bool vtd_create_fs_faultq(VTDHostIOMMUDevice *vtd_hiod,
+                                 uint32_t *fault_id_p, int *fault_fd_p,
+                                 Error **errp)
+{
+    HostIOMMUDeviceIOMMUFD *hiodi = 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;

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 164

Quoted 10 lines
hw/i386/intel_iommu_accel.c +10 -0
+    int flags;
+
+    if (!s->svm ||
+        vtd_dev_to_context_entry(s, bus_n, vtd_hiod->devfn, &ce) ||
+        !VTD_CE_GET_PRE(&ce)) {
+        *fault_id_p = 0;
+        *fault_fd_p = -1;
+        return true;
+    }
+

this PRE validation hunk better in the caller side. This helper would be pure creation helper. BTW. is there any point that we can cach the CE instead of fetching it here. It seems we won't come to this helper if no CE.

Quoted 83 lines
hw/i386/intel_iommu_accel.c +58 -2
+    if (!iommufd_backend_alloc_faultq(hiodi->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:
+    vtd_destroy_fs_faultq(vtd_hiod, fault_id, fault_fd);
+    return false;
+}
+
+static void vtd_destroy_old_fs_faultq(VTDAccelPASIDCacheEntry *vtd_pce)
+{
+    if (vtd_pce->fault_fd < 0) {
+        return;
+    }
+
+    qemu_set_fd_handler(vtd_pce->fault_fd, NULL, NULL, NULL);
+    vtd_destroy_fs_faultq(vtd_pce->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, int fault_fd)
+{
+    if (fault_fd < 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,
+                               bool has_fault_id, uint32_t fault_id,
+                               uint32_t *fs_hwpt_id, Error **errp)
  {
      HostIOMMUDeviceIOMMUFD *hiodi = HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
      struct iommu_hwpt_vtd_s1 vtd = {};
      uint32_t flags = vtd_hiod->iommu_state->pasid ? IOMMU_HWPT_ALLOC_PASID : 0;
 
+    flags |= has_fault_id ? IOMMU_HWPT_FAULT_ID_VALID : 0;
+
      vtd.flags = (VTD_SM_PASID_ENTRY_SRE(pe) ? IOMMU_VTD_S1_SRE : 0) |
                  (VTD_SM_PASID_ENTRY_WPE(pe) ? IOMMU_VTD_S1_WPE : 0) |
                  (VTD_SM_PASID_ENTRY_EAFE(pe) ? IOMMU_VTD_S1_EAFE : 0);
@@ -94,7 +231,7 @@ static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,
      return iommufd_backend_alloc_hwpt(hiodi->iommufd, hiodi->devid,
                                        hiodi->hwpt_id, flags,
                                        IOMMU_HWPT_DATA_VTD_S1, sizeof(vtd), &vtd,
-                                      0, fs_hwpt_id, errp);
+                                      fault_id, fs_hwpt_id, errp);
  }
 
  static void vtd_destroy_old_fs_hwpt(VTDAccelPASIDCacheEntry *vtd_pce)
@@ -115,7 +252,8 @@ static bool vtd_device_attach_iommufd(VTDAccelPASIDCacheEntry *vtd_pce,
      VTDHostIOMMUDevice *vtd_hiod = vtd_pce->vtd_hiod;
      HostIOMMUDeviceIOMMUFD *hiodi = HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
      VTDPASIDEntry *pe = &vtd_pce->pasid_entry;
-    uint32_t hwpt_id = hiodi->hwpt_id, pasid = vtd_pce->pasid;
+    uint32_t hwpt_id = hiodi->hwpt_id, pasid = vtd_pce->pasid, fault_id = 0;
+    int fault_fd = -1;
      bool ret;
 
      /*

hw/i386/intel_iommu_accel.c static bool vtd_device_attach_iommufd(VTDAccelPASIDCacheEntry *vtd_pce, · line 274

Quoted 8 lines
hw/i386/intel_iommu_accel.c +4 -1
@@ -130,7 +268,12 @@ 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_fd >= 0, fault_id,

this funciton call can reuse the PRE bit validation result instead of checking fault_fd.

Quoted 48 lines
hw/i386/intel_iommu_accel.c +7 -0
+                                &hwpt_id, errp)) {
+            vtd_destroy_fs_faultq(vtd_hiod, fault_id, fault_fd);
              return false;
          }
      }
@@ -140,11 +283,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_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);
      }
 
      return ret;
@@ -177,6 +323,7 @@ static bool vtd_device_detach_iommufd(VTDAccelPASIDCacheEntry *vtd_pce,
 
      if (ret) {
          vtd_destroy_old_fs_hwpt(vtd_pce);
+        vtd_destroy_old_fs_faultq(vtd_pce);
      }
 
      return ret;
@@ -276,6 +423,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;
+    vtd_pce->fault_fd = -1;
      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
index a1a50d0910..dca726c9b8 100644
@@ -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
#9 lore raw

Re: [PATCH v5 4/6] intel_iommu_accel: Add PRQ injection for passthrough device

Quoted 100 lines · Yi Liu
Quoted 3 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.

I thought FAULTQ object is allocated at the time when PRE bit is set. :) How about below?

When PRQ is enabled in guest, vIOMMU side allocates FAULTQ object for each s1_hwpt, so that host-side recoverable fault events can be received and propagated to guest.

Quoted 49 lines

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_internal.h |   3 + hw/i386/intel_iommu.c          |   9 +- hw/i386/intel_iommu_accel.c    | 160 +++++++++++++++++++++++++++++++-- hw/i386/trace-events           |   1 +

  5 files changed, 164 insertions(+), 11 deletions(-)
hw/i386/intel_iommu_accel.h +2 -0
index 46c1a29409..e0319749a5 100644
@@ -18,6 +18,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_internal.h +3 -0
index 924e91cb8a..5ebc204e9a 100644
@@ -786,4 +786,7 @@ int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
   VTDAddressSpace *vtd_get_as_by_sid(IntelIOMMUState *s, uint16_t sid);
   int vtd_dev_get_pe_from_pasid(IntelIOMMUState *s, PCIBus *bus, uint8_t devfn,
                                 uint32_t pasid, VTDPASIDEntry *pe);
+int vtd_pri_request_page(PCIBus *bus, void *opaque, int devfn, uint32_t pasid,
+                         bool priv_req, bool exec_req, hwaddr addr, bool lpig,
+                         uint16_t prgi, bool is_read, bool is_write);
   #endif
hw/i386/intel_iommu.c +0 -0
index 82c3c3b2c3..dcf23924ef 100644
@@ -5369,11 +5369,10 @@ static int vtd_pri_perform_implicit_invalidation(VTDAddressSpace *vtd_as,
       return ret;
   }

hw/i386/intel_iommu.c static int vtd_pri_perform_implicit_invalidation(VTDAddressSpace *vtd_as, · line 5372

Quoted 6 lines
hw/i386/intel_iommu.c +1 -5
-/* Page Request Descriptor : 7.4.1.1 */
-static int vtd_pri_request_page(PCIBus *bus, void *opaque, int devfn,
-                                uint32_t pasid, bool priv_req, bool exec_req,
-                                hwaddr addr, bool lpig, uint16_t prgi,
-                                bool is_read, bool is_write)
+/* Accept the PCI PASID as input. Page Request Descriptor : 7.4.1.1 */

can this "Accept the PCI PASID" be more accurate? Because it is facing the emulated device models, so this should be PCI PASID instead of the pasids used within vIOMMU.

Quoted 23 lines

+int vtd_pri_request_page(PCIBus *bus, void *opaque, int devfn, uint32_t pasid, +                         bool priv_req, bool exec_req, hwaddr addr, bool lpig, +                         uint16_t prgi, bool is_read, bool is_write) {
      IntelIOMMUState *s = opaque;
      VTDAddressSpace *vtd_as;

hw/i386/intel_iommu_accel.c +0 -0
index d2f41f18f1..3808112f79 100644
@@ -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"

hw/i386/intel_iommu_accel.c +0 -0
@@ -77,14 +78,150 @@ VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as)
       return NULL;
   }


-static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,

Quoted 9 lines
Quoted 4 lines

-                               VTDPASIDEntry *pe, uint32_t *fs_hwpt_id, -                               Error **errp)
+static void vtd_prq_report_fault(VTDAccelPASIDCacheEntry *vtd_pce, +                                 struct iommu_hwpt_pgfault *fault, unsigned cnt)

vtd_prq_report_fault is a bit confusing. seems like reporting a fault of PRQ. how about naming vtd_recoverable_fault_report? or any other name?

We named it "propagate" on the response path ([5/6]). Maybe use the same pattern?

Quoted 249 lines
Quoted 12 lines
hw/i386/intel_iommu_accel.c +11 -0
+{
+    VTDHostIOMMUDevice *vtd_hiod = vtd_pce->vtd_hiod;
+    uint32_t pasid =
+        vtd_pce->pasid == IOMMU_NO_PASID ? PCI_NO_PASID : vtd_pce->pasid;
+
+    for (; cnt--; fault++) {
+        bool last_page = fault->flags & IOMMU_PGFAULT_FLAGS_LAST_PAGE;
+
+        vtd_pri_request_page(vtd_hiod->bus, vtd_hiod->iommu_state,
+                             vtd_hiod->devfn, pasid,
+                             fault->perm & IOMMU_PGFAULT_PERM_PRIV,

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 103

Quoted 10 lines
hw/i386/intel_iommu_accel.c +10 -0
+                             fault->perm & IOMMU_PGFAULT_PERM_EXEC,
+                             fault->addr, last_page, fault->grpid,
+                             fault->perm & IOMMU_PGFAULT_PERM_READ,
+                             fault->perm & IOMMU_PGFAULT_PERM_WRITE);
+    }
+}
+
+/* Batch size per read(); remaining faults trigger another callback */
+#define FAULTQ_BUF_SIZE 100
+

anyhow, we won't miss any fault. right?

hw/i386/intel_iommu_accel.c

Quoted 2 lines
hw/i386/intel_iommu_accel.c +2 -0
+static void vtd_prq_read_fault(void *opaque)
+{

perhapss s/vtd_prq_read_fault/vtd_fs_faultq_read/, then aligned with the below create/destroy/setup helpers.

Quoted 49 lines

+    VTDAccelPASIDCacheEntry *vtd_pce = opaque; +    struct iommu_hwpt_pgfault fault[FAULTQ_BUF_SIZE]; +    uint32_t id = vtd_pce->fault_id;
+    int fd = vtd_pce->fault_fd;
+    ssize_t bytes, last_bytes;

hw/i386/intel_iommu_accel.c +44 -0
+
+    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, int fault_fd)
+{
+    HostIOMMUDeviceIOMMUFD *hiodi = HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
+
+    if (fault_fd < 0) {
+        return;
+    }
+
+    close(fault_fd);
+    iommufd_backend_free_id(hiodi->iommufd, fault_id);
+}
+
+static bool vtd_create_fs_faultq(VTDHostIOMMUDevice *vtd_hiod,
+                                 uint32_t *fault_id_p, int *fault_fd_p,
+                                 Error **errp)
+{
+    HostIOMMUDeviceIOMMUFD *hiodi = 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;

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 164

Quoted 10 lines
hw/i386/intel_iommu_accel.c +10 -0
+    int flags;
+
+    if (!s->svm ||
+        vtd_dev_to_context_entry(s, bus_n, vtd_hiod->devfn, &ce) ||
+        !VTD_CE_GET_PRE(&ce)) {
+        *fault_id_p = 0;
+        *fault_fd_p = -1;
+        return true;
+    }
+

this PRE validation hunk better in the caller side. This helper would be pure creation helper. BTW. is there any point that we can cach the CE instead of fetching it here. It seems we won't come to this helper if no CE.

Quoted 84 lines

+    if (!iommufd_backend_alloc_faultq(hiodi->iommufd, &fault_id, &fault_fd, +                                      errp)) {
+        return false;
+    }

hw/i386/intel_iommu_accel.c +49 -0
+
+    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:
+    vtd_destroy_fs_faultq(vtd_hiod, fault_id, fault_fd);
+    return false;
+}
+
+static void vtd_destroy_old_fs_faultq(VTDAccelPASIDCacheEntry *vtd_pce)
+{
+    if (vtd_pce->fault_fd < 0) {
+        return;
+    }
+
+    qemu_set_fd_handler(vtd_pce->fault_fd, NULL, NULL, NULL);
+    vtd_destroy_fs_faultq(vtd_pce->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, int fault_fd)
+{
+    if (fault_fd < 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,
+                               bool has_fault_id, uint32_t fault_id,
+                               uint32_t *fs_hwpt_id, Error **errp)

  {
      HostIOMMUDeviceIOMMUFD *hiodi = HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod); struct iommu_hwpt_vtd_s1 vtd = {};
      uint32_t flags = vtd_hiod->iommu_state->pasid ? IOMMU_HWPT_ALLOC_PASID : 0;
+    flags |= has_fault_id ? IOMMU_HWPT_FAULT_ID_VALID : 0;
+
      vtd.flags = (VTD_SM_PASID_ENTRY_SRE(pe) ? IOMMU_VTD_S1_SRE : 0) | (VTD_SM_PASID_ENTRY_WPE(pe) ? IOMMU_VTD_S1_WPE : 0) | (VTD_SM_PASID_ENTRY_EAFE(pe) ? IOMMU_VTD_S1_EAFE : 0);

hw/i386/intel_iommu_accel.c +1 -1
@@ -94,7 +231,7 @@ static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,
       return iommufd_backend_alloc_hwpt(hiodi->iommufd, hiodi->devid,
                                         hiodi->hwpt_id, flags,
                                         IOMMU_HWPT_DATA_VTD_S1, sizeof(vtd), &vtd,
-                                      0, fs_hwpt_id, errp);
+                                      fault_id, fs_hwpt_id, errp);
   }


  static void vtd_destroy_old_fs_hwpt(VTDAccelPASIDCacheEntry *vtd_pce)

hw/i386/intel_iommu_accel.c +2 -1
@@ -115,7 +252,8 @@ static bool vtd_device_attach_iommufd(VTDAccelPASIDCacheEntry *vtd_pce,
       VTDHostIOMMUDevice *vtd_hiod = vtd_pce->vtd_hiod;
       HostIOMMUDeviceIOMMUFD *hiodi = HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);
       VTDPASIDEntry *pe = &vtd_pce->pasid_entry;
-    uint32_t hwpt_id = hiodi->hwpt_id, pasid = vtd_pce->pasid;
+    uint32_t hwpt_id = hiodi->hwpt_id, pasid = vtd_pce->pasid, fault_id = 0;
+    int fault_fd = -1;
       bool ret;


      /*

hw/i386/intel_iommu_accel.c +0 -0
@@ -130,7 +268,12 @@ static bool vtd_device_attach_iommufd(VTDAccelPASIDCacheEntry *vtd_pce,
       }
Quoted 7 lines


      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_fd >= 0, fault_id,

this funciton call can reuse the PRE bit validation result instead of checking fault_fd.

Quoted 48 lines

+                                &hwpt_id, errp)) {
+            vtd_destroy_fs_faultq(vtd_hiod, fault_id, fault_fd); return false;
          }
      }

hw/i386/intel_iommu_accel.c +3 -0
@@ -140,11 +283,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_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);
       }


      return ret;

hw/i386/intel_iommu_accel.c +0 -0
@@ -177,6 +323,7 @@ static bool vtd_device_detach_iommufd(VTDAccelPASIDCacheEntry *vtd_pce,


      if (ret) {
          vtd_destroy_old_fs_hwpt(vtd_pce);
+        vtd_destroy_old_fs_faultq(vtd_pce);
      }

      return ret;

hw/i386/intel_iommu_accel.c +1 -0
@@ -276,6 +423,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;
+    vtd_pce->fault_fd = -1;
       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
index a1a50d0910..dca726c9b8 100644
@@ -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
#10 lore raw

RE: [PATCH v5 4/6] intel_iommu_accel: Add PRQ injection for passthrough device

Quoted 129 lines

-----Original Message-----
From: Clément MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v5 4/6] intel_iommu_accel: Add PRQ injection for passthrough device

Quoted 7 lines · Yi Liu
Quoted 3 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.

Quoted 15 lines

I thought FAULTQ object is allocated at the time when PRE bit is set. :) How about below?

When PRQ is enabled in guest, vIOMMU side allocates FAULTQ object for each s1_hwpt, so that host-side recoverable fault events can be received and propagated to guest.

Quoted 7 lines

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]](mailto:[email protected])>

Quoted 1 line
Quoted 1 line
Tested-by Xudong Hao

<[[email protected]](mailto:[email protected])>

Quoted 5 lines
Quoted 5 lines

---
  hw/i386/intel_iommu_accel.h    |   2 + hw/i386/intel_iommu_internal.h |   3 + hw/i386/intel_iommu.c          |   9 +- hw/i386/intel_iommu_accel.c    | 160 +++++++++++++++++++++++++++++++-

-

Quoted 16 lines
Quoted 16 lines

  hw/i386/trace-events           |   1 +

  5 files changed, 164 insertions(+), 11 deletions(-)
hw/i386/intel_iommu_accel.h +2 -0
index 46c1a29409..e0319749a5 100644
@@ -18,6 +18,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;
 
Quoted 1 line

Review

Quoted 1 line
(quoted diff) +0 -0

b/hw/i386/intel_iommu_internal.h

Quoted 4 lines

hw/i386/intel_iommu_internal.h int vtd_dev_to_context_entry(IntelIOMMUState *s,

Quoted 4 lines

index 924e91cb8a..5ebc204e9a 100644

hw/i386/intel_iommu_internal.h +0 -0
@@ -786,4 +786,7 @@ int vtd_dev_to_context_entry(IntelIOMMUState *s,

uint8_t bus_num,

Quoted 2 lines
Quoted 2 lines
hw/i386/intel_iommu_internal.h +0 -0
   VTDAddressSpace *vtd_get_as_by_sid(IntelIOMMUState *s, uint16_t sid);

  int vtd_dev_get_pe_from_pasid(IntelIOMMUState *s, PCIBus *bus, uint8_t

devfn,

Quoted 2 lines
Quoted 2 lines

                                uint32_t pasid, VTDPASIDEntry *pe); +int vtd_pri_request_page(PCIBus *bus, void *opaque, int devfn, uint32_t

pasid,

Quoted 8 lines

hw/i386/intel_iommu.c static int

Quoted 8 lines

+                         bool priv_req, bool exec_req, hwaddr addr, bool lpig, +                         uint16_t prgi, bool is_read, bool is_write); #endif

hw/i386/intel_iommu.c +0 -0
index 82c3c3b2c3..dcf23924ef 100644
@@ -5369,11 +5369,10 @@ static int

vtd_pri_perform_implicit_invalidation(VTDAddressSpace *vtd_as,

Quoted 17 lines

hw/i386/intel_iommu.c static int vtd_pri_perform_implicit_invalidation(VTDAddressSpace *vtd_as, · line 5372

Quoted 9 lines
hw/i386/intel_iommu.c +1 -5
       return ret;
   }
 
-/* Page Request Descriptor : 7.4.1.1 */
-static int vtd_pri_request_page(PCIBus *bus, void *opaque, int devfn,
-                                uint32_t pasid, bool priv_req, bool exec_req,
-                                hwaddr addr, bool lpig, uint16_t prgi,
-                                bool is_read, bool is_write)
+/* Accept the PCI PASID as input. Page Request Descriptor : 7.4.1.1 */

can this "Accept the PCI PASID" be more accurate? Because it is facing the emulated device models, so this should be PCI PASID instead of the pasids used within vIOMMU.

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+int vtd_pri_request_page(PCIBus *bus, void *opaque, int devfn, uint32_t

pasid,

Quoted 12 lines
Quoted 12 lines
hw/i386/intel_iommu.c +2 -0
+                         bool priv_req, bool exec_req, hwaddr addr, bool lpig,
+                         uint16_t prgi, bool is_read, bool is_write)
   {
       IntelIOMMUState *s = opaque;
       VTDAddressSpace *vtd_as;
hw/i386/intel_iommu_accel.c +0 -0
index d2f41f18f1..3808112f79 100644
@@ -9,6 +9,7 @@
    */
 
Quoted 6 lines

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice

Quoted 6 lines
hw/i386/intel_iommu_accel.c +1 -0
   #include "qemu/osdep.h"
+#include "qemu/error-report.h"
   #include "system/iommufd.h"
   #include "intel_iommu_internal.h"
   #include "intel_iommu_accel.h"
@@ -77,14 +78,150 @@ VTDHostIOMMUDevice

*vtd_find_hiod_iommufd(VTDAddressSpace *as)

Quoted 13 lines

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 82

Quoted 8 lines
hw/i386/intel_iommu_accel.c +2 -3
       return NULL;
   }
 
-static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,
-                               VTDPASIDEntry *pe, uint32_t *fs_hwpt_id,
-                               Error **errp)
+static void vtd_prq_report_fault(VTDAccelPASIDCacheEntry *vtd_pce,
+                                 struct iommu_hwpt_pgfault *fault, unsigned cnt)

vtd_prq_report_fault is a bit confusing. seems like reporting a fault of PRQ. how about naming vtd_recoverable_fault_report? or any other name?

Quoted 2 lines

We named it "propagate" on the response path ([5/6]). Maybe use the same pattern?

OK, I can combine your suggestion with Yi's, so use vtd_propagate_recoverable_fault() instead.

Thanks
Zhenzhong

#11 lore raw

RE: [PATCH v5 4/6] intel_iommu_accel: Add PRQ injection for passthrough device

In reply to Yi Liu
Quoted 12 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH v5 4/6] intel_iommu_accel: Add PRQ injection for passthrough device

Quoted 3 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.

Quoted 6 lines

I thought FAULTQ object is allocated at the time when PRE bit is set. :) How about below?

When PRQ is enabled in guest, vIOMMU side allocates FAULTQ object for each s1_hwpt, so that host-side recoverable fault events can be received and propagated to guest.

The difficulty is we can't attach FAULTQ object to HWPT object at runtime, We must destroy existing hwpt and creating new one with a FAULTQ attached which is not allowed at runtime.

Quoted 53 lines

hw/i386/intel_iommu_internal.h int vtd_dev_to_context_entry(IntelIOMMUState *s,

Quoted 35 lines

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_internal.h |   3 +
  hw/i386/intel_iommu.c          |   9 +-
  hw/i386/intel_iommu_accel.c    | 160 +++++++++++++++++++++++++++++++--
  hw/i386/trace-events           |   1 +
  5 files changed, 164 insertions(+), 11 deletions(-)
hw/i386/intel_iommu_accel.h +2 -0
index 46c1a29409..e0319749a5 100644
@@ -18,6 +18,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_internal.h +0 -0
index 924e91cb8a..5ebc204e9a 100644
@@ -786,4 +786,7 @@ int vtd_dev_to_context_entry(IntelIOMMUState *s,

uint8_t bus_num,

hw/i386/intel_iommu_internal.h int vtd_dev_to_context_entry(IntelIOMMUState *s, · line 787

Quoted 2 lines
hw/i386/intel_iommu_internal.h +0 -0
  VTDAddressSpace *vtd_get_as_by_sid(IntelIOMMUState *s, uint16_t sid);
  int vtd_dev_get_pe_from_pasid(IntelIOMMUState *s, PCIBus *bus, uint8_t

devfn,

hw/i386/intel_iommu.c static int

Quoted 10 lines
hw/i386/intel_iommu_internal.h +3 -0
                                uint32_t pasid, VTDPASIDEntry *pe);
+int vtd_pri_request_page(PCIBus *bus, void *opaque, int devfn, uint32_t pasid,
+                         bool priv_req, bool exec_req, hwaddr addr, bool lpig,
+                         uint16_t prgi, bool is_read, bool is_write);
  #endif
hw/i386/intel_iommu.c +0 -0
index 82c3c3b2c3..dcf23924ef 100644
@@ -5369,11 +5369,10 @@ static int

vtd_pri_perform_implicit_invalidation(VTDAddressSpace *vtd_as,

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
      return ret;
  }
 
Quoted 10 lines

hw/i386/intel_iommu.c static int vtd_pri_perform_implicit_invalidation(VTDAddressSpace *vtd_as, · line 5372

Quoted 6 lines
hw/i386/intel_iommu.c +1 -5
-/* Page Request Descriptor : 7.4.1.1 */
-static int vtd_pri_request_page(PCIBus *bus, void *opaque, int devfn,
-                                uint32_t pasid, bool priv_req, bool exec_req,
-                                hwaddr addr, bool lpig, uint16_t prgi,
-                                bool is_read, bool is_write)
+/* Accept the PCI PASID as input. Page Request Descriptor : 7.4.1.1 */

can this "Accept the PCI PASID" be more accurate? Because it is facing the emulated device models, so this should be PCI PASID instead of the pasids used within vIOMMU.

OK, will add below doc:

/*
* Page Request Descriptor : 7.4.1.1
*
* Because it is facing the emulated device models, so this should be PCI
* PASID instead of the pasids used within vIOMMU.
*/

Quoted 24 lines

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice

Quoted 20 lines
hw/i386/intel_iommu.c +3 -0
+int vtd_pri_request_page(PCIBus *bus, void *opaque, int devfn, uint32_t pasid,
+                         bool priv_req, bool exec_req, hwaddr addr, bool lpig,
+                         uint16_t prgi, bool is_read, bool is_write)
  {
      IntelIOMMUState *s = opaque;
      VTDAddressSpace *vtd_as;
hw/i386/intel_iommu_accel.c +1 -0
index d2f41f18f1..3808112f79 100644
@@ -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"
@@ -77,14 +78,150 @@ VTDHostIOMMUDevice

*vtd_find_hiod_iommufd(VTDAddressSpace *as)

Quoted 3 lines
hw/i386/intel_iommu_accel.c +0 -0
      return NULL;
  }
 
Quoted 8 lines

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 82

Quoted 5 lines
hw/i386/intel_iommu_accel.c +2 -3
-static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice *vtd_hiod,
-                               VTDPASIDEntry *pe, uint32_t *fs_hwpt_id,
-                               Error **errp)
+static void vtd_prq_report_fault(VTDAccelPASIDCacheEntry *vtd_pce,
+                                 struct iommu_hwpt_pgfault *fault, unsigned cnt)

vtd_prq_report_fault is a bit confusing. seems like reporting a fault of PRQ. how about naming vtd_recoverable_fault_report? or any other name?

OK, vtd_recoverable_fault_report looks good.

Quoted 16 lines
Quoted 16 lines
hw/i386/intel_iommu_accel.c +15 -0
+{
+    VTDHostIOMMUDevice *vtd_hiod = vtd_pce->vtd_hiod;
+    uint32_t pasid =
+        vtd_pce->pasid == IOMMU_NO_PASID ? PCI_NO_PASID : vtd_pce->pasid;
+
+    for (; cnt--; fault++) {
+        bool last_page = fault->flags & IOMMU_PGFAULT_FLAGS_LAST_PAGE;
+
+        vtd_pri_request_page(vtd_hiod->bus, vtd_hiod->iommu_state,
+                             vtd_hiod->devfn, 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);
Quoted 8 lines

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 103

Quoted 6 lines
hw/i386/intel_iommu_accel.c +6 -0
+    }
+}
+
+/* Batch size per read(); remaining faults trigger another callback */
+#define FAULTQ_BUF_SIZE 100
+

anyhow, we won't miss any fault. right?

Kernel limit it to PRQ_DEPTH(2048), in theory, if a pci device generates a group of faults of more than 100, read will fail with -EINVAL.

To ensure we don't break, maybe we need to us a buffer of 2048 entries.

Quoted 5 lines

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice · line 105

Quoted 3 lines
hw/i386/intel_iommu_accel.c +2 -0
+static void vtd_prq_read_fault(void *opaque)
+{

perhapss s/vtd_prq_read_fault/vtd_fs_faultq_read/, then aligned with the below create/destroy/setup helpers.

Will do.

Quoted 57 lines
Quoted 22 lines
hw/i386/intel_iommu_accel.c +21 -0
+    VTDAccelPASIDCacheEntry *vtd_pce = opaque;
+    struct iommu_hwpt_pgfault fault[FAULTQ_BUF_SIZE];
+    uint32_t id = vtd_pce->fault_id;
+    int 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, int fault_fd)
+{
+    HostIOMMUDeviceIOMMUFD *hiodi =

HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);

Quoted 4 lines
hw/i386/intel_iommu_accel.c +4 -0
+
+    if (fault_fd < 0) {
+        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(hiodi->iommufd, fault_id);
+}
+
+static bool vtd_create_fs_faultq(VTDHostIOMMUDevice *vtd_hiod,
+                                 uint32_t *fault_id_p, int *fault_fd_p,
+                                 Error **errp)
+{
+    HostIOMMUDeviceIOMMUFD *hiodi =

HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);

Quoted 9 lines
hw/i386/intel_iommu_accel.c +9 -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 ||
+        vtd_dev_to_context_entry(s, bus_n, vtd_hiod->devfn, &ce) ||
+        !VTD_CE_GET_PRE(&ce)) {
Quoted 8 lines

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 164

Quoted 5 lines
hw/i386/intel_iommu_accel.c +5 -0
+        *fault_id_p = 0;
+        *fault_fd_p = -1;
+        return true;
+    }
+

this PRE validation hunk better in the caller side. This helper would be pure creation helper.

Will do.

Quoted 3 lines

BTW. is there any point that we can cach the CE
instead of fetching it here. It seems we won't come to this helper if no CE.

Looks not deserved of a cache like that, it is just some memory reads. Having such a cache makes us extra burden to invalidate it when cc_inv_dsc.

Quoted 100 lines
Quoted 42 lines
hw/i386/intel_iommu_accel.c +41 -0
+    if (!iommufd_backend_alloc_faultq(hiodi->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:
+    vtd_destroy_fs_faultq(vtd_hiod, fault_id, fault_fd);
+    return false;
+}
+
+static void vtd_destroy_old_fs_faultq(VTDAccelPASIDCacheEntry *vtd_pce)
+{
+    if (vtd_pce->fault_fd < 0) {
+        return;
+    }
+
+    qemu_set_fd_handler(vtd_pce->fault_fd, NULL, NULL, NULL);
+    vtd_destroy_fs_faultq(vtd_pce->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, int fault_fd)
+{

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as)

Quoted 10 lines
hw/i386/intel_iommu_accel.c +10 -0
+    if (fault_fd < 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 +2 -0
+                               bool has_fault_id, uint32_t fault_id,
+                               uint32_t *fs_hwpt_id, Error **errp)
  {
      HostIOMMUDeviceIOMMUFD *hiodi =

HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice · line 221

Quoted 2 lines
hw/i386/intel_iommu_accel.c +0 -0
      struct iommu_hwpt_vtd_s1 vtd = {};
      uint32_t flags = vtd_hiod->iommu_state->pasid ?

IOMMU_HWPT_ALLOC_PASID : 0;

hw/i386/intel_iommu_accel.c static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice

Quoted 7 lines
hw/i386/intel_iommu_accel.c +2 -0
+    flags |= has_fault_id ? IOMMU_HWPT_FAULT_ID_VALID : 0;
+
      vtd.flags = (VTD_SM_PASID_ENTRY_SRE(pe) ? IOMMU_VTD_S1_SRE : 0) |
                  (VTD_SM_PASID_ENTRY_WPE(pe) ? IOMMU_VTD_S1_WPE : 0) |
                  (VTD_SM_PASID_ENTRY_EAFE(pe) ? IOMMU_VTD_S1_EAFE : 0);
@@ -94,7 +231,7 @@ static bool vtd_create_fs_hwpt(VTDHostIOMMUDevice

*vtd_hiod,

hw/i386/intel_iommu_accel.c static bool

Quoted 9 lines
hw/i386/intel_iommu_accel.c +1 -1
      return iommufd_backend_alloc_hwpt(hiodi->iommufd, hiodi->devid,
                                        hiodi->hwpt_id, flags,
                                        IOMMU_HWPT_DATA_VTD_S1, sizeof(vtd), &vtd,
-                                      0, fs_hwpt_id, errp);
+                                      fault_id, fs_hwpt_id, errp);
  }
 
  static void vtd_destroy_old_fs_hwpt(VTDAccelPASIDCacheEntry *vtd_pce)
@@ -115,7 +252,8 @@ static bool

vtd_device_attach_iommufd(VTDAccelPASIDCacheEntry *vtd_pce,

hw/i386/intel_iommu_accel.c static bool · line 253

Quoted 2 lines
hw/i386/intel_iommu_accel.c +0 -0
      VTDHostIOMMUDevice *vtd_hiod = vtd_pce->vtd_hiod;
      HostIOMMUDeviceIOMMUFD *hiodi =

HOST_IOMMU_DEVICE_IOMMUFD(vtd_hiod->hiod);

hw/i386/intel_iommu_accel.c static bool

Quoted 8 lines
hw/i386/intel_iommu_accel.c +2 -1
      VTDPASIDEntry *pe = &vtd_pce->pasid_entry;
-    uint32_t hwpt_id = hiodi->hwpt_id, pasid = vtd_pce->pasid;
+    uint32_t hwpt_id = hiodi->hwpt_id, pasid = vtd_pce->pasid, fault_id = 0;
+    int fault_fd = -1;
      bool ret;
 
      /*
@@ -130,7 +268,12 @@ static bool

vtd_device_attach_iommufd(VTDAccelPASIDCacheEntry *vtd_pce,

hw/i386/intel_iommu_accel.c static bool vtd_device_attach_iommufd(VTDAccelPASIDCacheEntry *vtd_pce, · line 274

Quoted 8 lines
hw/i386/intel_iommu_accel.c +4 -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_fd >= 0, fault_id,
Quoted 2 lines

this funciton call can reuse the PRE bit validation result instead of checking fault_fd.

Yes, will do.

Thanks
Zhenzhong

#12 lore raw

PATCH v5 5/6 intel_iommu_accel: Accept PRQ response for passthrough device

In reply to 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.

If there is either PRQ overflow or full error condition, propagate a success response to host for LPIG request with the PRQ requests dropped. For other error conditions, propagate response of invalid request.

---
 hw/i386/intel_iommu_accel.h   | 12 +++++
 include/hw/i386/intel_iommu.h |  6 +++
 hw/i386/intel_iommu.c         |  4 ++
 hw/i386/intel_iommu_accel.c   | 98 ++++++++++++++++++++++++++++++++---
 hw/i386/trace-events          |  1 +
 5 files changed, 114 insertions(+), 7 deletions(-)
hw/i386/intel_iommu_accel.h +12 -0
index e0319749a5..0cc22b2cbb 100644
@@ -20,6 +20,7 @@ typedef struct VTDAccelPASIDCacheEntry {
     uint32_t fs_hwpt_id;
     uint32_t fault_id;
     int fault_fd;
+    QLIST_HEAD(, VTDPRQEntry) vtd_prq_list;
     QLIST_ENTRY(VTDAccelPASIDCacheEntry) next;
 } VTDAccelPASIDCacheEntry;
 
@@ -31,6 +32,9 @@ void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,
                                       uint64_t npages, bool ih);
 void vtd_accel_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info);
 void vtd_accel_pasid_cache_reset(IntelIOMMUState *s);
+bool vtd_accel_propagate_page_group_response(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 +73,14 @@ static inline void vtd_accel_pasid_cache_reset(IntelIOMMUState *s)
 {
 }
 
+static inline
+bool vtd_accel_propagate_page_group_response(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
index 1842ba5840..5d44eac0ed 100644
@@ -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
index dcf23924ef..4e54c38c7e 100644
@@ -3380,6 +3380,10 @@ static bool vtd_process_page_group_response_desc(IntelIOMMUState *s,
         response.response_code = IOMMU_PRI_RESP_FAILURE;
     }
 
+    if (vtd_accel_propagate_page_group_response(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 +91 -7

117 lines not on this page · raw message

hw/i386/trace-events +1 -0
index dca726c9b8..bfdb100e8d 100644
@@ -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_write_response(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.52.0
#13 lore raw

Re: [PATCH v5 5/6] intel_iommu_accel: Accept PRQ response for passthrough device

Quoted 81 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.

If there is either PRQ overflow or full error condition, propagate a success response to host for LPIG request with the PRQ requests dropped. For other error conditions, propagate response of invalid request.

---
 hw/i386/intel_iommu_accel.h   | 12 +++++ include/hw/i386/intel_iommu.h |  6 +++ hw/i386/intel_iommu.c         |  4 ++ hw/i386/intel_iommu_accel.c   | 98 ++++++++++++++++++++++++++++++++--- hw/i386/trace-events          |  1 +

 5 files changed, 114 insertions(+), 7 deletions(-)
hw/i386/intel_iommu_accel.h +1 -0
index e0319749a5..0cc22b2cbb 100644  
@@ -20,6 +20,7 @@ typedef struct VTDAccelPASIDCacheEntry {  
      uint32_t fs_hwpt_id;  
      uint32_t fault_id;  
      int fault_fd;  
+    QLIST_HEAD(, VTDPRQEntry) vtd_prq_list;  
      QLIST_ENTRY(VTDAccelPASIDCacheEntry) next;  
  } VTDAccelPASIDCacheEntry;  

hw/i386/intel_iommu_accel.h +3 -0
@@ -31,6 +32,9 @@ void vtd_flush_host_piotlb_all_locked(IntelIOMMUState *s, uint16_t domain_id,  
                                        uint64_t npages, bool ih);  
  void vtd_accel_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo *pc_info);  
  void vtd_accel_pasid_cache_reset(IntelIOMMUState *s);  
+bool vtd_accel_propagate_page_group_response(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 +73,14 @@ static inline void vtd_accel_pasid_cache_reset(IntelIOMMUState *s)  
  {  
  }  

hw/i386/intel_iommu_accel.h +8 -0
+static inline  
+bool vtd_accel_propagate_page_group_response(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
index 1842ba5840..5d44eac0ed 100644  
@@ -100,6 +100,12 @@ typedef struct VTDPASIDCacheEntry {  
      bool valid;  
  } VTDPASIDCacheEntry;  

include/hw/i386/intel_iommu.h +6 -0
+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
index dcf23924ef..4e54c38c7e 100644  
@@ -3380,6 +3380,10 @@ static bool vtd_process_page_group_response_desc(IntelIOMMUState *s,  
          response.response_code = IOMMU_PRI_RESP_FAILURE;  
      }  
Quoted 4 lines


+    if (vtd_accel_propagate_page_group_response(s, rid, pasid, &response)) { +        return true;
+    }

Question to understand why we are implementing is this way.
Is there any reason why we don't consider the accel part as a regular pri client? Would it be an issue to make it register a pri_notifier to get the completion?

I'm not against the current implement, just curiosity to get your thoughts. Maybe @Yi has an opinion about that as well?

Quoted 24 lines

+
     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
index 3808112f79..7d54ce12e9 100644  
@@ -78,6 +78,61 @@ VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as)  
      return NULL;  
  }  

hw/i386/intel_iommu_accel.c +12 -0
+static void vtd_prq_write_response(VTDAccelPASIDCacheEntry *vtd_pce,  
+                                   uint32_t cookie, uint32_t code)  
+{  
+    struct iommu_hwpt_page_response resp = {cookie, code};  
+    uint32_t id = vtd_pce->fault_id;  
+    int fd = vtd_pce->fault_fd;  
+    ssize_t bytes;  
+  
+    bytes = write(fd, &resp, sizeof(resp));  
+    trace_vtd_prq_write_response(id, fd, cookie, code, bytes);  
+    if (bytes < 0) {  
+        error_report_once("FAULTQ(id %u): write failed "  

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 102

Quoted 10 lines
hw/i386/intel_iommu_accel.c +10 -0
+                          "[cookie 0x%x code 0x%x] (%m)", id, cookie, code);  
+    }  
+}  
+  
+static void vtd_prq_response_notify(VTDAccelPASIDCacheEntry *vtd_pce,  
+                                    IOMMUPRIResponse *response)  
+{  
+    VTDPRQEntry *prqe, *tmp;  
+  
+    QLIST_FOREACH_SAFE(prqe, &vtd_pce->vtd_prq_list, next, tmp) {  

@Yi do you have an opinion about the data structure choice for vtd_prq_list?
I know this is already merged but maybe we are reaching the point where we perform too costly lookups here?

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 110

Quoted 8 lines

+        if (prqe->grpid != response->prgi) {
+            continue;
+        }

hw/i386/intel_iommu_accel.c +5 -0
+  
+        vtd_prq_write_response(vtd_pce, prqe->cookie, response->response_code);  
+  
+        QLIST_REMOVE(prqe, next);  
+        g_free(prqe);  

Would it be safe to break here?
We expect to have a single match in the list right?

Quoted 67 lines

+    }

hw/i386/intel_iommu_accel.c +24 -0
+}  
+  
+bool vtd_accel_propagate_page_group_response(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) {  
+            vtd_prq_response_notify(vtd_pce, response);  
+            return true;  
+        }  
+    }  
+  
+    return false;  
+}  
+  

 static void vtd_prq_report_fault(VTDAccelPASIDCacheEntry *vtd_pce, struct iommu_hwpt_pgfault *fault, unsigned cnt) {

hw/i386/intel_iommu_accel.c +0 -0
@@ -87,14 +142,42 @@ static void vtd_prq_report_fault(VTDAccelPASIDCacheEntry *vtd_pce,  


     for (; cnt--; fault++) {
         bool last_page = fault->flags & IOMMU_PGFAULT_FLAGS_LAST_PAGE; +        int ret;

hw/i386/intel_iommu_accel.c +20 -0
+  
+        ret = vtd_pri_request_page(vtd_hiod->bus, vtd_hiod->iommu_state,  
+                                   vtd_hiod->devfn, 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);  
+        if (!last_page) {  
+            continue;  
+        }  
+  
+        if (!ret) {  
+            VTDPRQEntry *prqe = g_malloc0(sizeof(*prqe));  
+  
+            prqe->grpid = fault->grpid;  
+            prqe->cookie = fault->cookie;  
+            QLIST_INSERT_HEAD(&vtd_pce->vtd_prq_list, prqe, next);  
+            continue;  
+        }  


-        vtd_pri_request_page(vtd_hiod->bus, vtd_hiod->iommu_state, -                             vtd_hiod->devfn, 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); +        /*
+         * vt-d spec 7.4.1:
+         *
+         * Hardware generates page group response with code of success for +         * PRQ overflow and PRQ full, with code of invalid request for +         * other error conditions.

Quoted 8 lines

+         */
+        if (ret == -ENOSPC) {
+            vtd_prq_write_response(vtd_pce, fault->cookie, +                                   IOMMUFD_PAGE_RESP_SUCCESS); +        } else {
+            vtd_prq_write_response(vtd_pce, fault->cookie, +                                   IOMMUFD_PAGE_RESP_INVALID); +        }

Need to remove and free elements from the linked list in addition to the notification?

Quoted 16 lines

     }
 }

hw/i386/intel_iommu_accel.c +1 -0
@@ -424,6 +507,7 @@ static void vtd_accel_fill_pc(VTDHostIOMMUDevice *vtd_hiod, uint32_t pasid,  
      vtd_pce->pasid = pasid;  
      vtd_pce->pasid_entry = *pe;  
      vtd_pce->fault_fd = -1;  
+    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
index dca726c9b8..bfdb100e8d 100644  
@@ -78,6 +78,7 @@ vtd_device_attach_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t hwpt_id, int re  
Quoted 4 lines

 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_write_response(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"

Why is "ret" called "bytes" in the parameters?
Is it because of how is it named at call site?

Quoted 3 lines

 # amd_iommu.c  
 amdvi_evntlog_fail(uint64_t addr, uint32_t head) "error: fail to write at addr 0x%"PRIx64" +  offset 0x%"PRIx32
#14 lore raw

RE: [PATCH v5 5/6] intel_iommu_accel: Accept PRQ response for passthrough device

Quoted 98 lines

-----Original Message-----
From: Clément MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v5 5/6] intel_iommu_accel: Accept PRQ response for passthrough device

Quoted 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.

If there is either PRQ overflow or full error condition, propagate a success response to host for LPIG request with the PRQ requests dropped. For other error conditions, propagate response of invalid request.

Signed-off-by Zhenzhong Duan

<[[email protected]](mailto:[email protected])>

Quoted 1 line
Reviewed-by Clement Mathieu--Drif <[clement.mathieu--

[email protected]](mailto:[email protected])>

Quoted 1 line
Tested-by Xudong Hao

<[[email protected]](mailto:[email protected])>

hw/i386/intel_iommu_accel.h void vtd_flush_host_piotlb_all_locked(IntelIOMMUState

Quoted 21 lines

---
 hw/i386/intel_iommu_accel.h   | 12 +++++ include/hw/i386/intel_iommu.h |  6 +++ hw/i386/intel_iommu.c         |  4 ++ hw/i386/intel_iommu_accel.c   | 98 ++++++++++++++++++++++++++++++++--- hw/i386/trace-events          |  1 +

 5 files changed, 114 insertions(+), 7 deletions(-)
hw/i386/intel_iommu_accel.h +1 -0
index e0319749a5..0cc22b2cbb 100644
@@ -20,6 +20,7 @@ typedef struct VTDAccelPASIDCacheEntry {
      uint32_t fs_hwpt_id;
      uint32_t fault_id;
      int fault_fd;
+    QLIST_HEAD(, VTDPRQEntry) vtd_prq_list;
      QLIST_ENTRY(VTDAccelPASIDCacheEntry) next;
  } VTDAccelPASIDCacheEntry;
 
@@ -31,6 +32,9 @@ void vtd_flush_host_piotlb_all_locked(IntelIOMMUState

*s, uint16_t domain_id,

Quoted 2 lines
hw/i386/intel_iommu_accel.h +0 -0
                                        uint64_t npages, bool ih);

 void vtd_accel_pasid_cache_sync(IntelIOMMUState *s, VTDPASIDCacheInfo

*pc_info);

hw/i386/intel_iommu_accel.h static inline void

Quoted 8 lines

 void vtd_accel_pasid_cache_reset(IntelIOMMUState *s);
+bool vtd_accel_propagate_page_group_response(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,

hw/i386/intel_iommu_accel.h +0 -0
@@ -69,6 +73,14 @@ static inline void

vtd_accel_pasid_cache_reset(IntelIOMMUState *s)

hw/i386/intel_iommu.c static bool

Quoted 36 lines
hw/i386/intel_iommu_accel.h +8 -0
  {
  }
 
+static inline
+bool vtd_accel_propagate_page_group_response(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
index 1842ba5840..5d44eac0ed 100644
@@ -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
index dcf23924ef..4e54c38c7e 100644
@@ -3380,6 +3380,10 @@ static bool

vtd_process_page_group_response_desc(IntelIOMMUState *s,

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
          response.response_code = IOMMU_PRI_RESP_FAILURE;
      }
 
Quoted 7 lines

hw/i386/intel_iommu.c static bool vtd_process_page_group_response_desc(IntelIOMMUState *s, · line 3385

Quoted 3 lines
hw/i386/intel_iommu.c +3 -0
+    if (vtd_accel_propagate_page_group_response(s, rid, pasid, &response)) {
+        return true;
+    }

Question to understand why we are implementing is this way.
Is there any reason why we don't consider the accel part as a regular pri client? Would it be an issue to make it register a pri_notifier to get the completion?

Pri_notifier is defined in VTDAddressSpace which accel doesn't use, accel uses VTDAccelPASIDCacheEntry.

Quoted 44 lines

I'm not against the current implement, just curiosity to get your thoughts. Maybe @Yi has an opinion about that as well?

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice

Quoted 9 lines
hw/i386/intel_iommu.c +1 -0
+
      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
index 3808112f79..7d54ce12e9 100644
@@ -78,6 +78,61 @@ VTDHostIOMMUDevice

*vtd_find_hiod_iommufd(VTDAddressSpace *as)

Quoted 15 lines
hw/i386/intel_iommu_accel.c +12 -0
      return NULL;
  }
 
+static void vtd_prq_write_response(VTDAccelPASIDCacheEntry *vtd_pce,
+                                   uint32_t cookie, uint32_t code)
+{
+    struct iommu_hwpt_page_response resp = {cookie, code};
+    uint32_t id = vtd_pce->fault_id;
+    int fd = vtd_pce->fault_fd;
+    ssize_t bytes;
+
+    bytes = write(fd, &resp, sizeof(resp));
+    trace_vtd_prq_write_response(id, fd, cookie, code, bytes);
+    if (bytes < 0) {
+        error_report_once("FAULTQ(id %u): write failed "

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 102

Quoted 10 lines
hw/i386/intel_iommu_accel.c +10 -0
+                          "[cookie 0x%x code 0x%x] (%m)", id, cookie, code);
+    }
+}
+
+static void vtd_prq_response_notify(VTDAccelPASIDCacheEntry *vtd_pce,
+                                    IOMMUPRIResponse *response)
+{
+    VTDPRQEntry *prqe, *tmp;
+
+    QLIST_FOREACH_SAFE(prqe, &vtd_pce->vtd_prq_list, next, tmp) {

@Yi do you have an opinion about the data structure choice for vtd_prq_list? I know this is already merged but maybe we are reaching the point where we perform too costly lookups here?

Quoted 12 lines

hw/i386/intel_iommu_accel.c VTDHostIOMMUDevice *vtd_find_hiod_iommufd(VTDAddressSpace *as) · line 110

Quoted 9 lines
hw/i386/intel_iommu_accel.c +8 -0
+        if (prqe->grpid != response->prgi) {
+            continue;
+        }
+
+        vtd_prq_write_response(vtd_pce, prqe->cookie, response-
 response_code);
+
+        QLIST_REMOVE(prqe, next);
+        g_free(prqe);

Would it be safe to break here?
We expect to have a single match in the list right?

Right, will do.

Quoted 72 lines

hw/i386/intel_iommu_accel.c static void

Quoted 30 lines
hw/i386/intel_iommu_accel.c +25 -0
+    }
+}
+
+bool vtd_accel_propagate_page_group_response(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) {
+            vtd_prq_response_notify(vtd_pce, response);
+            return true;
+        }
+    }
+
+    return false;
+}
+
  static void vtd_prq_report_fault(VTDAccelPASIDCacheEntry *vtd_pce,
                                   struct iommu_hwpt_pgfault *fault, unsigned cnt)

 {

hw/i386/intel_iommu_accel.c +0 -0
@@ -87,14 +142,42 @@ static void

vtd_prq_report_fault(VTDAccelPASIDCacheEntry *vtd_pce,

Quoted 41 lines
hw/i386/intel_iommu_accel.c +30 -7
      for (; cnt--; fault++) {
          bool last_page = fault->flags & IOMMU_PGFAULT_FLAGS_LAST_PAGE;
+        int ret;
+
+        ret = vtd_pri_request_page(vtd_hiod->bus, vtd_hiod->iommu_state,
+                                   vtd_hiod->devfn, 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);
+        if (!last_page) {
+            continue;
+        }
+
+        if (!ret) {
+            VTDPRQEntry *prqe = g_malloc0(sizeof(*prqe));
+
+            prqe->grpid = fault->grpid;
+            prqe->cookie = fault->cookie;
+            QLIST_INSERT_HEAD(&vtd_pce->vtd_prq_list, prqe, next);
+            continue;
+        }
 
-        vtd_pri_request_page(vtd_hiod->bus, vtd_hiod->iommu_state,
-                             vtd_hiod->devfn, 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);
+        /*
+         * vt-d spec 7.4.1:
+         *
+         * Hardware generates page group response with code of success for
+         * PRQ overflow and PRQ full, with code of invalid request for
+         * other error conditions.
+         */
+        if (ret == -ENOSPC) {
+            vtd_prq_write_response(vtd_pce, fault->cookie,
Quoted 8 lines

hw/i386/intel_iommu_accel.c static void vtd_prq_report_fault(VTDAccelPASIDCacheEntry *vtd_pce, · line 180

Quoted 5 lines
hw/i386/intel_iommu_accel.c +5 -0
+                                   IOMMUFD_PAGE_RESP_SUCCESS);
+        } else {
+            vtd_prq_write_response(vtd_pce, fault->cookie,
+                                   IOMMUFD_PAGE_RESP_INVALID);
+        }

Need to remove and free elements from the linked list in addition to the notification?

Not get, why to remove elements before PRQ FULL/Overflow happen, their requests are already sent to guest.

Quoted 22 lines

hw/i386/intel_iommu_accel.c static void vtd_accel_fill_pc(VTDHostIOMMUDevice

Quoted 5 lines
hw/i386/intel_iommu_accel.c +0 -0
      }
  }
 
@@ -424,6 +507,7 @@ static void vtd_accel_fill_pc(VTDHostIOMMUDevice

*vtd_hiod, uint32_t pasid,

hw/i386/trace-events vtd_device_attach_hwpt(uint32_t dev_id, uint32_t pasid,

Quoted 12 lines
hw/i386/intel_iommu_accel.c +1 -0
      vtd_pce->pasid = pasid;
      vtd_pce->pasid_entry = *pe;
      vtd_pce->fault_fd = -1;
+    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
index dca726c9b8..bfdb100e8d 100644
@@ -78,6 +78,7 @@ vtd_device_attach_hwpt(uint32_t dev_id, uint32_t pasid,

uint32_t hwpt_id, int re

Quoted 1 line

 vtd_device_detach_hwpt(uint32_t dev_id, uint32_t pasid, int ret) "dev_id %d

pasid %d ret: %d"

Quoted 1 line

 vtd_device_reattach_def_hwpt(uint32_t dev_id, uint32_t pasid, uint32_t

Quoted 8 lines

hwpt_id, int ret) "dev_id %d pasid %d hwpt_id %d, ret: %d"

Quoted 1 line

 vtd_prq_read_fault(uint32_t fault_id, uint32_t fault_fd, ssize_t bytes)

"fault_id %d fault_fd %d ret: %zd"

Quoted 1 line

+vtd_prq_write_response(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"

Why is "ret" called "bytes" in the parameters?
Is it because of how is it named at call site?

Yes, let me know if you prefer "ret", I'm fine to change.

Thanks
Zhenzhong

#15 lore raw

PATCH v5 6/6 intel_iommu_accel: teardown FAULTQ resources in bottom half

In reply to Zhenzhong Duan

When 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 | 40 ++++++++++++++++++++++++++++++++++---
 1 file changed, 37 insertions(+), 3 deletions(-)
hw/i386/intel_iommu_accel.c +37 -3
index 7d54ce12e9..ff63ef8300 100644
@@ -270,17 +270,51 @@ free_faultq:
     return false;
 }
 
+typedef struct IOMMUFaultQueue {
+    IOMMUFDBackend *iommufd;
+    uint32_t id;
+    int fd;
+    QLIST_HEAD(, VTDPRQEntry) vtd_prq_list;
+} IOMMUFaultQueue;
+
+static void faultq_teardown_bh(void *opaque)
+{
+    IOMMUFaultQueue *fq = opaque;
+    VTDPRQEntry *prqe, *next;
+
+    QLIST_FOREACH_SAFE(prqe, &fq->vtd_prq_list, next, next) {
+        QLIST_REMOVE(prqe, next);
+        g_free(prqe);
+    }
+
+    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(VTDAccelPASIDCacheEntry *vtd_pce)
 {
+    HostIOMMUDeviceIOMMUFD *idev =
+        HOST_IOMMU_DEVICE_IOMMUFD(vtd_pce->vtd_hiod->hiod);
+    IOMMUFaultQueue *fq;
+
     if (vtd_pce->fault_fd < 0) {
         return;
     }
 
-    qemu_set_fd_handler(vtd_pce->fault_fd, NULL, NULL, NULL);
-    vtd_destroy_fs_faultq(vtd_pce->vtd_hiod, vtd_pce->fault_id,
-                          vtd_pce->fault_fd);
+    fq = g_malloc(sizeof(IOMMUFaultQueue));
+    fq->iommufd = idev->iommufd;
+    fq->fd = vtd_pce->fault_fd;
+    fq->id = vtd_pce->fault_id;
     vtd_pce->fault_id = 0;
     vtd_pce->fault_fd = -1;
+    fq->vtd_prq_list.lh_first = vtd_pce->vtd_prq_list.lh_first;
+    QLIST_INIT(&vtd_pce->vtd_prq_list);
+
+    aio_bh_schedule_oneshot(iohandler_get_aio_context(),
+                            faultq_teardown_bh, fq);
 }
 
 static void vtd_setup_fs_faultq(VTDAccelPASIDCacheEntry *vtd_pce,
Signature · 1 line
2.52.0
#16 lore raw
In reply to Zhenzhong Duan

Hi Zhenzhong,

Thanks for the respin, I will try to review it by the end of the week.

I will also send a patch soon to make PRI thread safe.

cmd

Quoted 76 lines · Zhenzhong Duan

Caution: External email. Do not open attachments or click links, unless this email comes from a known sender and you know the content is safe.

Hi,

When svm=on,pasid-bits=N,fsts=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.

GIT branch: [https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_prq.v5](https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_prq.v5)

Tests:
Tested with DSA wq attached to an user APP which triggers IO page fault on process page table durging DMA.
Build test with window cross-build.

Thanks
Zhenzhong

Changelog:
v5:
- add comment "Accept the PCI PASID as input" to docstring of function vtd_pri_request_page (Clement)
- change vtd_prq_report_fault() parameter type "int cnt" -> "unsigned cnt" (Clement)
- check return value of vtd_pri_request_page() and send response (Clement)
- free all vtd_prq_list entries when detaching (Clement)

v4:
- add the comment to the docstring of the function vtd_prq_report_fault itself (Clement)
- add "out_" prefix for fault_id & fault_fd output parameter (Clement)

v3:
- convert to PCI pasid before passing to vtd_pri_request_page (Clement)
- expose vtd_pri_request_page and drop accel_ops pointer (Clement)
- add a cleanup patch "intel_iommu_accel: Guard VTDAccelPASIDCacheEntry definition" (Philippe)

v2:
- define fault_fd int type (Liuyi)
- update comments on FAULTQ_BUF_SIZE (Liuyi)
- drop param vtd_hiod in vtd_destroy_old_fs_faultq() (Liuyi)
- s/vtd_propagate_page_group_response_accel/vtd_accel_propagate_page_group_response (Liuyi)
- call vtd_prq_response_notify() directly instead of through pri_notifier (Liuyi)
- replace "fault_id != 0" check with "fault_fd < 0" check (Liuyi)

Zhenzhong Duan (6):
  intel_iommu_accel: Guard VTDAccelPASIDCacheEntry definition 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 +- hw/i386/intel_iommu_internal.h |   3 + include/hw/i386/intel_iommu.h  |   6 + include/system/iommufd.h       |   7 +-
 backends/iommufd.c             |  28 +++- hw/arm/smmuv3-accel.c          |   6 +- hw/i386/intel_iommu.c          |  13 +- hw/i386/intel_iommu_accel.c    | 278 ++++++++++++++++++++++++++++++++- hw/vfio/iommufd.c              |   2 +- backends/trace-events          |   3 +- hw/i386/trace-events           |   2 +

 11 files changed, 342 insertions(+), 22 deletions(-)
Signature · 1 line
2.52.0