Skip to contentSkip to conversation

hw: Few more QOM/QDev cleanups

PATCH In review Philippe Mathieu-Daudé 7 participants 26 messages Oct 17, 2023 – Oct 31, 2023 Arm/Zynq architecture

lore
Share & tools
lore
#0 lore raw

cover hw: Few more QOM/QDev cleanups

- Remove a pointless check,
- Use QOM cast macros,
- Declare QDev links statically using DEFINE_PROP_LINK()

Philippe Mathieu-Daudé (7):
hw/virtio/virtio-pmem: Replace impossible check by assertion hw/block/vhost-user-blk: Use DEVICE() / VIRTIO_DEVICE() macros hw/display/virtio-gpu: Use VIRTIO_DEVICE() macro hw/scsi/virtio-scsi: Use VIRTIO_SCSI_COMMON() macro hw/dma: Declare link using static DEFINE_PROP_LINK() macro hw/net: Declare link using static DEFINE_PROP_LINK() macro hw/usb: Declare link using static DEFINE_PROP_LINK() macro

 hw/block/vhost-user-blk.c |  4 ++--
 hw/display/virtio-gpu.c   |  2 +-
 hw/dma/xilinx_axidma.c    |  6 ++----
 hw/dma/xlnx-zdma.c        |  7 ++-----
 hw/dma/xlnx_csu_dma.c     | 13 ++++---------
 hw/net/cadence_gem.c      |  7 ++-----
 hw/scsi/virtio-scsi.c     |  2 +-
 hw/usb/hcd-xhci-sysbus.c  |  4 ----
 hw/usb/hcd-xhci.c         |  2 ++
 hw/virtio/virtio-pmem.c   |  5 +----
 10 files changed, 17 insertions(+), 35 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH 1/7 hw/virtio/virtio-pmem: Replace impossible check by assertion

The get_memory_region() handler is used when (un)plugging the device, which can only occur *after* it is realized.

virtio_pmem_realize() ensure the instance can not be realized without 'memdev'. Remove the superfluous check, replacing it by an assertion.

---
 hw/virtio/virtio-pmem.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
