Skip to contentSkip to conversation

virtio/vhost: fix alignment requirements

PATCH In review Michael S. Tsirkin 2 participants 5 messages Dec 25, 2014 – Dec 27, 2014

Subsystems (6)
lore
Share & tools
lore

Michael S. Tsirkin

author maintainer
#0 lore raw

v2 cover virtio/vhost: fix alignment requirements

vhost incorrectly asked for 8 byte alignment for used ring pointer, it should be 4 byte.

Let's add explicit macros for ring element alignment, this makes it easier to make sure our requirements match the spec.

Rusty, OK to merge this through my vhost tree, or do you prefer merging this through yours?

Michael S. Tsirkin (2):
virtio_ring: document alignment requirements
vhost: relax used address alignment

 include/uapi/linux/virtio_ring.h |  7 +++++++
 drivers/vhost/vhost.c            | 10 +++++++---
 2 files changed, 14 insertions(+), 3 deletions(-)
Signature · 1 line
MST
author maintainer
#1 lore raw

PATCH v2 1/2 virtio_ring: document alignment requirements

Host needs to know vring element alignment requirements:
simply doing alignof on structures doesn't work reliably: on some platforms gcc has alignof(uint32_t) == 2.

Add macros for alignment as specified in virtio 1.0 cs01, export them to userspace as well.

---
 include/uapi/linux/virtio_ring.h | 7 +++++++
 1 file changed, 7 insertions(+)
include/uapi/linux/virtio_ring.h +7 -0
index 61c818a..a3318f3 100644
@@ -101,6 +101,13 @@ struct vring {
 	struct vring_used *used;
 };
 
+/* Alignment requirements for vring elements.
+ * When using pre-virtio 1.0 layout, these fall out naturally.
+ */
+#define VRING_AVAIL_ALIGN_SIZE 2
+#define VRING_USED_ALIGN_SIZE 4
+#define VRING_DESC_ALIGN_SIZE 16
+
 /* The standard layout for the ring is a continuous chunk of memory which looks
  * like this.  We assume num is a power of 2.
  *
Signature · 1 line
MST
author maintainer
#2 lore raw

PATCH v2 2/2 vhost: relax used address alignment

In reply to Michael S. Tsirkin

virtio 1.0 only requires used address to be 4 byte aligned, vhost required 8 bytes (size of vring_used_elem). Fix up vhost to match that.

Additionally, while vhost correctly requires 8 byte alignment for log, it's unconnected to used ring:
it's a consequence that log has u64 entries. Tweak code to make that clearer.

---
 drivers/vhost/vhost.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/vhost/vhost.c +7 -3
index ed71b53..cb807d0 100644
@@ -713,9 +713,13 @@ long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp)
 			r = -EFAULT;
 			break;
 		}
-		if ((a.avail_user_addr & (sizeof *vq->avail->ring - 1)) ||
-		    (a.used_user_addr & (sizeof *vq->used->ring - 1)) ||
-		    (a.log_guest_addr & (sizeof *vq->used->ring - 1))) {
+
+		/* Make sure it's safe to cast pointers to vring types. */
+		BUILD_BUG_ON(__alignof__ *vq->avail > VRING_AVAIL_ALIGN_SIZE);
+		BUILD_BUG_ON(__alignof__ *vq->used > VRING_USED_ALIGN_SIZE);
+		if ((a.avail_user_addr & (VRING_AVAIL_ALIGN_SIZE - 1)) ||
+		    (a.used_user_addr & (VRING_USED_ALIGN_SIZE - 1)) ||
+		    (a.log_guest_addr & (sizeof(u64) - 1))) {
 			r = -EINVAL;
 			break;
 		}
Signature · 1 line
MST
author maintainer
#3 lore raw
In reply to Michael S. Tsirkin
Quoted 9 lines · Michael S. Tsirkin

vhost incorrectly asked for 8 byte alignment for used ring pointer, it should be 4 byte.

Let's add explicit macros for ring element alignment, this makes it easier to make sure our requirements match the spec.

Rusty, OK to merge this through my vhost tree, or do you prefer merging this through yours?

Just to clarify, this is needed in 3.19.

Quoted 11 lines

Michael S. Tsirkin (2):
virtio_ring: document alignment requirements
vhost: relax used address alignment

 include/uapi/linux/virtio_ring.h |  7 +++++++
 drivers/vhost/vhost.c            | 10 +++++++---
 2 files changed, 14 insertions(+), 3 deletions(-)
Signature · 1 line
MST
Acked-by
#4 lore raw
Quoted 4 lines · Michael S. Tsirkin
Quoted 3 lines · Michael S. Tsirkin

vhost incorrectly asked for 8 byte alignment for used ring pointer, it should be 4 byte.

Quoted 8 lines
Quoted 6 lines

Let's add explicit macros for ring element alignment, this makes it easier to make sure our requirements match the spec.

Rusty, OK to merge this through my vhost tree, or do you prefer merging this through yours?

Just to clarify, this is needed in 3.19.

Sure, please take this via the vhost tree.

Thanks,
Rusty.