Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] virtio_ring: 4-byte WRITE_ONCE() on u16 last_used_idx in
  tools/virtio
- [Medium] virtio_ring: virtqueue_get_last_used_idx() misses in-order
  batch buffers
- [Medium] virtio: virtqueue_get_last_used_idx() ignores maintainer API
  objection
--

commit e3f548bc724c634761028f08e7565898a68423a0
Author: Longjun Tang <[email protected]>

virtio: add virtqueue_get_last_used_idx() helper

This commit adds a virtqueue_get_last_used_idx() helper to expose
vring_virtqueue's last_used_idx and converts the split-ring updates of
last_used_idx to use WRITE_ONCE().

> diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> index f683a2d6a2002..e8af6d5c6432b 100644
> --- a/drivers/virtio/virtio_ring.c
> +++ b/drivers/virtio/virtio_ring.c
> @@ -1005,7 +1005,7 @@ static void *virtqueue_get_buf_ctx_split(struct 
> vring_virtqueue *vq,
>       /* detach_buf_split clears data, so grab it now. */
>       ret = vq->split.desc_state[i].data;
>       detach_buf_split(vq, i, ctx);
> -     vq->last_used_idx++;
> +     WRITE_ONCE(vq->last_used_idx, vq->last_used_idx + 1);
>       /* If we expect an interrupt for the next entry, tell host
>        * by writing event index and flush out the write before
>        * the read in the next get_buf call. */
> @@ -1072,7 +1072,7 @@ static void 
> *virtqueue_get_buf_ctx_split_in_order(struct vring_virtqueue *vq,
>       ret = vq->split.desc_state[last_used].data;
>       detach_buf_split_in_order(vq, last_used, ctx);
>  
> -     vq->last_used_idx++;
> +     WRITE_ONCE(vq->last_used_idx, vq->last_used_idx + 1);
>       vq->last_used += (vq->vq.num_free - num_free);

[Severity: Medium]
Does WRITE_ONCE(vq->last_used_idx, vq->last_used_idx + 1) perform a 4-byte
store into the 2-byte u16 last_used_idx field when compiled under
tools/virtio/?

Because vq->last_used_idx + 1 undergoes integer promotion to int, the
userspace WRITE_ONCE() stub in tools/virtio/linux/compiler.h casts the
destination pointer to typeof(val) * (4 bytes):

tools/virtio/linux/compiler.h:
#define WRITE_ONCE(var, val) \
        (*((volatile typeof(val) *)(&(var))) = (val))

In struct vring_virtqueue, u16 last_used_idx is immediately followed by
u16 last_used, so a 4-byte write clobbers vq->last_used right before
virtqueue_get_buf_ctx_split_in_order() increments it (and on big-endian
hosts writes zero into vq->last_used_idx in virtqueue_get_buf_ctx_split()).
Casting the value to u16 (or fixing the tools/virtio/ macro to use
typeof(var)) avoids the out-of-bounds write.

> @@ -3236,6 +3236,20 @@ bool virtqueue_poll(struct virtqueue *_vq, unsigned 
> int last_used_idx)
>  }
>  EXPORT_SYMBOL_GPL(virtqueue_poll);
>  
> +/**
> + * virtqueue_get_last_used_idx - get the ring's last consumed position
> + * @_vq: the struct virtqueue we're talking about.
> + *
> + * Returns: the vring_virtqueue's last_used_idx.
> + */
> +unsigned int virtqueue_get_last_used_idx(const struct virtqueue *_vq)
> +{
> +     const struct vring_virtqueue *vq = to_vvq(_vq);
> +
> +     return READ_ONCE(vq->last_used_idx);
> +}
> +EXPORT_SYMBOL_GPL(virtqueue_get_last_used_idx);

[Severity: Medium]
Can passing virtqueue_get_last_used_idx() to virtqueue_poll() falsely
report no pending used buffers when VIRTIO_F_IN_ORDER is negotiated?

When a device completes a multi-buffer batch in order, it only writes the
batch's single used entry (or the first descriptor on a packed ring), and
virtqueue_get_buf_ctx_{split,packed}_in_order() caches the end of the batch
in vq->batch_last.id while advancing vq->last_used_idx on each detached
buffer.  If the driver stops consuming mid-batch (for example, when NAPI
exhausts its budget), vq->batch_last.id is not UINT_MAX, yet
virtqueue_poll() only inspects the ring at last_used_idx where the device
wrote no descriptor flag or used entry, returning false while unconsumed
buffers remain in the batch:

drivers/virtio/virtio_ring.c:more_used_packed_in_order() {
        if (vq->batch_last.id != UINT_MAX)
                return true;

        return virtqueue_poll_packed(vq, READ_ONCE(vq->last_used_idx));
}

[Severity: Medium]
As reported by Michael S. Tsirkin on v1 [1], is exposing last_used_idx
through virtqueue_get_last_used_idx() an appropriate public virtio API given
that packed virtqueues do not have a used ring and encode both the
descriptor index and VRING_PACKED_EVENT_F_WRAP_CTR wrap counter in
vq->last_used_idx?

[1] https://lore.kernel.org/r/[email protected]

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=1

Reply via email to