hw/virtio/virtio-pmem.c +1 -4
index c3512c2dae..cc24812d2e 100644
@@ -147,10 +147,7 @@ static void virtio_pmem_fill_device_info(const VirtIOPMEM *pmem,
 static MemoryRegion *virtio_pmem_get_memory_region(VirtIOPMEM *pmem,
                                                    Error **errp)
 {
-    if (!pmem->memdev) {
-        error_setg(errp, "'%s' property must be set", VIRTIO_PMEM_MEMDEV_PROP);
-        return NULL;
-    }
+    assert(pmem->memdev);
 
     return &pmem->memdev->mr;
 }
Signature · 1 line
2.41.0
Reviewed-by on v1 1/7
#2 lore raw

Re: [PATCH 1/7] hw/virtio/virtio-pmem: Replace impossible check by assertion

Quoted 31 lines · Philippe Mathieu-Daudé

The get_memory_region() handler is used when (un)plugging the device, which can only occur *after* it is realized.

virtio_pmem_realize() ensure the instance can not be realized without 'memdev'. Remove the superfluous check, replacing it by an assertion.

---
hw/virtio/virtio-pmem.c | 5 +----

1 file changed, 1 insertion(+), 4 deletions(-)
hw/virtio/virtio-pmem.c +1 -4
index c3512c2dae..cc24812d2e 100644
@@ -147,10 +147,7 @@ static void virtio_pmem_fill_device_info(const VirtIOPMEM *pmem,
 static MemoryRegion *virtio_pmem_get_memory_region(VirtIOPMEM *pmem,
                                                   Error **errp)
 {
-    if (!pmem->memdev) {
-        error_setg(errp, "'%s' property must be set", VIRTIO_PMEM_MEMDEV_PROP);
-        return NULL;
-    }
+    assert(pmem->memdev);
 
    return &pmem->memdev->mr;
 }
Signature · 1 line
2.41.0
#3 lore raw

PATCH 2/7 hw/block/vhost-user-blk: Use DEVICE() / VIRTIO_DEVICE() macros

In reply to Philippe Mathieu-Daudé

Access QOM parent with the proper QOM [VIRTIO_]DEVICE() macros.

---
 hw/block/vhost-user-blk.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/block/vhost-user-blk.c +2 -2
index eecf3f7a81..4b37e26120 100644
@@ -405,7 +405,7 @@ static void vhost_user_blk_event(void *opaque, QEMUChrEvent event)
 
 static int vhost_user_blk_realize_connect(VHostUserBlk *s, Error **errp)
 {
-    DeviceState *dev = &s->parent_obj.parent_obj;
+    DeviceState *dev = DEVICE(s);
     int ret;
 
     s->connected = false;
@@ -423,7 +423,7 @@ static int vhost_user_blk_realize_connect(VHostUserBlk *s, Error **errp)
     assert(s->connected);
 
     ret = vhost_dev_get_config(&s->dev, (uint8_t *)&s->blkcfg,
-                               s->parent_obj.config_len, errp);
+                               VIRTIO_DEVICE(s)->config_len, errp);
     if (ret < 0) {
         qemu_chr_fe_disconnect(&s->chardev);
         vhost_dev_cleanup(&s->dev);
Signature · 1 line
2.41.0
Reviewed-by on v1 2/7
#4 lore raw

Re: [PATCH 2/7] hw/block/vhost-user-blk: Use DEVICE() / VIRTIO_DEVICE() macros

Quoted 33 lines · Philippe Mathieu-Daudé

Access QOM parent with the proper QOM [VIRTIO_]DEVICE() macros.

---
hw/block/vhost-user-blk.c | 4 ++--

1 file changed, 2 insertions(+), 2 deletions(-)
hw/block/vhost-user-blk.c +2 -2
index eecf3f7a81..4b37e26120 100644
@@ -405,7 +405,7 @@ static void vhost_user_blk_event(void *opaque, QEMUChrEvent event)
 
 static int vhost_user_blk_realize_connect(VHostUserBlk *s, Error **errp)
 {
-    DeviceState *dev = &s->parent_obj.parent_obj;
+    DeviceState *dev = DEVICE(s);
    int ret;
 
    s->connected = false;
@@ -423,7 +423,7 @@ static int vhost_user_blk_realize_connect(VHostUserBlk *s, Error **errp)
    assert(s->connected);
 
    ret = vhost_dev_get_config(&s->dev, (uint8_t *)&s->blkcfg,
-                               s->parent_obj.config_len, errp);
+                               VIRTIO_DEVICE(s)->config_len, errp);
    if (ret < 0) {
        qemu_chr_fe_disconnect(&s->chardev);
        vhost_dev_cleanup(&s->dev);
Signature · 1 line
2.41.0
Reviewed-by on v1 2/7
#5 lore raw

Re: [PATCH 2/7] hw/block/vhost-user-blk: Use DEVICE() / VIRTIO_DEVICE() macros

In reply to Philippe Mathieu-Daudé
Quoted 21 lines · Philippe Mathieu-Daudé

Access QOM parent with the proper QOM [VIRTIO_]DEVICE() macros.

---
  hw/block/vhost-user-blk.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
hw/block/vhost-user-blk.c +1 -1
index eecf3f7a81..4b37e26120 100644
@@ -405,7 +405,7 @@ static void vhost_user_blk_event(void *opaque, QEMUChrEvent event)
 
  static int vhost_user_blk_realize_connect(VHostUserBlk *s, Error **errp)
  {
-    DeviceState *dev = &s->parent_obj.parent_obj;
+    DeviceState *dev = DEVICE(s);
      int ret;
 
      s->connected = false;

hw/block/vhost-user-blk.c static int vhost_user_blk_realize_connect(VHostUserBlk *s, Error **errp) · line 429

Quoted 8 lines
hw/block/vhost-user-blk.c +1 -1
@@ -423,7 +423,7 @@ static int vhost_user_blk_realize_connect(VHostUserBlk *s, Error **errp)
      assert(s->connected);
 
      ret = vhost_dev_get_config(&s->dev, (uint8_t *)&s->blkcfg,
-                               s->parent_obj.config_len, errp);
+                               VIRTIO_DEVICE(s)->config_len, errp);
      if (ret < 0) {
          qemu_chr_fe_disconnect(&s->chardev);
          vhost_dev_cleanup(&s->dev);

ATB,

Mark.

#6 lore raw

PATCH 3/7 hw/display/virtio-gpu: Use VIRTIO_DEVICE() macro

In reply to Philippe Mathieu-Daudé

Access QOM parent with the proper QOM VIRTIO_DEVICE() macro.

---
 hw/display/virtio-gpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/display/virtio-gpu.c +1 -1
index 93857ad523..51cb517999 100644
@@ -1132,7 +1132,7 @@ static void virtio_gpu_ctrl_bh(void *opaque)
     VirtIOGPU *g = opaque;
     VirtIOGPUClass *vgc = VIRTIO_GPU_GET_CLASS(g);
 
-    vgc->handle_ctrl(&g->parent_obj.parent_obj, g->ctrl_vq);
+    vgc->handle_ctrl(VIRTIO_DEVICE(g), g->ctrl_vq);
 }
 
 static void virtio_gpu_handle_cursor(VirtIODevice *vdev, VirtQueue *vq)
Signature · 1 line
2.41.0
Reviewed-by on v1 3/7
#7 lore raw

Re: [PATCH 3/7] hw/display/virtio-gpu: Use VIRTIO_DEVICE() macro

Quoted 24 lines · Philippe Mathieu-Daudé

Access QOM parent with the proper QOM VIRTIO_DEVICE() macro.

---
hw/display/virtio-gpu.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
hw/display/virtio-gpu.c +1 -1
index 93857ad523..51cb517999 100644
@@ -1132,7 +1132,7 @@ static void virtio_gpu_ctrl_bh(void *opaque)
    VirtIOGPU *g = opaque;
    VirtIOGPUClass *vgc = VIRTIO_GPU_GET_CLASS(g);
 
-    vgc->handle_ctrl(&g->parent_obj.parent_obj, g->ctrl_vq);
+    vgc->handle_ctrl(VIRTIO_DEVICE(g), g->ctrl_vq);
 }
 
 static void virtio_gpu_handle_cursor(VirtIODevice *vdev, VirtQueue *vq)
Signature · 1 line
2.41.0
Reviewed-by on v1 3/7
#8 lore raw

Re: [PATCH 3/7] hw/display/virtio-gpu: Use VIRTIO_DEVICE() macro

In reply to Philippe Mathieu-Daudé
Quoted 12 lines · Philippe Mathieu-Daudé

Access QOM parent with the proper QOM VIRTIO_DEVICE() macro.

---
  hw/display/virtio-gpu.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/display/virtio-gpu.c +0 -0
index 93857ad523..51cb517999 100644

hw/display/virtio-gpu.c static void virtio_gpu_ctrl_bh(void *opaque) · line 1138

Quoted 8 lines
hw/display/virtio-gpu.c +1 -1
@@ -1132,7 +1132,7 @@ static void virtio_gpu_ctrl_bh(void *opaque)
      VirtIOGPU *g = opaque;
      VirtIOGPUClass *vgc = VIRTIO_GPU_GET_CLASS(g);
 
-    vgc->handle_ctrl(&g->parent_obj.parent_obj, g->ctrl_vq);
+    vgc->handle_ctrl(VIRTIO_DEVICE(g), g->ctrl_vq);
  }
 
  static void virtio_gpu_handle_cursor(VirtIODevice *vdev, VirtQueue *vq)

ATB,

Mark.

#9 lore raw

PATCH 4/7 hw/scsi/virtio-scsi: Use VIRTIO_SCSI_COMMON() macro

In reply to Philippe Mathieu-Daudé

Access QOM parent with the proper QOM VIRTIO_SCSI_COMMON() macro.

---
 hw/scsi/virtio-scsi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/scsi/virtio-scsi.c +1 -1
index 45b95ea070..fa53f0902c 100644
@@ -761,7 +761,7 @@ static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req)
 
 static int virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI *s, VirtIOSCSIReq *req)
 {
-    VirtIOSCSICommon *vs = &s->parent_obj;
+    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
     SCSIDevice *d;
     int rc;
 
Signature · 1 line
2.41.0
Reviewed-by on v1 4/7
#10 lore raw

Re: [PATCH 4/7] hw/scsi/virtio-scsi: Use VIRTIO_SCSI_COMMON() macro

Quoted 24 lines · Philippe Mathieu-Daudé

Access QOM parent with the proper QOM VIRTIO_SCSI_COMMON() macro.

---
hw/scsi/virtio-scsi.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
hw/scsi/virtio-scsi.c +1 -1
index 45b95ea070..fa53f0902c 100644
@@ -761,7 +761,7 @@ static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req)
 
 static int virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI *s, VirtIOSCSIReq *req)
 {
-    VirtIOSCSICommon *vs = &s->parent_obj;
+    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
    SCSIDevice *d;
    int rc;
 
Signature · 1 line
2.41.0
Reviewed-by on v1 4/7
#11 lore raw

Re: [PATCH 4/7] hw/scsi/virtio-scsi: Use VIRTIO_SCSI_COMMON() macro

In reply to Philippe Mathieu-Daudé
Quoted 12 lines · Philippe Mathieu-Daudé

Access QOM parent with the proper QOM VIRTIO_SCSI_COMMON() macro.

---
  hw/scsi/virtio-scsi.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/scsi/virtio-scsi.c +0 -0
index 45b95ea070..fa53f0902c 100644

hw/scsi/virtio-scsi.c static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req) · line 766

