Skip to contentSkip to conversation

vfio: Introduce helper vfio_pci_from_vfio_device()

PATCH In review Zhenzhong Duan 4 participants 8 messages Aug 1, 2025 – Aug 4, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 vfio: Introduce helper vfio_pci_from_vfio_device()

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

---
v3: add one line comment to the helper
v2: move helper to hw/vfio/pci.[hc]
rename with vfio_pci_ prefix

 hw/vfio/pci.h       | 1 +
 hw/vfio/container.c | 4 ++--
 hw/vfio/device.c    | 2 +-
 hw/vfio/iommufd.c   | 4 ++--
 hw/vfio/listener.c  | 4 ++--
 hw/vfio/pci.c       | 9 +++++++++
 6 files changed, 17 insertions(+), 7 deletions(-)
hw/vfio/pci.h +1 -0
index 81465a8214..53842cb149 100644
@@ -219,6 +219,7 @@ void vfio_pci_write_config(PCIDevice *pdev,
 uint64_t vfio_vga_read(void *opaque, hwaddr addr, unsigned size);
 void vfio_vga_write(void *opaque, hwaddr addr, uint64_t data, unsigned size);
 
+VFIOPCIDevice *vfio_pci_from_vfio_device(VFIODevice *vbasedev);
 void vfio_sub_page_bar_update_mappings(VFIOPCIDevice *vdev);
 bool vfio_opt_rom_in_denylist(VFIOPCIDevice *vdev);
 bool vfio_config_quirk_setup(VFIOPCIDevice *vdev, Error **errp);
hw/vfio/container.c +2 -2
index 3e13feaa74..134ddccc52 100644
@@ -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_pci_from_vfio_device(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_pci_from_vfio_device(vbasedev_iter)) {
                 continue;
             }
             tmp = container_of(vbasedev_iter, VFIOPCIDevice, vbasedev);
hw/vfio/device.c +1 -1
index 52a1996dc4..08f12ac31f 100644
@@ -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_pci_from_vfio_device(vbasedev)) {
         return NULL;
     }
 
hw/vfio/iommufd.c +2 -2
index 48c590b6a9..8c27222f75 100644
@@ -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_pci_from_vfio_device(vbasedev_tmp) ||
+        !vbasedev_tmp->dev->realized) {
         return NULL;
     }
 
hw/vfio/listener.c +2 -2
index f498e23a93..903dfd8bf2 100644
@@ -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_pci_from_vfio_device(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_pci_from_vfio_device(vbasedev)) {
             continue;
         }
         pcidev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
hw/vfio/pci.c +9 -0
index 4fa692c1a3..85761544ba 100644
@@ -2824,6 +2824,15 @@ static int vfio_pci_load_config(VFIODevice *vbasedev, QEMUFile *f)
     return ret;
 }
 
+/* Transform from VFIODevice to VFIOPCIDevice. Return NULL if fails. */
+VFIOPCIDevice *vfio_pci_from_vfio_device(VFIODevice *vbasedev)
+{
+    if (vbasedev && vbasedev->type == VFIO_DEVICE_TYPE_PCI) {
+        return container_of(vbasedev, VFIOPCIDevice, vbasedev);
+    }
+    return NULL;
+}
+
 void vfio_sub_page_bar_update_mappings(VFIOPCIDevice *vdev)
 {
     PCIDevice *pdev = &vdev->pdev;
Signature · 1 line
2.47.1
Reviewed-by on v3 1/1
#1 lore raw
Quoted 10 lines · Zhenzhong Duan

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

---
v3: add one line comment to the helper
v2: move helper to hw/vfio/pci.[hc]
rename with vfio_pci_ prefix

Quoted 7 lines
  hw/vfio/pci.h       | 1 +
  hw/vfio/container.c | 4 ++--
  hw/vfio/device.c    | 2 +-
  hw/vfio/iommufd.c   | 4 ++--
  hw/vfio/listener.c  | 4 ++--
  hw/vfio/pci.c       | 9 +++++++++
  6 files changed, 17 insertions(+), 7 deletions(-)

Thanks,

C.

#2 lore raw

Zhenzhong,

Quoted 20 lines · Cédric Le Goater
Quoted 10 lines · Zhenzhong Duan

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

---
v3: add one line comment to the helper
v2: move helper to hw/vfio/pci.[hc]
     rename with vfio_pci_ prefix

Quoted 7 lines

  hw/vfio/pci.h       | 1 +
  hw/vfio/container.c | 4 ++--
  hw/vfio/device.c    | 2 +-
  hw/vfio/iommufd.c   | 4 ++--
  hw/vfio/listener.c  | 4 ++--
  hw/vfio/pci.c       | 9 +++++++++

  6 files changed, 17 insertions(+), 7 deletions(-)
Quoted 5 lines

Thanks,

C.

I have modified your patch with :

+/**

+  * vfio_pci_from_vfio_device: Transform from VFIODevice to
+  * VFIOPCIDevice
+  *
+  * This function checks if the given @vbasedev is a VFIO PCI device.
+  * If it is, it returns the containing VFIOPCIDevice.
+  *
+  * @vbasedev: The VFIODevice to transform
+  *
+  * Return: The VFIOPCIDevice on success, NULL on failure.
+  */

See https://github.com/legoater/qemu/tree/vfio-10.2.

I don't think it's really necessary, as these are internal APIs and none are documented, but Philippe seems keen on it. I guess he plans to volunteer to document the rest ;)

