diff mbox series

[RFC,v4,21/24] vfio/as: Add vfio device iterator callback for iommufd

Message ID 20230712072528.275577-22-zhenzhong.duan@intel.com
State New
Headers show
Series vfio: Adopt iommufd | expand

Commit Message

Duan, Zhenzhong July 12, 2023, 7:25 a.m. UTC
The way to get vfio device pointer is different between legacy
container and iommufd container, with iommufd backend support
added, it's time to add the iterator support for iommufd.

In order to implement it, a pointer to hwpt is added in vbasedev.

Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
---
 hw/vfio/iommufd.c             | 31 +++++++++++++++++++++++++++++++
 include/hw/vfio/vfio-common.h |  1 +
 2 files changed, 32 insertions(+)

Comments

Nicolin Chen Aug. 17, 2023, 5:48 a.m. UTC | #1
On Wed, Jul 12, 2023 at 03:25:25PM +0800, Zhenzhong Duan wrote:

> The way to get vfio device pointer is different between legacy
> container and iommufd container, with iommufd backend support
> added, it's time to add the iterator support for iommufd.
> 
> In order to implement it, a pointer to hwpt is added in vbasedev.
[...]
> diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h
> index 6434a442fd..d596e802b0 100644
> --- a/include/hw/vfio/vfio-common.h
> +++ b/include/hw/vfio/vfio-common.h
> @@ -133,6 +133,7 @@ typedef struct VFIODevice {
>  #ifdef CONFIG_IOMMUFD
>      int devid;
>      IOMMUFDBackend *iommufd;
> +    VFIOIOASHwpt *hwpt;

I don't feel quite confident about this, since a patch prior just
added the following function:

+static VFIOIOASHwpt *vfio_find_hwpt_for_dev(VFIOIOMMUFDContainer *container,
+                                            VFIODevice *vbasedev)

This feels a bit of conflict in the same series. Mind elaborating?

Thanks
Nicolin
Duan, Zhenzhong Aug. 17, 2023, 6:05 a.m. UTC | #2
>-----Original Message-----
>From: Nicolin Chen <nicolinc@nvidia.com>
>Sent: Thursday, August 17, 2023 1:49 PM
>Subject: Re: [RFC PATCH v4 21/24] vfio/as: Add vfio device iterator callback for
>iommufd
>
>On Wed, Jul 12, 2023 at 03:25:25PM +0800, Zhenzhong Duan wrote:
>
>> The way to get vfio device pointer is different between legacy
>> container and iommufd container, with iommufd backend support
>> added, it's time to add the iterator support for iommufd.
>>
>> In order to implement it, a pointer to hwpt is added in vbasedev.
>[...]
>> diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-
>common.h
>> index 6434a442fd..d596e802b0 100644
>> --- a/include/hw/vfio/vfio-common.h
>> +++ b/include/hw/vfio/vfio-common.h
>> @@ -133,6 +133,7 @@ typedef struct VFIODevice {
>>  #ifdef CONFIG_IOMMUFD
>>      int devid;
>>      IOMMUFDBackend *iommufd;
>> +    VFIOIOASHwpt *hwpt;
>
>I don't feel quite confident about this, since a patch prior just
>added the following function:
>
>+static VFIOIOASHwpt *vfio_find_hwpt_for_dev(VFIOIOMMUFDContainer
>*container,
>+                                            VFIODevice *vbasedev)
>
>This feels a bit of conflict in the same series. Mind elaborating?

Good finding, I'll move " VFIOIOASHwpt *hwpt" to the prior patch,
then vfio_find_hwpt_for_dev() could also use it.

Thanks
Zhenzhong
diff mbox series

Patch

diff --git a/hw/vfio/iommufd.c b/hw/vfio/iommufd.c
index 286ad0b766..e532eed2ac 100644
--- a/hw/vfio/iommufd.c
+++ b/hw/vfio/iommufd.c
@@ -36,6 +36,34 @@ 
 #include "qemu/cutils.h"
 #include "qemu/char_dev.h"
 
+static VFIODevice *iommufd_dev_iter_next(VFIOContainer *bcontainer,
+                                           VFIODevice *curr)
+{
+
+    VFIOIOASHwpt *hwpt;
+
+    assert(object_class_dynamic_cast(OBJECT_CLASS(bcontainer->ops),
+                                     TYPE_VFIO_IOMMU_BACKEND_IOMMUFD_OPS));
+
+    VFIOIOMMUFDContainer *container = container_of(bcontainer,
+                                                   VFIOIOMMUFDContainer,
+                                                   bcontainer);
+
+    if (!curr) {
+        hwpt = QLIST_FIRST(&container->hwpt_list);
+    } else {
+        if (curr->next.le_next) {
+            return curr->next.le_next;
+        }
+        hwpt = curr->hwpt->next.le_next;
+    }
+
+    if (!hwpt) {
+        return NULL;
+    }
+    return QLIST_FIRST(&hwpt->device_list);
+}
+
 static int iommufd_map(VFIOContainer *bcontainer, hwaddr iova,
                        ram_addr_t size, void *vaddr, bool readonly)
 {
@@ -218,6 +246,7 @@  static void vfio_device_detach_container(VFIODevice *vbasedev,
     hwpt = vfio_find_hwpt_for_dev(container, vbasedev);
     if (hwpt) {
         QLIST_REMOVE(vbasedev, next);
+        vbasedev->hwpt = NULL;
         if (QLIST_EMPTY(&hwpt->device_list)) {
             vfio_container_put_hwpt(hwpt);
         }
@@ -281,6 +310,7 @@  static int vfio_device_attach_container(VFIODevice *vbasedev,
     hwpt = vfio_container_get_hwpt(container, attach_data.pt_id);
 
     QLIST_INSERT_HEAD(&hwpt->device_list, vbasedev, next);
+    vbasedev->hwpt = hwpt;
     return 0;
 }
 
@@ -490,6 +520,7 @@  static void vfio_iommu_backend_iommufd_ops_class_init(ObjectClass *oc,
                                                      void *data) {
     VFIOIOMMUBackendOpsClass *ops = VFIO_IOMMU_BACKEND_OPS_CLASS(oc);
 
+    ops->dev_iter_next = iommufd_dev_iter_next;
     ops->dma_map = iommufd_map;
     ops->dma_unmap = iommufd_unmap;
     ops->attach_device = iommufd_attach_device;
diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h
index 6434a442fd..d596e802b0 100644
--- a/include/hw/vfio/vfio-common.h
+++ b/include/hw/vfio/vfio-common.h
@@ -133,6 +133,7 @@  typedef struct VFIODevice {
 #ifdef CONFIG_IOMMUFD
     int devid;
     IOMMUFDBackend *iommufd;
+    VFIOIOASHwpt *hwpt;
 #endif
 } VFIODevice;