* [PATCH] vhost: wrap the in-order used ring log index with a mask too
@ 2026-12-01 11:27 Peng Hao
2026-12-01 12:29 ` Michael S. Tsirkin
0 siblings, 1 reply; 2+ messages in thread
From: Peng Hao @ 2026-12-01 11:27 UTC (permalink / raw)
To: mst, jasowangio; +Cc: kvm
Commit e244725e86be ("vhost: fix used ring index wrap in
vhost_add_used_n_in_order") made the loop that writes the used ring
entries wrap start with a mask, because start + nheads[i] can reach
2 * vq->num, e.g. on the vhost-net TX path where done_idx can reach
VHOST_NET_BATCH (64) with a 32-entry ring.
The loop that logs those entries for dirty tracking, added just before
by commit bfcde6b64769 ("vhost: fix dirty logging for in-order used ring
entries"), walks the same sequence of positions but still wraps with a
single subtraction:
next = start + nheads[i];
if (next >= vq->num)
next -= vq->num;
so with the same inputs next stays at or above vq->num. The following
run is then logged at vq->used->ring + log_start with log_start past the
end of the ring, and the entries that were actually written after the
wrap are not logged. With migration in progress that leaves stale used
ring entries on the destination, and log_used() can set bits for guest
memory beyond the used ring.
Use the same mask as the write loop. VHOST_SET_VRING_NUM guarantees
vq->num is a power of two.
Fixes: bfcde6b64769 ("vhost: fix dirty logging for in-order used ring entries")
Signed-off-by: Peng Hao <flyingpeng@tencent.com>
---
drivers/vhost/vhost.c | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index dc97328756..80b887e151 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -3206,9 +3206,7 @@ static int vhost_add_used_n_in_order(struct vhost_virtqueue *vq,
log_start = start;
log_count++;
- next = start + nheads[i];
- if (next >= vq->num)
- next -= vq->num;
+ next = (start + nheads[i]) & (vq->num - 1);
if (i + 1 == count || next != start + 1) {
used = vq->used->ring + log_start;
log_used(vq,
--
2.43.7
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] vhost: wrap the in-order used ring log index with a mask too
2026-12-01 11:27 [PATCH] vhost: wrap the in-order used ring log index with a mask too Peng Hao
@ 2026-12-01 12:29 ` Michael S. Tsirkin
0 siblings, 0 replies; 2+ messages in thread
From: Michael S. Tsirkin @ 2026-12-01 12:29 UTC (permalink / raw)
To: Peng Hao; +Cc: jasowangio, kvm
On Tue, Dec 01, 2026 at 07:27:33PM +0800, Peng Hao wrote:
> Commit e244725e86be ("vhost: fix used ring index wrap in
> vhost_add_used_n_in_order") made the loop that writes the used ring
> entries wrap start with a mask, because start + nheads[i] can reach
> 2 * vq->num, e.g. on the vhost-net TX path where done_idx can reach
> VHOST_NET_BATCH (64) with a 32-entry ring.
>
> The loop that logs those entries for dirty tracking, added just before
> by commit bfcde6b64769 ("vhost: fix dirty logging for in-order used ring
> entries"), walks the same sequence of positions but still wraps with a
> single subtraction:
>
> next = start + nheads[i];
> if (next >= vq->num)
> next -= vq->num;
>
> so with the same inputs next stays at or above vq->num. The following
> run is then logged at vq->used->ring + log_start with log_start past the
> end of the ring, and the entries that were actually written after the
> wrap are not logged. With migration in progress that leaves stale used
> ring entries on the destination, and log_used() can set bits for guest
> memory beyond the used ring.
>
> Use the same mask as the write loop. VHOST_SET_VRING_NUM guarantees
> vq->num is a power of two.
>
> Fixes: bfcde6b64769 ("vhost: fix dirty logging for in-order used ring entries")
> Signed-off-by: Peng Hao <flyingpeng@tencent.com>
Yea, this was reported already. But it's broken, isn't it? There's no
reason to allow this behaviour? I was testing this patch - does the
following fix your issue? Works for you? Testing appreciated.
--->
vhost: reject used ring batches larger than the ring
A compliant driver never has more than vq->num buffers in flight at
once, so a batch accumulated before used->idx advances never exceeds
vq->num.
Reject used count > vq->num in
vhost_add_used_n()/vhost_add_used_n_in_order().
Cc: stable@vger.kernel.org
Reported-by: Seohyeon Maeng <bioloidgp@gmail.com>
Reported-by: minki@morningstarsec.dev
Reported-by: YanXin Li <fadouse@pm.me>
Reported-by: Peng Hao <flyingpenghao@gmail.com>
Fixes: e2b3b35eb989 ("vhost_net: batch used ring update in rx")
Fixes: 4afb52c2af44 ("vhost_net: batch update used ring for datacopy TX")
Fixes: bfcde6b64769 ("vhost: fix dirty logging for in-order used ring entries")
Cc: Jason Wang <jasowangio@gmail.com>
Cc: Eugenio Pérez <eperezma@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index d1831f4e09981..a00151932e4e5 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -3181,6 +3181,7 @@ static int vhost_add_used_n_in_order(struct vhost_virtqueue *vq,
{
vring_used_elem_t __user *used;
u16 old, new = vq->last_used_idx;
+ unsigned int total = 0;
int start, i;
if (!nheads)
@@ -3189,6 +3190,12 @@ static int vhost_add_used_n_in_order(struct vhost_virtqueue *vq,
start = vq->last_used_idx & (vq->num - 1);
for (i = 0; i < count; i++) {
+ total += nheads[i];
+ if (unlikely(total > vq->num)) {
+ vq_err(vq, "Added %u > %u used heads\n",
+ total, vq->num);
+ return -EINVAL;
+ }
if (vhost_put_used(vq, &heads[i], start, 1)) {
vq_err(vq, "Failed to write used");
return -EFAULT;
@@ -3245,6 +3252,11 @@ int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
bool in_order = vhost_has_feature(vq, VIRTIO_F_IN_ORDER);
int r;
+ if (unlikely(count > vq->num)) {
+ vq_err(vq, "Added %u > %u used entries\n", count, vq->num);
+ return -EINVAL;
+ }
+
if (!in_order || !nheads)
r = vhost_add_used_n_ooo(vq, heads, count);
else
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-01 12:29 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 11:27 [PATCH] vhost: wrap the in-order used ring log index with a mask too Peng Hao
2026-12-01 12:29 ` Michael S. Tsirkin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox