Skip to contentSkip to conversation

dataplane: use endian-aware memory accessors

PATCH NAK Stefan Hajnoczi 5 participants 8 messages Jan 19, 2015 – Jan 21, 2015

lore
Share & tools
lore
#0 lore raw

cover dataplane: use endian-aware memory accessors

In commit 0f5d1d2a49778863db54b4b1ac2dc008a8f21f11 ("virtio: memory accessors for endian-ambivalent targets") endian-aware memory accessors were added to support bi-endian targets like recent ppc64 systems.

The dataplane vring.c code does not use these accessors and is therefore unable to emulate virtio devices when the endianness differs between the device and host.

These patches modify vring.c to use the endian-aware accessors.

I have tested that x86_64 guests still function correctly.

I have only compile-tested on ppc64 and require help testing that dataplane works. If you have access to a bi-endian system, please try:

  qemu ... -object iothread,id=iothread0 \
           -drive if=none,id=drive0,file=... \
           -device virtio-blk-pci,iothread=iothread0,drive=drive0

Stefan Hajnoczi (2):
dataplane: move vring_more_avail() into vring.c
dataplane: use virtio_ld/st_p() for endian-aware memory access

 hw/block/dataplane/virtio-blk.c     |  2 +-
 hw/scsi/virtio-scsi-dataplane.c     |  2 +-
 hw/virtio/Makefile.objs             |  2 +-
 hw/virtio/dataplane/Makefile.objs   |  2 +-
 hw/virtio/dataplane/vring.c         | 69 ++++++++++++++++++++++++++-----------
 include/hw/virtio/dataplane/vring.h |  9 ++---
 6 files changed, 55 insertions(+), 31 deletions(-)
Signature · 1 line
2.1.0
#1 lore raw

PATCH 1/2 dataplane: move vring_more_avail() into vring.c

vring_more_avail() was an inline function in vring.h. No external callers use it so it's not necessary to export it.

Furthermore, we'll need virtio-access.h for endian-aware memory accesses but that only works in per-target object files (obj-y) and not build-once object files (common-obj-y) like the virtio-blk and virtio-scsi devices.

Move vring_more_avail() into vring.c so that virtio devices like virtio-blk and virtio-scsi can continue to use vring.h without being built once per target.

---
 hw/virtio/dataplane/vring.c         | 6 ++++++
 include/hw/virtio/dataplane/vring.h | 6 ------
 2 files changed, 6 insertions(+), 6 deletions(-)
hw/virtio/dataplane/vring.c +6 -0
index 61f6d83..cb31d7f 100644
@@ -21,6 +21,12 @@
 #include "hw/virtio/dataplane/vring.h"
 #include "qemu/error-report.h"
 
+/* Are there more descriptors available? */
+static inline bool vring_more_avail(Vring *vring)
+{
+    return vring->vr.avail->idx != vring->last_avail_idx;
+}
+
 /* vring_map can be coupled with vring_unmap or (if you still have the
  * value returned in *mr) memory_region_unref.
  */
include/hw/virtio/dataplane/vring.h +0 -6
index d3e086a..1e871e6 100644
@@ -36,12 +36,6 @@ static inline unsigned int vring_get_num(Vring *vring)
     return vring->vr.num;
 }
 
-/* Are there more descriptors available? */
-static inline bool vring_more_avail(Vring *vring)
-{
-    return vring->vr.avail->idx != vring->last_avail_idx;
-}
-
 /* Fail future vring_pop() and vring_push() calls until reset */
 static inline void vring_set_broken(Vring *vring)
 {
Signature · 1 line
2.1.0
Reviewed-by Tested-by on v1 1/2
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] dataplane: move vring_more_avail() into vring.c

Quoted 8 lines · Stefan Hajnoczi

vring_more_avail() was an inline function in vring.h. No external callers use it so it's not necessary to export it.

Furthermore, we'll need virtio-access.h for endian-aware memory accesses but that only works in per-target object files (obj-y) and not build-once object files (common-obj-y) like the virtio-blk and virtio-scsi devices.

Quoted 5 lines

Move vring_more_avail() into vring.c so that virtio devices like virtio-blk and virtio-scsi can continue to use vring.h without being built once per target.

Looks sensible to me.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
Reviewed-by on v1 1/2
#3 lore raw

Re: [Qemu-devel] [PATCH 1/2] dataplane: move vring_more_avail() into vring.c

In reply to Stefan Hajnoczi
Quoted 8 lines · On Mon, 19 Jan 2015 17:04:36 +0000 Stefan Hajnoczi <[email protected]> wrote:

vring_more_avail() was an inline function in vring.h. No external callers use it so it's not necessary to export it.

Furthermore, we'll need virtio-access.h for endian-aware memory accesses but that only works in per-target object files (obj-y) and not build-once object files (common-obj-y) like the virtio-blk and virtio-scsi devices.