No need to resend a v4.

Thanks,

C.

#3 lore raw
Quoted 14 lines · Cédric Le Goater

I have modified your patch with :

(quoted diff) +11 -0
+/**
+  * vfio_pci_from_vfio_device: Transform from VFIODevice to
+  * VFIOPCIDevice
+  *
+  * This function checks if the given @vbasedev is a VFIO PCI device.
+  * If it is, it returns the containing VFIOPCIDevice.
+  *
+  * @vbasedev: The VFIODevice to transform
+  *
+  * Return: The VFIOPCIDevice on success, NULL on failure.
+  */
Quoted 5 lines

See https://github.com/legoater/qemu/tree/vfio-10.2.

I don't think it's really necessary, as these are internal APIs and none are documented, but Philippe seems keen on it. I guess he plans to volunteer to document the rest ;)

This is one of those cases where we have a rule in place that we apply on code review for newly added prototypes in header files, as an exercise in trying to at least not make an existing problem worse :-)

I think Philippe's intent was just to say "put that one line comment by the prototype in the .h file rather than by the implementation in the .c file", rather than to require a full-on doc-comment format comment, though I do appreciate having one.

thanks
-- PMM

#4 lore raw
Quoted 17 lines · Peter Maydell
Quoted 16 lines · Cédric Le Goater

I have modified your patch with :

(quoted diff) +11 -0
+/**
+  * vfio_pci_from_vfio_device: Transform from VFIODevice to
+  * VFIOPCIDevice
+  *
+  * This function checks if the given @vbasedev is a VFIO PCI device.
+  * If it is, it returns the containing VFIOPCIDevice.
+  *
+  * @vbasedev: The VFIODevice to transform
+  *
+  * Return: The VFIOPCIDevice on success, NULL on failure.
+  */

See https://github.com/legoater/qemu/tree/vfio-10.2.

Quoted 8 lines
Quoted 3 lines

I don't think it's really necessary, as these are internal APIs and none are documented, but Philippe seems keen on it. I guess he plans to volunteer to document the rest ;)

This is one of those cases where we have a rule in place that we apply on code review for newly added prototypes in header files, as an exercise in trying to at least not make an existing problem worse :-)

Sure. May be, this is something we could add to checkpatch.pl then ? This would reduce the resends for simple things.

Quoted 5 lines

I think Philippe's intent was just to say "put that one line comment by the prototype in the .h file rather than by the implementation in the .c file", rather than to require a full-on doc-comment format comment, though I do appreciate having one.

I've tried to reorganize the VFIO code to define some software subcomponents and layers in previous versions. This to handle recent (rather significant) changes and also because vfio-pci was initially a device and is gradually evolving into a kind of library.

I'd also like to address documentation, so my comment was self-interested :) as it's a pretty broad topic; any help is welcome !

I should find a way to generate some of it to start with. At least.

Thanks,

C.

