From: =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Subject: [RFC PATCH 3/4] vhost: byteswap virtqueue attributes Date: Wed, 29 Oct 2014 09:38:44 +0100 Message-ID: <1414571925-16918-4-git-send-email-clg@fr.ibm.com> References: <1414571925-16918-1-git-send-email-clg@fr.ibm.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, agraf@suse.de, paulus@samba.org, gkurz@linux.vnet.ibm.com.patch, aik@ozlabs.ru, =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= To: "Michael S. Tsirkin" Return-path: In-Reply-To: <1414571925-16918-1-git-send-email-clg@fr.ibm.com> Sender: kvm-ppc-owner@vger.kernel.org List-Id: kvm.vger.kernel.org The virtqueue structure shares a few attributes with the guest OS which need to be byteswapped when the endian order of the host is different. This patch uses the vq->byteswap attribute to decide whether to byteswap or not data being accessed in the guest memory. Signed-off-by: C=C3=A9dric Le Goater --- drivers/vhost/vhost.c | 52 +++++++++++++++++++++++++++++++++++++----= -------- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index afcb3368370c..9a538f4d2ff1 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -1097,7 +1097,7 @@ static void vring_desc_swap(struct vring_desc *de= sc) static int vhost_update_used_flags(struct vhost_virtqueue *vq) { void __user *used; - if (__put_user(vq->used_flags, &vq->used->flags) < 0) + if (__vq_put_user(vq, vq->used_flags, &vq->used->flags) < 0) return -EFAULT; if (unlikely(vq->log_used)) { /* Make sure the flag is seen before log. */ @@ -1115,7 +1115,7 @@ static int vhost_update_used_flags(struct vhost_v= irtqueue *vq) =20 static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 av= ail_event) { - if (__put_user(vq->avail_idx, vhost_avail_event(vq))) + if (__vq_put_user(vq, vq->avail_idx, vhost_avail_event(vq))) return -EFAULT; if (unlikely(vq->log_used)) { void __user *used; @@ -1142,7 +1142,7 @@ int vhost_init_used(struct vhost_virtqueue *vq) if (r) return r; vq->signalled_used_valid =3D false; - return get_user(vq->last_used_idx, &vq->used->idx); + return vq_get_user(vq, vq->last_used_idx, &vq->used->idx); } EXPORT_SYMBOL_GPL(vhost_init_used); =20 @@ -1254,6 +1254,10 @@ static int get_indirect(struct vhost_virtqueue *= vq, i, (size_t)indirect->addr + i * sizeof desc); return -EINVAL; } + + if (vq->byteswap) + vring_desc_swap(&desc); + if (unlikely(desc.flags & VRING_DESC_F_INDIRECT)) { vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n", i, (size_t)indirect->addr + i * sizeof desc); @@ -1309,7 +1313,7 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq, =20 /* Check it isn't doing very strange things with descriptor numbers. = */ last_avail_idx =3D vq->last_avail_idx; - if (unlikely(__get_user(vq->avail_idx, &vq->avail->idx))) { + if (unlikely(__vq_get_user(vq, vq->avail_idx, &vq->avail->idx))) { vq_err(vq, "Failed to access avail idx at %p\n", &vq->avail->idx); return -EFAULT; @@ -1330,7 +1334,7 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq, =20 /* Grab the next descriptor number they're advertising, and increment * the index we've seen. */ - if (unlikely(__get_user(head, + if (unlikely(__vq_get_user(vq, head, &vq->avail->ring[last_avail_idx % vq->num]))) { vq_err(vq, "Failed to read head: idx %d address %p\n", last_avail_idx, @@ -1370,6 +1374,10 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq= , i, vq->desc + i); return -EFAULT; } + + if (vq->byteswap) + vring_desc_swap(&desc); + if (desc.flags & VRING_DESC_F_INDIRECT) { ret =3D get_indirect(vq, iov, iov_size, out_num, in_num, @@ -1437,6 +1445,26 @@ int vhost_add_used(struct vhost_virtqueue *vq, u= nsigned int head, int len) } EXPORT_SYMBOL_GPL(vhost_add_used); =20 +static int __copyhead_to_user(struct vhost_virtqueue *vq, + struct vring_used_elem *heads, + struct vring_used_elem __user *used, + unsigned count) +{ + int i; + + for (i =3D 0; i < count; i++) { + if (__vq_put_user(vq, heads[i].id, &used[i].id)) { + vq_err(vq, "Failed to write used id"); + return -EFAULT; + } + if (__vq_put_user(vq, heads[i].len, &used[i].len)) { + vq_err(vq, "Failed to write used len"); + return -EFAULT; + } + } + return 0; +} + static int __vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads, unsigned count) @@ -1448,15 +1476,15 @@ static int __vhost_add_used_n(struct vhost_virt= queue *vq, start =3D vq->last_used_idx % vq->num; used =3D vq->used->ring + start; if (count =3D=3D 1) { - if (__put_user(heads[0].id, &used->id)) { + if (__vq_put_user(vq, heads[0].id, &used->id)) { vq_err(vq, "Failed to write used id"); return -EFAULT; } - if (__put_user(heads[0].len, &used->len)) { + if (__vq_put_user(vq, heads[0].len, &used->len)) { vq_err(vq, "Failed to write used len"); return -EFAULT; } - } else if (__copy_to_user(used, heads, count * sizeof *used)) { + } else if (__copyhead_to_user(vq, heads, used, count)) { vq_err(vq, "Failed to write used"); return -EFAULT; } @@ -1500,7 +1528,7 @@ int vhost_add_used_n(struct vhost_virtqueue *vq, = struct vring_used_elem *heads, =20 /* Make sure buffer is written before we update index. */ smp_wmb(); - if (put_user(vq->last_used_idx, &vq->used->idx)) { + if (vq_put_user(vq, vq->last_used_idx, &vq->used->idx)) { vq_err(vq, "Failed to increment used idx"); return -EFAULT; } @@ -1531,7 +1559,7 @@ static bool vhost_notify(struct vhost_dev *dev, s= truct vhost_virtqueue *vq) =20 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) { __u16 flags; - if (__get_user(flags, &vq->avail->flags)) { + if (__vq_get_user(vq, flags, &vq->avail->flags)) { vq_err(vq, "Failed to get flags"); return true; } @@ -1545,7 +1573,7 @@ static bool vhost_notify(struct vhost_dev *dev, s= truct vhost_virtqueue *vq) if (unlikely(!v)) return true; =20 - if (get_user(event, vhost_used_event(vq))) { + if (vq_get_user(vq, event, vhost_used_event(vq))) { vq_err(vq, "Failed to get used event idx"); return true; } @@ -1608,7 +1636,7 @@ bool vhost_enable_notify(struct vhost_dev *dev, s= truct vhost_virtqueue *vq) /* They could have slipped one in as we were doing that: make * sure it's written, then check again. */ smp_mb(); - r =3D __get_user(avail_idx, &vq->avail->idx); + r =3D __vq_get_user(vq, avail_idx, &vq->avail->idx); if (r) { vq_err(vq, "Failed to check avail idx at %p: %d\n", &vq->avail->idx, r); --=20 1.7.10.4