diff mbox series

[vhost,v11,06/10] virtio_ring: skip unmap for premapped

Message ID 20230710034237.12391-7-xuanzhuo@linux.alibaba.com (mailing list archive)
State Superseded
Headers show
Series virtio core prepares for AF_XDP | expand

Checks

Context Check Description
netdev/tree_selection success Guessing tree name failed - patch did not apply

Commit Message

Xuan Zhuo July 10, 2023, 3:42 a.m. UTC
Now we add a case where we skip dma unmap, the vq->premapped is true.

We can't just rely on use_dma_api to determine whether to skip the dma
operation. For convenience, I introduced the "do_unmap". By default, it
is the same as use_dma_api. If the driver is configured with premapped,
then do_unmap is false.

So as long as do_unmap is false, for addr of desc, we should skip dma
unmap operation.

Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
 drivers/virtio/virtio_ring.c | 42 ++++++++++++++++++++++++------------
 1 file changed, 28 insertions(+), 14 deletions(-)

Comments

Jason Wang July 13, 2023, 3:50 a.m. UTC | #1
On Mon, Jul 10, 2023 at 11:42 AM Xuan Zhuo <xuanzhuo@linux.alibaba.com> wrote:
>
> Now we add a case where we skip dma unmap, the vq->premapped is true.
>
> We can't just rely on use_dma_api to determine whether to skip the dma
> operation. For convenience, I introduced the "do_unmap". By default, it
> is the same as use_dma_api. If the driver is configured with premapped,
> then do_unmap is false.
>
> So as long as do_unmap is false, for addr of desc, we should skip dma
> unmap operation.
>
> Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
> ---
>  drivers/virtio/virtio_ring.c | 42 ++++++++++++++++++++++++------------
>  1 file changed, 28 insertions(+), 14 deletions(-)
>
> diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> index 1fb2c6dca9ea..10ee3b7ce571 100644
> --- a/drivers/virtio/virtio_ring.c
> +++ b/drivers/virtio/virtio_ring.c
> @@ -175,6 +175,11 @@ struct vring_virtqueue {
>         /* Do DMA mapping by driver */
>         bool premapped;
>
> +       /* Do unmap or not for desc. Just when premapped is False and
> +        * use_dma_api is true, this is true.
> +        */
> +       bool do_unmap;
> +
>         /* Head of free buffer list. */
>         unsigned int free_head;
>         /* Number we've added since last sync. */
> @@ -440,7 +445,7 @@ static void vring_unmap_one_split_indirect(const struct vring_virtqueue *vq,
>  {
>         u16 flags;
>
> -       if (!vq->use_dma_api)
> +       if (!vq->do_unmap)
>                 return;
>
>         flags = virtio16_to_cpu(vq->vq.vdev, desc->flags);
> @@ -458,18 +463,21 @@ static unsigned int vring_unmap_one_split(const struct vring_virtqueue *vq,
>         struct vring_desc_extra *extra = vq->split.desc_extra;
>         u16 flags;
>
> -       if (!vq->use_dma_api)
> -               goto out;
> -
>         flags = extra[i].flags;
>
>         if (flags & VRING_DESC_F_INDIRECT) {
> +               if (!vq->use_dma_api)
> +                       goto out;
> +
>                 dma_unmap_single(vring_dma_dev(vq),
>                                  extra[i].addr,
>                                  extra[i].len,
>                                  (flags & VRING_DESC_F_WRITE) ?
>                                  DMA_FROM_DEVICE : DMA_TO_DEVICE);
>         } else {
> +               if (!vq->do_unmap)
> +                       goto out;
> +
>                 dma_unmap_page(vring_dma_dev(vq),
>                                extra[i].addr,
>                                extra[i].len,
> @@ -635,7 +643,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
>         }
>         /* Last one doesn't continue. */
>         desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT);
> -       if (!indirect && vq->use_dma_api)
> +       if (!indirect && vq->do_unmap)
>                 vq->split.desc_extra[prev & (vq->split.vring.num - 1)].flags &=
>                         ~VRING_DESC_F_NEXT;
>
> @@ -794,7 +802,7 @@ static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,
>                                 VRING_DESC_F_INDIRECT));
>                 BUG_ON(len == 0 || len % sizeof(struct vring_desc));
>
> -               if (vq->use_dma_api) {
> +               if (vq->do_unmap) {
>                         for (j = 0; j < len / sizeof(struct vring_desc); j++)
>                                 vring_unmap_one_split_indirect(vq, &indir_desc[j]);
>                 }
> @@ -1217,17 +1225,20 @@ static void vring_unmap_extra_packed(const struct vring_virtqueue *vq,
>  {
>         u16 flags;
>
> -       if (!vq->use_dma_api)
> -               return;
> -
>         flags = extra->flags;
>
>         if (flags & VRING_DESC_F_INDIRECT) {
> +               if (!vq->use_dma_api)
> +                       return;
> +
>                 dma_unmap_single(vring_dma_dev(vq),
>                                  extra->addr, extra->len,
>                                  (flags & VRING_DESC_F_WRITE) ?
>                                  DMA_FROM_DEVICE : DMA_TO_DEVICE);
>         } else {
> +               if (!vq->do_unmap)
> +                       return;

This seems not straightforward than:

if (!vq->use_dma_api)
    return;

if (INDIRECT) {
} else if (!vq->premapped) {
}

?

Thanks

> +
>                 dma_unmap_page(vring_dma_dev(vq),
>                                extra->addr, extra->len,
>                                (flags & VRING_DESC_F_WRITE) ?
> @@ -1240,7 +1251,7 @@ static void vring_unmap_desc_packed(const struct vring_virtqueue *vq,
>  {
>         u16 flags;
>
> -       if (!vq->use_dma_api)
> +       if (!vq->do_unmap)
>                 return;
>
>         flags = le16_to_cpu(desc->flags);
> @@ -1329,7 +1340,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
>                                 sizeof(struct vring_packed_desc));
>         vq->packed.vring.desc[head].id = cpu_to_le16(id);
>
> -       if (vq->use_dma_api) {
> +       if (vq->do_unmap) {
>                 vq->packed.desc_extra[id].addr = addr;
>                 vq->packed.desc_extra[id].len = total_sg *
>                                 sizeof(struct vring_packed_desc);
> @@ -1470,7 +1481,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,
>                         desc[i].len = cpu_to_le32(sg->length);
>                         desc[i].id = cpu_to_le16(id);
>
> -                       if (unlikely(vq->use_dma_api)) {
> +                       if (unlikely(vq->do_unmap)) {
>                                 vq->packed.desc_extra[curr].addr = addr;
>                                 vq->packed.desc_extra[curr].len = sg->length;
>                                 vq->packed.desc_extra[curr].flags =
> @@ -1604,7 +1615,7 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
>         vq->free_head = id;
>         vq->vq.num_free += state->num;
>
> -       if (unlikely(vq->use_dma_api)) {
> +       if (unlikely(vq->do_unmap)) {
>                 curr = id;
>                 for (i = 0; i < state->num; i++) {
>                         vring_unmap_extra_packed(vq,
> @@ -1621,7 +1632,7 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
>                 if (!desc)
>                         return;
>
> -               if (vq->use_dma_api) {
> +               if (vq->do_unmap) {
>                         len = vq->packed.desc_extra[id].len;
>                         for (i = 0; i < len / sizeof(struct vring_packed_desc);
>                                         i++)
> @@ -2080,6 +2091,7 @@ static struct virtqueue *vring_create_virtqueue_packed(
>         vq->dma_dev = dma_dev;
>         vq->use_dma_api = vring_use_dma_api(vdev);
>         vq->premapped = false;
> +       vq->do_unmap = vq->use_dma_api;
>
>         vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
>                 !context;
> @@ -2587,6 +2599,7 @@ static struct virtqueue *__vring_new_virtqueue(unsigned int index,
>         vq->dma_dev = dma_dev;
>         vq->use_dma_api = vring_use_dma_api(vdev);
>         vq->premapped = false;
> +       vq->do_unmap = vq->use_dma_api;
>
>         vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
>                 !context;
> @@ -2765,6 +2778,7 @@ int virtqueue_set_premapped(struct virtqueue *_vq)
>                 return -EINVAL;
>
>         vq->premapped = true;
> +       vq->do_unmap = false;
>
>         return 0;
>  }
> --
> 2.32.0.3.g01195cf9f
>
Xuan Zhuo July 13, 2023, 4:02 a.m. UTC | #2
On Thu, 13 Jul 2023 11:50:57 +0800, Jason Wang <jasowang@redhat.com> wrote:
> On Mon, Jul 10, 2023 at 11:42 AM Xuan Zhuo <xuanzhuo@linux.alibaba.com> wrote:
> >
> > Now we add a case where we skip dma unmap, the vq->premapped is true.
> >
> > We can't just rely on use_dma_api to determine whether to skip the dma
> > operation. For convenience, I introduced the "do_unmap". By default, it
> > is the same as use_dma_api. If the driver is configured with premapped,
> > then do_unmap is false.
> >
> > So as long as do_unmap is false, for addr of desc, we should skip dma
> > unmap operation.
> >
> > Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
> > ---
> >  drivers/virtio/virtio_ring.c | 42 ++++++++++++++++++++++++------------
> >  1 file changed, 28 insertions(+), 14 deletions(-)
> >
> > diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> > index 1fb2c6dca9ea..10ee3b7ce571 100644
> > --- a/drivers/virtio/virtio_ring.c
> > +++ b/drivers/virtio/virtio_ring.c
> > @@ -175,6 +175,11 @@ struct vring_virtqueue {
> >         /* Do DMA mapping by driver */
> >         bool premapped;
> >
> > +       /* Do unmap or not for desc. Just when premapped is False and
> > +        * use_dma_api is true, this is true.
> > +        */
> > +       bool do_unmap;
> > +
> >         /* Head of free buffer list. */
> >         unsigned int free_head;
> >         /* Number we've added since last sync. */
> > @@ -440,7 +445,7 @@ static void vring_unmap_one_split_indirect(const struct vring_virtqueue *vq,
> >  {
> >         u16 flags;
> >
> > -       if (!vq->use_dma_api)
> > +       if (!vq->do_unmap)
> >                 return;
> >
> >         flags = virtio16_to_cpu(vq->vq.vdev, desc->flags);
> > @@ -458,18 +463,21 @@ static unsigned int vring_unmap_one_split(const struct vring_virtqueue *vq,
> >         struct vring_desc_extra *extra = vq->split.desc_extra;
> >         u16 flags;
> >
> > -       if (!vq->use_dma_api)
> > -               goto out;
> > -
> >         flags = extra[i].flags;
> >
> >         if (flags & VRING_DESC_F_INDIRECT) {
> > +               if (!vq->use_dma_api)
> > +                       goto out;
> > +
> >                 dma_unmap_single(vring_dma_dev(vq),
> >                                  extra[i].addr,
> >                                  extra[i].len,
> >                                  (flags & VRING_DESC_F_WRITE) ?
> >                                  DMA_FROM_DEVICE : DMA_TO_DEVICE);
> >         } else {
> > +               if (!vq->do_unmap)
> > +                       goto out;
> > +
> >                 dma_unmap_page(vring_dma_dev(vq),
> >                                extra[i].addr,
> >                                extra[i].len,
> > @@ -635,7 +643,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
> >         }
> >         /* Last one doesn't continue. */
> >         desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT);
> > -       if (!indirect && vq->use_dma_api)
> > +       if (!indirect && vq->do_unmap)
> >                 vq->split.desc_extra[prev & (vq->split.vring.num - 1)].flags &=
> >                         ~VRING_DESC_F_NEXT;
> >
> > @@ -794,7 +802,7 @@ static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,
> >                                 VRING_DESC_F_INDIRECT));
> >                 BUG_ON(len == 0 || len % sizeof(struct vring_desc));
> >
> > -               if (vq->use_dma_api) {
> > +               if (vq->do_unmap) {
> >                         for (j = 0; j < len / sizeof(struct vring_desc); j++)
> >                                 vring_unmap_one_split_indirect(vq, &indir_desc[j]);
> >                 }
> > @@ -1217,17 +1225,20 @@ static void vring_unmap_extra_packed(const struct vring_virtqueue *vq,
> >  {
> >         u16 flags;
> >
> > -       if (!vq->use_dma_api)
> > -               return;
> > -
> >         flags = extra->flags;
> >
> >         if (flags & VRING_DESC_F_INDIRECT) {
> > +               if (!vq->use_dma_api)
> > +                       return;
> > +
> >                 dma_unmap_single(vring_dma_dev(vq),
> >                                  extra->addr, extra->len,
> >                                  (flags & VRING_DESC_F_WRITE) ?
> >                                  DMA_FROM_DEVICE : DMA_TO_DEVICE);
> >         } else {
> > +               if (!vq->do_unmap)
> > +                       return;
>
> This seems not straightforward than:
>
> if (!vq->use_dma_api)
>     return;
>
> if (INDIRECT) {
> } else if (!vq->premapped) {
> }
>
> ?


My logic here is that for the real buffer, we use do_unmap to judge uniformly.
And indirect still use use_dma_api to judge.

From this point of view, how do you feel?

Thanks.


>
> Thanks
>
> > +
> >                 dma_unmap_page(vring_dma_dev(vq),
> >                                extra->addr, extra->len,
> >                                (flags & VRING_DESC_F_WRITE) ?
> > @@ -1240,7 +1251,7 @@ static void vring_unmap_desc_packed(const struct vring_virtqueue *vq,
> >  {
> >         u16 flags;
> >
> > -       if (!vq->use_dma_api)
> > +       if (!vq->do_unmap)
> >                 return;
> >
> >         flags = le16_to_cpu(desc->flags);
> > @@ -1329,7 +1340,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
> >                                 sizeof(struct vring_packed_desc));
> >         vq->packed.vring.desc[head].id = cpu_to_le16(id);
> >
> > -       if (vq->use_dma_api) {
> > +       if (vq->do_unmap) {
> >                 vq->packed.desc_extra[id].addr = addr;
> >                 vq->packed.desc_extra[id].len = total_sg *
> >                                 sizeof(struct vring_packed_desc);
> > @@ -1470,7 +1481,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,
> >                         desc[i].len = cpu_to_le32(sg->length);
> >                         desc[i].id = cpu_to_le16(id);
> >
> > -                       if (unlikely(vq->use_dma_api)) {
> > +                       if (unlikely(vq->do_unmap)) {
> >                                 vq->packed.desc_extra[curr].addr = addr;
> >                                 vq->packed.desc_extra[curr].len = sg->length;
> >                                 vq->packed.desc_extra[curr].flags =
> > @@ -1604,7 +1615,7 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
> >         vq->free_head = id;
> >         vq->vq.num_free += state->num;
> >
> > -       if (unlikely(vq->use_dma_api)) {
> > +       if (unlikely(vq->do_unmap)) {
> >                 curr = id;
> >                 for (i = 0; i < state->num; i++) {
> >                         vring_unmap_extra_packed(vq,
> > @@ -1621,7 +1632,7 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
> >                 if (!desc)
> >                         return;
> >
> > -               if (vq->use_dma_api) {
> > +               if (vq->do_unmap) {
> >                         len = vq->packed.desc_extra[id].len;
> >                         for (i = 0; i < len / sizeof(struct vring_packed_desc);
> >                                         i++)
> > @@ -2080,6 +2091,7 @@ static struct virtqueue *vring_create_virtqueue_packed(
> >         vq->dma_dev = dma_dev;
> >         vq->use_dma_api = vring_use_dma_api(vdev);
> >         vq->premapped = false;
> > +       vq->do_unmap = vq->use_dma_api;
> >
> >         vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
> >                 !context;
> > @@ -2587,6 +2599,7 @@ static struct virtqueue *__vring_new_virtqueue(unsigned int index,
> >         vq->dma_dev = dma_dev;
> >         vq->use_dma_api = vring_use_dma_api(vdev);
> >         vq->premapped = false;
> > +       vq->do_unmap = vq->use_dma_api;
> >
> >         vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
> >                 !context;
> > @@ -2765,6 +2778,7 @@ int virtqueue_set_premapped(struct virtqueue *_vq)
> >                 return -EINVAL;
> >
> >         vq->premapped = true;
> > +       vq->do_unmap = false;
> >
> >         return 0;
> >  }
> > --
> > 2.32.0.3.g01195cf9f
> >
>
Jason Wang July 13, 2023, 4:21 a.m. UTC | #3
On Thu, Jul 13, 2023 at 12:06 PM Xuan Zhuo <xuanzhuo@linux.alibaba.com> wrote:
>
> On Thu, 13 Jul 2023 11:50:57 +0800, Jason Wang <jasowang@redhat.com> wrote:
> > On Mon, Jul 10, 2023 at 11:42 AM Xuan Zhuo <xuanzhuo@linux.alibaba.com> wrote:
> > >
> > > Now we add a case where we skip dma unmap, the vq->premapped is true.
> > >
> > > We can't just rely on use_dma_api to determine whether to skip the dma
> > > operation. For convenience, I introduced the "do_unmap". By default, it
> > > is the same as use_dma_api. If the driver is configured with premapped,
> > > then do_unmap is false.
> > >
> > > So as long as do_unmap is false, for addr of desc, we should skip dma
> > > unmap operation.
> > >
> > > Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
> > > ---
> > >  drivers/virtio/virtio_ring.c | 42 ++++++++++++++++++++++++------------
> > >  1 file changed, 28 insertions(+), 14 deletions(-)
> > >
> > > diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> > > index 1fb2c6dca9ea..10ee3b7ce571 100644
> > > --- a/drivers/virtio/virtio_ring.c
> > > +++ b/drivers/virtio/virtio_ring.c
> > > @@ -175,6 +175,11 @@ struct vring_virtqueue {
> > >         /* Do DMA mapping by driver */
> > >         bool premapped;
> > >
> > > +       /* Do unmap or not for desc. Just when premapped is False and
> > > +        * use_dma_api is true, this is true.
> > > +        */
> > > +       bool do_unmap;
> > > +
> > >         /* Head of free buffer list. */
> > >         unsigned int free_head;
> > >         /* Number we've added since last sync. */
> > > @@ -440,7 +445,7 @@ static void vring_unmap_one_split_indirect(const struct vring_virtqueue *vq,
> > >  {
> > >         u16 flags;
> > >
> > > -       if (!vq->use_dma_api)
> > > +       if (!vq->do_unmap)
> > >                 return;
> > >
> > >         flags = virtio16_to_cpu(vq->vq.vdev, desc->flags);
> > > @@ -458,18 +463,21 @@ static unsigned int vring_unmap_one_split(const struct vring_virtqueue *vq,
> > >         struct vring_desc_extra *extra = vq->split.desc_extra;
> > >         u16 flags;
> > >
> > > -       if (!vq->use_dma_api)
> > > -               goto out;
> > > -
> > >         flags = extra[i].flags;
> > >
> > >         if (flags & VRING_DESC_F_INDIRECT) {
> > > +               if (!vq->use_dma_api)
> > > +                       goto out;
> > > +
> > >                 dma_unmap_single(vring_dma_dev(vq),
> > >                                  extra[i].addr,
> > >                                  extra[i].len,
> > >                                  (flags & VRING_DESC_F_WRITE) ?
> > >                                  DMA_FROM_DEVICE : DMA_TO_DEVICE);
> > >         } else {
> > > +               if (!vq->do_unmap)
> > > +                       goto out;
> > > +
> > >                 dma_unmap_page(vring_dma_dev(vq),
> > >                                extra[i].addr,
> > >                                extra[i].len,
> > > @@ -635,7 +643,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
> > >         }
> > >         /* Last one doesn't continue. */
> > >         desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT);
> > > -       if (!indirect && vq->use_dma_api)
> > > +       if (!indirect && vq->do_unmap)
> > >                 vq->split.desc_extra[prev & (vq->split.vring.num - 1)].flags &=
> > >                         ~VRING_DESC_F_NEXT;
> > >
> > > @@ -794,7 +802,7 @@ static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,
> > >                                 VRING_DESC_F_INDIRECT));
> > >                 BUG_ON(len == 0 || len % sizeof(struct vring_desc));
> > >
> > > -               if (vq->use_dma_api) {
> > > +               if (vq->do_unmap) {
> > >                         for (j = 0; j < len / sizeof(struct vring_desc); j++)
> > >                                 vring_unmap_one_split_indirect(vq, &indir_desc[j]);
> > >                 }
> > > @@ -1217,17 +1225,20 @@ static void vring_unmap_extra_packed(const struct vring_virtqueue *vq,
> > >  {
> > >         u16 flags;
> > >
> > > -       if (!vq->use_dma_api)
> > > -               return;
> > > -
> > >         flags = extra->flags;
> > >
> > >         if (flags & VRING_DESC_F_INDIRECT) {
> > > +               if (!vq->use_dma_api)
> > > +                       return;
> > > +
> > >                 dma_unmap_single(vring_dma_dev(vq),
> > >                                  extra->addr, extra->len,
> > >                                  (flags & VRING_DESC_F_WRITE) ?
> > >                                  DMA_FROM_DEVICE : DMA_TO_DEVICE);
> > >         } else {
> > > +               if (!vq->do_unmap)
> > > +                       return;
> >
> > This seems not straightforward than:
> >
> > if (!vq->use_dma_api)
> >     return;
> >
> > if (INDIRECT) {
> > } else if (!vq->premapped) {
> > }
> >
> > ?
>
>
> My logic here is that for the real buffer, we use do_unmap to judge uniformly.
> And indirect still use use_dma_api to judge.
>
> From this point of view, how do you feel?

We can hear from others but a state machine with three booleans seems
not easy for me to read.

Thanks

>
> Thanks.
>
>
> >
> > Thanks
> >
> > > +
> > >                 dma_unmap_page(vring_dma_dev(vq),
> > >                                extra->addr, extra->len,
> > >                                (flags & VRING_DESC_F_WRITE) ?
> > > @@ -1240,7 +1251,7 @@ static void vring_unmap_desc_packed(const struct vring_virtqueue *vq,
> > >  {
> > >         u16 flags;
> > >
> > > -       if (!vq->use_dma_api)
> > > +       if (!vq->do_unmap)
> > >                 return;
> > >
> > >         flags = le16_to_cpu(desc->flags);
> > > @@ -1329,7 +1340,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
> > >                                 sizeof(struct vring_packed_desc));
> > >         vq->packed.vring.desc[head].id = cpu_to_le16(id);
> > >
> > > -       if (vq->use_dma_api) {
> > > +       if (vq->do_unmap) {
> > >                 vq->packed.desc_extra[id].addr = addr;
> > >                 vq->packed.desc_extra[id].len = total_sg *
> > >                                 sizeof(struct vring_packed_desc);
> > > @@ -1470,7 +1481,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,
> > >                         desc[i].len = cpu_to_le32(sg->length);
> > >                         desc[i].id = cpu_to_le16(id);
> > >
> > > -                       if (unlikely(vq->use_dma_api)) {
> > > +                       if (unlikely(vq->do_unmap)) {
> > >                                 vq->packed.desc_extra[curr].addr = addr;
> > >                                 vq->packed.desc_extra[curr].len = sg->length;
> > >                                 vq->packed.desc_extra[curr].flags =
> > > @@ -1604,7 +1615,7 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
> > >         vq->free_head = id;
> > >         vq->vq.num_free += state->num;
> > >
> > > -       if (unlikely(vq->use_dma_api)) {
> > > +       if (unlikely(vq->do_unmap)) {
> > >                 curr = id;
> > >                 for (i = 0; i < state->num; i++) {
> > >                         vring_unmap_extra_packed(vq,
> > > @@ -1621,7 +1632,7 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
> > >                 if (!desc)
> > >                         return;
> > >
> > > -               if (vq->use_dma_api) {
> > > +               if (vq->do_unmap) {
> > >                         len = vq->packed.desc_extra[id].len;
> > >                         for (i = 0; i < len / sizeof(struct vring_packed_desc);
> > >                                         i++)
> > > @@ -2080,6 +2091,7 @@ static struct virtqueue *vring_create_virtqueue_packed(
> > >         vq->dma_dev = dma_dev;
> > >         vq->use_dma_api = vring_use_dma_api(vdev);
> > >         vq->premapped = false;
> > > +       vq->do_unmap = vq->use_dma_api;
> > >
> > >         vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
> > >                 !context;
> > > @@ -2587,6 +2599,7 @@ static struct virtqueue *__vring_new_virtqueue(unsigned int index,
> > >         vq->dma_dev = dma_dev;
> > >         vq->use_dma_api = vring_use_dma_api(vdev);
> > >         vq->premapped = false;
> > > +       vq->do_unmap = vq->use_dma_api;
> > >
> > >         vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
> > >                 !context;
> > > @@ -2765,6 +2778,7 @@ int virtqueue_set_premapped(struct virtqueue *_vq)
> > >                 return -EINVAL;
> > >
> > >         vq->premapped = true;
> > > +       vq->do_unmap = false;
> > >
> > >         return 0;
> > >  }
> > > --
> > > 2.32.0.3.g01195cf9f
> > >
> >
>
Xuan Zhuo July 13, 2023, 5:45 a.m. UTC | #4
On Thu, 13 Jul 2023 12:21:26 +0800, Jason Wang <jasowang@redhat.com> wrote:
> On Thu, Jul 13, 2023 at 12:06 PM Xuan Zhuo <xuanzhuo@linux.alibaba.com> wrote:
> >
> > On Thu, 13 Jul 2023 11:50:57 +0800, Jason Wang <jasowang@redhat.com> wrote:
> > > On Mon, Jul 10, 2023 at 11:42 AM Xuan Zhuo <xuanzhuo@linux.alibaba.com> wrote:
> > > >
> > > > Now we add a case where we skip dma unmap, the vq->premapped is true.
> > > >
> > > > We can't just rely on use_dma_api to determine whether to skip the dma
> > > > operation. For convenience, I introduced the "do_unmap". By default, it
> > > > is the same as use_dma_api. If the driver is configured with premapped,
> > > > then do_unmap is false.
> > > >
> > > > So as long as do_unmap is false, for addr of desc, we should skip dma
> > > > unmap operation.
> > > >
> > > > Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
> > > > ---
> > > >  drivers/virtio/virtio_ring.c | 42 ++++++++++++++++++++++++------------
> > > >  1 file changed, 28 insertions(+), 14 deletions(-)
> > > >
> > > > diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> > > > index 1fb2c6dca9ea..10ee3b7ce571 100644
> > > > --- a/drivers/virtio/virtio_ring.c
> > > > +++ b/drivers/virtio/virtio_ring.c
> > > > @@ -175,6 +175,11 @@ struct vring_virtqueue {
> > > >         /* Do DMA mapping by driver */
> > > >         bool premapped;
> > > >
> > > > +       /* Do unmap or not for desc. Just when premapped is False and
> > > > +        * use_dma_api is true, this is true.
> > > > +        */
> > > > +       bool do_unmap;
> > > > +
> > > >         /* Head of free buffer list. */
> > > >         unsigned int free_head;
> > > >         /* Number we've added since last sync. */
> > > > @@ -440,7 +445,7 @@ static void vring_unmap_one_split_indirect(const struct vring_virtqueue *vq,
> > > >  {
> > > >         u16 flags;
> > > >
> > > > -       if (!vq->use_dma_api)
> > > > +       if (!vq->do_unmap)
> > > >                 return;
> > > >
> > > >         flags = virtio16_to_cpu(vq->vq.vdev, desc->flags);
> > > > @@ -458,18 +463,21 @@ static unsigned int vring_unmap_one_split(const struct vring_virtqueue *vq,
> > > >         struct vring_desc_extra *extra = vq->split.desc_extra;
> > > >         u16 flags;
> > > >
> > > > -       if (!vq->use_dma_api)
> > > > -               goto out;
> > > > -
> > > >         flags = extra[i].flags;
> > > >
> > > >         if (flags & VRING_DESC_F_INDIRECT) {
> > > > +               if (!vq->use_dma_api)
> > > > +                       goto out;
> > > > +
> > > >                 dma_unmap_single(vring_dma_dev(vq),
> > > >                                  extra[i].addr,
> > > >                                  extra[i].len,
> > > >                                  (flags & VRING_DESC_F_WRITE) ?
> > > >                                  DMA_FROM_DEVICE : DMA_TO_DEVICE);
> > > >         } else {
> > > > +               if (!vq->do_unmap)
> > > > +                       goto out;
> > > > +
> > > >                 dma_unmap_page(vring_dma_dev(vq),
> > > >                                extra[i].addr,
> > > >                                extra[i].len,
> > > > @@ -635,7 +643,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
> > > >         }
> > > >         /* Last one doesn't continue. */
> > > >         desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT);
> > > > -       if (!indirect && vq->use_dma_api)
> > > > +       if (!indirect && vq->do_unmap)
> > > >                 vq->split.desc_extra[prev & (vq->split.vring.num - 1)].flags &=
> > > >                         ~VRING_DESC_F_NEXT;
> > > >
> > > > @@ -794,7 +802,7 @@ static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,
> > > >                                 VRING_DESC_F_INDIRECT));
> > > >                 BUG_ON(len == 0 || len % sizeof(struct vring_desc));
> > > >
> > > > -               if (vq->use_dma_api) {
> > > > +               if (vq->do_unmap) {
> > > >                         for (j = 0; j < len / sizeof(struct vring_desc); j++)
> > > >                                 vring_unmap_one_split_indirect(vq, &indir_desc[j]);
> > > >                 }
> > > > @@ -1217,17 +1225,20 @@ static void vring_unmap_extra_packed(const struct vring_virtqueue *vq,
> > > >  {
> > > >         u16 flags;
> > > >
> > > > -       if (!vq->use_dma_api)
> > > > -               return;
> > > > -
> > > >         flags = extra->flags;
> > > >
> > > >         if (flags & VRING_DESC_F_INDIRECT) {
> > > > +               if (!vq->use_dma_api)
> > > > +                       return;
> > > > +
> > > >                 dma_unmap_single(vring_dma_dev(vq),
> > > >                                  extra->addr, extra->len,
> > > >                                  (flags & VRING_DESC_F_WRITE) ?
> > > >                                  DMA_FROM_DEVICE : DMA_TO_DEVICE);
> > > >         } else {
> > > > +               if (!vq->do_unmap)
> > > > +                       return;
> > >
> > > This seems not straightforward than:
> > >
> > > if (!vq->use_dma_api)
> > >     return;
> > >
> > > if (INDIRECT) {
> > > } else if (!vq->premapped) {
> > > }
> > >
> > > ?
> >
> >
> > My logic here is that for the real buffer, we use do_unmap to judge uniformly.
> > And indirect still use use_dma_api to judge.
> >
> > From this point of view, how do you feel?
>
> We can hear from others but a state machine with three booleans seems
> not easy for me to read.

Yes, I also think too many booleans, so I introduce do_unmap, then
for the real buffer(not the indirect desc array), we just check do_unmap.

Thanks.


>
> Thanks
>
> >
> > Thanks.
> >
> >
> > >
> > > Thanks
> > >
> > > > +
> > > >                 dma_unmap_page(vring_dma_dev(vq),
> > > >                                extra->addr, extra->len,
> > > >                                (flags & VRING_DESC_F_WRITE) ?
> > > > @@ -1240,7 +1251,7 @@ static void vring_unmap_desc_packed(const struct vring_virtqueue *vq,
> > > >  {
> > > >         u16 flags;
> > > >
> > > > -       if (!vq->use_dma_api)
> > > > +       if (!vq->do_unmap)
> > > >                 return;
> > > >
> > > >         flags = le16_to_cpu(desc->flags);
> > > > @@ -1329,7 +1340,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
> > > >                                 sizeof(struct vring_packed_desc));
> > > >         vq->packed.vring.desc[head].id = cpu_to_le16(id);
> > > >
> > > > -       if (vq->use_dma_api) {
> > > > +       if (vq->do_unmap) {
> > > >                 vq->packed.desc_extra[id].addr = addr;
> > > >                 vq->packed.desc_extra[id].len = total_sg *
> > > >                                 sizeof(struct vring_packed_desc);
> > > > @@ -1470,7 +1481,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,
> > > >                         desc[i].len = cpu_to_le32(sg->length);
> > > >                         desc[i].id = cpu_to_le16(id);
> > > >
> > > > -                       if (unlikely(vq->use_dma_api)) {
> > > > +                       if (unlikely(vq->do_unmap)) {
> > > >                                 vq->packed.desc_extra[curr].addr = addr;
> > > >                                 vq->packed.desc_extra[curr].len = sg->length;
> > > >                                 vq->packed.desc_extra[curr].flags =
> > > > @@ -1604,7 +1615,7 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
> > > >         vq->free_head = id;
> > > >         vq->vq.num_free += state->num;
> > > >
> > > > -       if (unlikely(vq->use_dma_api)) {
> > > > +       if (unlikely(vq->do_unmap)) {
> > > >                 curr = id;
> > > >                 for (i = 0; i < state->num; i++) {
> > > >                         vring_unmap_extra_packed(vq,
> > > > @@ -1621,7 +1632,7 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
> > > >                 if (!desc)
> > > >                         return;
> > > >
> > > > -               if (vq->use_dma_api) {
> > > > +               if (vq->do_unmap) {
> > > >                         len = vq->packed.desc_extra[id].len;
> > > >                         for (i = 0; i < len / sizeof(struct vring_packed_desc);
> > > >                                         i++)
> > > > @@ -2080,6 +2091,7 @@ static struct virtqueue *vring_create_virtqueue_packed(
> > > >         vq->dma_dev = dma_dev;
> > > >         vq->use_dma_api = vring_use_dma_api(vdev);
> > > >         vq->premapped = false;
> > > > +       vq->do_unmap = vq->use_dma_api;
> > > >
> > > >         vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
> > > >                 !context;
> > > > @@ -2587,6 +2599,7 @@ static struct virtqueue *__vring_new_virtqueue(unsigned int index,
> > > >         vq->dma_dev = dma_dev;
> > > >         vq->use_dma_api = vring_use_dma_api(vdev);
> > > >         vq->premapped = false;
> > > > +       vq->do_unmap = vq->use_dma_api;
> > > >
> > > >         vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
> > > >                 !context;
> > > > @@ -2765,6 +2778,7 @@ int virtqueue_set_premapped(struct virtqueue *_vq)
> > > >                 return -EINVAL;
> > > >
> > > >         vq->premapped = true;
> > > > +       vq->do_unmap = false;
> > > >
> > > >         return 0;
> > > >  }
> > > > --
> > > > 2.32.0.3.g01195cf9f
> > > >
> > >
> >
>
diff mbox series

Patch

diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
index 1fb2c6dca9ea..10ee3b7ce571 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -175,6 +175,11 @@  struct vring_virtqueue {
 	/* Do DMA mapping by driver */
 	bool premapped;
 
+	/* Do unmap or not for desc. Just when premapped is False and
+	 * use_dma_api is true, this is true.
+	 */
+	bool do_unmap;
+
 	/* Head of free buffer list. */
 	unsigned int free_head;
 	/* Number we've added since last sync. */
@@ -440,7 +445,7 @@  static void vring_unmap_one_split_indirect(const struct vring_virtqueue *vq,
 {
 	u16 flags;
 
-	if (!vq->use_dma_api)
+	if (!vq->do_unmap)
 		return;
 
 	flags = virtio16_to_cpu(vq->vq.vdev, desc->flags);
@@ -458,18 +463,21 @@  static unsigned int vring_unmap_one_split(const struct vring_virtqueue *vq,
 	struct vring_desc_extra *extra = vq->split.desc_extra;
 	u16 flags;
 
-	if (!vq->use_dma_api)
-		goto out;
-
 	flags = extra[i].flags;
 
 	if (flags & VRING_DESC_F_INDIRECT) {
+		if (!vq->use_dma_api)
+			goto out;
+
 		dma_unmap_single(vring_dma_dev(vq),
 				 extra[i].addr,
 				 extra[i].len,
 				 (flags & VRING_DESC_F_WRITE) ?
 				 DMA_FROM_DEVICE : DMA_TO_DEVICE);
 	} else {
+		if (!vq->do_unmap)
+			goto out;
+
 		dma_unmap_page(vring_dma_dev(vq),
 			       extra[i].addr,
 			       extra[i].len,
@@ -635,7 +643,7 @@  static inline int virtqueue_add_split(struct virtqueue *_vq,
 	}
 	/* Last one doesn't continue. */
 	desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT);
-	if (!indirect && vq->use_dma_api)
+	if (!indirect && vq->do_unmap)
 		vq->split.desc_extra[prev & (vq->split.vring.num - 1)].flags &=
 			~VRING_DESC_F_NEXT;
 
@@ -794,7 +802,7 @@  static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,
 				VRING_DESC_F_INDIRECT));
 		BUG_ON(len == 0 || len % sizeof(struct vring_desc));
 
-		if (vq->use_dma_api) {
+		if (vq->do_unmap) {
 			for (j = 0; j < len / sizeof(struct vring_desc); j++)
 				vring_unmap_one_split_indirect(vq, &indir_desc[j]);
 		}
@@ -1217,17 +1225,20 @@  static void vring_unmap_extra_packed(const struct vring_virtqueue *vq,
 {
 	u16 flags;
 
-	if (!vq->use_dma_api)
-		return;
-
 	flags = extra->flags;
 
 	if (flags & VRING_DESC_F_INDIRECT) {
+		if (!vq->use_dma_api)
+			return;
+
 		dma_unmap_single(vring_dma_dev(vq),
 				 extra->addr, extra->len,
 				 (flags & VRING_DESC_F_WRITE) ?
 				 DMA_FROM_DEVICE : DMA_TO_DEVICE);
 	} else {
+		if (!vq->do_unmap)
+			return;
+
 		dma_unmap_page(vring_dma_dev(vq),
 			       extra->addr, extra->len,
 			       (flags & VRING_DESC_F_WRITE) ?
@@ -1240,7 +1251,7 @@  static void vring_unmap_desc_packed(const struct vring_virtqueue *vq,
 {
 	u16 flags;
 
-	if (!vq->use_dma_api)
+	if (!vq->do_unmap)
 		return;
 
 	flags = le16_to_cpu(desc->flags);
@@ -1329,7 +1340,7 @@  static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
 				sizeof(struct vring_packed_desc));
 	vq->packed.vring.desc[head].id = cpu_to_le16(id);
 
-	if (vq->use_dma_api) {
+	if (vq->do_unmap) {
 		vq->packed.desc_extra[id].addr = addr;
 		vq->packed.desc_extra[id].len = total_sg *
 				sizeof(struct vring_packed_desc);
@@ -1470,7 +1481,7 @@  static inline int virtqueue_add_packed(struct virtqueue *_vq,
 			desc[i].len = cpu_to_le32(sg->length);
 			desc[i].id = cpu_to_le16(id);
 
-			if (unlikely(vq->use_dma_api)) {
+			if (unlikely(vq->do_unmap)) {
 				vq->packed.desc_extra[curr].addr = addr;
 				vq->packed.desc_extra[curr].len = sg->length;
 				vq->packed.desc_extra[curr].flags =
@@ -1604,7 +1615,7 @@  static void detach_buf_packed(struct vring_virtqueue *vq,
 	vq->free_head = id;
 	vq->vq.num_free += state->num;
 
-	if (unlikely(vq->use_dma_api)) {
+	if (unlikely(vq->do_unmap)) {
 		curr = id;
 		for (i = 0; i < state->num; i++) {
 			vring_unmap_extra_packed(vq,
@@ -1621,7 +1632,7 @@  static void detach_buf_packed(struct vring_virtqueue *vq,
 		if (!desc)
 			return;
 
-		if (vq->use_dma_api) {
+		if (vq->do_unmap) {
 			len = vq->packed.desc_extra[id].len;
 			for (i = 0; i < len / sizeof(struct vring_packed_desc);
 					i++)
@@ -2080,6 +2091,7 @@  static struct virtqueue *vring_create_virtqueue_packed(
 	vq->dma_dev = dma_dev;
 	vq->use_dma_api = vring_use_dma_api(vdev);
 	vq->premapped = false;
+	vq->do_unmap = vq->use_dma_api;
 
 	vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
 		!context;
@@ -2587,6 +2599,7 @@  static struct virtqueue *__vring_new_virtqueue(unsigned int index,
 	vq->dma_dev = dma_dev;
 	vq->use_dma_api = vring_use_dma_api(vdev);
 	vq->premapped = false;
+	vq->do_unmap = vq->use_dma_api;
 
 	vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
 		!context;
@@ -2765,6 +2778,7 @@  int virtqueue_set_premapped(struct virtqueue *_vq)
 		return -EINVAL;
 
 	vq->premapped = true;
+	vq->do_unmap = false;
 
 	return 0;
 }