#5 lore raw
In reply to Cédric Le Goater
Quoted 51 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v3] vfio: Introduce helper vfio_pci_from_vfio_device()

Zhenzhong,

Quoted 2 lines · Cédric Le Goater
Quoted 1 line · Zhenzhong Duan

Introduce helper vfio_pci_from_vfio_device() to transform from

VFIODevice

Quoted 1 line
Quoted 1 line

to VFIOPCIDevice, also to hide low level VFIO_DEVICE_TYPE_PCI type

check.

Quoted 17 lines
Quoted 8 lines

---
v3: add one line comment to the helper
v2: move helper to hw/vfio/pci.[hc]
     rename with vfio_pci_ prefix

Quoted 7 lines

  hw/vfio/pci.h       | 1 +
  hw/vfio/container.c | 4 ++--
  hw/vfio/device.c    | 2 +-
  hw/vfio/iommufd.c   | 4 ++--
  hw/vfio/listener.c  | 4 ++--
  hw/vfio/pci.c       | 9 +++++++++

  6 files changed, 17 insertions(+), 7 deletions(-)
Quoted 5 lines

Thanks,

C.

I have modified your patch with :

(quoted diff) +11 -0
+/**
+  * vfio_pci_from_vfio_device: Transform from VFIODevice to
+  * VFIOPCIDevice
+  *
+  * This function checks if the given @vbasedev is a VFIO PCI device.
+  * If it is, it returns the containing VFIOPCIDevice.
+  *
+  * @vbasedev: The VFIODevice to transform
+  *
+  * Return: The VFIOPCIDevice on success, NULL on failure.
+  */
Quoted 1 line

See https://github.com/legoater/qemu/tree/vfio-10.2.

Thanks Cédric, looks better.

Quoted 6 lines

I don't think it's really necessary, as these are internal APIs and none are documented, but Philippe seems keen on it. I guess he plans to volunteer to document the rest ;)

No need to resend a v4.

Got it.

BRs,
Zhenzhong

Reviewed-by on v3 1/1
#6 lore raw
In reply to Zhenzhong Duan
Quoted 23 lines · Zhenzhong Duan

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

---
v3: add one line comment to the helper
v2: move helper to hw/vfio/pci.[hc]
rename with vfio_pci_ prefix

  hw/vfio/pci.h       | 1 +
  hw/vfio/container.c | 4 ++--
  hw/vfio/device.c    | 2 +-
  hw/vfio/iommufd.c   | 4 ++--
  hw/vfio/listener.c  | 4 ++--
  hw/vfio/pci.c       | 9 +++++++++
  6 files changed, 17 insertions(+), 7 deletions(-)
hw/vfio/pci.h +0 -0
index 81465a8214..53842cb149 100644