Quoted 7 lines
hw/scsi/virtio-scsi.c +1 -1
@@ -761,7 +761,7 @@ static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req)
 
  static int virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI *s, VirtIOSCSIReq *req)
  {
-    VirtIOSCSICommon *vs = &s->parent_obj;
+    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
      SCSIDevice *d;
      int rc;

ATB,

Mark.

#12 lore raw

Re: [PATCH 4/7] hw/scsi/virtio-scsi: Use VIRTIO_SCSI_COMMON() macro

In reply to Philippe Mathieu-Daudé

Am 17.10.2023 um 16:01 hat Philippe Mathieu-Daudé geschrieben:

Quoted 12 lines

Access QOM parent with the proper QOM VIRTIO_SCSI_COMMON() macro.

---
 hw/scsi/virtio-scsi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/scsi/virtio-scsi.c +0 -0
index 45b95ea070..fa53f0902c 100644

hw/scsi/virtio-scsi.c static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req) · line 766

Quoted 7 lines
hw/scsi/virtio-scsi.c +1 -1
@@ -761,7 +761,7 @@ static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req)
 
 static int virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI *s, VirtIOSCSIReq *req)
 {
-    VirtIOSCSICommon *vs = &s->parent_obj;
+    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
     SCSIDevice *d;
     int rc;

Why is a dynamic cast more "proper" than a static type-safe access, even more so in a hot I/O path?

Rich Jones posted a flamegraph the other day that surprised me because object_class_dynamic_class_assert() and object_dynamic_cast_assert() were shown to be a big part of scsi_req_new(). In the overall performance, it's probably dwarved by other issues, but unnecessary little things can add up, too.

Kevin

#13 lore raw

Re: [PATCH 4/7] hw/scsi/virtio-scsi: Use VIRTIO_SCSI_COMMON() macro

Quoted 15 lines · Kevin Wolf

Am 17.10.2023 um 16:01 hat Philippe Mathieu-Daudé geschrieben:

Quoted 13 lines

Access QOM parent with the proper QOM VIRTIO_SCSI_COMMON() macro.

---
 hw/scsi/virtio-scsi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/scsi/virtio-scsi.c +0 -0
index 45b95ea070..fa53f0902c 100644
@@ -761,7 +761,7 @@ static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req)
 
Quoted 9 lines

hw/scsi/virtio-scsi.c static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req) · line 766

Quoted 6 lines
hw/scsi/virtio-scsi.c +1 -1
 static int virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI *s, VirtIOSCSIReq *req)
 {
-    VirtIOSCSICommon *vs = &s->parent_obj;
+    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
     SCSIDevice *d;
     int rc;

Why is a dynamic cast more "proper" than a static type-safe access, even more so in a hot I/O path?

I dunno, but it is absolutely the standard QOM style to use the cast macro to do the downcast, rather than fishing about in the struct for the parent_obj field. If QOM casts are getting too expensive we should ideally fix them rather than adding more and more code that takes some side route around them...

thanks
-- PMM

#14 lore raw

Re: [PATCH 4/7] hw/scsi/virtio-scsi: Use VIRTIO_SCSI_COMMON() macro

In reply to Kevin Wolf
Quoted 24 lines · Kevin Wolf

Am 17.10.2023 um 16:01 hat Philippe Mathieu-Daudé geschrieben:

Quoted 12 lines

Access QOM parent with the proper QOM VIRTIO_SCSI_COMMON() macro.

---
 hw/scsi/virtio-scsi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/scsi/virtio-scsi.c +0 -0
index 45b95ea070..fa53f0902c 100644

hw/scsi/virtio-scsi.c static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req) · line 766

