[PATCH v2 02/12] virtio_ring: Add helper to attach vring descriptor

Bin Meng bmeng.cn at gmail.com
Fri Apr 22 11:07:38 CEST 2022


On Wed, Apr 13, 2022 at 10:22 PM Andrew Scull <ascull at google.com> wrote:
>
> Move the logic for attaching a descriptor to its own function.
>
> Signed-off-by: Andrew Scull <ascull at google.com>
> Reviewed-by: Simon Glass <sjg at chromium.org>
> ---
>  drivers/virtio/virtio_ring.c | 30 +++++++++++++++---------------
>  1 file changed, 15 insertions(+), 15 deletions(-)
>
> diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> index a6922ce1b8..8e0cb3d666 100644
> --- a/drivers/virtio/virtio_ring.c
> +++ b/drivers/virtio/virtio_ring.c
> @@ -16,6 +16,18 @@
>  #include <linux/bug.h>
>  #include <linux/compat.h>
>
> +static unsigned int virtqueue_attach_desc(struct virtqueue *vq, unsigned int i,
> +                                         struct virtio_sg *sg, u16 flags)
> +{
> +       struct vring_desc *desc = &vq->vring.desc[i];
> +
> +       desc->addr = cpu_to_virtio64(vq->vdev, (u64)(uintptr_t)sg->addr);
> +       desc->len = cpu_to_virtio32(vq->vdev, sg->length);
> +       desc->flags = cpu_to_virtio16(vq->vdev, flags);
> +
> +       return virtio16_to_cpu(vq->vdev, desc->next);
> +}
> +
>  int virtqueue_add(struct virtqueue *vq, struct virtio_sg *sgs[],
>                   unsigned int out_sgs, unsigned int in_sgs)
>  {
> @@ -45,26 +57,14 @@ int virtqueue_add(struct virtqueue *vq, struct virtio_sg *sgs[],
>         }
>
>         for (n = 0; n < out_sgs; n++) {
> -               struct virtio_sg *sg = sgs[n];
> -
> -               desc[i].flags = cpu_to_virtio16(vq->vdev, VRING_DESC_F_NEXT);
> -               desc[i].addr = cpu_to_virtio64(vq->vdev, (u64)(size_t)sg->addr);
> -               desc[i].len = cpu_to_virtio32(vq->vdev, sg->length);
> -
>                 prev = i;
> -               i = virtio16_to_cpu(vq->vdev, desc[i].next);
> +               i = virtqueue_attach_desc(vq, i, sgs[n], VRING_DESC_F_NEXT);
>         }
>         for (; n < (out_sgs + in_sgs); n++) {
> -               struct virtio_sg *sg = sgs[n];
> -
> -               desc[i].flags = cpu_to_virtio16(vq->vdev, VRING_DESC_F_NEXT |
> -                                               VRING_DESC_F_WRITE);
> -               desc[i].addr = cpu_to_virtio64(vq->vdev,
> -                                              (u64)(uintptr_t)sg->addr);
> -               desc[i].len = cpu_to_virtio32(vq->vdev, sg->length);
> +               u16 flags = VRING_DESC_F_NEXT | VRING_DESC_F_WRITE;
>
>                 prev = i;
> -               i = virtio16_to_cpu(vq->vdev, desc[i].next);
> +               i = virtqueue_attach_desc(vq, i, sgs[n], flags);

The above 2 for loops can be further merged into one loop:

u16 flags = VRING_DESC_F_NEXT;
for (n = 0; n < (out_sgs + in_sgs); n++) {
   if (n >= out_sgs)
        flags |= VRING_DESC_F_WRITE;
   prev = i;
   i = virtqueue_attach_desc(vq, i, sgs[n], flags);
}

>         }
>         /* Last one doesn't continue */
>         desc[prev].flags &= cpu_to_virtio16(vq->vdev, ~VRING_DESC_F_NEXT);

Regards,
Bin


More information about the U-Boot mailing list