[PATCH V6 15/19] virtio_ring: factor out core logic of buffer detaching

Jason Wang posted 19 patches 1 week, 6 days ago
[PATCH V6 15/19] virtio_ring: factor out core logic of buffer detaching
Posted by Jason Wang 1 week, 6 days ago
Factor out core logic of buffer detaching and leave the id population
to the caller so in order can just call the core logic.

Acked-by: Eugenio Pérez <eperezma@redhat.com>
Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/virtio/virtio_ring.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
index 0949675a3d12..a83d08070e8a 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -1662,8 +1662,8 @@ static bool virtqueue_kick_prepare_packed(struct vring_virtqueue *vq)
 	return needs_kick;
 }
 
-static void detach_buf_packed(struct vring_virtqueue *vq,
-			      unsigned int id, void **ctx)
+static void detach_buf_packed_in_order(struct vring_virtqueue *vq,
+				       unsigned int id, void **ctx)
 {
 	struct vring_desc_state_packed *state = NULL;
 	struct vring_packed_desc *desc;
@@ -1674,8 +1674,6 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
 	/* Clear data ptr. */
 	state->data = NULL;
 
-	vq->packed.desc_extra[state->last].next = vq->free_head;
-	vq->free_head = id;
 	vq->vq.num_free += state->num;
 
 	if (unlikely(vq->use_map_api)) {
@@ -1712,6 +1710,17 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
 	}
 }
 
+static void detach_buf_packed(struct vring_virtqueue *vq,
+			      unsigned int id, void **ctx)
+{
+	struct vring_desc_state_packed *state = &vq->packed.desc_state[id];
+
+	vq->packed.desc_extra[state->last].next = vq->free_head;
+	vq->free_head = id;
+
+	return detach_buf_packed_in_order(vq, id, ctx);
+}
+
 static inline bool is_used_desc_packed(const struct vring_virtqueue *vq,
 				       u16 idx, bool used_wrap_counter)
 {
-- 
2.31.1

Re: [PATCH V6 15/19] virtio_ring: factor out core logic of buffer detaching
Posted by Michael S. Tsirkin 1 week, 3 days ago
On Fri, Sep 19, 2025 at 03:31:50PM +0800, Jason Wang wrote:
> Factor out core logic of buffer detaching and leave the id population
> to the caller so in order can just call the core logic.
> 
> Acked-by: Eugenio Pérez <eperezma@redhat.com>
> Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> ---
>  drivers/virtio/virtio_ring.c | 17 +++++++++++++----
>  1 file changed, 13 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> index 0949675a3d12..a83d08070e8a 100644
> --- a/drivers/virtio/virtio_ring.c
> +++ b/drivers/virtio/virtio_ring.c
> @@ -1662,8 +1662,8 @@ static bool virtqueue_kick_prepare_packed(struct vring_virtqueue *vq)
>  	return needs_kick;
>  }
>  
> -static void detach_buf_packed(struct vring_virtqueue *vq,
> -			      unsigned int id, void **ctx)
> +static void detach_buf_packed_in_order(struct vring_virtqueue *vq,
> +				       unsigned int id, void **ctx)
>  {
>  	struct vring_desc_state_packed *state = NULL;
>  	struct vring_packed_desc *desc;
> @@ -1674,8 +1674,6 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
>  	/* Clear data ptr. */
>  	state->data = NULL;
>  
> -	vq->packed.desc_extra[state->last].next = vq->free_head;
> -	vq->free_head = id;
>  	vq->vq.num_free += state->num;
>  
>  	if (unlikely(vq->use_map_api)) {
> @@ -1712,6 +1710,17 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
>  	}
>  }
>  
> +static void detach_buf_packed(struct vring_virtqueue *vq,
> +			      unsigned int id, void **ctx)
> +{
> +	struct vring_desc_state_packed *state = &vq->packed.desc_state[id];
> +
> +	vq->packed.desc_extra[state->last].next = vq->free_head;
> +	vq->free_head = id;
> +
> +	return detach_buf_packed_in_order(vq, id, ctx);
> +}
> +

Wait what? return in a void function?
How does this compile? I'm guessing it's tweaked by a follow
up patch?


>  static inline bool is_used_desc_packed(const struct vring_virtqueue *vq,
>  				       u16 idx, bool used_wrap_counter)
>  {
> -- 
> 2.31.1
Re: [PATCH V6 15/19] virtio_ring: factor out core logic of buffer detaching
Posted by Jason Wang 1 week, 3 days ago
On Mon, Sep 22, 2025 at 1:42 AM Michael S. Tsirkin <mst@redhat.com> wrote:
>
> On Fri, Sep 19, 2025 at 03:31:50PM +0800, Jason Wang wrote:
> > Factor out core logic of buffer detaching and leave the id population
> > to the caller so in order can just call the core logic.
> >
> > Acked-by: Eugenio Pérez <eperezma@redhat.com>
> > Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
> > Signed-off-by: Jason Wang <jasowang@redhat.com>
> > ---
> >  drivers/virtio/virtio_ring.c | 17 +++++++++++++----
> >  1 file changed, 13 insertions(+), 4 deletions(-)
> >
> > diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> > index 0949675a3d12..a83d08070e8a 100644
> > --- a/drivers/virtio/virtio_ring.c
> > +++ b/drivers/virtio/virtio_ring.c
> > @@ -1662,8 +1662,8 @@ static bool virtqueue_kick_prepare_packed(struct vring_virtqueue *vq)
> >       return needs_kick;
> >  }
> >
> > -static void detach_buf_packed(struct vring_virtqueue *vq,
> > -                           unsigned int id, void **ctx)
> > +static void detach_buf_packed_in_order(struct vring_virtqueue *vq,
> > +                                    unsigned int id, void **ctx)
> >  {
> >       struct vring_desc_state_packed *state = NULL;
> >       struct vring_packed_desc *desc;
> > @@ -1674,8 +1674,6 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
> >       /* Clear data ptr. */
> >       state->data = NULL;
> >
> > -     vq->packed.desc_extra[state->last].next = vq->free_head;
> > -     vq->free_head = id;
> >       vq->vq.num_free += state->num;
> >
> >       if (unlikely(vq->use_map_api)) {
> > @@ -1712,6 +1710,17 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
> >       }
> >  }
> >
> > +static void detach_buf_packed(struct vring_virtqueue *vq,
> > +                           unsigned int id, void **ctx)
> > +{
> > +     struct vring_desc_state_packed *state = &vq->packed.desc_state[id];
> > +
> > +     vq->packed.desc_extra[state->last].next = vq->free_head;
> > +     vq->free_head = id;
> > +
> > +     return detach_buf_packed_in_order(vq, id, ctx);
> > +}
> > +
>
> Wait what? return in a void function?
> How does this compile?

It looks like it passes the compiling since detach_buf_packed_in_order
returns void as well :(

> I'm guessing it's tweaked by a follow
> up patch?

Nope. Let me drop the return here.

Thanks

>
>
> >  static inline bool is_used_desc_packed(const struct vring_virtqueue *vq,
> >                                      u16 idx, bool used_wrap_counter)
> >  {
> > --
> > 2.31.1
>