Quoted 7 lines
hw/scsi/virtio-scsi.c +1 -1
@@ -761,7 +761,7 @@ static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req)
 
 static int virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI *s, VirtIOSCSIReq *req)
 {
-    VirtIOSCSICommon *vs = &s->parent_obj;
+    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
     SCSIDevice *d;
     int rc;

Why is a dynamic cast more "proper" than a static type-safe access, even more so in a hot I/O path?

Quoted 5 lines

Rich Jones posted a flamegraph the other day that surprised me because object_class_dynamic_class_assert() and object_dynamic_cast_assert() were shown to be a big part of scsi_req_new(). In the overall performance, it's probably dwarved by other issues, but unnecessary little things can add up, too.

I think Kevin is referring to one of these flamegraphs:

http://oirase.annexia.org/tmp/2023-kvm-build-on-device.svg
http://oirase.annexia.org/tmp/2023-kvm-build.svg

Here's a zoom showing scsi_req_new (hopefully this URL is stable ...):

http://oirase.annexia.org/tmp/2023-kvm-build-on-device.svg?s=scsi_req_new&x=512.9&y=501

Note that qemu has been compiled with QOM cast debug. This is the default for Fedora (not RHEL) because we'd like to get early detection of bugs from Fedora users.

There was another patch recently where a simple change saved about 5% of total runtime in RISC-V TCG guests (admittedly a much more hot path than this one).

https://lists.nongnu.org/archive/html/qemu-devel/2023-10/msg02388.html

Rich.

Signature · 5 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
Fedora Windows cross-compiler. Compile Windows programs, test, and
build Windows installers. Over 100 libraries supported.
http://fedoraproject.org/wiki/MinGW
#15 lore raw

Re: [PATCH 4/7] hw/scsi/virtio-scsi: Use VIRTIO_SCSI_COMMON() macro

Am 31.10.2023 um 14:48 hat Richard W.M. Jones geschrieben:

Quoted 36 lines · Kevin Wolf
Quoted 24 lines

Am 17.10.2023 um 16:01 hat Philippe Mathieu-Daudé geschrieben:

Quoted 12 lines

Access QOM parent with the proper QOM VIRTIO_SCSI_COMMON() macro.

---
 hw/scsi/virtio-scsi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/scsi/virtio-scsi.c +0 -0
index 45b95ea070..fa53f0902c 100644

hw/scsi/virtio-scsi.c static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req) · line 766

Quoted 7 lines
hw/scsi/virtio-scsi.c +1 -1
@@ -761,7 +761,7 @@ static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req)
 
 static int virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI *s, VirtIOSCSIReq *req)
 {
-    VirtIOSCSICommon *vs = &s->parent_obj;
+    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
     SCSIDevice *d;
     int rc;

Why is a dynamic cast more "proper" than a static type-safe access, even more so in a hot I/O path?

Quoted 5 lines

Rich Jones posted a flamegraph the other day that surprised me because object_class_dynamic_class_assert() and object_dynamic_cast_assert() were shown to be a big part of scsi_req_new(). In the overall performance, it's probably dwarved by other issues, but unnecessary little things can add up, too.

I think Kevin is referring to one of these flamegraphs:

http://oirase.annexia.org/tmp/2023-kvm-build-on-device.svg
http://oirase.annexia.org/tmp/2023-kvm-build.svg

Quoted 7 lines

Here's a zoom showing scsi_req_new (hopefully this URL is stable ...):

http://oirase.annexia.org/tmp/2023-kvm-build-on-device.svg?s=scsi_req_new&x=512.9&y=501

Note that qemu has been compiled with QOM cast debug. This is the default for Fedora (not RHEL) because we'd like to get early detection of bugs from Fedora users.

Indeed, object_dynamic_cast_assert() probably wouldn't show up any more if you disabled QOM cast debugging. But we enable it by default, so that's an explicit setting users have to make, and as you say, at least Fedora doesn't.

The other one, object_class_dynamic_cast_assert() isn't even affected by the debug setting because it does more than just asserting a plain cast. Working around it isn't as trivial either, but I did think about caching it in the object to avoid a string based class lookup for every single request.

Looking at it again, strangely CONFIG_QOM_CAST_DEBUG enables a cache in object_class_dynamic_cast_assert() that isn't used otherwise. Why don't we want this optimisation without the debug flag?

Kevin

#16 lore raw

Re: [PATCH 4/7] hw/scsi/virtio-scsi: Use VIRTIO_SCSI_COMMON() macro

In reply to Richard W.M. Jones

Am 31.10.2023 um 14:48 hat Richard W.M. Jones geschrieben:

Quoted 31 lines · Kevin Wolf
Quoted 24 lines

Am 17.10.2023 um 16:01 hat Philippe Mathieu-Daudé geschrieben:

Quoted 12 lines

Access QOM parent with the proper QOM VIRTIO_SCSI_COMMON() macro.

---
 hw/scsi/virtio-scsi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/scsi/virtio-scsi.c +0 -0
index 45b95ea070..fa53f0902c 100644

hw/scsi/virtio-scsi.c static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req) · line 766

