* [PATCH v2 0/5] VFIO device init cleanup
@ 2023-11-16 7:59 Zhenzhong Duan
2023-11-16 7:59 ` [PATCH v2 1/5] vfio/pci: Move VFIODevice initializations in vfio_instance_init Zhenzhong Duan
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: Zhenzhong Duan @ 2023-11-16 7:59 UTC (permalink / raw)
To: qemu-devel
Cc: alex.williamson, clg, jgg, nicolinc, joao.m.martins, eric.auger,
peterx, jasowang, kevin.tian, yi.l.liu, yi.y.sun, chao.p.peng,
Zhenzhong Duan
Hi,
This is a cleanup based on Cédric's suggestion at
https://lists.gnu.org/archive/html/qemu-devel/2023-11/msg02722.html
VFIO device initializations are all moved from realize to instance_init.
Based on https://github.com/legoater/qemu/commits/vfio-8.2
Thanks
Zhenzhong
Changelog:
v2: Append a new patch to introduce vfio_device_init (Cédric)
Add RB
Zhenzhong Duan (5):
vfio/pci: Move VFIODevice initializations in vfio_instance_init
vfio/platform: Move VFIODevice initializations in
vfio_platform_instance_init
vfio/ap: Move VFIODevice initializations in vfio_ap_instance_init
vfio/ccw: Move VFIODevice initializations in vfio_ccw_instance_init
vfio: Introduce a helper function to initialize VFIODevice
include/hw/vfio/vfio-common.h | 2 ++
hw/vfio/ap.c | 22 +++++++++-------------
hw/vfio/ccw.c | 26 +++++++++++---------------
hw/vfio/helpers.c | 11 +++++++++++
hw/vfio/pci.c | 8 ++++----
hw/vfio/platform.c | 8 +++-----
6 files changed, 40 insertions(+), 37 deletions(-)
--
2.34.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v2 1/5] vfio/pci: Move VFIODevice initializations in vfio_instance_init
2023-11-16 7:59 [PATCH v2 0/5] VFIO device init cleanup Zhenzhong Duan
@ 2023-11-16 7:59 ` Zhenzhong Duan
2023-11-16 7:59 ` [PATCH v2 2/5] vfio/platform: Move VFIODevice initializations in vfio_platform_instance_init Zhenzhong Duan
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Zhenzhong Duan @ 2023-11-16 7:59 UTC (permalink / raw)
To: qemu-devel
Cc: alex.williamson, clg, jgg, nicolinc, joao.m.martins, eric.auger,
peterx, jasowang, kevin.tian, yi.l.liu, yi.y.sun, chao.p.peng,
Zhenzhong Duan, Philippe Mathieu-Daudé
Some of the VFIODevice initializations is in vfio_realize,
move all of them in vfio_instance_init.
No functional change intended.
Suggested-by: Cédric Le Goater <clg@redhat.com>
Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
hw/vfio/pci.c | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
index b23b492cce..5a2b7a2d6b 100644
--- a/hw/vfio/pci.c
+++ b/hw/vfio/pci.c
@@ -2969,9 +2969,6 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
if (vfio_device_get_name(vbasedev, errp)) {
return;
}
- vbasedev->ops = &vfio_pci_ops;
- vbasedev->type = VFIO_DEVICE_TYPE_PCI;
- vbasedev->dev = DEVICE(vdev);
/*
* Mediated devices *might* operate compatibly with discarding of RAM, but
@@ -3320,6 +3317,7 @@ static void vfio_instance_init(Object *obj)
{
PCIDevice *pci_dev = PCI_DEVICE(obj);
VFIOPCIDevice *vdev = VFIO_PCI(obj);
+ VFIODevice *vbasedev = &vdev->vbasedev;
device_add_bootindex_property(obj, &vdev->bootindex,
"bootindex", NULL,
@@ -3328,7 +3326,11 @@ static void vfio_instance_init(Object *obj)
vdev->host.bus = ~0U;
vdev->host.slot = ~0U;
vdev->host.function = ~0U;
- vdev->vbasedev.fd = -1;
+
+ vbasedev->type = VFIO_DEVICE_TYPE_PCI;
+ vbasedev->ops = &vfio_pci_ops;
+ vbasedev->dev = DEVICE(vdev);
+ vbasedev->fd = -1;
vdev->nv_gpudirect_clique = 0xFF;
--
2.34.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v2 2/5] vfio/platform: Move VFIODevice initializations in vfio_platform_instance_init
2023-11-16 7:59 [PATCH v2 0/5] VFIO device init cleanup Zhenzhong Duan
2023-11-16 7:59 ` [PATCH v2 1/5] vfio/pci: Move VFIODevice initializations in vfio_instance_init Zhenzhong Duan
@ 2023-11-16 7:59 ` Zhenzhong Duan
2023-11-16 7:59 ` [PATCH v2 3/5] vfio/ap: Move VFIODevice initializations in vfio_ap_instance_init Zhenzhong Duan
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Zhenzhong Duan @ 2023-11-16 7:59 UTC (permalink / raw)
To: qemu-devel
Cc: alex.williamson, clg, jgg, nicolinc, joao.m.martins, eric.auger,
peterx, jasowang, kevin.tian, yi.l.liu, yi.y.sun, chao.p.peng,
Zhenzhong Duan, Philippe Mathieu-Daudé
Some of the VFIODevice initializations is in vfio_platform_realize,
move all of them in vfio_platform_instance_init.
No functional change intended.
Suggested-by: Cédric Le Goater <clg@redhat.com>
Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
hw/vfio/platform.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/hw/vfio/platform.c b/hw/vfio/platform.c
index a97d9c6234..506eb8193f 100644
--- a/hw/vfio/platform.c
+++ b/hw/vfio/platform.c
@@ -581,10 +581,6 @@ static void vfio_platform_realize(DeviceState *dev, Error **errp)
VFIODevice *vbasedev = &vdev->vbasedev;
int i, ret;
- vbasedev->type = VFIO_DEVICE_TYPE_PLATFORM;
- vbasedev->dev = dev;
- vbasedev->ops = &vfio_platform_ops;
-
qemu_mutex_init(&vdev->intp_mutex);
trace_vfio_platform_realize(vbasedev->sysfsdev ?
@@ -659,8 +655,12 @@ static Property vfio_platform_dev_properties[] = {
static void vfio_platform_instance_init(Object *obj)
{
VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(obj);
+ VFIODevice *vbasedev = &vdev->vbasedev;
- vdev->vbasedev.fd = -1;
+ vbasedev->type = VFIO_DEVICE_TYPE_PLATFORM;
+ vbasedev->ops = &vfio_platform_ops;
+ vbasedev->dev = DEVICE(vdev);
+ vbasedev->fd = -1;
}
#ifdef CONFIG_IOMMUFD
--
2.34.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v2 3/5] vfio/ap: Move VFIODevice initializations in vfio_ap_instance_init
2023-11-16 7:59 [PATCH v2 0/5] VFIO device init cleanup Zhenzhong Duan
2023-11-16 7:59 ` [PATCH v2 1/5] vfio/pci: Move VFIODevice initializations in vfio_instance_init Zhenzhong Duan
2023-11-16 7:59 ` [PATCH v2 2/5] vfio/platform: Move VFIODevice initializations in vfio_platform_instance_init Zhenzhong Duan
@ 2023-11-16 7:59 ` Zhenzhong Duan
2023-11-16 7:59 ` [PATCH v2 4/5] vfio/ccw: Move VFIODevice initializations in vfio_ccw_instance_init Zhenzhong Duan
2023-11-16 7:59 ` [PATCH v2 5/5] vfio: Introduce a helper function to initialize VFIODevice Zhenzhong Duan
4 siblings, 0 replies; 6+ messages in thread
From: Zhenzhong Duan @ 2023-11-16 7:59 UTC (permalink / raw)
To: qemu-devel
Cc: alex.williamson, clg, jgg, nicolinc, joao.m.martins, eric.auger,
peterx, jasowang, kevin.tian, yi.l.liu, yi.y.sun, chao.p.peng,
Zhenzhong Duan, Philippe Mathieu-Daudé, Eric Farman,
Tony Krowiak, Halil Pasic, Jason Herne, Thomas Huth,
open list:vfio-ap
Some of the VFIODevice initializations is in vfio_ap_realize,
move all of them in vfio_ap_instance_init.
No functional change intended.
Suggested-by: Cédric Le Goater <clg@redhat.com>
Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Eric Farman <farman@linux.ibm.com>
---
hw/vfio/ap.c | 26 +++++++++++++-------------
1 file changed, 13 insertions(+), 13 deletions(-)
diff --git a/hw/vfio/ap.c b/hw/vfio/ap.c
index b21f92291e..31ea9644c5 100644
--- a/hw/vfio/ap.c
+++ b/hw/vfio/ap.c
@@ -164,18 +164,6 @@ static void vfio_ap_realize(DeviceState *dev, Error **errp)
return;
}
- vbasedev->ops = &vfio_ap_ops;
- vbasedev->type = VFIO_DEVICE_TYPE_AP;
- vbasedev->dev = dev;
-
- /*
- * vfio-ap devices operate in a way compatible with discarding of
- * memory in RAM blocks, as no pages are pinned in the host.
- * This needs to be set before vfio_get_device() for vfio common to
- * handle ram_block_discard_disable().
- */
- vapdev->vdev.ram_block_discard_allowed = true;
-
ret = vfio_attach_device(vbasedev->name, vbasedev,
&address_space_memory, errp);
if (ret) {
@@ -236,8 +224,20 @@ static const VMStateDescription vfio_ap_vmstate = {
static void vfio_ap_instance_init(Object *obj)
{
VFIOAPDevice *vapdev = VFIO_AP_DEVICE(obj);
+ VFIODevice *vbasedev = &vapdev->vdev;
- vapdev->vdev.fd = -1;
+ vbasedev->type = VFIO_DEVICE_TYPE_AP;
+ vbasedev->ops = &vfio_ap_ops;
+ vbasedev->dev = DEVICE(vapdev);
+ vbasedev->fd = -1;
+
+ /*
+ * vfio-ap devices operate in a way compatible with discarding of
+ * memory in RAM blocks, as no pages are pinned in the host.
+ * This needs to be set before vfio_get_device() for vfio common to
+ * handle ram_block_discard_disable().
+ */
+ vbasedev->ram_block_discard_allowed = true;
}
#ifdef CONFIG_IOMMUFD
--
2.34.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v2 4/5] vfio/ccw: Move VFIODevice initializations in vfio_ccw_instance_init
2023-11-16 7:59 [PATCH v2 0/5] VFIO device init cleanup Zhenzhong Duan
` (2 preceding siblings ...)
2023-11-16 7:59 ` [PATCH v2 3/5] vfio/ap: Move VFIODevice initializations in vfio_ap_instance_init Zhenzhong Duan
@ 2023-11-16 7:59 ` Zhenzhong Duan
2023-11-16 7:59 ` [PATCH v2 5/5] vfio: Introduce a helper function to initialize VFIODevice Zhenzhong Duan
4 siblings, 0 replies; 6+ messages in thread
From: Zhenzhong Duan @ 2023-11-16 7:59 UTC (permalink / raw)
To: qemu-devel
Cc: alex.williamson, clg, jgg, nicolinc, joao.m.martins, eric.auger,
peterx, jasowang, kevin.tian, yi.l.liu, yi.y.sun, chao.p.peng,
Zhenzhong Duan, Philippe Mathieu-Daudé, Eric Farman,
Thomas Huth, Matthew Rosato, open list:S390 general arch...
Some of the VFIODevice initializations is in vfio_ccw_realize,
move all of them in vfio_ccw_instance_init.
No functional change intended.
Suggested-by: Cédric Le Goater <clg@redhat.com>
Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Eric Farman <farman@linux.ibm.com>
---
hw/vfio/ccw.c | 30 +++++++++++++++---------------
1 file changed, 15 insertions(+), 15 deletions(-)
diff --git a/hw/vfio/ccw.c b/hw/vfio/ccw.c
index b116b10fe7..8de2fd809b 100644
--- a/hw/vfio/ccw.c
+++ b/hw/vfio/ccw.c
@@ -594,20 +594,6 @@ static void vfio_ccw_realize(DeviceState *dev, Error **errp)
return;
}
- vbasedev->ops = &vfio_ccw_ops;
- vbasedev->type = VFIO_DEVICE_TYPE_CCW;
- vbasedev->dev = dev;
-
- /*
- * All vfio-ccw devices are believed to operate in a way compatible with
- * discarding of memory in RAM blocks, ie. pages pinned in the host are
- * in the current working set of the guest driver and therefore never
- * overlap e.g., with pages available to the guest balloon driver. This
- * needs to be set before vfio_get_device() for vfio common to handle
- * ram_block_discard_disable().
- */
- vbasedev->ram_block_discard_allowed = true;
-
ret = vfio_attach_device(cdev->mdevid, vbasedev,
&address_space_memory, errp);
if (ret) {
@@ -695,8 +681,22 @@ static const VMStateDescription vfio_ccw_vmstate = {
static void vfio_ccw_instance_init(Object *obj)
{
VFIOCCWDevice *vcdev = VFIO_CCW(obj);
+ VFIODevice *vbasedev = &vcdev->vdev;
+
+ vbasedev->type = VFIO_DEVICE_TYPE_CCW;
+ vbasedev->ops = &vfio_ccw_ops;
+ vbasedev->dev = DEVICE(vcdev);
+ vbasedev->fd = -1;
- vcdev->vdev.fd = -1;
+ /*
+ * All vfio-ccw devices are believed to operate in a way compatible with
+ * discarding of memory in RAM blocks, ie. pages pinned in the host are
+ * in the current working set of the guest driver and therefore never
+ * overlap e.g., with pages available to the guest balloon driver. This
+ * needs to be set before vfio_get_device() for vfio common to handle
+ * ram_block_discard_disable().
+ */
+ vbasedev->ram_block_discard_allowed = true;
}
#ifdef CONFIG_IOMMUFD
--
2.34.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v2 5/5] vfio: Introduce a helper function to initialize VFIODevice
2023-11-16 7:59 [PATCH v2 0/5] VFIO device init cleanup Zhenzhong Duan
` (3 preceding siblings ...)
2023-11-16 7:59 ` [PATCH v2 4/5] vfio/ccw: Move VFIODevice initializations in vfio_ccw_instance_init Zhenzhong Duan
@ 2023-11-16 7:59 ` Zhenzhong Duan
4 siblings, 0 replies; 6+ messages in thread
From: Zhenzhong Duan @ 2023-11-16 7:59 UTC (permalink / raw)
To: qemu-devel
Cc: alex.williamson, clg, jgg, nicolinc, joao.m.martins, eric.auger,
peterx, jasowang, kevin.tian, yi.l.liu, yi.y.sun, chao.p.peng,
Zhenzhong Duan, Thomas Huth, Tony Krowiak, Halil Pasic,
Jason Herne, Eric Farman, Matthew Rosato,
open list:S390 general arch...
Introduce a helper function to replace the common code to initialize
VFIODevice in pci, platform, ap and ccw VFIO device.
No functional change intended.
Suggested-by: Cédric Le Goater <clg@redhat.com>
Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
---
include/hw/vfio/vfio-common.h | 2 ++
hw/vfio/ap.c | 8 ++------
hw/vfio/ccw.c | 8 ++------
hw/vfio/helpers.c | 11 +++++++++++
hw/vfio/pci.c | 6 ++----
hw/vfio/platform.c | 6 ++----
6 files changed, 21 insertions(+), 20 deletions(-)
diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h
index 7954531d05..fa430cb288 100644
--- a/include/hw/vfio/vfio-common.h
+++ b/include/hw/vfio/vfio-common.h
@@ -256,4 +256,6 @@ int vfio_get_dirty_bitmap(const VFIOContainerBase *bcontainer, uint64_t iova,
int vfio_device_get_name(VFIODevice *vbasedev, Error **errp);
void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp);
+void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops,
+ DeviceState *dev, bool ram_discard);
#endif /* HW_VFIO_VFIO_COMMON_H */
diff --git a/hw/vfio/ap.c b/hw/vfio/ap.c
index 31ea9644c5..84c83f66fe 100644
--- a/hw/vfio/ap.c
+++ b/hw/vfio/ap.c
@@ -226,18 +226,14 @@ static void vfio_ap_instance_init(Object *obj)
VFIOAPDevice *vapdev = VFIO_AP_DEVICE(obj);
VFIODevice *vbasedev = &vapdev->vdev;
- vbasedev->type = VFIO_DEVICE_TYPE_AP;
- vbasedev->ops = &vfio_ap_ops;
- vbasedev->dev = DEVICE(vapdev);
- vbasedev->fd = -1;
-
/*
* vfio-ap devices operate in a way compatible with discarding of
* memory in RAM blocks, as no pages are pinned in the host.
* This needs to be set before vfio_get_device() for vfio common to
* handle ram_block_discard_disable().
*/
- vbasedev->ram_block_discard_allowed = true;
+ vfio_device_init(vbasedev, VFIO_DEVICE_TYPE_AP, &vfio_ap_ops,
+ DEVICE(vapdev), true);
}
#ifdef CONFIG_IOMMUFD
diff --git a/hw/vfio/ccw.c b/hw/vfio/ccw.c
index 8de2fd809b..0b1873e922 100644
--- a/hw/vfio/ccw.c
+++ b/hw/vfio/ccw.c
@@ -683,11 +683,6 @@ static void vfio_ccw_instance_init(Object *obj)
VFIOCCWDevice *vcdev = VFIO_CCW(obj);
VFIODevice *vbasedev = &vcdev->vdev;
- vbasedev->type = VFIO_DEVICE_TYPE_CCW;
- vbasedev->ops = &vfio_ccw_ops;
- vbasedev->dev = DEVICE(vcdev);
- vbasedev->fd = -1;
-
/*
* All vfio-ccw devices are believed to operate in a way compatible with
* discarding of memory in RAM blocks, ie. pages pinned in the host are
@@ -696,7 +691,8 @@ static void vfio_ccw_instance_init(Object *obj)
* needs to be set before vfio_get_device() for vfio common to handle
* ram_block_discard_disable().
*/
- vbasedev->ram_block_discard_allowed = true;
+ vfio_device_init(vbasedev, VFIO_DEVICE_TYPE_CCW, &vfio_ccw_ops,
+ DEVICE(vcdev), true);
}
#ifdef CONFIG_IOMMUFD
diff --git a/hw/vfio/helpers.c b/hw/vfio/helpers.c
index 3592c3d54e..6789870802 100644
--- a/hw/vfio/helpers.c
+++ b/hw/vfio/helpers.c
@@ -652,3 +652,14 @@ void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp)
}
vbasedev->fd = fd;
}
+
+void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops,
+ DeviceState *dev, bool ram_discard)
+{
+ vbasedev->type = type;
+ vbasedev->ops = ops;
+ vbasedev->dev = dev;
+ vbasedev->fd = -1;
+
+ vbasedev->ram_block_discard_allowed = ram_discard;
+}
diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
index 5a2b7a2d6b..31ad27d716 100644
--- a/hw/vfio/pci.c
+++ b/hw/vfio/pci.c
@@ -3327,10 +3327,8 @@ static void vfio_instance_init(Object *obj)
vdev->host.slot = ~0U;
vdev->host.function = ~0U;
- vbasedev->type = VFIO_DEVICE_TYPE_PCI;
- vbasedev->ops = &vfio_pci_ops;
- vbasedev->dev = DEVICE(vdev);
- vbasedev->fd = -1;
+ vfio_device_init(vbasedev, VFIO_DEVICE_TYPE_PCI, &vfio_pci_ops,
+ DEVICE(vdev), false);
vdev->nv_gpudirect_clique = 0xFF;
diff --git a/hw/vfio/platform.c b/hw/vfio/platform.c
index 506eb8193f..a8d9b7da63 100644
--- a/hw/vfio/platform.c
+++ b/hw/vfio/platform.c
@@ -657,10 +657,8 @@ static void vfio_platform_instance_init(Object *obj)
VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(obj);
VFIODevice *vbasedev = &vdev->vbasedev;
- vbasedev->type = VFIO_DEVICE_TYPE_PLATFORM;
- vbasedev->ops = &vfio_platform_ops;
- vbasedev->dev = DEVICE(vdev);
- vbasedev->fd = -1;
+ vfio_device_init(vbasedev, VFIO_DEVICE_TYPE_PLATFORM, &vfio_platform_ops,
+ DEVICE(vdev), false);
}
#ifdef CONFIG_IOMMUFD
--
2.34.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
end of thread, other threads:[~2023-11-16 8:16 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-16 7:59 [PATCH v2 0/5] VFIO device init cleanup Zhenzhong Duan
2023-11-16 7:59 ` [PATCH v2 1/5] vfio/pci: Move VFIODevice initializations in vfio_instance_init Zhenzhong Duan
2023-11-16 7:59 ` [PATCH v2 2/5] vfio/platform: Move VFIODevice initializations in vfio_platform_instance_init Zhenzhong Duan
2023-11-16 7:59 ` [PATCH v2 3/5] vfio/ap: Move VFIODevice initializations in vfio_ap_instance_init Zhenzhong Duan
2023-11-16 7:59 ` [PATCH v2 4/5] vfio/ccw: Move VFIODevice initializations in vfio_ccw_instance_init Zhenzhong Duan
2023-11-16 7:59 ` [PATCH v2 5/5] vfio: Introduce a helper function to initialize VFIODevice Zhenzhong Duan
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).