Skip to content

Commit cef2586

Browse files
mikechristiemstsirkin
authored andcommitted
vhost: add helper to parse userspace vring state/file
The next patches add new vhost worker ioctls which will need to get a vhost_virtqueue from a userspace struct which specifies the vq's index. This moves the vhost_vring_ioctl code to do this to a helper so it can be shared. Signed-off-by: Mike Christie <michael.christie@oracle.com> Message-Id: <20230626232307.97930-14-michael.christie@oracle.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
1 parent 27eca18 commit cef2586

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

drivers/vhost/vhost.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,27 @@ static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev)
599599
return NULL;
600600
}
601601

602+
static int vhost_get_vq_from_user(struct vhost_dev *dev, void __user *argp,
603+
struct vhost_virtqueue **vq, u32 *id)
604+
{
605+
u32 __user *idxp = argp;
606+
u32 idx;
607+
long r;
608+
609+
r = get_user(idx, idxp);
610+
if (r < 0)
611+
return r;
612+
613+
if (idx >= dev->nvqs)
614+
return -ENOBUFS;
615+
616+
idx = array_index_nospec(idx, dev->nvqs);
617+
618+
*vq = dev->vqs[idx];
619+
*id = idx;
620+
return 0;
621+
}
622+
602623
/* Caller should have device mutex */
603624
long vhost_dev_set_owner(struct vhost_dev *dev)
604625
{
@@ -1618,21 +1639,15 @@ long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *arg
16181639
struct file *eventfp, *filep = NULL;
16191640
bool pollstart = false, pollstop = false;
16201641
struct eventfd_ctx *ctx = NULL;
1621-
u32 __user *idxp = argp;
16221642
struct vhost_virtqueue *vq;
16231643
struct vhost_vring_state s;
16241644
struct vhost_vring_file f;
16251645
u32 idx;
16261646
long r;
16271647

1628-
r = get_user(idx, idxp);
1648+
r = vhost_get_vq_from_user(d, argp, &vq, &idx);
16291649
if (r < 0)
16301650
return r;
1631-
if (idx >= d->nvqs)
1632-
return -ENOBUFS;
1633-
1634-
idx = array_index_nospec(idx, d->nvqs);
1635-
vq = d->vqs[idx];
16361651

16371652
if (ioctl == VHOST_SET_VRING_NUM ||
16381653
ioctl == VHOST_SET_VRING_ADDR) {

0 commit comments

Comments
 (0)