Quoted 7 lines
hw/scsi/virtio-scsi.c +1 -1
@@ -761,7 +761,7 @@ static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req)
 
 static int virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI *s, VirtIOSCSIReq *req)
 {
-    VirtIOSCSICommon *vs = &s->parent_obj;
+    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
     SCSIDevice *d;
     int rc;

Why is a dynamic cast more "proper" than a static type-safe access, even more so in a hot I/O path?

Quoted 5 lines

Rich Jones posted a flamegraph the other day that surprised me because object_class_dynamic_class_assert() and object_dynamic_cast_assert() were shown to be a big part of scsi_req_new(). In the overall performance, it's probably dwarved by other issues, but unnecessary little things can add up, too.

Quoted 8 lines

I think Kevin is referring to one of these flamegraphs:

http://oirase.annexia.org/tmp/2023-kvm-build-on-device.svg
http://oirase.annexia.org/tmp/2023-kvm-build.svg

Here's a zoom showing scsi_req_new (hopefully this URL is stable ...):

http://oirase.annexia.org/tmp/2023-kvm-build-on-device.svg?s=scsi_req_new&x=512.9&y=501

Oh, this one (kvm-build-on-device) doesn't even show the object cast. I was looking at kvm-build, it seems, where both the class and the object cast are visible:

http://oirase.annexia.org/tmp/2023-kvm-build.svg?s=scsi_req_new&x=455.4&y=533

Kevin

#17 lore raw

Re: [PATCH 4/7] hw/scsi/virtio-scsi: Use VIRTIO_SCSI_COMMON() macro

Quoted 39 lines · Kevin Wolf

Am 31.10.2023 um 14:48 hat Richard W.M. Jones geschrieben:

Quoted 38 lines · Kevin Wolf
Quoted 24 lines

Am 17.10.2023 um 16:01 hat Philippe Mathieu-Daudé geschrieben:

Quoted 12 lines

Access QOM parent with the proper QOM VIRTIO_SCSI_COMMON() macro.

---
 hw/scsi/virtio-scsi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/scsi/virtio-scsi.c +0 -0
index 45b95ea070..fa53f0902c 100644

hw/scsi/virtio-scsi.c static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req) · line 766

Quoted 7 lines
hw/scsi/virtio-scsi.c +1 -1
@@ -761,7 +761,7 @@ static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req)
 
 static int virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI *s, VirtIOSCSIReq *req)
 {
-    VirtIOSCSICommon *vs = &s->parent_obj;
+    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
     SCSIDevice *d;
     int rc;

Why is a dynamic cast more "proper" than a static type-safe access, even more so in a hot I/O path?

Quoted 5 lines

Rich Jones posted a flamegraph the other day that surprised me because object_class_dynamic_class_assert() and object_dynamic_cast_assert() were shown to be a big part of scsi_req_new(). In the overall performance, it's probably dwarved by other issues, but unnecessary little things can add up, too.

I think Kevin is referring to one of these flamegraphs:

http://oirase.annexia.org/tmp/2023-kvm-build-on-device.svg
http://oirase.annexia.org/tmp/2023-kvm-build.svg

Here's a zoom showing scsi_req_new (hopefully this URL is stable ...):

Quoted 7 lines
Quoted 1 line

http://oirase.annexia.org/tmp/2023-kvm-build-on-device.svg?s=scsi_req_new&x=512.9&y=501

Oh, this one (kvm-build-on-device) doesn't even show the object cast. I was looking at kvm-build, it seems, where both the class and the object cast are visible:

http://oirase.annexia.org/tmp/2023-kvm-build.svg?s=scsi_req_new&x=455.4&y=533

Not sure if this is the reason why, but the difference between these two runs is that kvm-build is backed by a qcow2 file and kvm-build-on-device is backed by a host block device. I believe they both were otherwise identically configured qemu.

More background in this Fedora thread:

https://lists.fedoraproject.org/archives/list/[email protected]/thread/MSJAL7OE2TBO6U4ZWXKTKQLDSGRFK6YR/

Rich.

Signature · 5 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into KVM guests.
http://libguestfs.org/virt-v2v
#18 lore raw

PATCH 5/7 hw/dma: Declare link using static DEFINE_PROP_LINK() macro

In reply to Philippe Mathieu-Daudé

Declare link statically using DEFINE_PROP_LINK().

---
 hw/dma/xilinx_axidma.c |  6 ++----
 hw/dma/xlnx-zdma.c     |  7 ++-----
 hw/dma/xlnx_csu_dma.c  | 13 ++++---------
 3 files changed, 8 insertions(+), 18 deletions(-)
hw/dma/xilinx_axidma.c +2 -4
index 12c90267df..0ae056ed06 100644
@@ -577,10 +577,6 @@ static void xilinx_axidma_init(Object *obj)
     object_initialize_child(OBJECT(s), "axistream-control-connected-target",
                             &s->rx_control_dev,
                             TYPE_XILINX_AXI_DMA_CONTROL_STREAM);
-    object_property_add_link(obj, "dma", TYPE_MEMORY_REGION,
-                             (Object **)&s->dma_mr,
-                             qdev_prop_allow_set_link_before_realize,
-                             OBJ_PROP_LINK_STRONG);
 
     sysbus_init_irq(sbd, &s->streams[0].irq);
     sysbus_init_irq(sbd, &s->streams[1].irq);
@@ -596,6 +592,8 @@ static Property axidma_properties[] = {
                      tx_data_dev, TYPE_STREAM_SINK, StreamSink *),
     DEFINE_PROP_LINK("axistream-control-connected", XilinxAXIDMA,
                      tx_control_dev, TYPE_STREAM_SINK, StreamSink *),
+    DEFINE_PROP_LINK("dma", XilinxAXIDMA, dma_mr,
+                     TYPE_MEMORY_REGION, MemoryRegion *),
     DEFINE_PROP_END_OF_LIST(),
 };
 