hw/vfio/pci.h void vfio_pci_write_config(PCIDevice *pdev, · line 221

Quoted 3 lines
hw/vfio/pci.h +0 -0
@@ -219,6 +219,7 @@ void vfio_pci_write_config(PCIDevice *pdev,
  uint64_t vfio_vga_read(void *opaque, hwaddr addr, unsigned size);
  void vfio_vga_write(void *opaque, hwaddr addr, uint64_t data, unsigned size);
 

[*]

hw/vfio/pci.h void vfio_pci_write_config(PCIDevice *pdev, · line 225

Quoted 4 lines
hw/vfio/pci.h +1 -0
+VFIOPCIDevice *vfio_pci_from_vfio_device(VFIODevice *vbasedev);
  void vfio_sub_page_bar_update_mappings(VFIOPCIDevice *vdev);
  bool vfio_opt_rom_in_denylist(VFIOPCIDevice *vdev);
  bool vfio_config_quirk_setup(VFIOPCIDevice *vdev, Error **errp);

Quoting https://lore.kernel.org/qemu-devel/[email protected]/:

Generally APIs to the rest of QEMU should be documented in the headers. Comments on individual functions or internal details are fine to live in the C files.

hw/vfio/pci.c static int vfio_pci_load_config(VFIODevice *vbasedev, QEMUFile *f) · line 2827

Quoted 9 lines
hw/vfio/pci.c +1 -0
index 4fa692c1a3..85761544ba 100644
@@ -2824,6 +2824,15 @@ static int vfio_pci_load_config(VFIODevice *vbasedev, QEMUFile *f)
      return ret;
  }
 
+/* Transform from VFIODevice to VFIOPCIDevice. Return NULL if fails. */

So this comment preferably goes in [*]. Otherwise,

Quoted 7 lines
hw/vfio/pci.c +7 -0
+VFIOPCIDevice *vfio_pci_from_vfio_device(VFIODevice *vbasedev)
+{
+    if (vbasedev && vbasedev->type == VFIO_DEVICE_TYPE_PCI) {
+        return container_of(vbasedev, VFIOPCIDevice, vbasedev);
+    }
+    return NULL;
+}
#7 lore raw
Quoted 43 lines

-----Original Message-----
From: Philippe Mathieu-Daudé <[email protected]>
Subject: Re: [PATCH v3] vfio: Introduce helper vfio_pci_from_vfio_device()

Quoted 2 lines · Zhenzhong Duan

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

check.

Quoted 21 lines

---
v3: add one line comment to the helper
v2: move helper to hw/vfio/pci.[hc]
rename with vfio_pci_ prefix

  hw/vfio/pci.h       | 1 +
  hw/vfio/container.c | 4 ++--
  hw/vfio/device.c    | 2 +-
  hw/vfio/iommufd.c   | 4 ++--
  hw/vfio/listener.c  | 4 ++--
  hw/vfio/pci.c       | 9 +++++++++
  6 files changed, 17 insertions(+), 7 deletions(-)
hw/vfio/pci.h +0 -0
index 81465a8214..53842cb149 100644

hw/vfio/pci.h void vfio_pci_write_config(PCIDevice *pdev, · line 220

Quoted 2 lines
hw/vfio/pci.h +0 -0
@@ -219,6 +219,7 @@ void vfio_pci_write_config(PCIDevice *pdev,
  uint64_t vfio_vga_read(void *opaque, hwaddr addr, unsigned size);
  void vfio_vga_write(void *opaque, hwaddr addr, uint64_t data, unsigned

size);

Quoted 1 line

[*]

Quoted 4 lines

+VFIOPCIDevice *vfio_pci_from_vfio_device(VFIODevice *vbasedev);
void vfio_sub_page_bar_update_mappings(VFIOPCIDevice *vdev); bool vfio_opt_rom_in_denylist(VFIOPCIDevice *vdev); bool vfio_config_quirk_setup(VFIOPCIDevice *vdev, Error **errp);

Quoted 6 lines

Quoting
https://lore.kernel.org/qemu-devel/[email protected]/:

Generally APIs to the rest of QEMU should be documented in the headers. Comments on individual functions or internal details are fine to live in the C files.

I see, thanks

Quoted 7 lines

hw/vfio/pci.c static int vfio_pci_load_config(VFIODevice

Quoted 6 lines
hw/vfio/pci.c +0 -0
index 4fa692c1a3..85761544ba 100644
@@ -2824,6 +2824,15 @@ static int vfio_pci_load_config(VFIODevice

*vbasedev, QEMUFile *f)

Quoted 6 lines

hw/vfio/pci.c static int vfio_pci_load_config(VFIODevice *vbasedev, QEMUFile *f) · line 2827

Quoted 4 lines
hw/vfio/pci.c +1 -0
      return ret;
  }
 
+/* Transform from VFIODevice to VFIOPCIDevice. Return NULL if fails. */

So this comment preferably goes in [*]. Otherwise,

FYI, Cedric helped to make the suggested change, so I'll not send v4.
https://github.com/legoater/qemu/commit/d0ec15af9a5c0a8a9c1a7a71903eb4e00cae71b1

BRs,
Zhenzhong

Quoted 10 lines
Quoted 7 lines
hw/vfio/pci.c +7 -0
+VFIOPCIDevice *vfio_pci_from_vfio_device(VFIODevice *vbasedev)
+{
+    if (vbasedev && vbasedev->type == VFIO_DEVICE_TYPE_PCI) {
+        return container_of(vbasedev, VFIOPCIDevice, vbasedev);
+    }
+    return NULL;
+}