Quoted 6 lines

Move vring_more_avail() into vring.c so that virtio devices like virtio-blk and virtio-scsi can continue to use vring.h without being built once per target.

---

Yes, this function should definitely be declared in more appropriate scope. And BTW I remember Cornelia had the same concern in the virtio-1 serie. :)

Quoted 38 lines
 hw/virtio/dataplane/vring.c         | 6 ++++++
 include/hw/virtio/dataplane/vring.h | 6 ------
 2 files changed, 6 insertions(+), 6 deletions(-)
hw/virtio/dataplane/vring.c +6 -0
index 61f6d83..cb31d7f 100644
@@ -21,6 +21,12 @@
 #include "hw/virtio/dataplane/vring.h"
 #include "qemu/error-report.h"
 
+/* Are there more descriptors available? */
+static inline bool vring_more_avail(Vring *vring)
+{
+    return vring->vr.avail->idx != vring->last_avail_idx;
+}
+
 /* vring_map can be coupled with vring_unmap or (if you still have the
  * value returned in *mr) memory_region_unref.
  */
include/hw/virtio/dataplane/vring.h +0 -6
index d3e086a..1e871e6 100644
@@ -36,12 +36,6 @@ static inline unsigned int vring_get_num(Vring *vring)
     return vring->vr.num;
 }
 
