[PATCH vhost v2 05/12] virtio_ring: packed: support premapped

Xuan Zhuo
2023-03-08T06:44:36Z

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 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 dma_address must be null.

On the non-indirect path, if dma_address is used, extra.addr will be
set to DMA_MAPPING_ERROR. So when do unmap, we can pass it.

Signed-off-by: Xuan Zhuo <xuanzhuo at linux.alibaba.com>
---
 drivers/virtio/virtio_ring.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
index 61deaf0a4faf..66a071e3bdef 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -1258,6 +1258,9 @@ static void vring_unmap_extra_packed(const struct
vring_virtqueue *vq,
 				 (flags & VRING_DESC_F_WRITE) ?
 				 DMA_FROM_DEVICE : DMA_TO_DEVICE);
 	} else {
+		if (extra->addr == DMA_MAPPING_ERROR)
+			return;
+
 		dma_unmap_page(vring_dma_dev(vq),
 			       extra->addr, extra->len,
 			       (flags & VRING_DESC_F_WRITE) ?
@@ -1423,6 +1426,7 @@ static inline int virtqueue_add_packed(struct virtqueue
*_vq,
 	unsigned int i, n, c, descs_used;
 	__le16 head_flags, flags;
 	u16 head, id, prev, curr;
+	bool do_map;
 	int err;

 	START_USE(vq);
@@ -1468,7 +1472,8 @@ static inline int virtqueue_add_packed(struct virtqueue
*_vq,
 	id = vq->free_head;
 	BUG_ON(id == vq->packed.vring.num);

-	if (virtqueue_map_sgs(vq, sgs, total_sg, out_sgs, in_sgs))
+	do_map = !sgs[0]->dma_address;
+	if (do_map && virtqueue_map_sgs(vq, sgs, total_sg, out_sgs, in_sgs))
 		return -ENOMEM;

 	curr = id;
@@ -1488,7 +1493,8 @@ static inline int virtqueue_add_packed(struct virtqueue
*_vq,
 			desc[i].id = cpu_to_le16(id);

 			if (unlikely(vq->use_dma_api)) {
-				vq->packed.desc_extra[curr].addr = sg->dma_address;
+				vq->packed.desc_extra[curr].addr +					do_map ? sg->dma_address :
DMA_MAPPING_ERROR;
 				vq->packed.desc_extra[curr].len = sg->length;
 				vq->packed.desc_extra[curr].flags  					le16_to_cpu(flags);
--
2.32.0.3.g01195cf9f