From: sashiko-bot@kernel.org
To: "Longjun Tang" <lange_tang@163.com>
Cc: virtualization@lists.linux.dev,
"Michael S. Tsirkin" <mst@redhat.com>,
"Eugenio Perez" <eperezma@redhat.com>
Subject: Re: [PATCH v2 1/3] virtio: add virtqueue_get_last_used_idx() helper
Date: Sat, 21 Nov 2026 07:18:57 +0000 [thread overview]
Message-ID: <sashiko-outbox-226756@kernel.org> (raw)
In-Reply-To: <20261120071826.198604-2-lange_tang@163.com>
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 <tanglongjun@kylinos.cn>
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/20261112174936-mutt-send-email-mst@kernel.org
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261120071826.198604-1-lange_tang@163.com?part=1
next prev parent reply other threads:[~2026-11-21 7:18 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 7:18 [PATCH v2 0/3] virtio_net: introduce an RX queue watchdog for stuck detection Longjun Tang
2026-11-20 7:18 ` [PATCH v2 1/3] virtio: add virtqueue_get_last_used_idx() helper Longjun Tang
2026-11-20 17:43 ` Michael S. Tsirkin
2026-11-21 7:18 ` sashiko-bot [this message]
2026-11-20 7:18 ` [PATCH v2 2/3] virtio_net: introduce an RX queue watchdog for stuck detection Longjun Tang
2026-11-21 7:18 ` sashiko-bot
2026-11-20 7:18 ` [PATCH v2 3/3] virtio_net: add stalls per-queue statistic Longjun Tang
2026-11-21 7:18 ` sashiko-bot
2026-11-20 17:45 ` [PATCH v2 0/3] virtio_net: introduce an RX queue watchdog for stuck detection Michael S. Tsirkin
2026-11-24 8:41 ` Lange Tang
2026-11-21 8:27 ` Jiaming Fan
2026-11-24 8:43 ` Lange Tang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-226756@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=eperezma@redhat.com \
--cc=lange_tang@163.com \
--cc=mst@redhat.com \
--cc=sashiko-reviews@lists.linux.dev \
--cc=virtualization@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox