qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] vfio: Introduce helper vfio_device_to_vfio_pci()
@ 2025-07-31  3:31 Zhenzhong Duan
  2025-07-31  8:01 ` Cédric Le Goater
  2025-07-31  8:26 ` Philippe Mathieu-Daudé
  0 siblings, 2 replies; 6+ messages in thread
From: Zhenzhong Duan @ 2025-07-31  3:31 UTC (permalink / raw)
  To: qemu-devel; +Cc: alex.williamson, clg, eric.auger, chao.p.peng, Zhenzhong Duan

Introduce helper vfio_device_to_vfio_pci() to transform from VFIODevice to
VFIOPCIDevice, also to hide low level VFIO_DEVICE_TYPE_PCI type check.

Suggested-by: Cédric Le Goater <clg@redhat.com>
Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
---
 include/hw/vfio/vfio-device.h |  1 +
 hw/vfio/container.c           |  4 ++--
 hw/vfio/device.c              | 10 +++++++++-
 hw/vfio/iommufd.c             |  4 ++--
 hw/vfio/listener.c            |  4 ++--
 5 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h
index 6e4d5ccdac..00df40d997 100644
--- a/include/hw/vfio/vfio-device.h
+++ b/include/hw/vfio/vfio-device.h
@@ -157,6 +157,7 @@ bool vfio_device_attach_by_iommu_type(const char *iommu_type, char *name,
                                       Error **errp);
 void vfio_device_detach(VFIODevice *vbasedev);
 VFIODevice *vfio_get_vfio_device(Object *obj);
+struct VFIOPCIDevice *vfio_device_to_vfio_pci(VFIODevice *vbasedev);
 
 typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList;
 extern VFIODeviceList vfio_device_list;
