diff mbox series

[v2,15/21] hw/vfio/pci: Check CONFIG_IOMMUFD at runtime using iommufd_builtin()

Message ID 20250308230917.18907-16-philmd@linaro.org (mailing list archive)
State New
Headers show
Series hw/vfio: Build various objects once | expand

Commit Message

Philippe Mathieu-Daudé March 8, 2025, 11:09 p.m. UTC
Convert the compile time check on the CONFIG_IOMMUFD definition
by a runtime one by calling iommufd_builtin().

Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 hw/vfio/pci.c | 38 ++++++++++++++++++--------------------
 1 file changed, 18 insertions(+), 20 deletions(-)

Comments

Duan, Zhenzhong March 10, 2025, 4:11 a.m. UTC | #1
Hi Philippe,

>-----Original Message-----
>From: Philippe Mathieu-Daudé <philmd@linaro.org>
>Subject: [PATCH v2 15/21] hw/vfio/pci: Check CONFIG_IOMMUFD at runtime
>using iommufd_builtin()
>
>Convert the compile time check on the CONFIG_IOMMUFD definition
>by a runtime one by calling iommufd_builtin().
>
>Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
>Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
>Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
>---
> hw/vfio/pci.c | 38 ++++++++++++++++++--------------------
> 1 file changed, 18 insertions(+), 20 deletions(-)
>
>diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
>index 9872884ff8a..e83252766d1 100644
>--- a/hw/vfio/pci.c
>+++ b/hw/vfio/pci.c
>@@ -19,7 +19,6 @@
>  */
>
> #include "qemu/osdep.h"
>-#include CONFIG_DEVICES /* CONFIG_IOMMUFD */
> #include <linux/vfio.h>
> #include <sys/ioctl.h>
>
>@@ -2973,11 +2972,10 @@ static void vfio_realize(PCIDevice *pdev, Error
>**errp)
>         if (!(~vdev->host.domain || ~vdev->host.bus ||
>               ~vdev->host.slot || ~vdev->host.function)) {
>             error_setg(errp, "No provided host device");
>-            error_append_hint(errp, "Use -device vfio-pci,host=DDDD:BB:DD.F "
>-#ifdef CONFIG_IOMMUFD
>-                              "or -device vfio-pci,fd=DEVICE_FD "
>-#endif
>-                              "or -device vfio-pci,sysfsdev=PATH_TO_DEVICE\n");
>+            error_append_hint(errp, "Use -device vfio-pci,host=DDDD:BB:DD.F %s"
>+                              "or -device vfio-pci,sysfsdev=PATH_TO_DEVICE\n",
>+                              iommufd_builtin()
>+                              ? "or -device vfio-pci,fd=DEVICE_FD " : "");
>             return;
>         }
>         vbasedev->sysfsdev =
>@@ -3412,19 +3410,18 @@ static const Property vfio_pci_dev_properties[] = {
>                                    qdev_prop_nv_gpudirect_clique, uint8_t),
>     DEFINE_PROP_OFF_AUTO_PCIBAR("x-msix-relocation", VFIOPCIDevice,
>msix_relo,
>                                 OFF_AUTO_PCIBAR_OFF),
>-#ifdef CONFIG_IOMMUFD
>-    DEFINE_PROP_LINK("iommufd", VFIOPCIDevice, vbasedev.iommufd,
>-                     TYPE_IOMMUFD_BACKEND, IOMMUFDBackend *),
>-#endif
>     DEFINE_PROP_BOOL("skip-vsc-check", VFIOPCIDevice, skip_vsc_check, true),
> };
>
>-#ifdef CONFIG_IOMMUFD
>+static const Property vfio_pci_dev_iommufd_properties[] = {
>+    DEFINE_PROP_LINK("iommufd", VFIOPCIDevice, vbasedev.iommufd,
>+                     TYPE_IOMMUFD_BACKEND, IOMMUFDBackend *),
>+};
>+
> static void vfio_pci_set_fd(Object *obj, const char *str, Error **errp)
> {
>     vfio_device_set_fd(&VFIO_PCI(obj)->vbasedev, str, errp);
> }
>-#endif
>
> static void vfio_pci_dev_class_init(ObjectClass *klass, void *data)
> {
>@@ -3433,9 +3430,10 @@ static void vfio_pci_dev_class_init(ObjectClass *klass,
>void *data)
>
>     device_class_set_legacy_reset(dc, vfio_pci_reset);
>     device_class_set_props(dc, vfio_pci_dev_properties);
>-#ifdef CONFIG_IOMMUFD
>-    object_class_property_add_str(klass, "fd", NULL, vfio_pci_set_fd);
>-#endif
>+    if (iommufd_builtin()) {
>+        device_class_set_props(dc, vfio_pci_dev_iommufd_properties);

device_class_set_props() is called twice. Won't it break qdev_print_props() and qdev_prop_walk()?

Thanks
Zhenzhong
Philippe Mathieu-Daudé March 10, 2025, 1:37 p.m. UTC | #2
On 10/3/25 05:11, Duan, Zhenzhong wrote:
> Hi Philippe,
> 
>> -----Original Message-----
>> From: Philippe Mathieu-Daudé <philmd@linaro.org>
>> Subject: [PATCH v2 15/21] hw/vfio/pci: Check CONFIG_IOMMUFD at runtime
>> using iommufd_builtin()
>>
>> Convert the compile time check on the CONFIG_IOMMUFD definition
>> by a runtime one by calling iommufd_builtin().
>>
>> Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
>> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
>> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
>> ---
>> hw/vfio/pci.c | 38 ++++++++++++++++++--------------------
>> 1 file changed, 18 insertions(+), 20 deletions(-)


>> static void vfio_pci_dev_class_init(ObjectClass *klass, void *data)
>> {
>> @@ -3433,9 +3430,10 @@ static void vfio_pci_dev_class_init(ObjectClass *klass,
>> void *data)
>>
>>      device_class_set_legacy_reset(dc, vfio_pci_reset);
>>      device_class_set_props(dc, vfio_pci_dev_properties);
>> -#ifdef CONFIG_IOMMUFD
>> -    object_class_property_add_str(klass, "fd", NULL, vfio_pci_set_fd);
>> -#endif
>> +    if (iommufd_builtin()) {
>> +        device_class_set_props(dc, vfio_pci_dev_iommufd_properties);
> 
> device_class_set_props() is called twice. Won't it break qdev_print_props() and qdev_prop_walk()?

device_class_set_props() is misnamed, as it doesn't SET an array of
properties, but ADD them (or 'register') to the class.

See device_class_set_props_n() in hw/core/qdev-properties.c.

I'll see to rename the QDev methods for clarity.

Regards,

Phil.
diff mbox series

Patch

diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
index 9872884ff8a..e83252766d1 100644
--- a/hw/vfio/pci.c
+++ b/hw/vfio/pci.c
@@ -19,7 +19,6 @@ 
  */
 
 #include "qemu/osdep.h"
-#include CONFIG_DEVICES /* CONFIG_IOMMUFD */
 #include <linux/vfio.h>
 #include <sys/ioctl.h>
 
@@ -2973,11 +2972,10 @@  static void vfio_realize(PCIDevice *pdev, Error **errp)
         if (!(~vdev->host.domain || ~vdev->host.bus ||
               ~vdev->host.slot || ~vdev->host.function)) {
             error_setg(errp, "No provided host device");
-            error_append_hint(errp, "Use -device vfio-pci,host=DDDD:BB:DD.F "
-#ifdef CONFIG_IOMMUFD
-                              "or -device vfio-pci,fd=DEVICE_FD "
-#endif
-                              "or -device vfio-pci,sysfsdev=PATH_TO_DEVICE\n");
+            error_append_hint(errp, "Use -device vfio-pci,host=DDDD:BB:DD.F %s"
+                              "or -device vfio-pci,sysfsdev=PATH_TO_DEVICE\n",
+                              iommufd_builtin()
+                              ? "or -device vfio-pci,fd=DEVICE_FD " : "");
             return;
         }
         vbasedev->sysfsdev =
@@ -3412,19 +3410,18 @@  static const Property vfio_pci_dev_properties[] = {
                                    qdev_prop_nv_gpudirect_clique, uint8_t),
     DEFINE_PROP_OFF_AUTO_PCIBAR("x-msix-relocation", VFIOPCIDevice, msix_relo,
                                 OFF_AUTO_PCIBAR_OFF),
-#ifdef CONFIG_IOMMUFD
-    DEFINE_PROP_LINK("iommufd", VFIOPCIDevice, vbasedev.iommufd,
-                     TYPE_IOMMUFD_BACKEND, IOMMUFDBackend *),
-#endif
     DEFINE_PROP_BOOL("skip-vsc-check", VFIOPCIDevice, skip_vsc_check, true),
 };
 
-#ifdef CONFIG_IOMMUFD
+static const Property vfio_pci_dev_iommufd_properties[] = {
+    DEFINE_PROP_LINK("iommufd", VFIOPCIDevice, vbasedev.iommufd,
+                     TYPE_IOMMUFD_BACKEND, IOMMUFDBackend *),
+};
+
 static void vfio_pci_set_fd(Object *obj, const char *str, Error **errp)
 {
     vfio_device_set_fd(&VFIO_PCI(obj)->vbasedev, str, errp);
 }
-#endif
 
 static void vfio_pci_dev_class_init(ObjectClass *klass, void *data)
 {
@@ -3433,9 +3430,10 @@  static void vfio_pci_dev_class_init(ObjectClass *klass, void *data)
 
     device_class_set_legacy_reset(dc, vfio_pci_reset);
     device_class_set_props(dc, vfio_pci_dev_properties);
-#ifdef CONFIG_IOMMUFD
-    object_class_property_add_str(klass, "fd", NULL, vfio_pci_set_fd);
-#endif
+    if (iommufd_builtin()) {
+        device_class_set_props(dc, vfio_pci_dev_iommufd_properties);
+        object_class_property_add_str(klass, "fd", NULL, vfio_pci_set_fd);
+    }
     dc->desc = "VFIO-based PCI device assignment";
     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
     pdc->realize = vfio_realize;
@@ -3540,11 +3538,11 @@  static void vfio_pci_dev_class_init(ObjectClass *klass, void *data)
                                           "vf-token",
                                           "Specify UUID VF token. Required for VF when PF is owned "
                                           "by another VFIO driver");
-#ifdef CONFIG_IOMMUFD
-    object_class_property_set_description(klass, /* 9.0 */
-                                          "iommufd",
-                                          "Set host IOMMUFD backend device");
-#endif
+    if (iommufd_builtin()) {
+        object_class_property_set_description(klass, /* 9.0 */
+                                              "iommufd",
+                                              "Set host IOMMUFD backend device");
+    }
     object_class_property_set_description(klass, /* 9.1 */
                                           "x-device-dirty-page-tracking",
                                           "Disable device dirty page tracking and use "