[PATCH] virtio: Reset free_head on shadow virtqueue start

lirongqing posted 1 patch 2 weeks, 5 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261104020053.2548-1-lirongqing@baidu.com
Maintainers: "Eugenio Pérez" <eperezma@redhat.com>, "Michael S. Tsirkin" <mst@redhat.com>, Stefano Garzarella <sgarzare@redhat.com>
hw/virtio/vhost-shadow-virtqueue.c | 1 +
1 file changed, 1 insertion(+)
[PATCH] virtio: Reset free_head on shadow virtqueue start
Posted by lirongqing 2 weeks, 5 days ago
From: Li RongQing <lirongqing@baidu.com>

free_head tracks the index of the next free descriptor in the SVQ
descriptor ring.  It is used by vhost_svq_vring_write_descs() to
choose where to place new descriptors, and is updated as descriptors
are allocated and recycled.

free_head is zero-initialized when the SVQ is created through
vhost_svq_new() (g_new0), but it is not reset in vhost_svq_start().
The start function already resets the rest of the ring state
(shadow_avail_idx, shadow_used_idx, last_used, last_used_idx and
num_free), yet free_head is left untouched.

Since an SVQ can be stopped and restarted on the same object, for
example across vDPA device migration or control virtqueue processing,
a stale free_head from the previous run survives into the new one.
This causes new descriptors to be written at the wrong position of
the SVQ descriptor ring, corrupting it.

Reset free_head to 0 in vhost_svq_start() so that descriptor
allocation always begins from the head of a fresh ring.

Fixes: 100890f7cad5 ("vhost: Shadow virtqueue buffers forwarding")
Signed-off-by: Li RongQing <lirongqing@baidu.com>
---
 hw/virtio/vhost-shadow-virtqueue.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-virtqueue.c
index c97bd9f..462c86d 100644
--- a/hw/virtio/vhost-shadow-virtqueue.c
+++ b/hw/virtio/vhost-shadow-virtqueue.c
@@ -857,6 +857,7 @@ void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev,
     svq->shadow_used_idx = 0;
     memset(&svq->batch_last, 0, sizeof(svq->batch_last));
     svq->last_used = 0;
+    svq->free_head = 0;
     svq->last_used_idx = 0;
     svq->vdev = vdev;
     svq->vq = vq;
-- 
2.9.4
Re: [PATCH] virtio: Reset free_head on shadow virtqueue start
Posted by Michael Tokarev 2 days, 20 hours ago
On 11/4/26 05:00, lirongqing wrote:
> From: Li RongQing <lirongqing@baidu.com>
> 
> free_head tracks the index of the next free descriptor in the SVQ
> descriptor ring.  It is used by vhost_svq_vring_write_descs() to
> choose where to place new descriptors, and is updated as descriptors
> are allocated and recycled.
> 
> free_head is zero-initialized when the SVQ is created through
> vhost_svq_new() (g_new0), but it is not reset in vhost_svq_start().
> The start function already resets the rest of the ring state
> (shadow_avail_idx, shadow_used_idx, last_used, last_used_idx and
> num_free), yet free_head is left untouched.
> 
> Since an SVQ can be stopped and restarted on the same object, for
> example across vDPA device migration or control virtqueue processing,
> a stale free_head from the previous run survives into the new one.
> This causes new descriptors to be written at the wrong position of
> the SVQ descriptor ring, corrupting it.
> 
> Reset free_head to 0 in vhost_svq_start() so that descriptor
> allocation always begins from the head of a fresh ring.
> 
> Fixes: 100890f7cad5 ("vhost: Shadow virtqueue buffers forwarding")
> Signed-off-by: Li RongQing <lirongqing@baidu.com>
> ---
>   hw/virtio/vhost-shadow-virtqueue.c | 1 +
>   1 file changed, 1 insertion(+)

That smells like a qemu-stable material, is it not?

Thanks,

/mjt

> diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-virtqueue.c
> index c97bd9f..462c86d 100644
> --- a/hw/virtio/vhost-shadow-virtqueue.c
> +++ b/hw/virtio/vhost-shadow-virtqueue.c
> @@ -857,6 +857,7 @@ void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev,
>       svq->shadow_used_idx = 0;
>       memset(&svq->batch_last, 0, sizeof(svq->batch_last));
>       svq->last_used = 0;
> +    svq->free_head = 0;
>       svq->last_used_idx = 0;
>       svq->vdev = vdev;
>       svq->vq = vq;
Re: [PATCH] virtio: Reset free_head on shadow virtqueue start
Posted by Eugenio Perez Martin 2 days, 17 hours ago
On Fri, Nov 20, 2026 at 9:13 AM Michael Tokarev <mjt@tls.msk.ru> wrote:
>
> On 11/4/26 05:00, lirongqing wrote:
> > From: Li RongQing <lirongqing@baidu.com>
> >
> > free_head tracks the index of the next free descriptor in the SVQ
> > descriptor ring.  It is used by vhost_svq_vring_write_descs() to
> > choose where to place new descriptors, and is updated as descriptors
> > are allocated and recycled.
> >
> > free_head is zero-initialized when the SVQ is created through
> > vhost_svq_new() (g_new0), but it is not reset in vhost_svq_start().
> > The start function already resets the rest of the ring state
> > (shadow_avail_idx, shadow_used_idx, last_used, last_used_idx and
> > num_free), yet free_head is left untouched.
> >
> > Since an SVQ can be stopped and restarted on the same object, for
> > example across vDPA device migration or control virtqueue processing,
> > a stale free_head from the previous run survives into the new one.
> > This causes new descriptors to be written at the wrong position of
> > the SVQ descriptor ring, corrupting it.
> >
> > Reset free_head to 0 in vhost_svq_start() so that descriptor
> > allocation always begins from the head of a fresh ring.
> >
> > Fixes: 100890f7cad5 ("vhost: Shadow virtqueue buffers forwarding")
> > Signed-off-by: Li RongQing <lirongqing@baidu.com>
> > ---
> >   hw/virtio/vhost-shadow-virtqueue.c | 1 +
> >   1 file changed, 1 insertion(+)
>
> That smells like a qemu-stable material, is it not?
>

Right, thanks for noticing that!

> Thanks,
>
> /mjt
>
> > diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-virtqueue.c
> > index c97bd9f..462c86d 100644
> > --- a/hw/virtio/vhost-shadow-virtqueue.c
> > +++ b/hw/virtio/vhost-shadow-virtqueue.c
> > @@ -857,6 +857,7 @@ void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev,
> >       svq->shadow_used_idx = 0;
> >       memset(&svq->batch_last, 0, sizeof(svq->batch_last));
> >       svq->last_used = 0;
> > +    svq->free_head = 0;
> >       svq->last_used_idx = 0;
> >       svq->vdev = vdev;
> >       svq->vq = vq;
>
>
Re: [PATCH] virtio: Reset free_head on shadow virtqueue start
Posted by Eugenio Perez Martin 2 weeks, 4 days ago
On Wed, Nov 4, 2026 at 3:01 AM lirongqing <lirongqing@baidu.com> wrote:
>
> From: Li RongQing <lirongqing@baidu.com>
>
> free_head tracks the index of the next free descriptor in the SVQ
> descriptor ring.  It is used by vhost_svq_vring_write_descs() to
> choose where to place new descriptors, and is updated as descriptors
> are allocated and recycled.
>
> free_head is zero-initialized when the SVQ is created through
> vhost_svq_new() (g_new0), but it is not reset in vhost_svq_start().
> The start function already resets the rest of the ring state
> (shadow_avail_idx, shadow_used_idx, last_used, last_used_idx and
> num_free), yet free_head is left untouched.
>
> Since an SVQ can be stopped and restarted on the same object, for
> example across vDPA device migration or control virtqueue processing,
> a stale free_head from the previous run survives into the new one.
> This causes new descriptors to be written at the wrong position of
> the SVQ descriptor ring, corrupting it.
>
> Reset free_head to 0 in vhost_svq_start() so that descriptor
> allocation always begins from the head of a fresh ring.
>
> Fixes: 100890f7cad5 ("vhost: Shadow virtqueue buffers forwarding")

Reviewed-by: Eugenio Pérez <eperezma@redhat.com>

Thanks!

> Signed-off-by: Li RongQing <lirongqing@baidu.com>
> ---
>  hw/virtio/vhost-shadow-virtqueue.c | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-virtqueue.c
> index c97bd9f..462c86d 100644
> --- a/hw/virtio/vhost-shadow-virtqueue.c
> +++ b/hw/virtio/vhost-shadow-virtqueue.c
> @@ -857,6 +857,7 @@ void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev,
>      svq->shadow_used_idx = 0;
>      memset(&svq->batch_last, 0, sizeof(svq->batch_last));
>      svq->last_used = 0;
> +    svq->free_head = 0;
>      svq->last_used_idx = 0;
>      svq->vdev = vdev;
>      svq->vq = vq;
> --
> 2.9.4
>