Shannon Nelson
2023-Apr-24 22:50 UTC
[PATCH v2 2/3] vhost: support PACKED when setting-getting vring_base
Use the right structs for PACKED or split vqs when setting and getting the vring base. Signed-off-by: Shannon Nelson <shannon.nelson at amd.com> --- drivers/vhost/vhost.c | 18 +++++++++++++----- drivers/vhost/vhost.h | 8 ++++++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index f11bdbe4c2c5..f64efda48f21 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -1633,17 +1633,25 @@ long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *arg r = -EFAULT; break; } - if (s.num > 0xffff) { - r = -EINVAL; - break; + if (vhost_has_feature(vq, VIRTIO_F_RING_PACKED)) { + vq->last_avail_idx = s.num & 0xffff; + vq->last_used_idx = (s.num >> 16) & 0xffff; + } else { + if (s.num > 0xffff) { + r = -EINVAL; + break; + } + vq->last_avail_idx = s.num; } - vq->last_avail_idx = s.num; /* Forget the cached index value. */ vq->avail_idx = vq->last_avail_idx; break; case VHOST_GET_VRING_BASE: s.index = idx; - s.num = vq->last_avail_idx; + if (vhost_has_feature(vq, VIRTIO_F_RING_PACKED)) + s.num = (u32)vq->last_avail_idx | ((u32)vq->last_used_idx << 16); + else + s.num = vq->last_avail_idx; if (copy_to_user(argp, &s, sizeof s)) r = -EFAULT; break; diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h index 1647b750169c..6f73f29d5979 100644 --- a/drivers/vhost/vhost.h +++ b/drivers/vhost/vhost.h @@ -85,13 +85,17 @@ struct vhost_virtqueue { /* The routine to call when the Guest pings us, or timeout. */ vhost_work_fn_t handle_kick; - /* Last available index we saw. */ + /* Last available index we saw. + * Values are limited to 0x7fff, and the high bit is used as + * a wrap counter when using VIRTIO_F_RING_PACKED. */ u16 last_avail_idx; /* Caches available index value from user. */ u16 avail_idx; - /* Last index we used. */ + /* Last index we used. + * Values are limited to 0x7fff, and the high bit is used as + * a wrap counter when using VIRTIO_F_RING_PACKED. */ u16 last_used_idx; /* Used flags */ -- 2.17.1
Jason Wang
2023-Apr-25 06:01 UTC
[PATCH v2 2/3] vhost: support PACKED when setting-getting vring_base
On Tue, Apr 25, 2023 at 6:50?AM Shannon Nelson <shannon.nelson at amd.com> wrote:> > Use the right structs for PACKED or split vqs when setting and > getting the vring base. > > Signed-off-by: Shannon Nelson <shannon.nelson at amd.com>Acked-by: Jason Wang <jasowang at redhat.com> Thanks> --- > drivers/vhost/vhost.c | 18 +++++++++++++----- > drivers/vhost/vhost.h | 8 ++++++-- > 2 files changed, 19 insertions(+), 7 deletions(-) > > diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c > index f11bdbe4c2c5..f64efda48f21 100644 > --- a/drivers/vhost/vhost.c > +++ b/drivers/vhost/vhost.c > @@ -1633,17 +1633,25 @@ long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *arg > r = -EFAULT; > break; > } > - if (s.num > 0xffff) { > - r = -EINVAL; > - break; > + if (vhost_has_feature(vq, VIRTIO_F_RING_PACKED)) { > + vq->last_avail_idx = s.num & 0xffff; > + vq->last_used_idx = (s.num >> 16) & 0xffff; > + } else { > + if (s.num > 0xffff) { > + r = -EINVAL; > + break; > + } > + vq->last_avail_idx = s.num; > } > - vq->last_avail_idx = s.num; > /* Forget the cached index value. */ > vq->avail_idx = vq->last_avail_idx; > break; > case VHOST_GET_VRING_BASE: > s.index = idx; > - s.num = vq->last_avail_idx; > + if (vhost_has_feature(vq, VIRTIO_F_RING_PACKED)) > + s.num = (u32)vq->last_avail_idx | ((u32)vq->last_used_idx << 16); > + else > + s.num = vq->last_avail_idx; > if (copy_to_user(argp, &s, sizeof s)) > r = -EFAULT; > break; > diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h > index 1647b750169c..6f73f29d5979 100644 > --- a/drivers/vhost/vhost.h > +++ b/drivers/vhost/vhost.h > @@ -85,13 +85,17 @@ struct vhost_virtqueue { > /* The routine to call when the Guest pings us, or timeout. */ > vhost_work_fn_t handle_kick; > > - /* Last available index we saw. */ > + /* Last available index we saw. > + * Values are limited to 0x7fff, and the high bit is used as > + * a wrap counter when using VIRTIO_F_RING_PACKED. */ > u16 last_avail_idx; > > /* Caches available index value from user. */ > u16 avail_idx; > > - /* Last index we used. */ > + /* Last index we used. > + * Values are limited to 0x7fff, and the high bit is used as > + * a wrap counter when using VIRTIO_F_RING_PACKED. */ > u16 last_used_idx; > > /* Used flags */ > -- > 2.17.1 >
Stefano Garzarella
2023-May-09 08:46 UTC
[PATCH v2 2/3] vhost: support PACKED when setting-getting vring_base
On Mon, Apr 24, 2023 at 03:50:30PM -0700, Shannon Nelson via Virtualization wrote:>Use the right structs for PACKED or split vqs when setting and >getting the vring base. > >Signed-off-by: Shannon Nelson <shannon.nelson at amd.com> >--- > drivers/vhost/vhost.c | 18 +++++++++++++----- > drivers/vhost/vhost.h | 8 ++++++-- > 2 files changed, 19 insertions(+), 7 deletions(-) > >diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c >index f11bdbe4c2c5..f64efda48f21 100644 >--- a/drivers/vhost/vhost.c >+++ b/drivers/vhost/vhost.c >@@ -1633,17 +1633,25 @@ long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *arg > r = -EFAULT; > break; > } >- if (s.num > 0xffff) { >- r = -EINVAL; >- break; >+ if (vhost_has_feature(vq, VIRTIO_F_RING_PACKED)) { >+ vq->last_avail_idx = s.num & 0xffff; >+ vq->last_used_idx = (s.num >> 16) & 0xffff; >+ } else { >+ if (s.num > 0xffff) { >+ r = -EINVAL; >+ break; >+ } >+ vq->last_avail_idx = s.num; > } >- vq->last_avail_idx = s.num; > /* Forget the cached index value. */ > vq->avail_idx = vq->last_avail_idx; > break; > case VHOST_GET_VRING_BASE: > s.index = idx; >- s.num = vq->last_avail_idx; >+ if (vhost_has_feature(vq, VIRTIO_F_RING_PACKED)) >+ s.num = (u32)vq->last_avail_idx | ((u32)vq->last_used_idx << 16); >+ else >+ s.num = vq->last_avail_idx;The changes LGTM, but since we are changing the UAPI, should we update the documentation of VHOST_SET_VRING_BASE and VHOST_GET_VRING_BASE in include/uapi/linux/vhost.h? Thanks, Stefano