hw/dma/xlnx-zdma.c +2 -5
index 4eb7f66e9f..84c0083013 100644
@@ -795,11 +795,6 @@ static void zdma_init(Object *obj)
                           TYPE_XLNX_ZDMA, ZDMA_R_MAX * 4);
     sysbus_init_mmio(sbd, &s->iomem);
     sysbus_init_irq(sbd, &s->irq_zdma_ch_imr);
-
-    object_property_add_link(obj, "dma", TYPE_MEMORY_REGION,
-                             (Object **)&s->dma_mr,
-                             qdev_prop_allow_set_link_before_realize,
-                             OBJ_PROP_LINK_STRONG);
 }
 
 static const VMStateDescription vmstate_zdma = {
@@ -817,6 +812,8 @@ static const VMStateDescription vmstate_zdma = {
 
 static Property zdma_props[] = {
     DEFINE_PROP_UINT32("bus-width", XlnxZDMA, cfg.bus_width, 64),
+    DEFINE_PROP_LINK("dma", XlnxZDMA, dma_mr,
+                     TYPE_MEMORY_REGION, MemoryRegion *),
     DEFINE_PROP_END_OF_LIST(),
 };
 
hw/dma/xlnx_csu_dma.c +4 -9
index 88002698a1..e89089821a 100644
@@ -702,6 +702,10 @@ static Property xlnx_csu_dma_properties[] = {
      * which channel the device is connected to.
      */
     DEFINE_PROP_BOOL("is-dst", XlnxCSUDMA, is_dst, true),
+    DEFINE_PROP_LINK("stream-connected-dma", XlnxCSUDMA, tx_dev,
+                     TYPE_STREAM_SINK, StreamSink *),
+    DEFINE_PROP_LINK("dma", XlnxCSUDMA, dma_mr,
+                     TYPE_MEMORY_REGION, MemoryRegion *),
     DEFINE_PROP_END_OF_LIST(),
 };
 
@@ -728,15 +732,6 @@ static void xlnx_csu_dma_init(Object *obj)
 
     memory_region_init(&s->iomem, obj, TYPE_XLNX_CSU_DMA,
                        XLNX_CSU_DMA_R_MAX * 4);
-
-    object_property_add_link(obj, "stream-connected-dma", TYPE_STREAM_SINK,
-                             (Object **)&s->tx_dev,
-                             qdev_prop_allow_set_link_before_realize,
-                             OBJ_PROP_LINK_STRONG);
-    object_property_add_link(obj, "dma", TYPE_MEMORY_REGION,
-                             (Object **)&s->dma_mr,
-                             qdev_prop_allow_set_link_before_realize,
-                             OBJ_PROP_LINK_STRONG);
 }
 
 static const TypeInfo xlnx_csu_dma_info = {
Signature · 1 line
2.41.0
Reviewed-by on v1 5/7
#19 lore raw

Re: [PATCH 5/7] hw/dma: Declare link using static DEFINE_PROP_LINK() macro

Quoted 79 lines · Philippe Mathieu-Daudé

Declare link statically using DEFINE_PROP_LINK().

---
  hw/dma/xilinx_axidma.c |  6 ++----
  hw/dma/xlnx-zdma.c     |  7 ++-----
  hw/dma/xlnx_csu_dma.c  | 13 ++++---------
  3 files changed, 8 insertions(+), 18 deletions(-)
hw/dma/xilinx_axidma.c +2 -4
index 12c90267df..0ae056ed06 100644
@@ -577,10 +577,6 @@ static void xilinx_axidma_init(Object *obj)
      object_initialize_child(OBJECT(s), "axistream-control-connected-target",
                              &s->rx_control_dev,
                              TYPE_XILINX_AXI_DMA_CONTROL_STREAM);
-    object_property_add_link(obj, "dma", TYPE_MEMORY_REGION,
-                             (Object **)&s->dma_mr,
-                             qdev_prop_allow_set_link_before_realize,
-                             OBJ_PROP_LINK_STRONG);
 
      sysbus_init_irq(sbd, &s->streams[0].irq);
      sysbus_init_irq(sbd, &s->streams[1].irq);
@@ -596,6 +592,8 @@ static Property axidma_properties[] = {
                       tx_data_dev, TYPE_STREAM_SINK, StreamSink *),
      DEFINE_PROP_LINK("axistream-control-connected", XilinxAXIDMA,
                       tx_control_dev, TYPE_STREAM_SINK, StreamSink *),
+    DEFINE_PROP_LINK("dma", XilinxAXIDMA, dma_mr,
+                     TYPE_MEMORY_REGION, MemoryRegion *),
      DEFINE_PROP_END_OF_LIST(),
  };
 
hw/dma/xlnx-zdma.c +2 -5
index 4eb7f66e9f..84c0083013 100644
@@ -795,11 +795,6 @@ static void zdma_init(Object *obj)
                            TYPE_XLNX_ZDMA, ZDMA_R_MAX * 4);
      sysbus_init_mmio(sbd, &s->iomem);
      sysbus_init_irq(sbd, &s->irq_zdma_ch_imr);
-
-    object_property_add_link(obj, "dma", TYPE_MEMORY_REGION,
-                             (Object **)&s->dma_mr,
-                             qdev_prop_allow_set_link_before_realize,
-                             OBJ_PROP_LINK_STRONG);
  }
 
  static const VMStateDescription vmstate_zdma = {
@@ -817,6 +812,8 @@ static const VMStateDescription vmstate_zdma = {
 
  static Property zdma_props[] = {
      DEFINE_PROP_UINT32("bus-width", XlnxZDMA, cfg.bus_width, 64),
+    DEFINE_PROP_LINK("dma", XlnxZDMA, dma_mr,
+                     TYPE_MEMORY_REGION, MemoryRegion *),
      DEFINE_PROP_END_OF_LIST(),
  };
 
hw/dma/xlnx_csu_dma.c +4 -2
index 88002698a1..e89089821a 100644
@@ -702,6 +702,10 @@ static Property xlnx_csu_dma_properties[] = {
       * which channel the device is connected to.
       */
      DEFINE_PROP_BOOL("is-dst", XlnxCSUDMA, is_dst, true),
+    DEFINE_PROP_LINK("stream-connected-dma", XlnxCSUDMA, tx_dev,
+                     TYPE_STREAM_SINK, StreamSink *),
+    DEFINE_PROP_LINK("dma", XlnxCSUDMA, dma_mr,
+                     TYPE_MEMORY_REGION, MemoryRegion *),
      DEFINE_PROP_END_OF_LIST(),
  };
 
@@ -728,15 +732,6 @@ static void xlnx_csu_dma_init(Object *obj)
 
      memory_region_init(&s->iomem, obj, TYPE_XLNX_CSU_DMA,
                         XLNX_CSU_DMA_R_MAX * 4);
-
-    object_property_add_link(obj, "stream-connected-dma", TYPE_STREAM_SINK,

hw/dma/xlnx_csu_dma.c static void xlnx_csu_dma_init(Object *obj) · line 737

Quoted 10 lines
hw/dma/xlnx_csu_dma.c +0 -7
-                             (Object **)&s->tx_dev,
-                             qdev_prop_allow_set_link_before_realize,
-                             OBJ_PROP_LINK_STRONG);
-    object_property_add_link(obj, "dma", TYPE_MEMORY_REGION,
-                             (Object **)&s->dma_mr,
-                             qdev_prop_allow_set_link_before_realize,
-                             OBJ_PROP_LINK_STRONG);
  }
 
  static const TypeInfo xlnx_csu_dma_info = {

ATB,

Mark.

#20 lore raw

PATCH 6/7 hw/net: Declare link using static DEFINE_PROP_LINK() macro

In reply to Philippe Mathieu-Daudé

Declare link statically using DEFINE_PROP_LINK().

---
 hw/net/cadence_gem.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
hw/net/cadence_gem.c +2 -5
index f445d8bb5e..37e209cda6 100644
@@ -1654,11 +1654,6 @@ static void gem_init(Object *obj)
                           "enet", sizeof(s->regs));
 
     sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);
-
-    object_property_add_link(obj, "dma", TYPE_MEMORY_REGION,
-                             (Object **)&s->dma_mr,
-                             qdev_prop_allow_set_link_before_realize,
-                             OBJ_PROP_LINK_STRONG);
 }
 
 static const VMStateDescription vmstate_cadence_gem = {
@@ -1691,6 +1686,8 @@ static Property gem_properties[] = {
                       num_type2_screeners, 4),
     DEFINE_PROP_UINT16("jumbo-max-len", CadenceGEMState,
                        jumbo_max_len, 10240),
+    DEFINE_PROP_LINK("dma", CadenceGEMState, dma_mr,
+                     TYPE_MEMORY_REGION, MemoryRegion *),
     DEFINE_PROP_END_OF_LIST(),
 };
 
Signature · 1 line
2.41.0
Reviewed-by on v1 6/7
#21 lore raw

Re: [PATCH 6/7] hw/net: Declare link using static DEFINE_PROP_LINK() macro

Quoted 24 lines · Philippe Mathieu-Daudé

Declare link statically using DEFINE_PROP_LINK().

---
  hw/net/cadence_gem.c | 7 ++-----
  1 file changed, 2 insertions(+), 5 deletions(-)
hw/net/cadence_gem.c +0 -5
index f445d8bb5e..37e209cda6 100644
@@ -1654,11 +1654,6 @@ static void gem_init(Object *obj)
                            "enet", sizeof(s->regs));
 
      sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);
-
-    object_property_add_link(obj, "dma", TYPE_MEMORY_REGION,
-                             (Object **)&s->dma_mr,
-                             qdev_prop_allow_set_link_before_realize,
-                             OBJ_PROP_LINK_STRONG);
  }
 
  static const VMStateDescription vmstate_cadence_gem = {

hw/net/cadence_gem.c static Property gem_properties[] = { · line 1692

Quoted 7 lines
hw/net/cadence_gem.c +2 -0
@@ -1691,6 +1686,8 @@ static Property gem_properties[] = {
                        num_type2_screeners, 4),
      DEFINE_PROP_UINT16("jumbo-max-len", CadenceGEMState,
                         jumbo_max_len, 10240),
+    DEFINE_PROP_LINK("dma", CadenceGEMState, dma_mr,
+                     TYPE_MEMORY_REGION, MemoryRegion *),
      DEFINE_PROP_END_OF_LIST(),
  };

ATB,

Mark.

#22 lore raw

PATCH 7/7 hw/usb: Declare link using static DEFINE_PROP_LINK() macro

In reply to Philippe Mathieu-Daudé

Pull the 'dma' property to the core XHCI type, declare its link statically using DEFINE_PROP_LINK().

---
 hw/usb/hcd-xhci-sysbus.c | 4 ----
 hw/usb/hcd-xhci.c        | 2 ++
 2 files changed, 2 insertions(+), 4 deletions(-)
hw/usb/hcd-xhci-sysbus.c +0 -4
index faf57b4797..15983d0b96 100644
@@ -60,10 +60,6 @@ static void xhci_sysbus_instance_init(Object *obj)
     object_initialize_child(obj, "xhci-core", &s->xhci, TYPE_XHCI);
     qdev_alias_all_properties(DEVICE(&s->xhci), obj);
 
-    object_property_add_link(obj, "dma", TYPE_MEMORY_REGION,
-                             (Object **)&s->xhci.dma_mr,
-                             qdev_prop_allow_set_link_before_realize,
-                             OBJ_PROP_LINK_STRONG);
     s->xhci.intr_update = NULL;
     s->xhci.intr_raise = xhci_sysbus_intr_raise;
 }
