On Mon, Mar 27, 2023 at 12:05 PM Xuan Zhuo <xuanzhuo@xxxxxxxxxxxxxxxxx> wrote: > > virtio core only supports virtual addresses, dma is completed in virtio > core. > > In some scenarios (such as the AF_XDP), the memory is allocated > and DMA mapping is completed in advance, so it is necessary for us to > support passing the DMA address to virtio core. > > Drives can use sg->dma_address to pass the mapped dma address to virtio > core. If one sg->dma_address is used then all sgs must use > sg->dma_address, otherwise all must be null when passing it to the APIs > of virtio. > > Signed-off-by: Xuan Zhuo <xuanzhuo@xxxxxxxxxxxxxxxxx> Acked-by: Jason Wang <jasowang@xxxxxxxxxx> Thanks > --- > drivers/virtio/virtio_ring.c | 31 +++++++++++++++++++++++-------- > 1 file changed, 23 insertions(+), 8 deletions(-) > > diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c > index 3ada30b475d2..9d6acd59e3e0 100644 > --- a/drivers/virtio/virtio_ring.c > +++ b/drivers/virtio/virtio_ring.c > @@ -67,9 +67,13 @@ > #define LAST_ADD_TIME_INVALID(vq) > #endif > > +#define VRING_STATE_F_MAP_INTERNAL BIT(0) > + > struct vring_desc_state_split { > void *data; /* Data for callback. */ > struct vring_desc *indir_desc; /* Indirect descriptor, if any. */ > + u32 flags; /* State flags. */ > + u32 padding; > }; > > struct vring_desc_state_packed { > @@ -448,7 +452,7 @@ static void vring_unmap_one_split_indirect(const struct vring_virtqueue *vq, > } > > static unsigned int vring_unmap_one_split(const struct vring_virtqueue *vq, > - unsigned int i) > + unsigned int i, bool dma_map_internal) > { > struct vring_desc_extra *extra = vq->split.desc_extra; > u16 flags; > @@ -465,6 +469,9 @@ static unsigned int vring_unmap_one_split(const struct vring_virtqueue *vq, > (flags & VRING_DESC_F_WRITE) ? > DMA_FROM_DEVICE : DMA_TO_DEVICE); > } else { > + if (!dma_map_internal) > + goto out; > + > dma_unmap_page(vring_dma_dev(vq), > extra[i].addr, > extra[i].len, > @@ -615,7 +622,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq, > struct scatterlist *sg; > struct vring_desc *desc; > unsigned int i, n, avail, descs_used, prev; > - bool indirect; > + bool indirect, dma_map_internal; > int head; > > START_USE(vq); > @@ -668,7 +675,8 @@ static inline int virtqueue_add_split(struct virtqueue *_vq, > return -ENOSPC; > } > > - if (virtqueue_map_sgs(vq, sgs, total_sg, out_sgs, in_sgs)) > + dma_map_internal = !sgs[0]->dma_address; > + if (dma_map_internal && virtqueue_map_sgs(vq, sgs, total_sg, out_sgs, in_sgs)) > goto err_map; > > for (n = 0; n < out_sgs; n++) { > @@ -735,6 +743,8 @@ static inline int virtqueue_add_split(struct virtqueue *_vq, > else > vq->split.desc_state[head].indir_desc = ctx; > > + vq->split.desc_state[head].flags = dma_map_internal ? VRING_STATE_F_MAP_INTERNAL : 0; > + > /* Put entry in available array (but don't update avail->idx until they > * do sync). */ > avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1); > @@ -759,7 +769,8 @@ static inline int virtqueue_add_split(struct virtqueue *_vq, > return 0; > > unmap_release: > - virtqueue_unmap_sgs(vq, sgs, total_sg, out_sgs, in_sgs); > + if (dma_map_internal) > + virtqueue_unmap_sgs(vq, sgs, total_sg, out_sgs, in_sgs); > > err_map: > if (indirect) > @@ -805,20 +816,22 @@ static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head, > { > unsigned int i, j; > __virtio16 nextflag = cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT); > + bool dma_map_internal; > > /* Clear data ptr. */ > vq->split.desc_state[head].data = NULL; > + dma_map_internal = !!(vq->split.desc_state[head].flags & VRING_STATE_F_MAP_INTERNAL); > > /* Put back on free list: unmap first-level descriptors and find end */ > i = head; > > while (vq->split.vring.desc[i].flags & nextflag) { > - vring_unmap_one_split(vq, i); > + vring_unmap_one_split(vq, i, dma_map_internal); > i = vq->split.desc_extra[i].next; > vq->vq.num_free++; > } > > - vring_unmap_one_split(vq, i); > + vring_unmap_one_split(vq, i, dma_map_internal); > vq->split.desc_extra[i].next = vq->free_head; > vq->free_head = head; > > @@ -840,8 +853,10 @@ 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)); > > - for (j = 0; j < len / sizeof(struct vring_desc); j++) > - vring_unmap_one_split_indirect(vq, &indir_desc[j]); > + if (dma_map_internal) { > + for (j = 0; j < len / sizeof(struct vring_desc); j++) > + vring_unmap_one_split_indirect(vq, &indir_desc[j]); > + } > > kfree(indir_desc); > vq->split.desc_state[head].indir_desc = NULL; > -- > 2.32.0.3.g01195cf9f > _______________________________________________ Virtualization mailing list Virtualization@xxxxxxxxxxxxxxxxxxxxxxxxxx https://lists.linuxfoundation.org/mailman/listinfo/virtualization