diff --git a/hw/vfio/container.c b/hw/vfio/container.c
index 3e13feaa74..f847e3e429 100644
--- a/hw/vfio/container.c
+++ b/hw/vfio/container.c
@@ -1087,7 +1087,7 @@ static int vfio_legacy_pci_hot_reset(VFIODevice *vbasedev, bool single)
         /* Prep dependent devices for reset and clear our marker. */
         QLIST_FOREACH(vbasedev_iter, &group->device_list, next) {
             if (!vbasedev_iter->dev->realized ||
-                vbasedev_iter->type != VFIO_DEVICE_TYPE_PCI) {
+                !vfio_device_to_vfio_pci(vbasedev_iter)) {
                 continue;
             }
             tmp = container_of(vbasedev_iter, VFIOPCIDevice, vbasedev);
@@ -1172,7 +1172,7 @@ out:
 
         QLIST_FOREACH(vbasedev_iter, &group->device_list, next) {
             if (!vbasedev_iter->dev->realized ||
-                vbasedev_iter->type != VFIO_DEVICE_TYPE_PCI) {
+                !vfio_device_to_vfio_pci(vbasedev_iter)) {
                 continue;
             }
             tmp = container_of(vbasedev_iter, VFIOPCIDevice, vbasedev);
diff --git a/hw/vfio/device.c b/hw/vfio/device.c
index 52a1996dc4..a4f9c9216c 100644
--- a/hw/vfio/device.c
+++ b/hw/vfio/device.c
@@ -129,7 +129,7 @@ static inline const char *action_to_str(int action)
 
 static const char *index_to_str(VFIODevice *vbasedev, int index)
 {
-    if (vbasedev->type != VFIO_DEVICE_TYPE_PCI) {
+    if (!vfio_device_to_vfio_pci(vbasedev)) {
         return NULL;
     }
 
@@ -429,6 +429,14 @@ VFIODevice *vfio_get_vfio_device(Object *obj)
     }
 }
 
+VFIOPCIDevice *vfio_device_to_vfio_pci(VFIODevice *vbasedev)
+{
+    if (vbasedev && vbasedev->type == VFIO_DEVICE_TYPE_PCI) {
+        return container_of(vbasedev, VFIOPCIDevice, vbasedev);
+    }
+    return NULL;
+}
+
 bool vfio_device_attach_by_iommu_type(const char *iommu_type, char *name,
                                       VFIODevice *vbasedev, AddressSpace *as,
                                       Error **errp)
diff --git a/hw/vfio/iommufd.c b/hw/vfio/iommufd.c
index 48c590b6a9..c5c89a700e 100644
--- a/hw/vfio/iommufd.c
+++ b/hw/vfio/iommufd.c
@@ -737,8 +737,8 @@ iommufd_cdev_dep_get_realized_vpdev(struct vfio_pci_dependent_device *dep_dev,
     }
 
     vbasedev_tmp = iommufd_cdev_pci_find_by_devid(dep_dev->devid);
-    if (!vbasedev_tmp || !vbasedev_tmp->dev->realized ||
-        vbasedev_tmp->type != VFIO_DEVICE_TYPE_PCI) {
+    if (!vfio_device_to_vfio_pci(vbasedev_tmp) ||
+        !vbasedev_tmp->dev->realized) {
         return NULL;
     }
 
diff --git a/hw/vfio/listener.c b/hw/vfio/listener.c
index f498e23a93..fe15172f22 100644
--- a/hw/vfio/listener.c
+++ b/hw/vfio/listener.c
@@ -450,7 +450,7 @@ static void vfio_device_error_append(VFIODevice *vbasedev, Error **errp)
      * MMIO region mapping failures are not fatal but in this case PCI
      * peer-to-peer transactions are broken.
      */
-    if (vbasedev && vbasedev->type == VFIO_DEVICE_TYPE_PCI) {
+    if (vfio_device_to_vfio_pci(vbasedev)) {
         error_append_hint(errp, "%s: PCI peer-to-peer transactions "
                           "on BARs are not supported.\n", vbasedev->name);
     }
@@ -751,7 +751,7 @@ static bool vfio_section_is_vfio_pci(MemoryRegionSection *section,
     owner = memory_region_owner(section->mr);
 
     QLIST_FOREACH(vbasedev, &bcontainer->device_list, container_next) {
-        if (vbasedev->type != VFIO_DEVICE_TYPE_PCI) {
+        if (!vfio_device_to_vfio_pci(vbasedev)) {
             continue;
         }
         pcidev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
-- 
2.47.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] vfio: Introduce helper vfio_device_to_vfio_pci()
  2025-07-31  3:31 [PATCH] vfio: Introduce helper vfio_device_to_vfio_pci() Zhenzhong Duan
@ 2025-07-31  8:01 ` Cédric Le Goater
  2025-07-31  8:26 ` Philippe Mathieu-Daudé
  1 sibling, 0 replies; 6+ messages in thread
From: Cédric Le Goater @ 2025-07-31  8:01 UTC (permalink / raw)
  To: Zhenzhong Duan, qemu-devel; +Cc: alex.williamson, eric.auger, chao.p.peng

On 7/31/25 05:31, Zhenzhong Duan wrote:
> Introduce helper vfio_device_to_vfio_pci() to transform from VFIODevice to
> VFIOPCIDevice, also to hide low level VFIO_DEVICE_TYPE_PCI type check.
> 
> Suggested-by: Cédric Le Goater <clg@redhat.com>
> Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>


Reviewed-by: Cédric Le Goater <clg@redhat.com>

Thanks,

C.




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] vfio: Introduce helper vfio_device_to_vfio_pci()
  2025-07-31  3:31 [PATCH] vfio: Introduce helper vfio_device_to_vfio_pci() Zhenzhong Duan
  2025-07-31  8:01 ` Cédric Le Goater
@ 2025-07-31  8:26 ` Philippe Mathieu-Daudé
  2025-07-31  8:49   ` Duan, Zhenzhong
  1 sibling, 1 reply; 6+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-07-31  8:26 UTC (permalink / raw)
  To: Zhenzhong Duan, qemu-devel; +Cc: alex.williamson, clg, eric.auger, chao.p.peng

Hi,

On 31/7/25 05:31, Zhenzhong Duan wrote:
> Introduce helper vfio_device_to_vfio_pci() to transform from VFIODevice to
> VFIOPCIDevice, also to hide low level VFIO_DEVICE_TYPE_PCI type check.
> 
> Suggested-by: Cédric Le Goater <clg@redhat.com>
> Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
> ---
>   include/hw/vfio/vfio-device.h |  1 +
>   hw/vfio/container.c           |  4 ++--
>   hw/vfio/device.c              | 10 +++++++++-
>   hw/vfio/iommufd.c             |  4 ++--
>   hw/vfio/listener.c            |  4 ++--
>   5 files changed, 16 insertions(+), 7 deletions(-)
> 
> diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h
> index 6e4d5ccdac..00df40d997 100644
> --- a/include/hw/vfio/vfio-device.h
> +++ b/include/hw/vfio/vfio-device.h
> @@ -157,6 +157,7 @@ bool vfio_device_attach_by_iommu_type(const char *iommu_type, char *name,
>                                         Error **errp);
>   void vfio_device_detach(VFIODevice *vbasedev);
>   VFIODevice *vfio_get_vfio_device(Object *obj);
> +struct VFIOPCIDevice *vfio_device_to_vfio_pci(VFIODevice *vbasedev);

Please return the typedef (like in the implementation), not the struct.

A one line comment describing what this helper does would he helpful.

Regards,

Phil.

> diff --git a/hw/vfio/device.c b/hw/vfio/device.c
> index 52a1996dc4..a4f9c9216c 100644
> --- a/hw/vfio/device.c
> +++ b/hw/vfio/device.c


> @@ -429,6 +429,14 @@ VFIODevice *vfio_get_vfio_device(Object *obj)
>       }
>   }
>   
> +VFIOPCIDevice *vfio_device_to_vfio_pci(VFIODevice *vbasedev)
> +{
> +    if (vbasedev && vbasedev->type == VFIO_DEVICE_TYPE_PCI) {
> +        return container_of(vbasedev, VFIOPCIDevice, vbasedev);
> +    }
> +    return NULL;
> +}



^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: [PATCH] vfio: Introduce helper vfio_device_to_vfio_pci()
  2025-07-31  8:26 ` Philippe Mathieu-Daudé
@ 2025-07-31  8:49   ` Duan, Zhenzhong
  2025-07-31 11:24     ` Philippe Mathieu-Daudé
  0 siblings, 1 reply; 6+ messages in thread
From: Duan, Zhenzhong @ 2025-07-31  8:49 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel@nongnu.org
  Cc: alex.williamson@redhat.com, clg@redhat.com, eric.auger@redhat.com,
	Peng, Chao P



>-----Original Message-----
>From: Philippe Mathieu-Daudé <philmd@linaro.org>
>Subject: Re: [PATCH] vfio: Introduce helper vfio_device_to_vfio_pci()
>
>Hi,
>
>On 31/7/25 05:31, Zhenzhong Duan wrote:
>> Introduce helper vfio_device_to_vfio_pci() to transform from VFIODevice to
>> VFIOPCIDevice, also to hide low level VFIO_DEVICE_TYPE_PCI type check.
>>
>> Suggested-by: Cédric Le Goater <clg@redhat.com>
>> Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
>> ---
>>   include/hw/vfio/vfio-device.h |  1 +
>>   hw/vfio/container.c           |  4 ++--
>>   hw/vfio/device.c              | 10 +++++++++-
>>   hw/vfio/iommufd.c             |  4 ++--
>>   hw/vfio/listener.c            |  4 ++--
>>   5 files changed, 16 insertions(+), 7 deletions(-)
>>
>> diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h
>> index 6e4d5ccdac..00df40d997 100644
>> --- a/include/hw/vfio/vfio-device.h
>> +++ b/include/hw/vfio/vfio-device.h
>> @@ -157,6 +157,7 @@ bool vfio_device_attach_by_iommu_type(const
>char *iommu_type, char *name,
>>                                         Error **errp);
>>   void vfio_device_detach(VFIODevice *vbasedev);
>>   VFIODevice *vfio_get_vfio_device(Object *obj);
>> +struct VFIOPCIDevice *vfio_device_to_vfio_pci(VFIODevice *vbasedev);
>
>Please return the typedef (like in the implementation), not the struct.

That will break build. VFIOPCIDevice is defined in internal header hw/vfio/pci.h,
while include/hw/vfio/vfio-device.h is public header, I'm not sure if it's right way to include internal header in public header.

>
>A one line comment describing what this helper does would he helpful.

Will do.

Thanks
Zhenzhong

>
>Regards,
>
>Phil.
>
>> diff --git a/hw/vfio/device.c b/hw/vfio/device.c
>> index 52a1996dc4..a4f9c9216c 100644
>> --- a/hw/vfio/device.c
>> +++ b/hw/vfio/device.c
>
>
>> @@ -429,6 +429,14 @@ VFIODevice *vfio_get_vfio_device(Object *obj)
>>       }
>>   }
>>
>> +VFIOPCIDevice *vfio_device_to_vfio_pci(VFIODevice *vbasedev)
>> +{
>> +    if (vbasedev && vbasedev->type == VFIO_DEVICE_TYPE_PCI) {
>> +        return container_of(vbasedev, VFIOPCIDevice, vbasedev);
>> +    }
>> +    return NULL;
>> +}


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] vfio: Introduce helper vfio_device_to_vfio_pci()
  2025-07-31  8:49   ` Duan, Zhenzhong
@ 2025-07-31 11:24     ` Philippe Mathieu-Daudé
  2025-07-31 12:03       ` Cédric Le Goater
  0 siblings, 1 reply; 6+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-07-31 11:24 UTC (permalink / raw)
  To: Duan, Zhenzhong, qemu-devel@nongnu.org
  Cc: alex.williamson@redhat.com, clg@redhat.com, eric.auger@redhat.com,
	Peng, Chao P

On 31/7/25 10:49, Duan, Zhenzhong wrote:
> 
> 
>> -----Original Message-----
>> From: Philippe Mathieu-Daudé <philmd@linaro.org>
>> Subject: Re: [PATCH] vfio: Introduce helper vfio_device_to_vfio_pci()
>>
>> Hi,
>>
>> On 31/7/25 05:31, Zhenzhong Duan wrote:
>>> Introduce helper vfio_device_to_vfio_pci() to transform from VFIODevice to
>>> VFIOPCIDevice, also to hide low level VFIO_DEVICE_TYPE_PCI type check.
>>>
>>> Suggested-by: Cédric Le Goater <clg@redhat.com>
>>> Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
>>> ---
>>>    include/hw/vfio/vfio-device.h |  1 +
>>>    hw/vfio/container.c           |  4 ++--
>>>    hw/vfio/device.c              | 10 +++++++++-
>>>    hw/vfio/iommufd.c             |  4 ++--
>>>    hw/vfio/listener.c            |  4 ++--
>>>    5 files changed, 16 insertions(+), 7 deletions(-)
>>>
>>> diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h
>>> index 6e4d5ccdac..00df40d997 100644
>>> --- a/include/hw/vfio/vfio-device.h
>>> +++ b/include/hw/vfio/vfio-device.h
>>> @@ -157,6 +157,7 @@ bool vfio_device_attach_by_iommu_type(const
>> char *iommu_type, char *name,
>>>                                          Error **errp);
>>>    void vfio_device_detach(VFIODevice *vbasedev);
>>>    VFIODevice *vfio_get_vfio_device(Object *obj);
>>> +struct VFIOPCIDevice *vfio_device_to_vfio_pci(VFIODevice *vbasedev);
>>
>> Please return the typedef (like in the implementation), not the struct.
> 
> That will break build. VFIOPCIDevice is defined in internal header hw/vfio/pci.h,
> while include/hw/vfio/vfio-device.h is public header, I'm not sure if it's right way to include internal header in public header.

Moving the following line:

   OBJECT_DECLARE_SIMPLE_TYPE(VFIOPCIDevice, VFIO_PCI_BASE)

from hw/vfio/pci.h to include/hw/vfio/vfio-device.h should be enough.

> 
>>
>> A one line comment describing what this helper does would he helpful.
> 
> Will do.

Thanks!



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] vfio: Introduce helper vfio_device_to_vfio_pci()
  2025-07-31 11:24     ` Philippe Mathieu-Daudé
@ 2025-07-31 12:03       ` Cédric Le Goater
  0 siblings, 0 replies; 6+ messages in thread
From: Cédric Le Goater @ 2025-07-31 12:03 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, Duan, Zhenzhong,
	qemu-devel@nongnu.org
  Cc: alex.williamson@redhat.com, eric.auger@redhat.com, Peng, Chao P

On 7/31/25 13:24, Philippe Mathieu-Daudé wrote:
> On 31/7/25 10:49, Duan, Zhenzhong wrote:
>>
>>
>>> -----Original Message-----
>>> From: Philippe Mathieu-Daudé <philmd@linaro.org>
>>> Subject: Re: [PATCH] vfio: Introduce helper vfio_device_to_vfio_pci()
>>>
>>> Hi,
>>>
>>> On 31/7/25 05:31, Zhenzhong Duan wrote:
>>>> Introduce helper vfio_device_to_vfio_pci() to transform from VFIODevice to
>>>> VFIOPCIDevice, also to hide low level VFIO_DEVICE_TYPE_PCI type check.
>>>>
>>>> Suggested-by: Cédric Le Goater <clg@redhat.com>
>>>> Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
>>>> ---
>>>>    include/hw/vfio/vfio-device.h |  1 +
>>>>    hw/vfio/container.c           |  4 ++--
>>>>    hw/vfio/device.c              | 10 +++++++++-
>>>>    hw/vfio/iommufd.c             |  4 ++--
>>>>    hw/vfio/listener.c            |  4 ++--
>>>>    5 files changed, 16 insertions(+), 7 deletions(-)
>>>>
>>>> diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h
>>>> index 6e4d5ccdac..00df40d997 100644
>>>> --- a/include/hw/vfio/vfio-device.h
>>>> +++ b/include/hw/vfio/vfio-device.h
>>>> @@ -157,6 +157,7 @@ bool vfio_device_attach_by_iommu_type(const
>>> char *iommu_type, char *name,
>>>>                                          Error **errp);
>>>>    void vfio_device_detach(VFIODevice *vbasedev);
>>>>    VFIODevice *vfio_get_vfio_device(Object *obj);
>>>> +struct VFIOPCIDevice *vfio_device_to_vfio_pci(VFIODevice *vbasedev);
>>>
>>> Please return the typedef (like in the implementation), not the struct.
>>
>> That will break build. VFIOPCIDevice is defined in internal header hw/vfio/pci.h,
>> while include/hw/vfio/vfio-device.h is public header, I'm not sure if it's right way to include internal header in public header.
> 
> Moving the following line:
> 
>    OBJECT_DECLARE_SIMPLE_TYPE(VFIOPCIDevice, VFIO_PCI_BASE)
> 
> from hw/vfio/pci.h to include/hw/vfio/vfio-device.h should be enough.

I rather not. These are 2 differents subcomponents.

vfio_device_to_vfio_pci() could be moved.

Thanks,

C.



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-07-31 12:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-31  3:31 [PATCH] vfio: Introduce helper vfio_device_to_vfio_pci() Zhenzhong Duan
2025-07-31  8:01 ` Cédric Le Goater
2025-07-31  8:26 ` Philippe Mathieu-Daudé
2025-07-31  8:49   ` Duan, Zhenzhong
2025-07-31 11:24     ` Philippe Mathieu-Daudé
2025-07-31 12:03       ` Cédric Le Goater

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).