hw/usb/hcd-xhci.c +2 -0
index 4b60114207..012a6f3644 100644
@@ -3638,6 +3638,8 @@ static Property xhci_properties[] = {
     DEFINE_PROP_UINT32("p3",    XHCIState, numports_3, 4),
     DEFINE_PROP_LINK("host",    XHCIState, hostOpaque, TYPE_DEVICE,
                      DeviceState *),
+    DEFINE_PROP_LINK("dma", XHCIState, dma_mr,
+                     TYPE_MEMORY_REGION, MemoryRegion *),
     DEFINE_PROP_END_OF_LIST(),
 };
 
Signature · 1 line
2.41.0
#23 lore raw

Re: [PATCH 7/7] hw/usb: Declare link using static DEFINE_PROP_LINK() macro

Quoted 29 lines · Philippe Mathieu-Daudé

Pull the 'dma' property to the core XHCI type, declare its link statically using DEFINE_PROP_LINK().

---
  hw/usb/hcd-xhci-sysbus.c | 4 ----
  hw/usb/hcd-xhci.c        | 2 ++
  2 files changed, 2 insertions(+), 4 deletions(-)
hw/usb/hcd-xhci-sysbus.c +0 -4
index faf57b4797..15983d0b96 100644
@@ -60,10 +60,6 @@ static void xhci_sysbus_instance_init(Object *obj)
      object_initialize_child(obj, "xhci-core", &s->xhci, TYPE_XHCI);
      qdev_alias_all_properties(DEVICE(&s->xhci), obj);
 
-    object_property_add_link(obj, "dma", TYPE_MEMORY_REGION,
-                             (Object **)&s->xhci.dma_mr,
-                             qdev_prop_allow_set_link_before_realize,
-                             OBJ_PROP_LINK_STRONG);
      s->xhci.intr_update = NULL;
      s->xhci.intr_raise = xhci_sysbus_intr_raise;
  }
hw/usb/hcd-xhci.c +0 -0
index 4b60114207..012a6f3644 100644

hw/usb/hcd-xhci.c static Property xhci_properties[] = { · line 3644

Quoted 7 lines
hw/usb/hcd-xhci.c +2 -0
@@ -3638,6 +3638,8 @@ static Property xhci_properties[] = {
      DEFINE_PROP_UINT32("p3",    XHCIState, numports_3, 4),
      DEFINE_PROP_LINK("host",    XHCIState, hostOpaque, TYPE_DEVICE,
                       DeviceState *),
+    DEFINE_PROP_LINK("dma", XHCIState, dma_mr,
+                     TYPE_MEMORY_REGION, MemoryRegion *),
      DEFINE_PROP_END_OF_LIST(),
  };

I'm slightly unsure about this one: does pulling the "dma" property into the core type cause any issues if the property is left unset for any non-sysbus xhci users?

ATB,

Mark.

Reviewed-by on the v1 series
#24 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 3 lines · Philippe Mathieu-Daudé

- Remove a pointless check,
- Use QOM cast macros,
- Declare QDev links statically using DEFINE_PROP_LINK()

virtio things

Quoted 23 lines

Philippe Mathieu-Daudé (7):
hw/virtio/virtio-pmem: Replace impossible check by assertion hw/block/vhost-user-blk: Use DEVICE() / VIRTIO_DEVICE() macros hw/display/virtio-gpu: Use VIRTIO_DEVICE() macro hw/scsi/virtio-scsi: Use VIRTIO_SCSI_COMMON() macro hw/dma: Declare link using static DEFINE_PROP_LINK() macro hw/net: Declare link using static DEFINE_PROP_LINK() macro hw/usb: Declare link using static DEFINE_PROP_LINK() macro

 hw/block/vhost-user-blk.c |  4 ++--
 hw/display/virtio-gpu.c   |  2 +-
 hw/dma/xilinx_axidma.c    |  6 ++----
 hw/dma/xlnx-zdma.c        |  7 ++-----
 hw/dma/xlnx_csu_dma.c     | 13 ++++---------
 hw/net/cadence_gem.c      |  7 ++-----
 hw/scsi/virtio-scsi.c     |  2 +-
 hw/usb/hcd-xhci-sysbus.c  |  4 ----
 hw/usb/hcd-xhci.c         |  2 ++
 hw/virtio/virtio-pmem.c   |  5 +----
 10 files changed, 17 insertions(+), 35 deletions(-)
Signature · 1 line
2.41.0
#25 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 7 lines · Philippe Mathieu-Daudé

Philippe Mathieu-Daudé (7):
hw/virtio/virtio-pmem: Replace impossible check by assertion hw/block/vhost-user-blk: Use DEVICE() / VIRTIO_DEVICE() macros hw/display/virtio-gpu: Use VIRTIO_DEVICE() macro hw/scsi/virtio-scsi: Use VIRTIO_SCSI_COMMON() macro hw/dma: Declare link using static DEFINE_PROP_LINK() macro hw/net: Declare link using static DEFINE_PROP_LINK() macro

Patches 1-6 queued to hw-misc.