-/* Are there more descriptors available? */
-static inline bool vring_more_avail(Vring *vring)
-{
-    return vring->vr.avail->idx != vring->last_avail_idx;
-}
-
 /* Fail future vring_pop() and vring_push() calls until reset */
 static inline void vring_set_broken(Vring *vring)
 {
#4 lore raw

PATCH 2/2 dataplane: use virtio_ld/st_p() for endian-aware memory access

In reply to Stefan Hajnoczi

The vring.c code was written under the assumption that virtio devices have the same endianness as the host.

This is wrong when emulating targets that differ in endianness from the host.

It is also wrong when emulating bi-endian targets like POWER 8 processors, which support both little- and big-endian at run-time. In this case the virtio device knows which endianness to use.

This change requires the virtio-access.h APIs and therefore builds vring.o for each target (obj-y instead of common-obj-y).

Note that $(CONFIG_VIRTIO) for dataplane/ was dropped in hw/virtio/Makefile.objs because hw/Makefile.objs conditionally includes hw/virtio/ on $(CONFIG_VIRTIO) already.

Only a small change is needed to the vring.h interface: vring_push() now takes a VirtIODevice *vdev argument.

Cc Paolo Bonzini <[email protected]>
Cc Fam Zheng <[email protected]>
---
 hw/block/dataplane/virtio-blk.c     |  2 +-
 hw/scsi/virtio-scsi-dataplane.c     |  2 +-
 hw/virtio/Makefile.objs             |  2 +-
 hw/virtio/dataplane/Makefile.objs   |  2 +-
 hw/virtio/dataplane/vring.c         | 67 +++++++++++++++++++++++++------------
 include/hw/virtio/dataplane/vring.h |  3 +-
 6 files changed, 51 insertions(+), 27 deletions(-)
hw/block/dataplane/virtio-blk.c +1 -1
index 39c5d71..aca2a8d 100644
@@ -75,7 +75,7 @@ static void complete_request_vring(VirtIOBlockReq *req, unsigned char status)
     VirtIOBlockDataPlane *s = req->dev->dataplane;
     stb_p(&req->in->status, status);
 
-    vring_push(&req->dev->dataplane->vring, &req->elem,
+    vring_push(s->vdev, &req->dev->dataplane->vring, &req->elem,
                req->qiov.size + sizeof(*req->in));
 
     /* Suppress notification to guest by BH and its scheduled
hw/scsi/virtio-scsi-dataplane.c +1 -1
index 03a1e8c..418d73b 100644
@@ -94,7 +94,7 @@ void virtio_scsi_vring_push_notify(VirtIOSCSIReq *req)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(req->vring->parent);
 
-    vring_push(&req->vring->vring, &req->elem,
+    vring_push(vdev, &req->vring->vring, &req->elem,
                req->qsgl.size + req->resp_iov.size);
 
     if (vring_should_notify(vdev, &req->vring->vring)) {
hw/virtio/Makefile.objs +1 -1
index d21c397..3e93a3a 100644
@@ -2,7 +2,7 @@ common-obj-y += virtio-rng.o
 common-obj-$(CONFIG_VIRTIO_PCI) += virtio-pci.o
 common-obj-y += virtio-bus.o
 common-obj-y += virtio-mmio.o
-common-obj-$(CONFIG_VIRTIO) += dataplane/
 
 obj-y += virtio.o virtio-balloon.o 
 obj-$(CONFIG_LINUX) += vhost.o vhost-backend.o vhost-user.o
+obj-y += dataplane/
hw/virtio/dataplane/Makefile.objs +1 -1
index 9a8cfc0..753a9ca 100644
@@ -1 +1 @@
-common-obj-y += vring.o
+obj-y += vring.o
hw/virtio/dataplane/vring.c +45 -22

178 lines not on this page · raw message

include/hw/virtio/dataplane/vring.h +2 -1
index 1e871e6..38e581b 100644
@@ -48,6 +48,7 @@ void vring_disable_notification(VirtIODevice *vdev, Vring *vring);
 bool vring_enable_notification(VirtIODevice *vdev, Vring *vring);
 bool vring_should_notify(VirtIODevice *vdev, Vring *vring);
 int vring_pop(VirtIODevice *vdev, Vring *vring, VirtQueueElement *elem);
-void vring_push(Vring *vring, VirtQueueElement *elem, int len);
+void vring_push(VirtIODevice *vdev, Vring *vring,
+                VirtQueueElement *elem, int len);
 
 #endif /* VRING_H */
Signature · 1 line
2.1.0
Reviewed-by Tested-by on v1 2/2
#5 lore raw

Re: [Qemu-devel] [PATCH 2/2] dataplane: use virtio_ld/st_p() for endian-aware memory access

Quoted 17 lines · Stefan Hajnoczi

The vring.c code was written under the assumption that virtio devices have the same endianness as the host.

This is wrong when emulating targets that differ in endianness from the host.

It is also wrong when emulating bi-endian targets like POWER 8 processors, which support both little- and big-endian at run-time. In this case the virtio device knows which endianness to use.

This change requires the virtio-access.h APIs and therefore builds vring.o for each target (obj-y instead of common-obj-y).

Note that $(CONFIG_VIRTIO) for dataplane/ was dropped in hw/virtio/Makefile.objs because hw/Makefile.objs conditionally includes hw/virtio/ on $(CONFIG_VIRTIO) already.

Quoted 6 lines

Only a small change is needed to the vring.h interface: vring_push() now takes a VirtIODevice *vdev argument.

Cc Paolo Bonzini <[email protected]>
Cc Fam Zheng <[email protected]>

Without this patch adding an iothread to a secondary virtio-blk disk caused the kernel to jam up during boot. With this patch it booted up and I was able to do things with the disk.

I'm still not able to add an iothread to a primary virtio-blk device:
SLOF (guest firmware) gets errors attempting to read the kernel to boot. However, SLOF is always big endian, so I think that's an unrelated bug.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#6 lore raw
In reply to Stefan Hajnoczi
Quoted 9 lines · On Mon, 19 Jan 2015 17:04:35 +0000 Stefan Hajnoczi <[email protected]> wrote:

In commit 0f5d1d2a49778863db54b4b1ac2dc008a8f21f11 ("virtio: memory accessors for endian-ambivalent targets") endian-aware memory accessors were added to support bi-endian targets like recent ppc64 systems.

The dataplane vring.c code does not use these accessors and is therefore unable to emulate virtio devices when the endianness differs between the device and host.

These patches modify vring.c to use the endian-aware accessors.

I had modified the accesses in vring.c to consider endianness for virtio-1 support as well. I wonder whether we can find some common ground there?

It's been a while since I have had time to work on this, but searching for "dataplane: allow virtio-1 devices" should lead you to the relevant patch.

#7 lore raw
In reply to Stefan Hajnoczi
Quoted 23 lines · Stefan Hajnoczi

In commit 0f5d1d2a49778863db54b4b1ac2dc008a8f21f11 ("virtio: memory accessors for endian-ambivalent targets") endian-aware memory accessors were added to support bi-endian targets like recent ppc64 systems.

The dataplane vring.c code does not use these accessors and is therefore unable to emulate virtio devices when the endianness differs between the device and host.

These patches modify vring.c to use the endian-aware accessors.

I have tested that x86_64 guests still function correctly.

I have only compile-tested on ppc64 and require help testing that dataplane works. If you have access to a bi-endian system, please try:

  qemu ... -object iothread,id=iothread0 \
           -drive if=none,id=drive0,file=... \
           -device virtio-blk-pci,iothread=iothread0,drive=drive0

Stefan Hajnoczi (2):
dataplane: move vring_more_avail() into vring.c
dataplane: use virtio_ld/st_p() for endian-aware memory access

Quoted 7 lines
 hw/block/dataplane/virtio-blk.c     |  2 +-
 hw/scsi/virtio-scsi-dataplane.c     |  2 +-
 hw/virtio/Makefile.objs             |  2 +-
 hw/virtio/dataplane/Makefile.objs   |  2 +-
 hw/virtio/dataplane/vring.c         | 69 ++++++++++++++++++++++++++-----------
 include/hw/virtio/dataplane/vring.h |  9 ++---
 6 files changed, 55 insertions(+), 31 deletions(-)

NACK

Cornelia Huck already has an equivalent patch.