* [PATCH] virtio_ring: reset IN_ORDER state in virtqueue_init()
@ 2026-09-10 12:39 lirongqing
2026-09-11 2:17 ` Jason Wang
0 siblings, 1 reply; 7+ messages in thread
From: lirongqing @ 2026-09-10 12:39 UTC (permalink / raw)
To: Michael S . Tsirkin, Jason Wang, Eugenio Pérez, Xuan Zhuo,
virtualization, linux-kernel
Cc: Li RongQing
From: Li RongQing <lirongqing@baidu.com>
The IN_ORDER feature uses additional software state to track the next
available descriptor and in-flight descriptor batches.
virtqueue_init() resets the common virtqueue state, but free_head and
batch_last.id are initialized only when the virtqueue is created. When
an IN_ORDER virtqueue is reset, these fields can retain state from the
previous queue instance.
Reset free_head and invalidate batch_last when initializing an IN_ORDER
virtqueue. Keep this in virtqueue_init() so the state is initialized
consistently for both newly created and reset virtqueues.
Fixes: f6a15d854986 ("virtio_ring: add in order support")
Signed-off-by: Li RongQing <lirongqing@baidu.com>
---
drivers/virtio/virtio_ring.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
index db678f5..d0dc464 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -559,6 +559,11 @@ static void virtqueue_init(struct vring_virtqueue *vq, u32 num)
vq->in_use = false;
vq->last_add_time_valid = false;
#endif
+
+ if (virtqueue_is_in_order(vq)) {
+ vq->free_head = 0;
+ vq->batch_last.id = UINT_MAX;
+ }
}
--
2.9.4
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] virtio_ring: reset IN_ORDER state in virtqueue_init()
2026-09-10 12:39 [PATCH] virtio_ring: reset IN_ORDER state in virtqueue_init() lirongqing
@ 2026-09-11 2:17 ` Jason Wang
2026-09-11 6:05 ` 答复: [外部邮件] " Li,Rongqing
0 siblings, 1 reply; 7+ messages in thread
From: Jason Wang @ 2026-09-11 2:17 UTC (permalink / raw)
To: lirongqing
Cc: Michael S . Tsirkin, Eugenio Pérez, Xuan Zhuo,
virtualization, linux-kernel
On Thu, Sep 10, 2026 at 8:39 PM lirongqing <lirongqing@baidu.com> wrote:
>
> From: Li RongQing <lirongqing@baidu.com>
>
> The IN_ORDER feature uses additional software state to track the next
> available descriptor and in-flight descriptor batches.
>
> virtqueue_init() resets the common virtqueue state, but free_head and
> batch_last.id are initialized only when the virtqueue is created. When
> an IN_ORDER virtqueue is reset, these fields can retain state from the
> previous queue instance.
>
> Reset free_head and invalidate batch_last when initializing an IN_ORDER
> virtqueue. Keep this in virtqueue_init() so the state is initialized
> consistently for both newly created and reset virtqueues.
>
> Fixes: f6a15d854986 ("virtio_ring: add in order support")
I think this is needed for -stable.
> Signed-off-by: Li RongQing <lirongqing@baidu.com>
> ---
> drivers/virtio/virtio_ring.c | 5 +++++
> 1 file changed, 5 insertions(+)
>
> diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> index db678f5..d0dc464 100644
> --- a/drivers/virtio/virtio_ring.c
> +++ b/drivers/virtio/virtio_ring.c
> @@ -559,6 +559,11 @@ static void virtqueue_init(struct vring_virtqueue *vq, u32 num)
> vq->in_use = false;
> vq->last_add_time_valid = false;
> #endif
> +
> + if (virtqueue_is_in_order(vq)) {
> + vq->free_head = 0;
> + vq->batch_last.id = UINT_MAX;
> + }
I would try to reuse or refactor virtqueue_vring_attach_split() to
reduce code duplication.
Btw have you checked Qemu shadow virtqueue as it might suffer from the
same issue.
Thanks
> }
>
>
> --
> 2.9.4
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* 答复: [外部邮件] Re: [PATCH] virtio_ring: reset IN_ORDER state in virtqueue_init()
2026-09-11 2:17 ` Jason Wang
@ 2026-09-11 6:05 ` Li,Rongqing
2026-11-02 11:23 ` Michael S. Tsirkin
2026-11-03 9:36 ` Eugenio Perez Martin
0 siblings, 2 replies; 7+ messages in thread
From: Li,Rongqing @ 2026-09-11 6:05 UTC (permalink / raw)
To: Jason Wang
Cc: Michael S . Tsirkin, Eugenio Pérez, Xuan Zhuo,
virtualization, linux-kernel
> >
> > The IN_ORDER feature uses additional software state to track the next
> > available descriptor and in-flight descriptor batches.
> >
> > virtqueue_init() resets the common virtqueue state, but free_head and
> > batch_last.id are initialized only when the virtqueue is created. When
> > an IN_ORDER virtqueue is reset, these fields can retain state from the
> > previous queue instance.
> >
> > Reset free_head and invalidate batch_last when initializing an
> > IN_ORDER virtqueue. Keep this in virtqueue_init() so the state is
> > initialized consistently for both newly created and reset virtqueues.
> >
> > Fixes: f6a15d854986 ("virtio_ring: add in order support")
>
> I think this is needed for -stable.
>
> > Signed-off-by: Li RongQing <lirongqing@baidu.com>
> > ---
> > drivers/virtio/virtio_ring.c | 5 +++++
> > 1 file changed, 5 insertions(+)
> >
> > diff --git a/drivers/virtio/virtio_ring.c
> > b/drivers/virtio/virtio_ring.c index db678f5..d0dc464 100644
> > --- a/drivers/virtio/virtio_ring.c
> > +++ b/drivers/virtio/virtio_ring.c
> > @@ -559,6 +559,11 @@ static void virtqueue_init(struct vring_virtqueue *vq,
> u32 num)
> > vq->in_use = false;
> > vq->last_add_time_valid = false; #endif
> > +
> > + if (virtqueue_is_in_order(vq)) {
> > + vq->free_head = 0;
> > + vq->batch_last.id = UINT_MAX;
> > + }
>
> I would try to reuse or refactor virtqueue_vring_attach_split() to reduce code
> duplication.
>
> Btw have you checked Qemu shadow virtqueue as it might suffer from the same
> issue.
>
I checked QEMU's shadow virtqueue implementation. batch_last is reinitialized in vhost_svq_start(),
but free_head is not. Since the SVQ object can be reused across stop/start, free_head may retain stale
state. So a corresponding fix should be needed
void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev,
VirtQueue *vq, VhostIOVATree *iova_tree)
{
...
svq->shadow_avail_idx = 0;
svq->shadow_used_idx = 0;
+ svq->free_head = 0;
memset(&svq->batch_last, 0, sizeof(svq->batch_last));
svq->last_used = 0;
svq->last_used_idx = 0;
thanks
[Li,Rongqing]
> Thanks
>
> > }
> >
> >
> > --
> > 2.9.4
> >
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: 答复: [外部邮件] Re: [PATCH] virtio_ring: reset IN_ORDER state in virtqueue_init()
2026-09-11 6:05 ` 答复: [外部邮件] " Li,Rongqing
@ 2026-11-02 11:23 ` Michael S. Tsirkin
2026-11-03 9:36 ` Eugenio Perez Martin
1 sibling, 0 replies; 7+ messages in thread
From: Michael S. Tsirkin @ 2026-11-02 11:23 UTC (permalink / raw)
To: Li,Rongqing
Cc: Jason Wang, Eugenio Pérez, Xuan Zhuo, virtualization, linux-kernel
Eugenio, could you look at the qemu side of this pls?
On Fri, Sep 11, 2026 at 06:05:25AM +0000, Li,Rongqing wrote:
> > >
> > > The IN_ORDER feature uses additional software state to track the next
> > > available descriptor and in-flight descriptor batches.
> > >
> > > virtqueue_init() resets the common virtqueue state, but free_head and
> > > batch_last.id are initialized only when the virtqueue is created. When
> > > an IN_ORDER virtqueue is reset, these fields can retain state from the
> > > previous queue instance.
> > >
> > > Reset free_head and invalidate batch_last when initializing an
> > > IN_ORDER virtqueue. Keep this in virtqueue_init() so the state is
> > > initialized consistently for both newly created and reset virtqueues.
> > >
> > > Fixes: f6a15d854986 ("virtio_ring: add in order support")
> >
> > I think this is needed for -stable.
> >
> > > Signed-off-by: Li RongQing <lirongqing@baidu.com>
> > > ---
> > > drivers/virtio/virtio_ring.c | 5 +++++
> > > 1 file changed, 5 insertions(+)
> > >
> > > diff --git a/drivers/virtio/virtio_ring.c
> > > b/drivers/virtio/virtio_ring.c index db678f5..d0dc464 100644
> > > --- a/drivers/virtio/virtio_ring.c
> > > +++ b/drivers/virtio/virtio_ring.c
> > > @@ -559,6 +559,11 @@ static void virtqueue_init(struct vring_virtqueue *vq,
> > u32 num)
> > > vq->in_use = false;
> > > vq->last_add_time_valid = false; #endif
> > > +
> > > + if (virtqueue_is_in_order(vq)) {
> > > + vq->free_head = 0;
> > > + vq->batch_last.id = UINT_MAX;
> > > + }
> >
> > I would try to reuse or refactor virtqueue_vring_attach_split() to reduce code
> > duplication.
> >
> > Btw have you checked Qemu shadow virtqueue as it might suffer from the same
> > issue.
> >
>
> I checked QEMU's shadow virtqueue implementation. batch_last is reinitialized in vhost_svq_start(),
> but free_head is not. Since the SVQ object can be reused across stop/start, free_head may retain stale
> state. So a corresponding fix should be needed
>
> void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev,
> VirtQueue *vq, VhostIOVATree *iova_tree)
> {
> ...
> svq->shadow_avail_idx = 0;
> svq->shadow_used_idx = 0;
> + svq->free_head = 0;
> memset(&svq->batch_last, 0, sizeof(svq->batch_last));
> svq->last_used = 0;
> svq->last_used_idx = 0;
>
> thanks
>
> [Li,Rongqing]
>
>
> > Thanks
> >
> > > }
> > >
> > >
> > > --
> > > 2.9.4
> > >
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [外部邮件] Re: [PATCH] virtio_ring: reset IN_ORDER state in virtqueue_init()
2026-09-11 6:05 ` 答复: [外部邮件] " Li,Rongqing
2026-11-02 11:23 ` Michael S. Tsirkin
@ 2026-11-03 9:36 ` Eugenio Perez Martin
2026-11-03 9:41 ` Michael S. Tsirkin
1 sibling, 1 reply; 7+ messages in thread
From: Eugenio Perez Martin @ 2026-11-03 9:36 UTC (permalink / raw)
To: Li,Rongqing
Cc: Jason Wang, Michael S . Tsirkin, Xuan Zhuo, virtualization, linux-kernel
On Fri, Sep 11, 2026 at 8:05 AM Li,Rongqing <lirongqing@baidu.com> wrote:
>
> > >
> > > The IN_ORDER feature uses additional software state to track the next
> > > available descriptor and in-flight descriptor batches.
> > >
> > > virtqueue_init() resets the common virtqueue state, but free_head and
> > > batch_last.id are initialized only when the virtqueue is created. When
> > > an IN_ORDER virtqueue is reset, these fields can retain state from the
> > > previous queue instance.
> > >
> > > Reset free_head and invalidate batch_last when initializing an
> > > IN_ORDER virtqueue. Keep this in virtqueue_init() so the state is
> > > initialized consistently for both newly created and reset virtqueues.
> > >
> > > Fixes: f6a15d854986 ("virtio_ring: add in order support")
> >
> > I think this is needed for -stable.
> >
> > > Signed-off-by: Li RongQing <lirongqing@baidu.com>
> > > ---
> > > drivers/virtio/virtio_ring.c | 5 +++++
> > > 1 file changed, 5 insertions(+)
> > >
> > > diff --git a/drivers/virtio/virtio_ring.c
> > > b/drivers/virtio/virtio_ring.c index db678f5..d0dc464 100644
> > > --- a/drivers/virtio/virtio_ring.c
> > > +++ b/drivers/virtio/virtio_ring.c
> > > @@ -559,6 +559,11 @@ static void virtqueue_init(struct vring_virtqueue *vq,
> > u32 num)
> > > vq->in_use = false;
> > > vq->last_add_time_valid = false; #endif
> > > +
> > > + if (virtqueue_is_in_order(vq)) {
> > > + vq->free_head = 0;
> > > + vq->batch_last.id = UINT_MAX;
> > > + }
> >
> > I would try to reuse or refactor virtqueue_vring_attach_split() to reduce code
> > duplication.
> >
> > Btw have you checked Qemu shadow virtqueue as it might suffer from the same
> > issue.
> >
>
> I checked QEMU's shadow virtqueue implementation. batch_last is reinitialized in vhost_svq_start(),
> but free_head is not. Since the SVQ object can be reused across stop/start, free_head may retain stale
> state. So a corresponding fix should be needed
>
> void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev,
> VirtQueue *vq, VhostIOVATree *iova_tree)
> {
> ...
> svq->shadow_avail_idx = 0;
> svq->shadow_used_idx = 0;
> + svq->free_head = 0;
> memset(&svq->batch_last, 0, sizeof(svq->batch_last));
> svq->last_used = 0;
> svq->last_used_idx = 0;
>
I'll ack this patch if it is sent to the QEMU mailing list, but the
guest is not able to reset a virtqueue without deallocating it as far
as I know. Do you have a reproducer or something similar?
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [外部邮件] Re: [PATCH] virtio_ring: reset IN_ORDER state in virtqueue_init()
2026-11-03 9:36 ` Eugenio Perez Martin
@ 2026-11-03 9:41 ` Michael S. Tsirkin
2026-11-03 11:44 ` Eugenio Perez Martin
0 siblings, 1 reply; 7+ messages in thread
From: Michael S. Tsirkin @ 2026-11-03 9:41 UTC (permalink / raw)
To: Eugenio Perez Martin
Cc: Li,Rongqing, Jason Wang, Xuan Zhuo, virtualization, linux-kernel
On Tue, Nov 03, 2026 at 10:36:26AM +0100, Eugenio Perez Martin wrote:
> On Fri, Sep 11, 2026 at 8:05 AM Li,Rongqing <lirongqing@baidu.com> wrote:
> >
> > > >
> > > > The IN_ORDER feature uses additional software state to track the next
> > > > available descriptor and in-flight descriptor batches.
> > > >
> > > > virtqueue_init() resets the common virtqueue state, but free_head and
> > > > batch_last.id are initialized only when the virtqueue is created. When
> > > > an IN_ORDER virtqueue is reset, these fields can retain state from the
> > > > previous queue instance.
> > > >
> > > > Reset free_head and invalidate batch_last when initializing an
> > > > IN_ORDER virtqueue. Keep this in virtqueue_init() so the state is
> > > > initialized consistently for both newly created and reset virtqueues.
> > > >
> > > > Fixes: f6a15d854986 ("virtio_ring: add in order support")
> > >
> > > I think this is needed for -stable.
> > >
> > > > Signed-off-by: Li RongQing <lirongqing@baidu.com>
> > > > ---
> > > > drivers/virtio/virtio_ring.c | 5 +++++
> > > > 1 file changed, 5 insertions(+)
> > > >
> > > > diff --git a/drivers/virtio/virtio_ring.c
> > > > b/drivers/virtio/virtio_ring.c index db678f5..d0dc464 100644
> > > > --- a/drivers/virtio/virtio_ring.c
> > > > +++ b/drivers/virtio/virtio_ring.c
> > > > @@ -559,6 +559,11 @@ static void virtqueue_init(struct vring_virtqueue *vq,
> > > u32 num)
> > > > vq->in_use = false;
> > > > vq->last_add_time_valid = false; #endif
> > > > +
> > > > + if (virtqueue_is_in_order(vq)) {
> > > > + vq->free_head = 0;
> > > > + vq->batch_last.id = UINT_MAX;
> > > > + }
> > >
> > > I would try to reuse or refactor virtqueue_vring_attach_split() to reduce code
> > > duplication.
> > >
> > > Btw have you checked Qemu shadow virtqueue as it might suffer from the same
> > > issue.
> > >
> >
> > I checked QEMU's shadow virtqueue implementation. batch_last is reinitialized in vhost_svq_start(),
> > but free_head is not. Since the SVQ object can be reused across stop/start, free_head may retain stale
> > state. So a corresponding fix should be needed
> >
> > void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev,
> > VirtQueue *vq, VhostIOVATree *iova_tree)
> > {
> > ...
> > svq->shadow_avail_idx = 0;
> > svq->shadow_used_idx = 0;
> > + svq->free_head = 0;
> > memset(&svq->batch_last, 0, sizeof(svq->batch_last));
> > svq->last_used = 0;
> > svq->last_used_idx = 0;
> >
>
> I'll ack this patch if it is sent to the QEMU mailing list, but the
> guest is not able to reset a virtqueue without deallocating it
what would "deallocating" mean here? who's doing the deallocating?
> as far
> as I know. Do you have a reproducer or something similar?
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [外部邮件] Re: [PATCH] virtio_ring: reset IN_ORDER state in virtqueue_init()
2026-11-03 9:41 ` Michael S. Tsirkin
@ 2026-11-03 11:44 ` Eugenio Perez Martin
0 siblings, 0 replies; 7+ messages in thread
From: Eugenio Perez Martin @ 2026-11-03 11:44 UTC (permalink / raw)
To: Michael S. Tsirkin
Cc: Li,Rongqing, Jason Wang, Xuan Zhuo, virtualization, linux-kernel
On Tue, Nov 3, 2026 at 10:41 AM Michael S. Tsirkin <mst@redhat.com> wrote:
>
> On Tue, Nov 03, 2026 at 10:36:26AM +0100, Eugenio Perez Martin wrote:
> > On Fri, Sep 11, 2026 at 8:05 AM Li,Rongqing <lirongqing@baidu.com> wrote:
> > >
> > > > >
> > > > > The IN_ORDER feature uses additional software state to track the next
> > > > > available descriptor and in-flight descriptor batches.
> > > > >
> > > > > virtqueue_init() resets the common virtqueue state, but free_head and
> > > > > batch_last.id are initialized only when the virtqueue is created. When
> > > > > an IN_ORDER virtqueue is reset, these fields can retain state from the
> > > > > previous queue instance.
> > > > >
> > > > > Reset free_head and invalidate batch_last when initializing an
> > > > > IN_ORDER virtqueue. Keep this in virtqueue_init() so the state is
> > > > > initialized consistently for both newly created and reset virtqueues.
> > > > >
> > > > > Fixes: f6a15d854986 ("virtio_ring: add in order support")
> > > >
> > > > I think this is needed for -stable.
> > > >
> > > > > Signed-off-by: Li RongQing <lirongqing@baidu.com>
> > > > > ---
> > > > > drivers/virtio/virtio_ring.c | 5 +++++
> > > > > 1 file changed, 5 insertions(+)
> > > > >
> > > > > diff --git a/drivers/virtio/virtio_ring.c
> > > > > b/drivers/virtio/virtio_ring.c index db678f5..d0dc464 100644
> > > > > --- a/drivers/virtio/virtio_ring.c
> > > > > +++ b/drivers/virtio/virtio_ring.c
> > > > > @@ -559,6 +559,11 @@ static void virtqueue_init(struct vring_virtqueue *vq,
> > > > u32 num)
> > > > > vq->in_use = false;
> > > > > vq->last_add_time_valid = false; #endif
> > > > > +
> > > > > + if (virtqueue_is_in_order(vq)) {
> > > > > + vq->free_head = 0;
> > > > > + vq->batch_last.id = UINT_MAX;
> > > > > + }
> > > >
> > > > I would try to reuse or refactor virtqueue_vring_attach_split() to reduce code
> > > > duplication.
> > > >
> > > > Btw have you checked Qemu shadow virtqueue as it might suffer from the same
> > > > issue.
> > > >
> > >
> > > I checked QEMU's shadow virtqueue implementation. batch_last is reinitialized in vhost_svq_start(),
> > > but free_head is not. Since the SVQ object can be reused across stop/start, free_head may retain stale
> > > state. So a corresponding fix should be needed
> > >
> > > void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev,
> > > VirtQueue *vq, VhostIOVATree *iova_tree)
> > > {
> > > ...
> > > svq->shadow_avail_idx = 0;
> > > svq->shadow_used_idx = 0;
> > > + svq->free_head = 0;
> > > memset(&svq->batch_last, 0, sizeof(svq->batch_last));
> > > svq->last_used = 0;
> > > svq->last_used_idx = 0;
> > >
> >
> > I'll ack this patch if it is sent to the QEMU mailing list, but the
> > guest is not able to reset a virtqueue without deallocating it
>
> what would "deallocating" mean here? who's doing the deallocating?
>
I meant that QEMU free the shadow virtqueue members on vring stop, but
I was wrong about free_head. What QEMU actually free is each
descriptor state (and the queue itself), as it could be resized on
each reset. free_head member should be set to 0 too. Li RongQing,
would you mind sending a patch to the QEMU mailing list with that?
Thanks!
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-03 11:44 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-10 12:39 [PATCH] virtio_ring: reset IN_ORDER state in virtqueue_init() lirongqing
2026-09-11 2:17 ` Jason Wang
2026-09-11 6:05 ` 答复: [外部邮件] " Li,Rongqing
2026-11-02 11:23 ` Michael S. Tsirkin
2026-11-03 9:36 ` Eugenio Perez Martin
2026-11-03 9:41 ` Michael S. Tsirkin
2026-11-03 11:44 ` Eugenio Perez Martin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®