Skip to contentSkip to conversation

virtio-net: support AF_XDP zero copy (tx)

PATCH In review Xuan Zhuo 4 participants 36 messages Aug 20, 2024 – Sep 13, 2024

Subsystems (5)
lore
Share & tools
lore

Xuan Zhuo

author reviewer
#0 lore raw

cover virtio-net: support AF_XDP zero copy (tx)

## AF_XDP

XDP socket(AF_XDP) is an excellent bypass kernel network framework. The zero copy feature of xsk (XDP socket) needs to be supported by the driver. The performance of zero copy is very good. mlx5 and intel ixgbe already support this feature, This patch set allows virtio-net to support xsk's zerocopy xmit feature.

At present, we have completed some preparation:

1. vq-reset (virtio spec and kernel code)
2. virtio-core premapped dma
3. virtio-net xdp refactor

So it is time for Virtio-Net to complete the support for the XDP Socket Zerocopy.

Virtio-net can not increase the queue num at will, so xsk shares the queue with kernel.

This patch set includes some refactor to the virtio-net to let that to support AF_XDP.

## About virtio premapped mode

The current configuration sets the virtqueue (vq) to premapped mode, implying that all buffers submitted to this queue must be mapped ahead of time. This presents a challenge for the virtnet send queue (sq): the virtnet driver would be required to keep track of dma information for vq size * 17, which can be substantial. However, if the premapped mode were applied on a per-buffer basis, the complexity would be greatly reduced. With AF_XDP enabled, AF_XDP buffers would become premapped, while kernel skb buffers could remain unmapped.

We can distinguish them by sg_page(sg), When sg_page(sg) is NULL, this indicates that the driver has performed DMA mapping in advance, allowing the Virtio core to directly utilize sg_dma_address(sg) without conducting any internal DMA mapping. Additionally, DMA unmap operations for this buffer will be bypassed.

## performance

ENV: Qemu with vhost-user(polling mode).
Host CPU: Intel(R) Xeon(R) Platinum 8163 CPU @ 2.50GHz

### virtio PMD in guest with testpmd

testpmd> show port stats all

######################## NIC statistics for port 0 ########################
RX-packets: 19531092064 RX-missed: 0 RX-bytes: 1093741155584
RX-errors: 0
RX-nombuf: 0
TX-packets: 5959955552 TX-errors: 0 TX-bytes: 371030645664

Throughput (since last show)

 Rx-pps:   8861574     Rx-bps:  3969985208
 Tx-pps:   8861493     Tx-bps:  3969962736

############################################################################

### AF_XDP PMD in guest with testpmd

testpmd> show port stats all

######################## NIC statistics for port 0 ########################
RX-packets: 68152727 RX-missed: 0 RX-bytes: 3816552712
RX-errors: 0
RX-nombuf: 0
TX-packets: 68114967 TX-errors: 33216 TX-bytes: 3814438152

Throughput (since last show)

  Rx-pps:      6333196          Rx-bps:   2837272088
  Tx-pps:      6333227          Tx-bps:   2837285936

############################################################################

But AF_XDP consumes more CPU for tx and rx napi(100% and 86%).

Please review.

Thanks.

Xuan Zhuo (13):
virtio_ring: introduce vring_need_unmap_buffer
virtio_ring: split: harden dma unmap for indirect
virtio_ring: packed: harden dma unmap for indirect
virtio_ring: perform premapped operations based on per-buffer
virtio-net: rq submits premapped buffer per buffer
virtio_ring: remove API virtqueue_set_dma_premapped
virtio_net: refactor the xmit type
virtio_net: xsk: bind/unbind xsk for tx
virtio_net: xsk: prevent disable tx napi
virtio_net: xsk: tx: support xmit xsk buffer
virtio_net: xsk: tx: handle the transmitted xsk buffer
virtio_net: update tx timeout record
virtio_net: xdp_features add NETDEV_XDP_ACT_XSK_ZEROCOPY

 drivers/net/virtio_net.c     | 363 ++++++++++++++++++++++++++++-------
 drivers/virtio/virtio_ring.c | 302 ++++++++++++-----------------
 include/linux/virtio.h       |   2 -
 3 files changed, 421 insertions(+), 246 deletions(-)

--
2.32.0.3.g01195cf9f

author reviewer
#1 lore raw

PATCH 1/13 virtio_ring: introduce vring_need_unmap_buffer

To make the code readable, introduce vring_need_unmap_buffer() to replace do_unmap.

use_dma_api premapped -> vring_need_unmap_buffer()

1. false       false        false
2. true        false        true
3. true        true         false
---
 drivers/virtio/virtio_ring.c | 27 ++++++++++++---------------
 1 file changed, 12 insertions(+), 15 deletions(-)
drivers/virtio/virtio_ring.c +12 -15
index be7309b1e860..228e9fbcba3f 100644
@@ -175,11 +175,6 @@ 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. */
@@ -297,6 +292,11 @@ static bool vring_use_dma_api(const struct virtio_device *vdev)
 	return false;
 }
 
+static bool vring_need_unmap_buffer(const struct vring_virtqueue *vring)
+{
+	return vring->use_dma_api && !vring->premapped;
+}
+
 size_t virtio_max_dma_size(const struct virtio_device *vdev)
 {
 	size_t max_segment_size = SIZE_MAX;
@@ -445,7 +445,7 @@ static void vring_unmap_one_split_indirect(const struct vring_virtqueue *vq,
 {
 	u16 flags;
 
-	if (!vq->do_unmap)
+	if (!vring_need_unmap_buffer(vq))
 		return;
 
 	flags = virtio16_to_cpu(vq->vq.vdev, desc->flags);
@@ -475,7 +475,7 @@ 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 (!vq->do_unmap)
+		if (!vring_need_unmap_buffer(vq))
 			goto out;
 
 		dma_unmap_page(vring_dma_dev(vq),
@@ -643,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->do_unmap)
+	if (!indirect && vring_need_unmap_buffer(vq))
 		vq->split.desc_extra[prev & (vq->split.vring.num - 1)].flags &=
 			~VRING_DESC_F_NEXT;
 
@@ -802,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->do_unmap) {
+		if (vring_need_unmap_buffer(vq)) {
 			for (j = 0; j < len / sizeof(struct vring_desc); j++)
 				vring_unmap_one_split_indirect(vq, &indir_desc[j]);
 		}
@@ -1236,7 +1236,7 @@ static void vring_unmap_extra_packed(const struct vring_virtqueue *vq,
 				 (flags & VRING_DESC_F_WRITE) ?
 				 DMA_FROM_DEVICE : DMA_TO_DEVICE);
 	} else {
-		if (!vq->do_unmap)
+		if (!vring_need_unmap_buffer(vq))
 			return;
 
 		dma_unmap_page(vring_dma_dev(vq),
@@ -1251,7 +1251,7 @@ static void vring_unmap_desc_packed(const struct vring_virtqueue *vq,
 {
 	u16 flags;
 
-	if (!vq->do_unmap)
+	if (!vring_need_unmap_buffer(vq))
 		return;
 
 	flags = le16_to_cpu(desc->flags);
@@ -1632,7 +1632,7 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
 		if (!desc)
 			return;
 
-		if (vq->do_unmap) {
+		if (vring_need_unmap_buffer(vq)) {
 			len = vq->packed.desc_extra[id].len;
 			for (i = 0; i < len / sizeof(struct vring_packed_desc);
 					i++)
@@ -2091,7 +2091,6 @@ 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;
@@ -2636,7 +2635,6 @@ 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;
@@ -2799,7 +2797,6 @@ int virtqueue_set_dma_premapped(struct virtqueue *_vq)
 	}
 
 	vq->premapped = true;
-	vq->do_unmap = false;
 
 	END_USE(vq);
 
Signature · 1 line
2.32.0.3.g01195cf9f
author reviewer
#2 lore raw

PATCH 2/13 virtio_ring: split: harden dma unmap for indirect

In reply to Xuan Zhuo

1. this commit hardens dma unmap for indirect
2. the subsequent commit uses the struct extra to record whether the buffers need to be unmapped or not. So we need a struct extra for every desc, whatever it is indirect or not.

---
 drivers/virtio/virtio_ring.c | 122 ++++++++++++++++-------------------
 1 file changed, 57 insertions(+), 65 deletions(-)
drivers/virtio/virtio_ring.c +57 -65
index 228e9fbcba3f..582d2c05498a 100644
@@ -67,9 +67,16 @@
 #define LAST_ADD_TIME_INVALID(vq)
 #endif
 
+struct vring_desc_extra {
+	dma_addr_t addr;		/* Descriptor DMA addr. */
+	u32 len;			/* Descriptor length. */
+	u16 flags;			/* Descriptor flags. */
+	u16 next;			/* The next desc state in a list. */
+};
+
 struct vring_desc_state_split {
 	void *data;			/* Data for callback. */
-	struct vring_desc *indir_desc;	/* Indirect descriptor, if any. */
+	struct vring_desc_extra *indir;	/* Indirect descriptor, if any. */
 };
 
 struct vring_desc_state_packed {
@@ -79,13 +86,6 @@ struct vring_desc_state_packed {
 	u16 last;			/* The last desc state in a list. */
 };
 
-struct vring_desc_extra {
-	dma_addr_t addr;		/* Descriptor DMA addr. */
-	u32 len;			/* Descriptor length. */
-	u16 flags;			/* Descriptor flags. */
-	u16 next;			/* The next desc state in a list. */
-};
-
 struct vring_virtqueue_split {
 	/* Actual memory layout for this queue. */
 	struct vring vring;
@@ -440,38 +440,20 @@ static void virtqueue_init(struct vring_virtqueue *vq, u32 num)
  * Split ring specific functions - *_split().
  */
 
-static void vring_unmap_one_split_indirect(const struct vring_virtqueue *vq,
-					   const struct vring_desc *desc)
-{
-	u16 flags;
-
-	if (!vring_need_unmap_buffer(vq))
-		return;
-
-	flags = virtio16_to_cpu(vq->vq.vdev, desc->flags);
-
-	dma_unmap_page(vring_dma_dev(vq),
-		       virtio64_to_cpu(vq->vq.vdev, desc->addr),
-		       virtio32_to_cpu(vq->vq.vdev, desc->len),
-		       (flags & VRING_DESC_F_WRITE) ?
-		       DMA_FROM_DEVICE : DMA_TO_DEVICE);
-}
-
 static unsigned int vring_unmap_one_split(const struct vring_virtqueue *vq,
-					  unsigned int i)
+					  struct vring_desc_extra *extra)
 {
-	struct vring_desc_extra *extra = vq->split.desc_extra;
 	u16 flags;
 
-	flags = extra[i].flags;
+	flags = extra->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,
+				 extra->addr,
+				 extra->len,
 				 (flags & VRING_DESC_F_WRITE) ?
 				 DMA_FROM_DEVICE : DMA_TO_DEVICE);
 	} else {
@@ -479,20 +461,22 @@ static unsigned int vring_unmap_one_split(const struct vring_virtqueue *vq,
 			goto out;
 
 		dma_unmap_page(vring_dma_dev(vq),
-			       extra[i].addr,
-			       extra[i].len,
+			       extra->addr,
+			       extra->len,
 			       (flags & VRING_DESC_F_WRITE) ?
 			       DMA_FROM_DEVICE : DMA_TO_DEVICE);
 	}
 
 out:
-	return extra[i].next;
+	return extra->next;
 }
 
 static struct vring_desc *alloc_indirect_split(struct virtqueue *_vq,
 					       unsigned int total_sg,
+					       struct vring_desc_extra **pextra,
 					       gfp_t gfp)
 {
+	struct vring_desc_extra *extra;
 	struct vring_desc *desc;
 	unsigned int i;
 
@@ -503,40 +487,45 @@ static struct vring_desc *alloc_indirect_split(struct virtqueue *_vq,
 	 */
 	gfp &= ~__GFP_HIGHMEM;
 
-	desc = kmalloc_array(total_sg, sizeof(struct vring_desc), gfp);
-	if (!desc)
+	extra = kmalloc_array(total_sg, sizeof(*desc) + sizeof(*extra), gfp);
+	if (!extra)
 		return NULL;
 
-	for (i = 0; i < total_sg; i++)
+	desc = (struct vring_desc *)&extra[total_sg];
+
+	for (i = 0; i < total_sg; i++) {
 		desc[i].next = cpu_to_virtio16(_vq->vdev, i + 1);
+		extra[i].next = i + 1;
+	}
+
+	*pextra = extra;
+
 	return desc;
 }
 
 static inline unsigned int virtqueue_add_desc_split(struct virtqueue *vq,
 						    struct vring_desc *desc,
+						    struct vring_desc_extra *extra,
 						    unsigned int i,
 						    dma_addr_t addr,
 						    unsigned int len,
 						    u16 flags,
 						    bool indirect)
 {
-	struct vring_virtqueue *vring = to_vvq(vq);
-	struct vring_desc_extra *extra = vring->split.desc_extra;
 	u16 next;
 
 	desc[i].flags = cpu_to_virtio16(vq->vdev, flags);
 	desc[i].addr = cpu_to_virtio64(vq->vdev, addr);
 	desc[i].len = cpu_to_virtio32(vq->vdev, len);
 
-	if (!indirect) {
-		next = extra[i].next;
-		desc[i].next = cpu_to_virtio16(vq->vdev, next);
+	extra[i].addr = addr;
+	extra[i].len = len;
+	extra[i].flags = flags;
 
-		extra[i].addr = addr;
-		extra[i].len = len;
-		extra[i].flags = flags;
-	} else
-		next = virtio16_to_cpu(vq->vdev, desc[i].next);
+	next = extra[i].next;
+
+	if (!indirect)
+		desc[i].next = cpu_to_virtio16(vq->vdev, next);
 
 	return next;
 }
@@ -551,6 +540,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 				      gfp_t gfp)
 {
 	struct vring_virtqueue *vq = to_vvq(_vq);
+	struct vring_desc_extra *extra;
 	struct scatterlist *sg;
 	struct vring_desc *desc;
 	unsigned int i, n, avail, descs_used, prev, err_idx;
@@ -574,7 +564,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 	head = vq->free_head;
 
 	if (virtqueue_use_indirect(vq, total_sg))
-		desc = alloc_indirect_split(_vq, total_sg, gfp);
+		desc = alloc_indirect_split(_vq, total_sg, &extra, gfp);
 	else {
 		desc = NULL;
 		WARN_ON_ONCE(total_sg > vq->split.vring.num && !vq->indirect);
@@ -589,6 +579,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 	} else {
 		indirect = false;
 		desc = vq->split.vring.desc;
+		extra = vq->split.desc_extra;
 		i = head;
 		descs_used = total_sg;
 	}
@@ -618,7 +609,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 			/* Note that we trust indirect descriptor
 			 * table since it use stream DMA mapping.
 			 */
-			i = virtqueue_add_desc_split(_vq, desc, i, addr, sg->length,
+			i = virtqueue_add_desc_split(_vq, desc, extra, i, addr, sg->length,
 						     VRING_DESC_F_NEXT,
 						     indirect);
 		}
@@ -634,7 +625,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 			/* Note that we trust indirect descriptor
 			 * table since it use stream DMA mapping.
 			 */
-			i = virtqueue_add_desc_split(_vq, desc, i, addr,
+			i = virtqueue_add_desc_split(_vq, desc, extra, i, addr,
 						     sg->length,
 						     VRING_DESC_F_NEXT |
 						     VRING_DESC_F_WRITE,
@@ -660,6 +651,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 		}
 
 		virtqueue_add_desc_split(_vq, vq->split.vring.desc,
+					 vq->split.desc_extra,
 					 head, addr,
 					 total_sg * sizeof(struct vring_desc),
 					 VRING_DESC_F_INDIRECT,
@@ -678,9 +670,9 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 	/* Store token and indirect buffer state. */
 	vq->split.desc_state[head].data = data;
 	if (indirect)
-		vq->split.desc_state[head].indir_desc = desc;
+		vq->split.desc_state[head].indir = extra;
 	else
-		vq->split.desc_state[head].indir_desc = ctx;
+		vq->split.desc_state[head].indir = ctx;
 
 	/* Put entry in available array (but don't update avail->idx until they
 	 * do sync). */
@@ -716,11 +708,8 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 	for (n = 0; n < total_sg; n++) {
 		if (i == err_idx)
 			break;
-		if (indirect) {
-			vring_unmap_one_split_indirect(vq, &desc[i]);
-			i = virtio16_to_cpu(_vq->vdev, desc[i].next);
-		} else
-			i = vring_unmap_one_split(vq, i);
+
+		i = vring_unmap_one_split(vq, &extra[i]);
 	}
 
 free_indirect:
@@ -765,22 +754,25 @@ static bool virtqueue_kick_prepare_split(struct virtqueue *_vq)
 static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,
 			     void **ctx)
 {
+	struct vring_desc_extra *extra;
 	unsigned int i, j;
 	__virtio16 nextflag = cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT);
 
 	/* Clear data ptr. */
 	vq->split.desc_state[head].data = NULL;
 
+	extra = vq->split.desc_extra;
+
 	/* 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, &extra[i]);
 		i = vq->split.desc_extra[i].next;
 		vq->vq.num_free++;
 	}
 
-	vring_unmap_one_split(vq, i);
+	vring_unmap_one_split(vq, &extra[i]);
 	vq->split.desc_extra[i].next = vq->free_head;
 	vq->free_head = head;
 
@@ -788,12 +780,12 @@ static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,
 	vq->vq.num_free++;
 
 	if (vq->indirect) {
-		struct vring_desc *indir_desc =
-				vq->split.desc_state[head].indir_desc;
 		u32 len;
 
+		extra = vq->split.desc_state[head].indir;
+
 		/* Free the indirect table, if any, now that it's unmapped. */
-		if (!indir_desc)
+		if (!extra)
 			return;
 
 		len = vq->split.desc_extra[head].len;
@@ -804,13 +796,13 @@ static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,
 
 		if (vring_need_unmap_buffer(vq)) {
 			for (j = 0; j < len / sizeof(struct vring_desc); j++)
-				vring_unmap_one_split_indirect(vq, &indir_desc[j]);
+				vring_unmap_one_split(vq, &extra[j]);
 		}
 
-		kfree(indir_desc);
-		vq->split.desc_state[head].indir_desc = NULL;
+		kfree(extra);
+		vq->split.desc_state[head].indir = NULL;
 	} else if (ctx) {
-		*ctx = vq->split.desc_state[head].indir_desc;
+		*ctx = vq->split.desc_state[head].indir;
 	}
 }
 
Signature · 1 line
2.32.0.3.g01195cf9f
#3 lore raw

Re: [PATCH net-next 02/13] virtio_ring: split: harden dma unmap for indirect

Quoted 2 lines · Xuan Zhuo

1. this commit hardens dma unmap for indirect

I think we need to explain why we need such hardening. For example indirect use stream mapping which is read-only from the device. So it looks to me like it doesn't require hardening by itself.

Quoted 2 lines

2. the subsequent commit uses the struct extra to record whether the buffers need to be unmapped or not.

It's better to explain why such a decision could not be implied with the existing metadata.

Quoted 2 lines

So we need a struct extra for
every desc, whatever it is indirect or not.

If this is the real reason, we need to tweak the title.

Quoted 15 lines
---
 drivers/virtio/virtio_ring.c | 122 ++++++++++++++++-------------------
 1 file changed, 57 insertions(+), 65 deletions(-)
drivers/virtio/virtio_ring.c +1 -0
index 228e9fbcba3f..582d2c05498a 100644
@@ -67,9 +67,16 @@
 #define LAST_ADD_TIME_INVALID(vq)
 #endif
 
+struct vring_desc_extra {

drivers/virtio/virtio_ring.c line 79

Quoted 10 lines
drivers/virtio/virtio_ring.c +7 -1
+       dma_addr_t addr;                /* Descriptor DMA addr. */
+       u32 len;                        /* Descriptor length. */
+       u16 flags;                      /* Descriptor flags. */
+       u16 next;                       /* The next desc state in a list. */
+};
+
 struct vring_desc_state_split {
        void *data;                     /* Data for callback. */
-       struct vring_desc *indir_desc;  /* Indirect descriptor, if any. */
+       struct vring_desc_extra *indir; /* Indirect descriptor, if any. */

Btw, it might be worth explaining that this will be allocated with an indirect descriptor table so we won't stress more to the memory allocator.

Thanks

#4 lore raw

Re: [PATCH net-next 02/13] virtio_ring: split: harden dma unmap for indirect

Quoted 5 lines · Jason Wang
Quoted 2 lines · Xuan Zhuo

1. this commit hardens dma unmap for indirect

I think we need to explain why we need such hardening.

yes pls be more specific. Recording same state in two places is just a source of bugs, not hardening.

author reviewer
#5 lore raw

Re: [PATCH net-next 02/13] virtio_ring: split: harden dma unmap for indirect

In reply to Jason Wang
Quoted 11 lines · Jason Wang
Quoted 2 lines · Xuan Zhuo

1. this commit hardens dma unmap for indirect

I think we need to explain why we need such hardening. For example indirect use stream mapping which is read-only from the device. So it looks to me like it doesn't require hardening by itself.

Quoted 2 lines

2. the subsequent commit uses the struct extra to record whether the buffers need to be unmapped or not.

Quoted 7 lines

It's better to explain why such a decision could not be implied with the existing metadata.

Quoted 2 lines

So we need a struct extra for
every desc, whatever it is indirect or not.

If this is the real reason, we need to tweak the title.

YES. It is.

I will tweak the title in next version.

Quoted 15 lines
Quoted 15 lines
---
 drivers/virtio/virtio_ring.c | 122 ++++++++++++++++-------------------
 1 file changed, 57 insertions(+), 65 deletions(-)
drivers/virtio/virtio_ring.c +0 -0
index 228e9fbcba3f..582d2c05498a 100644
@@ -67,9 +67,16 @@
 #define LAST_ADD_TIME_INVALID(vq)
 #endif
 
Quoted 15 lines

drivers/virtio/virtio_ring.c line 79

Quoted 11 lines
drivers/virtio/virtio_ring.c +8 -1
+struct vring_desc_extra {
+       dma_addr_t addr;                /* Descriptor DMA addr. */
+       u32 len;                        /* Descriptor length. */
+       u16 flags;                      /* Descriptor flags. */
+       u16 next;                       /* The next desc state in a list. */
+};
+
 struct vring_desc_state_split {
        void *data;                     /* Data for callback. */
-       struct vring_desc *indir_desc;  /* Indirect descriptor, if any. */
+       struct vring_desc_extra *indir; /* Indirect descriptor, if any. */

Btw, it might be worth explaining that this will be allocated with an indirect descriptor table so we won't stress more to the memory allocator.

Will do.

Thanks.

Quoted 4 lines

Thanks

author reviewer
#6 lore raw

PATCH 3/13 virtio_ring: packed: harden dma unmap for indirect

In reply to Xuan Zhuo

1. this commit hardens dma unmap for indirect
2. the subsequent commit uses the struct extra to record whether the buffers need to be unmapped or not. So we need a struct extra for every desc, whatever it is indirect or not.

---
 drivers/virtio/virtio_ring.c | 57 ++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 28 deletions(-)
drivers/virtio/virtio_ring.c +29 -28
index 582d2c05498a..b43eca93015c 100644
@@ -81,7 +81,7 @@ struct vring_desc_state_split {
 
 struct vring_desc_state_packed {
 	void *data;			/* Data for callback. */
-	struct vring_packed_desc *indir_desc; /* Indirect descriptor, if any. */
+	struct vring_desc_extra *indir; /* Indirect descriptor, if any. */
 	u16 num;			/* Descriptor list length. */
 	u16 last;			/* The last desc state in a list. */
 };
@@ -1238,27 +1238,13 @@ static void vring_unmap_extra_packed(const struct vring_virtqueue *vq,
 	}
 }
 
-static void vring_unmap_desc_packed(const struct vring_virtqueue *vq,
-				    const struct vring_packed_desc *desc)
-{
-	u16 flags;
-
-	if (!vring_need_unmap_buffer(vq))
-		return;
-
-	flags = le16_to_cpu(desc->flags);
-
-	dma_unmap_page(vring_dma_dev(vq),
-		       le64_to_cpu(desc->addr),
-		       le32_to_cpu(desc->len),
-		       (flags & VRING_DESC_F_WRITE) ?
-		       DMA_FROM_DEVICE : DMA_TO_DEVICE);
-}
-
 static struct vring_packed_desc *alloc_indirect_packed(unsigned int total_sg,
+						       struct vring_desc_extra **pextra,
 						       gfp_t gfp)
 {
+	struct vring_desc_extra *extra;
 	struct vring_packed_desc *desc;
+	int i;
 
 	/*
 	 * We require lowmem mappings for the descriptors because
@@ -1267,7 +1253,14 @@ static struct vring_packed_desc *alloc_indirect_packed(unsigned int total_sg,
 	 */
 	gfp &= ~__GFP_HIGHMEM;
 
-	desc = kmalloc_array(total_sg, sizeof(struct vring_packed_desc), gfp);
+	extra = kmalloc_array(total_sg, sizeof(*desc) + sizeof(*extra), gfp);
+
+	desc = (struct vring_packed_desc *)&extra[total_sg];
+
+	for (i = 0; i < total_sg; i++)
+		extra[i].next = i + 1;
+
+	*pextra = extra;
 
 	return desc;
 }
@@ -1280,6 +1273,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
 					 void *data,
 					 gfp_t gfp)
 {
+	struct vring_desc_extra *extra;
 	struct vring_packed_desc *desc;
 	struct scatterlist *sg;
 	unsigned int i, n, err_idx;
@@ -1287,7 +1281,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
 	dma_addr_t addr;
 
 	head = vq->packed.next_avail_idx;
-	desc = alloc_indirect_packed(total_sg, gfp);
+	desc = alloc_indirect_packed(total_sg, &extra, gfp);
 	if (!desc)
 		return -ENOMEM;
 
@@ -1313,6 +1307,12 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
 			desc[i].addr = cpu_to_le64(addr);
 			desc[i].len = cpu_to_le32(sg->length);
 			i++;
+
+			if (unlikely(vq->use_dma_api)) {
+				extra[i].addr = addr;
+				extra[i].len = sg->length;
+				extra[i].flags = n < out_sgs ?  0 : VRING_DESC_F_WRITE;
+			}
 		}
 	}
 
@@ -1367,7 +1367,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
 	/* Store token and indirect buffer state. */
 	vq->packed.desc_state[id].num = 1;
 	vq->packed.desc_state[id].data = data;
-	vq->packed.desc_state[id].indir_desc = desc;
+	vq->packed.desc_state[id].indir = extra;
 	vq->packed.desc_state[id].last = id;
 
 	vq->num_added += 1;
@@ -1381,7 +1381,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
 	err_idx = i;
 
 	for (i = 0; i < err_idx; i++)
-		vring_unmap_desc_packed(vq, &desc[i]);
+		vring_unmap_extra_packed(vq, &extra[i]);
 
 free_desc:
 	kfree(desc);
@@ -1504,7 +1504,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,
 	/* Store token. */
 	vq->packed.desc_state[id].num = descs_used;
 	vq->packed.desc_state[id].data = data;
-	vq->packed.desc_state[id].indir_desc = ctx;
+	vq->packed.desc_state[id].indir = ctx;
 	vq->packed.desc_state[id].last = prev;
 
 	/*
@@ -1617,23 +1617,24 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
 	}
 
 	if (vq->indirect) {
+		struct vring_desc_extra *extra;
 		u32 len;
 
 		/* Free the indirect table, if any, now that it's unmapped. */
-		desc = state->indir_desc;
-		if (!desc)
+		extra = state->indir;
+		if (!extra)
 			return;
 
 		if (vring_need_unmap_buffer(vq)) {
 			len = vq->packed.desc_extra[id].len;
 			for (i = 0; i < len / sizeof(struct vring_packed_desc);
 					i++)
-				vring_unmap_desc_packed(vq, &desc[i]);
+				vring_unmap_extra_packed(vq, &extra[i]);
 		}
 		kfree(desc);
-		state->indir_desc = NULL;
+		state->indir = NULL;
 	} else if (ctx) {
-		*ctx = state->indir_desc;
+		*ctx = state->indir;
 	}
 }
 
Signature · 1 line
2.32.0.3.g01195cf9f
#7 lore raw

Re: [PATCH net-next 03/13] virtio_ring: packed: harden dma unmap for indirect

Hi Xuan,

kernel test robot noticed the following build warnings:

url:    https://github.com/intel-lab-lkp/linux/commits/Xuan-Zhuo/virtio_ring-introduce-vring_need_unmap_buffer/20240820-153644
base:   net-next/main
patch link:    https://lore.kernel.org/r/20240820073330.9161-4-xuanzhuo%40linux.alibaba.com
patch subject: [PATCH net-next 03/13] virtio_ring: packed: harden dma unmap for indirect
config: x86_64-randconfig-161-20240820 (https://download.01.org/0day-ci/archive/20240821/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

New smatch warnings:
drivers/virtio/virtio_ring.c:1634 detach_buf_packed() error: uninitialized symbol 'desc'.

vim +/desc +1634 drivers/virtio/virtio_ring.c

1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1594  static void detach_buf_packed(struct vring_virtqueue *vq,
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1595  			      unsigned int id, void **ctx)
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1596  {
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1597  	struct vring_desc_state_packed *state = NULL;
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1598  	struct vring_packed_desc *desc;
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1599  	unsigned int i, curr;
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1600  
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1601  	state = &vq->packed.desc_state[id];
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1602  
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1603  	/* Clear data ptr. */
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1604  	state->data = NULL;
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1605  
aeef9b4733c5c2 Jason Wang 2021-06-04  1606  	vq->packed.desc_extra[state->last].next = vq->free_head;
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1607  	vq->free_head = id;
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1608  	vq->vq.num_free += state->num;
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1609  
d5c0ed17fea60c Xuan Zhuo  2024-02-23  1610  	if (unlikely(vq->use_dma_api)) {
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1611  		curr = id;
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1612  		for (i = 0; i < state->num; i++) {
d80dc15bb6e76a Xuan Zhuo  2022-02-24  1613  			vring_unmap_extra_packed(vq,
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1614  						 &vq->packed.desc_extra[curr]);
aeef9b4733c5c2 Jason Wang 2021-06-04  1615  			curr = vq->packed.desc_extra[curr].next;
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1616  		}
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1617  	}
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1618  
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1619  	if (vq->indirect) {
dfcc54f92ab71c Xuan Zhuo  2024-08-20  1620  		struct vring_desc_extra *extra;
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1621  		u32 len;
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1622  
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1623  		/* Free the indirect table, if any, now that it's unmapped. */
dfcc54f92ab71c Xuan Zhuo  2024-08-20  1624  		extra = state->indir;
dfcc54f92ab71c Xuan Zhuo  2024-08-20  1625  		if (!extra)
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1626  			return;
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1627  
de6a29c4b4c442 Xuan Zhuo  2024-08-20  1628  		if (vring_need_unmap_buffer(vq)) {
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1629  			len = vq->packed.desc_extra[id].len;
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1630  			for (i = 0; i < len / sizeof(struct vring_packed_desc);
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1631  					i++)
dfcc54f92ab71c Xuan Zhuo  2024-08-20  1632  				vring_unmap_extra_packed(vq, &extra[i]);
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1633  		}
1ce9e6055fa0a9 Tiwei Bie  2018-11-21 @1634  		kfree(desc);
                                                              ^^^^

desc is never initialized/used.

dfcc54f92ab71c Xuan Zhuo  2024-08-20  1635  		state->indir = NULL;
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1636  	} else if (ctx) {
dfcc54f92ab71c Xuan Zhuo  2024-08-20  1637  		*ctx = state->indir;
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1638  	}
1ce9e6055fa0a9 Tiwei Bie  2018-11-21  1639  }
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#8 lore raw

Re: [PATCH net-next 03/13] virtio_ring: packed: harden dma unmap for indirect

In reply to Xuan Zhuo

As gcc luckily noted:

drivers/virtio/virtio_ring.c static void detach_buf_packed(struct vring_virtqueue *vq, · line 1624

Quoted 10 lines · Xuan Zhuo
drivers/virtio/virtio_ring.c +1 -2
@@ -1617,23 +1617,24 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
 	}
 
 	if (vq->indirect) {
+		struct vring_desc_extra *extra;
 		u32 len;
 
 		/* Free the indirect table, if any, now that it's unmapped. */
-		desc = state->indir_desc;
-		if (!desc)

desc is no longer initialized here

drivers/virtio/virtio_ring.c static void detach_buf_packed(struct vring_virtqueue *vq, · line 1634

Quoted 12 lines
drivers/virtio/virtio_ring.c +3 -1
+		extra = state->indir;
+		if (!extra)
 			return;
 
 		if (vring_need_unmap_buffer(vq)) {
 			len = vq->packed.desc_extra[id].len;
 			for (i = 0; i < len / sizeof(struct vring_packed_desc);
 					i++)
-				vring_unmap_desc_packed(vq, &desc[i]);
+				vring_unmap_extra_packed(vq, &extra[i]);
 		}
 		kfree(desc);

but freed here

drivers/virtio/virtio_ring.c static void detach_buf_packed(struct vring_virtqueue *vq, · line 1639

Quoted 7 lines
drivers/virtio/virtio_ring.c +2 -2
-		state->indir_desc = NULL;
+		state->indir = NULL;
 	} else if (ctx) {
-		*ctx = state->indir_desc;
+		*ctx = state->indir;
 	}
 }

It seems unlikely this was always 0 on all paths with even a small amount of stress, so now I question how this was tested. Besides, do not ignore compiler warnings, and do not tweak code to just make compiler shut up - they are your friend.

Quoted 3 lines
Signature · 2 lines
-- 
2.32.0.3.g01195cf9f
author reviewer
#9 lore raw

Re: [PATCH net-next 03/13] virtio_ring: packed: harden dma unmap for indirect

Quoted 10 lines · Michael S. Tsirkin

As gcc luckily noted:

Quoted 7 lines · Xuan Zhuo
drivers/virtio/virtio_ring.c +1 -0
@@ -1617,23 +1617,24 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
 	}
 
 	if (vq->indirect) {
+		struct vring_desc_extra *extra;
 		u32 len;
 
Quoted 5 lines

drivers/virtio/virtio_ring.c static void detach_buf_packed(struct vring_virtqueue *vq, · line 1624

Quoted 3 lines
drivers/virtio/virtio_ring.c +0 -2
 		/* Free the indirect table, if any, now that it's unmapped. */
-		desc = state->indir_desc;
-		if (!desc)

desc is no longer initialized here

Will fix.

Quoted 17 lines

drivers/virtio/virtio_ring.c static void detach_buf_packed(struct vring_virtqueue *vq, · line 1634

Quoted 13 lines
drivers/virtio/virtio_ring.c +3 -1
+		extra = state->indir;
+		if (!extra)
 			return;
 
 		if (vring_need_unmap_buffer(vq)) {
 			len = vq->packed.desc_extra[id].len;
 			for (i = 0; i < len / sizeof(struct vring_packed_desc);
 					i++)
-				vring_unmap_desc_packed(vq, &desc[i]);
+				vring_unmap_extra_packed(vq, &extra[i]);
 		}
 		kfree(desc);

but freed here

Quoted 13 lines

drivers/virtio/virtio_ring.c static void detach_buf_packed(struct vring_virtqueue *vq, · line 1639

Quoted 7 lines
drivers/virtio/virtio_ring.c +2 -2
-		state->indir_desc = NULL;
+		state->indir = NULL;
 	} else if (ctx) {
-		*ctx = state->indir_desc;
+		*ctx = state->indir;
 	}
 }

It seems unlikely this was always 0 on all paths with even a small amount of stress, so now I question how this was tested. Besides, do not ignore compiler warnings, and do not tweak code to just make compiler shut up - they are your friend.

I agree.

Normally I do this by make W=12, but we have too many message, so I missed this.

make W=12 drivers/net/virtio_net.o drivers/virtio/virtio_ring.o

If not W=12, then I did not get any warning message. How do you get the message quickly?

Thanks.

Quoted 5 lines
Quoted 4 lines
(quoted diff) +0 -1
--

2.32.0.3.g01195cf9f

#10 lore raw

Re: [PATCH net-next 03/13] virtio_ring: packed: harden dma unmap for indirect

Quoted 57 lines · Xuan Zhuo
Quoted 10 lines · Michael S. Tsirkin

As gcc luckily noted:

Quoted 7 lines · Xuan Zhuo
drivers/virtio/virtio_ring.c +1 -0
@@ -1617,23 +1617,24 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
 	}
 
 	if (vq->indirect) {
+		struct vring_desc_extra *extra;
 		u32 len;
 
Quoted 5 lines

drivers/virtio/virtio_ring.c static void detach_buf_packed(struct vring_virtqueue *vq, · line 1624

Quoted 3 lines
drivers/virtio/virtio_ring.c +0 -2
 		/* Free the indirect table, if any, now that it's unmapped. */
-		desc = state->indir_desc;
-		if (!desc)

desc is no longer initialized here

Will fix.

Quoted 17 lines

drivers/virtio/virtio_ring.c static void detach_buf_packed(struct vring_virtqueue *vq, · line 1634

Quoted 13 lines
drivers/virtio/virtio_ring.c +3 -1
+		extra = state->indir;
+		if (!extra)
 			return;
 
 		if (vring_need_unmap_buffer(vq)) {
 			len = vq->packed.desc_extra[id].len;
 			for (i = 0; i < len / sizeof(struct vring_packed_desc);
 					i++)
-				vring_unmap_desc_packed(vq, &desc[i]);
+				vring_unmap_extra_packed(vq, &extra[i]);
 		}
 		kfree(desc);

but freed here

Quoted 13 lines

drivers/virtio/virtio_ring.c static void detach_buf_packed(struct vring_virtqueue *vq, · line 1639

Quoted 7 lines
drivers/virtio/virtio_ring.c +2 -2
-		state->indir_desc = NULL;
+		state->indir = NULL;
 	} else if (ctx) {
-		*ctx = state->indir_desc;
+		*ctx = state->indir;
 	}
 }

It seems unlikely this was always 0 on all paths with even a small amount of stress, so now I question how this was tested. Besides, do not ignore compiler warnings, and do not tweak code to just make compiler shut up - they are your friend.

I agree.

Normally I do this by make W=12, but we have too many message, so I missed this.

Quoted 6 lines

make W=12 drivers/net/virtio_net.o drivers/virtio/virtio_ring.o

If not W=12, then I did not get any warning message. How do you get the message quickly?

Thanks.

If you stress test this for a long enough time, and with debug enabled, you will see a crash.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
(quoted diff) +0 -1
--

2.32.0.3.g01195cf9f

author reviewer
#11 lore raw

Re: [PATCH net-next 03/13] virtio_ring: packed: harden dma unmap for indirect

Quoted 60 lines · Michael S. Tsirkin
Quoted 59 lines · Xuan Zhuo
Quoted 10 lines · Michael S. Tsirkin

As gcc luckily noted:

Quoted 7 lines · Xuan Zhuo
drivers/virtio/virtio_ring.c +1 -0
@@ -1617,23 +1617,24 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
 	}
 
 	if (vq->indirect) {
+		struct vring_desc_extra *extra;
 		u32 len;
 
Quoted 5 lines

drivers/virtio/virtio_ring.c static void detach_buf_packed(struct vring_virtqueue *vq, · line 1624

Quoted 3 lines
drivers/virtio/virtio_ring.c +0 -2
 		/* Free the indirect table, if any, now that it's unmapped. */
-		desc = state->indir_desc;
-		if (!desc)

desc is no longer initialized here

Will fix.

Quoted 17 lines

drivers/virtio/virtio_ring.c static void detach_buf_packed(struct vring_virtqueue *vq, · line 1634

Quoted 13 lines
drivers/virtio/virtio_ring.c +3 -1
+		extra = state->indir;
+		if (!extra)
 			return;
 
 		if (vring_need_unmap_buffer(vq)) {
 			len = vq->packed.desc_extra[id].len;
 			for (i = 0; i < len / sizeof(struct vring_packed_desc);
 					i++)
-				vring_unmap_desc_packed(vq, &desc[i]);
+				vring_unmap_extra_packed(vq, &extra[i]);
 		}
 		kfree(desc);

but freed here

Quoted 13 lines

drivers/virtio/virtio_ring.c static void detach_buf_packed(struct vring_virtqueue *vq, · line 1639

Quoted 7 lines
drivers/virtio/virtio_ring.c +2 -2
-		state->indir_desc = NULL;
+		state->indir = NULL;
 	} else if (ctx) {
-		*ctx = state->indir_desc;
+		*ctx = state->indir;
 	}
 }

It seems unlikely this was always 0 on all paths with even a small amount of stress, so now I question how this was tested. Besides, do not ignore compiler warnings, and do not tweak code to just make compiler shut up - they are your friend.

I agree.

Normally I do this by make W=12, but we have too many message, so I missed this.

make W=12 drivers/net/virtio_net.o drivers/virtio/virtio_ring.o

Quoted 8 lines
Quoted 4 lines

If not W=12, then I did not get any warning message. How do you get the message quickly?

Thanks.

If you stress test this for a long enough time, and with debug enabled, you will see a crash.

I only stress tested the split ring. For the packed ring, I just performed a simple verification.

I will street test for two mode in next version.

Thanks.

Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
(quoted diff) +0 -1
--

2.32.0.3.g01195cf9f

author reviewer
#12 lore raw

PATCH 4/13 virtio_ring: perform premapped operations based on per-buffer

In reply to Xuan Zhuo

The current configuration sets the virtqueue (vq) to premapped mode, implying that all buffers submitted to this queue must be mapped ahead of time. This presents a challenge for the virtnet send queue (sq): the virtnet driver would be required to keep track of dma information for vq size * 17, which can be substantial. However, if the premapped mode were applied on a per-buffer basis, the complexity would be greatly reduced. With AF_XDP enabled, AF_XDP buffers would become premapped, while kernel skb buffers could remain unmapped.

We can distinguish them by sg_page(sg), When sg_page(sg) is NULL, this indicates that the driver has performed DMA mapping in advance, allowing the Virtio core to directly utilize sg_dma_address(sg) without conducting any internal DMA mapping. Additionally, DMA unmap operations for this buffer will be bypassed.

---
 drivers/virtio/virtio_ring.c | 70 +++++++++++++++++++++---------------
 1 file changed, 41 insertions(+), 29 deletions(-)
drivers/virtio/virtio_ring.c +41 -29
index b43eca93015c..7efddc71af67 100644
@@ -235,6 +235,7 @@ static void vring_free(struct virtqueue *_vq);
  */
 
 #define to_vvq(_vq) container_of_const(_vq, struct vring_virtqueue, vq)
+#define sg_is_premapped(sg) (!sg_page(sg))
 
 static bool virtqueue_use_indirect(const struct vring_virtqueue *vq,
 				   unsigned int total_sg)
@@ -292,9 +293,10 @@ static bool vring_use_dma_api(const struct virtio_device *vdev)
 	return false;
 }
 
-static bool vring_need_unmap_buffer(const struct vring_virtqueue *vring)
+static bool vring_need_unmap_buffer(const struct vring_virtqueue *vring,
+				    const struct vring_desc_extra *extra)
 {
-	return vring->use_dma_api && !vring->premapped;
+	return vring->use_dma_api && (extra->addr != DMA_MAPPING_ERROR);
 }
 
 size_t virtio_max_dma_size(const struct virtio_device *vdev)
@@ -366,7 +368,7 @@ static struct device *vring_dma_dev(const struct vring_virtqueue *vq)
 static int vring_map_one_sg(const struct vring_virtqueue *vq, struct scatterlist *sg,
 			    enum dma_data_direction direction, dma_addr_t *addr)
 {
-	if (vq->premapped) {
+	if (sg_is_premapped(sg)) {
 		*addr = sg_dma_address(sg);
 		return 0;
 	}
@@ -457,7 +459,7 @@ 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 (!vring_need_unmap_buffer(vq))
+		if (!vring_need_unmap_buffer(vq, extra))
 			goto out;
 
 		dma_unmap_page(vring_dma_dev(vq),
@@ -510,7 +512,7 @@ static inline unsigned int virtqueue_add_desc_split(struct virtqueue *vq,
 						    dma_addr_t addr,
 						    unsigned int len,
 						    u16 flags,
-						    bool indirect)
+						    bool indirect, bool premapped)
 {
 	u16 next;
 
@@ -518,7 +520,7 @@ static inline unsigned int virtqueue_add_desc_split(struct virtqueue *vq,
 	desc[i].addr = cpu_to_virtio64(vq->vdev, addr);
 	desc[i].len = cpu_to_virtio32(vq->vdev, len);
 
-	extra[i].addr = addr;
+	extra[i].addr = premapped ? DMA_MAPPING_ERROR : addr;
 	extra[i].len = len;
 	extra[i].flags = flags;
 
@@ -611,7 +613,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 			 */
 			i = virtqueue_add_desc_split(_vq, desc, extra, i, addr, sg->length,
 						     VRING_DESC_F_NEXT,
-						     indirect);
+						     indirect, sg_is_premapped(sg));
 		}
 	}
 	for (; n < (out_sgs + in_sgs); n++) {
@@ -629,12 +631,12 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 						     sg->length,
 						     VRING_DESC_F_NEXT |
 						     VRING_DESC_F_WRITE,
-						     indirect);
+						     indirect, sg_is_premapped(sg));
 		}
 	}
 	/* Last one doesn't continue. */
 	desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT);
-	if (!indirect && vring_need_unmap_buffer(vq))
+	if (!indirect && vring_need_unmap_buffer(vq, &extra[prev]))
 		vq->split.desc_extra[prev & (vq->split.vring.num - 1)].flags &=
 			~VRING_DESC_F_NEXT;
 
@@ -643,19 +645,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 		dma_addr_t addr = vring_map_single(
 			vq, desc, total_sg * sizeof(struct vring_desc),
 			DMA_TO_DEVICE);
-		if (vring_mapping_error(vq, addr)) {
-			if (vq->premapped)
-				goto free_indirect;
-
+		if (vring_mapping_error(vq, addr))
 			goto unmap_release;
-		}
 
 		virtqueue_add_desc_split(_vq, vq->split.vring.desc,
 					 vq->split.desc_extra,
 					 head, addr,
 					 total_sg * sizeof(struct vring_desc),
 					 VRING_DESC_F_INDIRECT,
-					 false);
+					 false, false);
 	}
 
 	/* We're using some buffers from the free list. */
@@ -712,7 +710,6 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 		i = vring_unmap_one_split(vq, &extra[i]);
 	}
 
-free_indirect:
 	if (indirect)
 		kfree(desc);
 
@@ -794,7 +791,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 (vring_need_unmap_buffer(vq)) {
+		if (vq->use_dma_api) {
 			for (j = 0; j < len / sizeof(struct vring_desc); j++)
 				vring_unmap_one_split(vq, &extra[j]);
 		}
@@ -1228,7 +1225,7 @@ static void vring_unmap_extra_packed(const struct vring_virtqueue *vq,
 				 (flags & VRING_DESC_F_WRITE) ?
 				 DMA_FROM_DEVICE : DMA_TO_DEVICE);
 	} else {
-		if (!vring_need_unmap_buffer(vq))
+		if (!vring_need_unmap_buffer(vq, extra))
 			return;
 
 		dma_unmap_page(vring_dma_dev(vq),
@@ -1309,7 +1306,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
 			i++;
 
 			if (unlikely(vq->use_dma_api)) {
-				extra[i].addr = addr;
+				extra[i].addr = sg_is_premapped(sg) ? DMA_MAPPING_ERROR : addr;
 				extra[i].len = sg->length;
 				extra[i].flags = n < out_sgs ?  0 : VRING_DESC_F_WRITE;
 			}
@@ -1320,12 +1317,8 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
 	addr = vring_map_single(vq, desc,
 			total_sg * sizeof(struct vring_packed_desc),
 			DMA_TO_DEVICE);
-	if (vring_mapping_error(vq, addr)) {
-		if (vq->premapped)
-			goto free_desc;
-
+	if (vring_mapping_error(vq, addr))
 		goto unmap_release;
-	}
 
 	vq->packed.vring.desc[head].addr = cpu_to_le64(addr);
 	vq->packed.vring.desc[head].len = cpu_to_le32(total_sg *
@@ -1383,7 +1376,6 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
 	for (i = 0; i < err_idx; i++)
 		vring_unmap_extra_packed(vq, &extra[i]);
 
-free_desc:
 	kfree(desc);
 
 	END_USE(vq);
@@ -1474,7 +1466,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 = addr;
+				vq->packed.desc_extra[curr].addr = sg_is_premapped(sg) ?
+					DMA_MAPPING_ERROR : addr;
 				vq->packed.desc_extra[curr].len = sg->length;
 				vq->packed.desc_extra[curr].flags =
 					le16_to_cpu(flags);
@@ -1625,10 +1618,9 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
 		if (!extra)
 			return;
 
-		if (vring_need_unmap_buffer(vq)) {
+		if (vq->use_dma_api) {
 			len = vq->packed.desc_extra[id].len;
-			for (i = 0; i < len / sizeof(struct vring_packed_desc);
-					i++)
+			for (i = 0; i < len / sizeof(struct vring_packed_desc); i++)
 				vring_unmap_extra_packed(vq, &extra[i]);
 		}
 		kfree(desc);
@@ -2212,6 +2204,11 @@ static inline int virtqueue_add(struct virtqueue *_vq,
  * @data: the token identifying the buffer.
  * @gfp: how to do memory allocations (if necessary).
  *
+ * When sg_page(sg) is NULL, this indicates that the driver has performed DMA
+ * mapping in advance, allowing the virtio core to directly utilize
+ * sg_dma_address(sg) without conducting any internal DMA mapping. Additionally,
+ * DMA unmap operations for this buffer will be bypassed.
+ *
  * Caller must ensure we don't call this with other virtqueue operations
  * at the same time (except where noted).
  *
@@ -2246,6 +2243,11 @@ EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
  * @data: the token identifying the buffer.
  * @gfp: how to do memory allocations (if necessary).
  *
+ * When sg_page(sg) is NULL, this indicates that the driver has performed DMA
+ * mapping in advance, allowing the virtio core to directly utilize
+ * sg_dma_address(sg) without conducting any internal DMA mapping. Additionally,
+ * DMA unmap operations for this buffer will be bypassed.
+ *
  * Caller must ensure we don't call this with other virtqueue operations
  * at the same time (except where noted).
  *
@@ -2268,6 +2270,11 @@ EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
  * @data: the token identifying the buffer.
  * @gfp: how to do memory allocations (if necessary).
  *
+ * When sg_page(sg) is NULL, this indicates that the driver has performed DMA
+ * mapping in advance, allowing the virtio core to directly utilize
+ * sg_dma_address(sg) without conducting any internal DMA mapping. Additionally,
+ * DMA unmap operations for this buffer will be bypassed.
+ *
  * Caller must ensure we don't call this with other virtqueue operations
  * at the same time (except where noted).
  *
@@ -2291,6 +2298,11 @@ EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
  * @ctx: extra context for the token
  * @gfp: how to do memory allocations (if necessary).
  *
+ * When sg_page(sg) is NULL, this indicates that the driver has performed DMA
+ * mapping in advance, allowing the virtio core to directly utilize
+ * sg_dma_address(sg) without conducting any internal DMA mapping. Additionally,
+ * DMA unmap operations for this buffer will be bypassed.
+ *
  * Caller must ensure we don't call this with other virtqueue operations
  * at the same time (except where noted).
  *
Signature · 1 line
2.32.0.3.g01195cf9f
#13 lore raw

Re: [PATCH net-next 04/13] virtio_ring: perform premapped operations based on per-buffer

Quoted 9 lines · Xuan Zhuo

The current configuration sets the virtqueue (vq) to premapped mode, implying that all buffers submitted to this queue must be mapped ahead of time. This presents a challenge for the virtnet send queue (sq): the virtnet driver would be required to keep track of dma information for vq size * 17, which can be substantial. However, if the premapped mode were applied on a per-buffer basis, the complexity would be greatly reduced. With AF_XDP enabled, AF_XDP buffers would become premapped, while kernel skb buffers could remain unmapped.

Is this only applied to TX or both TX and RX.

Quoted 5 lines

We can distinguish them by sg_page(sg), When sg_page(sg) is NULL, this indicates that the driver has performed DMA mapping in advance, allowing the Virtio core to directly utilize sg_dma_address(sg) without conducting any internal DMA mapping.

This seems conflict with the code below?

#define sg_is_premapped(sg) (!sg_page(sg))

And rethink of the design, a question is that is there a chance that VM_PFNMAP area could be used for virtio-net? If it is true, the trick for sg_page() can not work?

A quick glance told me AF_XEP seems to be safe as it uses pin_user_pages(), but we need to check other possibilities.

Or we need to fall back to our previous idea, having new APIs.

Quoted 184 lines

Additionally, DMA unmap operations
for this buffer will be bypassed.

---
 drivers/virtio/virtio_ring.c | 70 +++++++++++++++++++++---------------
 1 file changed, 41 insertions(+), 29 deletions(-)
drivers/virtio/virtio_ring.c +19 -26
index b43eca93015c..7efddc71af67 100644
@@ -235,6 +235,7 @@ static void vring_free(struct virtqueue *_vq);
  */
 
 #define to_vvq(_vq) container_of_const(_vq, struct vring_virtqueue, vq)
+#define sg_is_premapped(sg) (!sg_page(sg))
 
 static bool virtqueue_use_indirect(const struct vring_virtqueue *vq,
                                   unsigned int total_sg)
@@ -292,9 +293,10 @@ static bool vring_use_dma_api(const struct virtio_device *vdev)
        return false;
 }
 
-static bool vring_need_unmap_buffer(const struct vring_virtqueue *vring)
+static bool vring_need_unmap_buffer(const struct vring_virtqueue *vring,
+                                   const struct vring_desc_extra *extra)
 {
-       return vring->use_dma_api && !vring->premapped;
+       return vring->use_dma_api && (extra->addr != DMA_MAPPING_ERROR);
 }
 
 size_t virtio_max_dma_size(const struct virtio_device *vdev)
@@ -366,7 +368,7 @@ static struct device *vring_dma_dev(const struct vring_virtqueue *vq)
 static int vring_map_one_sg(const struct vring_virtqueue *vq, struct scatterlist *sg,
                            enum dma_data_direction direction, dma_addr_t *addr)
 {
-       if (vq->premapped) {
+       if (sg_is_premapped(sg)) {
                *addr = sg_dma_address(sg);
                return 0;
        }
@@ -457,7 +459,7 @@ 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 (!vring_need_unmap_buffer(vq))
+               if (!vring_need_unmap_buffer(vq, extra))
                        goto out;
 
                dma_unmap_page(vring_dma_dev(vq),
@@ -510,7 +512,7 @@ static inline unsigned int virtqueue_add_desc_split(struct virtqueue *vq,
                                                    dma_addr_t addr,
                                                    unsigned int len,
                                                    u16 flags,
-                                                   bool indirect)
+                                                   bool indirect, bool premapped)
 {
        u16 next;
 
@@ -518,7 +520,7 @@ static inline unsigned int virtqueue_add_desc_split(struct virtqueue *vq,
        desc[i].addr = cpu_to_virtio64(vq->vdev, addr);
        desc[i].len = cpu_to_virtio32(vq->vdev, len);
 
-       extra[i].addr = addr;
+       extra[i].addr = premapped ? DMA_MAPPING_ERROR : addr;
        extra[i].len = len;
        extra[i].flags = flags;
 
@@ -611,7 +613,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
                         */
                        i = virtqueue_add_desc_split(_vq, desc, extra, i, addr, sg->length,
                                                     VRING_DESC_F_NEXT,
-                                                    indirect);
+                                                    indirect, sg_is_premapped(sg));
                }
        }
        for (; n < (out_sgs + in_sgs); n++) {
@@ -629,12 +631,12 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
                                                     sg->length,
                                                     VRING_DESC_F_NEXT |
                                                     VRING_DESC_F_WRITE,
-                                                    indirect);
+                                                    indirect, sg_is_premapped(sg));
                }
        }
        /* Last one doesn't continue. */
        desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT);
-       if (!indirect && vring_need_unmap_buffer(vq))
+       if (!indirect && vring_need_unmap_buffer(vq, &extra[prev]))
                vq->split.desc_extra[prev & (vq->split.vring.num - 1)].flags &=
                        ~VRING_DESC_F_NEXT;
 
@@ -643,19 +645,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
                dma_addr_t addr = vring_map_single(
                        vq, desc, total_sg * sizeof(struct vring_desc),
                        DMA_TO_DEVICE);
-               if (vring_mapping_error(vq, addr)) {
-                       if (vq->premapped)
-                               goto free_indirect;
-
+               if (vring_mapping_error(vq, addr))
                        goto unmap_release;
-               }
 
                virtqueue_add_desc_split(_vq, vq->split.vring.desc,
                                         vq->split.desc_extra,
                                         head, addr,
                                         total_sg * sizeof(struct vring_desc),
                                         VRING_DESC_F_INDIRECT,
-                                        false);
+                                        false, false);
        }
 
        /* We're using some buffers from the free list. */
@@ -712,7 +710,6 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
                i = vring_unmap_one_split(vq, &extra[i]);
        }
 
-free_indirect:
        if (indirect)
                kfree(desc);
 
@@ -794,7 +791,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 (vring_need_unmap_buffer(vq)) {
+               if (vq->use_dma_api) {
                        for (j = 0; j < len / sizeof(struct vring_desc); j++)
                                vring_unmap_one_split(vq, &extra[j]);
                }
@@ -1228,7 +1225,7 @@ static void vring_unmap_extra_packed(const struct vring_virtqueue *vq,
                                 (flags & VRING_DESC_F_WRITE) ?
                                 DMA_FROM_DEVICE : DMA_TO_DEVICE);
        } else {
-               if (!vring_need_unmap_buffer(vq))
+               if (!vring_need_unmap_buffer(vq, extra))
                        return;
 
                dma_unmap_page(vring_dma_dev(vq),
@@ -1309,7 +1306,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
                        i++;
 
                        if (unlikely(vq->use_dma_api)) {
-                               extra[i].addr = addr;
+                               extra[i].addr = sg_is_premapped(sg) ? DMA_MAPPING_ERROR : addr;
                                extra[i].len = sg->length;
                                extra[i].flags = n < out_sgs ?  0 : VRING_DESC_F_WRITE;
                        }
@@ -1320,12 +1317,8 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
        addr = vring_map_single(vq, desc,
                        total_sg * sizeof(struct vring_packed_desc),
                        DMA_TO_DEVICE);
-       if (vring_mapping_error(vq, addr)) {
-               if (vq->premapped)
-                       goto free_desc;
-
+       if (vring_mapping_error(vq, addr))
                goto unmap_release;
-       }
 
        vq->packed.vring.desc[head].addr = cpu_to_le64(addr);
        vq->packed.vring.desc[head].len = cpu_to_le32(total_sg *
@@ -1383,7 +1376,6 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
        for (i = 0; i < err_idx; i++)
                vring_unmap_extra_packed(vq, &extra[i]);
 
-free_desc:
        kfree(desc);
 
        END_USE(vq);
@@ -1474,7 +1466,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 = addr;
+                               vq->packed.desc_extra[curr].addr = sg_is_premapped(sg) ?
+                                       DMA_MAPPING_ERROR : addr;
                                vq->packed.desc_extra[curr].len = sg->length;
                                vq->packed.desc_extra[curr].flags =
                                        le16_to_cpu(flags);

drivers/virtio/virtio_ring.c static void detach_buf_packed(struct vring_virtqueue *vq, · line 1623

Quoted 9 lines
drivers/virtio/virtio_ring.c +2 -3
@@ -1625,10 +1618,9 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
                if (!extra)
                        return;
 
-               if (vring_need_unmap_buffer(vq)) {
+               if (vq->use_dma_api) {
                        len = vq->packed.desc_extra[id].len;
-                       for (i = 0; i < len / sizeof(struct vring_packed_desc);
-                                       i++)
+                       for (i = 0; i < len / sizeof(struct vring_packed_desc); i++)

Unnecessary changes?

Quoted 52 lines
drivers/virtio/virtio_ring.c +20 -1
                                vring_unmap_extra_packed(vq, &extra[i]);
                }
                kfree(desc);
@@ -2212,6 +2204,11 @@ static inline int virtqueue_add(struct virtqueue *_vq,
  * @data: the token identifying the buffer.
  * @gfp: how to do memory allocations (if necessary).
  *
+ * When sg_page(sg) is NULL, this indicates that the driver has performed DMA
+ * mapping in advance, allowing the virtio core to directly utilize
+ * sg_dma_address(sg) without conducting any internal DMA mapping. Additionally,
+ * DMA unmap operations for this buffer will be bypassed.
+ *
  * Caller must ensure we don't call this with other virtqueue operations
  * at the same time (except where noted).
  *
@@ -2246,6 +2243,11 @@ EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
  * @data: the token identifying the buffer.
  * @gfp: how to do memory allocations (if necessary).
  *
+ * When sg_page(sg) is NULL, this indicates that the driver has performed DMA
+ * mapping in advance, allowing the virtio core to directly utilize
+ * sg_dma_address(sg) without conducting any internal DMA mapping. Additionally,
+ * DMA unmap operations for this buffer will be bypassed.
+ *
  * Caller must ensure we don't call this with other virtqueue operations
  * at the same time (except where noted).
  *
@@ -2268,6 +2270,11 @@ EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
  * @data: the token identifying the buffer.
  * @gfp: how to do memory allocations (if necessary).
  *
+ * When sg_page(sg) is NULL, this indicates that the driver has performed DMA
+ * mapping in advance, allowing the virtio core to directly utilize
+ * sg_dma_address(sg) without conducting any internal DMA mapping. Additionally,
+ * DMA unmap operations for this buffer will be bypassed.
+ *
  * Caller must ensure we don't call this with other virtqueue operations
  * at the same time (except where noted).
  *
@@ -2291,6 +2298,11 @@ EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
  * @ctx: extra context for the token
  * @gfp: how to do memory allocations (if necessary).
  *
+ * When sg_page(sg) is NULL, this indicates that the driver has performed DMA
+ * mapping in advance, allowing the virtio core to directly utilize
+ * sg_dma_address(sg) without conducting any internal DMA mapping. Additionally,
+ * DMA unmap operations for this buffer will be bypassed.
+ *
  * Caller must ensure we don't call this with other virtqueue operations
  * at the same time (except where noted).
  *
--
Quoted 2 lines

2.32.0.3.g01195cf9f

Thanks

author reviewer
#14 lore raw

Re: [PATCH net-next 04/13] virtio_ring: perform premapped operations based on per-buffer

Quoted 11 lines · Jason Wang
Quoted 9 lines · Xuan Zhuo

The current configuration sets the virtqueue (vq) to premapped mode, implying that all buffers submitted to this queue must be mapped ahead of time. This presents a challenge for the virtnet send queue (sq): the virtnet driver would be required to keep track of dma information for vq size * 17, which can be substantial. However, if the premapped mode were applied on a per-buffer basis, the complexity would be greatly reduced. With AF_XDP enabled, AF_XDP buffers would become premapped, while kernel skb buffers could remain unmapped.

Quoted 1 line

Is this only applied to TX or both TX and RX.

For rx, if you mean per-buffer dma buffer, I think it is yes, rx can reuse this. If you mean should we do premapped for the normal rx buffers, I think we should, that can reduce the dma map operations.

Quoted 10 lines
Quoted 6 lines

We can distinguish them by sg_page(sg), When sg_page(sg) is NULL, this indicates that the driver has performed DMA mapping in advance, allowing the Virtio core to directly utilize sg_dma_address(sg) without conducting any internal DMA mapping.

This seems conflict with the code below?

#define sg_is_premapped(sg) (!sg_page(sg))

Sorry, I do not get for you.

The key point is that the sg->page is setted by driver.

So I mean if the driver sets sg->page = NULL, then for this sg, the virtio core can skip dma mapping. If the driver sets sg->page to the page of the buffer, then the virtio core should do dma mapping for this sg.

Quoted 197 lines

And rethink of the design, a question is that is there a chance that VM_PFNMAP area could be used for virtio-net? If it is true, the trick for sg_page() can not work?

A quick glance told me AF_XEP seems to be safe as it uses pin_user_pages(), but we need to check other possibilities.

Or we need to fall back to our previous idea, having new APIs.

Quoted 187 lines

Additionally, DMA unmap operations
for this buffer will be bypassed.

---
 drivers/virtio/virtio_ring.c | 70 +++++++++++++++++++++---------------
 1 file changed, 41 insertions(+), 29 deletions(-)
drivers/virtio/virtio_ring.c +19 -26
index b43eca93015c..7efddc71af67 100644
@@ -235,6 +235,7 @@ static void vring_free(struct virtqueue *_vq);
  */
 
 #define to_vvq(_vq) container_of_const(_vq, struct vring_virtqueue, vq)
+#define sg_is_premapped(sg) (!sg_page(sg))
 
 static bool virtqueue_use_indirect(const struct vring_virtqueue *vq,
                                   unsigned int total_sg)
@@ -292,9 +293,10 @@ static bool vring_use_dma_api(const struct virtio_device *vdev)
        return false;
 }
 
-static bool vring_need_unmap_buffer(const struct vring_virtqueue *vring)
+static bool vring_need_unmap_buffer(const struct vring_virtqueue *vring,
+                                   const struct vring_desc_extra *extra)
 {
-       return vring->use_dma_api && !vring->premapped;
+       return vring->use_dma_api && (extra->addr != DMA_MAPPING_ERROR);
 }
 
 size_t virtio_max_dma_size(const struct virtio_device *vdev)
@@ -366,7 +368,7 @@ static struct device *vring_dma_dev(const struct vring_virtqueue *vq)
 static int vring_map_one_sg(const struct vring_virtqueue *vq, struct scatterlist *sg,
                            enum dma_data_direction direction, dma_addr_t *addr)
 {
-       if (vq->premapped) {
+       if (sg_is_premapped(sg)) {
                *addr = sg_dma_address(sg);
                return 0;
        }
@@ -457,7 +459,7 @@ 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 (!vring_need_unmap_buffer(vq))
+               if (!vring_need_unmap_buffer(vq, extra))
                        goto out;
 
                dma_unmap_page(vring_dma_dev(vq),
@@ -510,7 +512,7 @@ static inline unsigned int virtqueue_add_desc_split(struct virtqueue *vq,
                                                    dma_addr_t addr,
                                                    unsigned int len,
                                                    u16 flags,
-                                                   bool indirect)
+                                                   bool indirect, bool premapped)
 {
        u16 next;
 
@@ -518,7 +520,7 @@ static inline unsigned int virtqueue_add_desc_split(struct virtqueue *vq,
        desc[i].addr = cpu_to_virtio64(vq->vdev, addr);
        desc[i].len = cpu_to_virtio32(vq->vdev, len);
 
-       extra[i].addr = addr;
+       extra[i].addr = premapped ? DMA_MAPPING_ERROR : addr;
        extra[i].len = len;
        extra[i].flags = flags;
 
@@ -611,7 +613,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
                         */
                        i = virtqueue_add_desc_split(_vq, desc, extra, i, addr, sg->length,
                                                     VRING_DESC_F_NEXT,
-                                                    indirect);
+                                                    indirect, sg_is_premapped(sg));
                }
        }
        for (; n < (out_sgs + in_sgs); n++) {
@@ -629,12 +631,12 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
                                                     sg->length,
                                                     VRING_DESC_F_NEXT |
                                                     VRING_DESC_F_WRITE,
-                                                    indirect);
+                                                    indirect, sg_is_premapped(sg));
                }
        }
        /* Last one doesn't continue. */
        desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT);
-       if (!indirect && vring_need_unmap_buffer(vq))
+       if (!indirect && vring_need_unmap_buffer(vq, &extra[prev]))
                vq->split.desc_extra[prev & (vq->split.vring.num - 1)].flags &=
                        ~VRING_DESC_F_NEXT;
 
@@ -643,19 +645,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
                dma_addr_t addr = vring_map_single(
                        vq, desc, total_sg * sizeof(struct vring_desc),
                        DMA_TO_DEVICE);
-               if (vring_mapping_error(vq, addr)) {
-                       if (vq->premapped)
-                               goto free_indirect;
-
+               if (vring_mapping_error(vq, addr))
                        goto unmap_release;
-               }
 
                virtqueue_add_desc_split(_vq, vq->split.vring.desc,
                                         vq->split.desc_extra,
                                         head, addr,
                                         total_sg * sizeof(struct vring_desc),
                                         VRING_DESC_F_INDIRECT,
-                                        false);
+                                        false, false);
        }
 
        /* We're using some buffers from the free list. */
@@ -712,7 +710,6 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
                i = vring_unmap_one_split(vq, &extra[i]);
        }
 
-free_indirect:
        if (indirect)
                kfree(desc);
 
@@ -794,7 +791,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 (vring_need_unmap_buffer(vq)) {
+               if (vq->use_dma_api) {
                        for (j = 0; j < len / sizeof(struct vring_desc); j++)
                                vring_unmap_one_split(vq, &extra[j]);
                }
@@ -1228,7 +1225,7 @@ static void vring_unmap_extra_packed(const struct vring_virtqueue *vq,
                                 (flags & VRING_DESC_F_WRITE) ?
                                 DMA_FROM_DEVICE : DMA_TO_DEVICE);
        } else {
-               if (!vring_need_unmap_buffer(vq))
+               if (!vring_need_unmap_buffer(vq, extra))
                        return;
 
                dma_unmap_page(vring_dma_dev(vq),
@@ -1309,7 +1306,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
                        i++;
 
                        if (unlikely(vq->use_dma_api)) {
-                               extra[i].addr = addr;
+                               extra[i].addr = sg_is_premapped(sg) ? DMA_MAPPING_ERROR : addr;
                                extra[i].len = sg->length;
                                extra[i].flags = n < out_sgs ?  0 : VRING_DESC_F_WRITE;
                        }
@@ -1320,12 +1317,8 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
        addr = vring_map_single(vq, desc,
                        total_sg * sizeof(struct vring_packed_desc),
                        DMA_TO_DEVICE);
-       if (vring_mapping_error(vq, addr)) {
-               if (vq->premapped)
-                       goto free_desc;
-
+       if (vring_mapping_error(vq, addr))
                goto unmap_release;
-       }
 
        vq->packed.vring.desc[head].addr = cpu_to_le64(addr);
        vq->packed.vring.desc[head].len = cpu_to_le32(total_sg *
@@ -1383,7 +1376,6 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
        for (i = 0; i < err_idx; i++)
                vring_unmap_extra_packed(vq, &extra[i]);
 
-free_desc:
        kfree(desc);
 
        END_USE(vq);
@@ -1474,7 +1466,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 = addr;
+                               vq->packed.desc_extra[curr].addr = sg_is_premapped(sg) ?
+                                       DMA_MAPPING_ERROR : addr;
                                vq->packed.desc_extra[curr].len = sg->length;
                                vq->packed.desc_extra[curr].flags =
                                        le16_to_cpu(flags);
@@ -1625,10 +1618,9 @@ static void detach_buf_packed(struct vring_virtqueue *vq,
                if (!extra)
                        return;
 
Quoted 8 lines

drivers/virtio/virtio_ring.c static void detach_buf_packed(struct vring_virtqueue *vq, · line 1623

Quoted 6 lines
drivers/virtio/virtio_ring.c +2 -3
-               if (vring_need_unmap_buffer(vq)) {
+               if (vq->use_dma_api) {
                        len = vq->packed.desc_extra[id].len;
-                       for (i = 0; i < len / sizeof(struct vring_packed_desc);
-                                       i++)
+                       for (i = 0; i < len / sizeof(struct vring_packed_desc); i++)

Unnecessary changes?

Will fix.

Thanks.

Quoted 59 lines
Quoted 54 lines
drivers/virtio/virtio_ring.c +20 -1
                                vring_unmap_extra_packed(vq, &extra[i]);
                }
                kfree(desc);
@@ -2212,6 +2204,11 @@ static inline int virtqueue_add(struct virtqueue *_vq,
  * @data: the token identifying the buffer.
  * @gfp: how to do memory allocations (if necessary).
  *
+ * When sg_page(sg) is NULL, this indicates that the driver has performed DMA
+ * mapping in advance, allowing the virtio core to directly utilize
+ * sg_dma_address(sg) without conducting any internal DMA mapping. Additionally,
+ * DMA unmap operations for this buffer will be bypassed.
+ *
  * Caller must ensure we don't call this with other virtqueue operations
  * at the same time (except where noted).
  *
@@ -2246,6 +2243,11 @@ EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
  * @data: the token identifying the buffer.
  * @gfp: how to do memory allocations (if necessary).
  *
+ * When sg_page(sg) is NULL, this indicates that the driver has performed DMA
+ * mapping in advance, allowing the virtio core to directly utilize
+ * sg_dma_address(sg) without conducting any internal DMA mapping. Additionally,
+ * DMA unmap operations for this buffer will be bypassed.
+ *
  * Caller must ensure we don't call this with other virtqueue operations
  * at the same time (except where noted).
  *
@@ -2268,6 +2270,11 @@ EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
  * @data: the token identifying the buffer.
  * @gfp: how to do memory allocations (if necessary).
  *
+ * When sg_page(sg) is NULL, this indicates that the driver has performed DMA
+ * mapping in advance, allowing the virtio core to directly utilize
+ * sg_dma_address(sg) without conducting any internal DMA mapping. Additionally,
+ * DMA unmap operations for this buffer will be bypassed.
+ *
  * Caller must ensure we don't call this with other virtqueue operations
  * at the same time (except where noted).
  *
@@ -2291,6 +2298,11 @@ EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
  * @ctx: extra context for the token
  * @gfp: how to do memory allocations (if necessary).
  *
+ * When sg_page(sg) is NULL, this indicates that the driver has performed DMA
+ * mapping in advance, allowing the virtio core to directly utilize
+ * sg_dma_address(sg) without conducting any internal DMA mapping. Additionally,
+ * DMA unmap operations for this buffer will be bypassed.
+ *
  * Caller must ensure we don't call this with other virtqueue operations
  * at the same time (except where noted).
  *
--
Quoted 2 lines

2.32.0.3.g01195cf9f

Thanks

#15 lore raw

Re: [PATCH net-next 04/13] virtio_ring: perform premapped operations based on per-buffer

Quoted 29 lines · Xuan Zhuo
Quoted 11 lines · Jason Wang
Quoted 9 lines · Xuan Zhuo

The current configuration sets the virtqueue (vq) to premapped mode, implying that all buffers submitted to this queue must be mapped ahead of time. This presents a challenge for the virtnet send queue (sq): the virtnet driver would be required to keep track of dma information for vq size * 17, which can be substantial. However, if the premapped mode were applied on a per-buffer basis, the complexity would be greatly reduced. With AF_XDP enabled, AF_XDP buffers would become premapped, while kernel skb buffers could remain unmapped.

Quoted 1 line

Is this only applied to TX or both TX and RX.

For rx, if you mean per-buffer dma buffer, I think it is yes, rx can reuse this. If you mean should we do premapped for the normal rx buffers, I think we should, that can reduce the dma map operations.

Quoted 7 lines
Quoted 6 lines

We can distinguish them by sg_page(sg), When sg_page(sg) is NULL, this indicates that the driver has performed DMA mapping in advance, allowing the Virtio core to directly utilize sg_dma_address(sg) without conducting any internal DMA mapping.

Quoted 7 lines
Quoted 3 lines

This seems conflict with the code below?

#define sg_is_premapped(sg) (!sg_page(sg))

Sorry, I do not get for you.

The key point is that the sg->page is setted by driver.

Ok, I forget that but let's document this assumption in the changelog.

Quoted 6 lines

So I mean if the driver sets sg->page = NULL, then for this sg, the virtio core can skip dma mapping. If the driver sets sg->page to the page of the buffer, then the virtio core should do dma mapping for this sg.

Ok, let's describe this in the changelog.

Thanks

author reviewer
#16 lore raw

PATCH 5/13 virtio-net: rq submits premapped buffer per buffer

In reply to Xuan Zhuo

virtio-net rq submits premapped buffer per buffer. And removes the call of the virtnet_rq_set_premapped().

---
 drivers/net/virtio_net.c | 31 ++++++++-----------------------
 1 file changed, 8 insertions(+), 23 deletions(-)
drivers/net/virtio_net.c +8 -23
index 6c79fc43fa31..41aaea3b90fd 100644
@@ -539,6 +539,13 @@ static struct sk_buff *ptr_to_skb(void *ptr)
 	return (struct sk_buff *)((unsigned long)ptr & ~VIRTIO_ORPHAN_FLAG);
 }
 
+static void sg_fill_dma(struct scatterlist *sg, dma_addr_t addr, u32 len)
+{
+	sg_assign_page(sg, NULL);
+	sg->dma_address = addr;
+	sg->length = len;
+}
+
 static void __free_old_xmit(struct send_queue *sq, struct netdev_queue *txq,
 			    bool in_napi, struct virtnet_sq_free_stats *stats)
 {
@@ -907,8 +914,7 @@ static void virtnet_rq_init_one_sg(struct receive_queue *rq, void *buf, u32 len)
 	addr = dma->addr - sizeof(*dma) + offset;
 
 	sg_init_table(rq->sg, 1);
-	rq->sg[0].dma_address = addr;
-	rq->sg[0].length = len;
+	sg_fill_dma(&rq->sg[0], addr, len);
 }
 
 static void *virtnet_rq_alloc(struct receive_queue *rq, u32 size, gfp_t gfp)
@@ -967,19 +973,6 @@ static void *virtnet_rq_alloc(struct receive_queue *rq, u32 size, gfp_t gfp)
 	return buf;
 }
 
-static void virtnet_rq_set_premapped(struct virtnet_info *vi)
-{
-	int i;
-
-	/* disable for big mode */
-	if (!vi->mergeable_rx_bufs && vi->big_packets)
-		return;
-
-	for (i = 0; i < vi->max_queue_pairs; i++)
-		/* error should never happen */
-		BUG_ON(virtqueue_set_dma_premapped(vi->rq[i].vq));
-}
-
 static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
 {
 	struct virtnet_info *vi = vq->vdev->priv;
@@ -1071,12 +1064,6 @@ static void check_sq_full_and_disable(struct virtnet_info *vi,
 	}
 }
 
-static void sg_fill_dma(struct scatterlist *sg, dma_addr_t addr, u32 len)
-{
-	sg->dma_address = addr;
-	sg->length = len;
-}
-
 static struct xdp_buff *buf_to_xdp(struct virtnet_info *vi,
 				   struct receive_queue *rq, void *buf, u32 len)
 {
@@ -6109,8 +6096,6 @@ static int init_vqs(struct virtnet_info *vi)
 	if (ret)
 		goto err_free;
 
-	virtnet_rq_set_premapped(vi);
-
 	cpus_read_lock();
 	virtnet_set_affinity(vi);
 	cpus_read_unlock();
Signature · 1 line
2.32.0.3.g01195cf9f
author reviewer
#17 lore raw

PATCH 6/13 virtio_ring: remove API virtqueue_set_dma_premapped

In reply to Xuan Zhuo

Now, this API is useless. remove it.

---
 drivers/virtio/virtio_ring.c | 48 ------------------------------------
 include/linux/virtio.h       |  2 --
 2 files changed, 50 deletions(-)
drivers/virtio/virtio_ring.c +0 -48
index 7efddc71af67..b920cdf1d513 100644
@@ -172,9 +172,6 @@ struct vring_virtqueue {
 	/* Host publishes avail event idx */
 	bool event;
 
-	/* Do DMA mapping by driver */
-	bool premapped;
-
 	/* Head of free buffer list. */
 	unsigned int free_head;
 	/* Number we've added since last sync. */
@@ -2075,7 +2072,6 @@ static struct virtqueue *vring_create_virtqueue_packed(
 	vq->packed_ring = true;
 	vq->dma_dev = dma_dev;
 	vq->use_dma_api = vring_use_dma_api(vdev);
-	vq->premapped = false;
 
 	vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
 		!context;
@@ -2639,7 +2635,6 @@ static struct virtqueue *__vring_new_virtqueue(unsigned int index,
 #endif
 	vq->dma_dev = dma_dev;
 	vq->use_dma_api = vring_use_dma_api(vdev);
-	vq->premapped = false;
 
 	vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
 		!context;
@@ -2766,49 +2761,6 @@ int virtqueue_resize(struct virtqueue *_vq, u32 num,
 }
 EXPORT_SYMBOL_GPL(virtqueue_resize);
 
-/**
- * virtqueue_set_dma_premapped - set the vring premapped mode
- * @_vq: the struct virtqueue we're talking about.
- *
- * Enable the premapped mode of the vq.
- *
- * The vring in premapped mode does not do dma internally, so the driver must
- * do dma mapping in advance. The driver must pass the dma_address through
- * dma_address of scatterlist. When the driver got a used buffer from
- * the vring, it has to unmap the dma address.
- *
- * This function must be called immediately after creating the vq, or after vq
- * reset, and before adding any buffers to it.
- *
- * Caller must ensure we don't call this with other virtqueue operations
- * at the same time (except where noted).
- *
- * Returns zero or a negative error.
- * 0: success.
- * -EINVAL: too late to enable premapped mode, the vq already contains buffers.
- */
-int virtqueue_set_dma_premapped(struct virtqueue *_vq)
-{
-	struct vring_virtqueue *vq = to_vvq(_vq);
-	u32 num;
-
-	START_USE(vq);
-
-	num = vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num;
-
-	if (num != vq->vq.num_free) {
-		END_USE(vq);
-		return -EINVAL;
-	}
-
-	vq->premapped = true;
-
-	END_USE(vq);
-
-	return 0;
-}
-EXPORT_SYMBOL_GPL(virtqueue_set_dma_premapped);
-
 /**
  * virtqueue_reset - detach and recycle all unused buffers
  * @_vq: the struct virtqueue we're talking about.
include/linux/virtio.h +0 -2
index 306137a15d07..238728ed62fa 100644
@@ -82,8 +82,6 @@ bool virtqueue_enable_cb(struct virtqueue *vq);
 
 unsigned virtqueue_enable_cb_prepare(struct virtqueue *vq);
 
-int virtqueue_set_dma_premapped(struct virtqueue *_vq);
-
 bool virtqueue_poll(struct virtqueue *vq, unsigned);
 
 bool virtqueue_enable_cb_delayed(struct virtqueue *vq);
Signature · 1 line
2.32.0.3.g01195cf9f
author reviewer
#18 lore raw

PATCH 7/13 virtio_net: refactor the xmit type

In reply to Xuan Zhuo

Because the af-xdp will introduce a new xmit type, so I refactor the xmit type mechanism first.

We use the last two bits of the pointer to distinguish the xmit type, so we can distinguish four xmit types. Now we have three types: skb, orphan and xdp.

---
 drivers/net/virtio_net.c | 90 +++++++++++++++++++++++-----------------
 1 file changed, 51 insertions(+), 39 deletions(-)
drivers/net/virtio_net.c +51 -39
index 41aaea3b90fd..96abee36738b 100644
@@ -45,9 +45,6 @@ module_param(napi_tx, bool, 0644);
 #define VIRTIO_XDP_TX		BIT(0)
 #define VIRTIO_XDP_REDIR	BIT(1)
 
-#define VIRTIO_XDP_FLAG		BIT(0)
-#define VIRTIO_ORPHAN_FLAG	BIT(1)
-
 /* RX packet size EWMA. The average packet size is used to determine the packet
  * buffer size when refilling RX rings. As the entire RX ring may be refilled
  * at once, the weight is chosen so that the EWMA will be insensitive to short-
@@ -509,34 +506,35 @@ static struct sk_buff *virtnet_skb_append_frag(struct sk_buff *head_skb,
 					       struct page *page, void *buf,
 					       int len, int truesize);
 
-static bool is_xdp_frame(void *ptr)
-{
-	return (unsigned long)ptr & VIRTIO_XDP_FLAG;
-}
+enum virtnet_xmit_type {
+	VIRTNET_XMIT_TYPE_SKB,
+	VIRTNET_XMIT_TYPE_ORPHAN,
+	VIRTNET_XMIT_TYPE_XDP,
+};
 
-static void *xdp_to_ptr(struct xdp_frame *ptr)
-{
-	return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG);
-}
+#define VIRTNET_XMIT_TYPE_MASK (VIRTNET_XMIT_TYPE_SKB | VIRTNET_XMIT_TYPE_ORPHAN \
+				| VIRTNET_XMIT_TYPE_XDP)
 
-static struct xdp_frame *ptr_to_xdp(void *ptr)
+static enum virtnet_xmit_type virtnet_xmit_ptr_strip(void **ptr)
 {
-	return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
-}
+	unsigned long p = (unsigned long)*ptr;
 
-static bool is_orphan_skb(void *ptr)
-{
-	return (unsigned long)ptr & VIRTIO_ORPHAN_FLAG;
+	*ptr = (void *)(p & ~VIRTNET_XMIT_TYPE_MASK);
+
+	return p & VIRTNET_XMIT_TYPE_MASK;
 }
 
-static void *skb_to_ptr(struct sk_buff *skb, bool orphan)
+static void *virtnet_xmit_ptr_mix(void *ptr, enum virtnet_xmit_type type)
 {
-	return (void *)((unsigned long)skb | (orphan ? VIRTIO_ORPHAN_FLAG : 0));
+	return (void *)((unsigned long)ptr | type);
 }
 
-static struct sk_buff *ptr_to_skb(void *ptr)
+static int virtnet_add_outbuf(struct send_queue *sq, int num, void *data,
+			      enum virtnet_xmit_type type)
 {
-	return (struct sk_buff *)((unsigned long)ptr & ~VIRTIO_ORPHAN_FLAG);
+	return virtqueue_add_outbuf(sq->vq, sq->sg, num,
+				    virtnet_xmit_ptr_mix(data, type),
+				    GFP_ATOMIC);
 }
 
 static void sg_fill_dma(struct scatterlist *sg, dma_addr_t addr, u32 len)
@@ -549,29 +547,37 @@ static void sg_fill_dma(struct scatterlist *sg, dma_addr_t addr, u32 len)
 static void __free_old_xmit(struct send_queue *sq, struct netdev_queue *txq,
 			    bool in_napi, struct virtnet_sq_free_stats *stats)
 {
+	struct xdp_frame *frame;
+	struct sk_buff *skb;
 	unsigned int len;
 	void *ptr;
 
 	while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
-		if (!is_xdp_frame(ptr)) {
-			struct sk_buff *skb = ptr_to_skb(ptr);
+		switch (virtnet_xmit_ptr_strip(&ptr)) {
+		case VIRTNET_XMIT_TYPE_SKB:
+			skb = ptr;
 
 			pr_debug("Sent skb %p\n", skb);
+			stats->napi_packets++;
+			stats->napi_bytes += skb->len;
+			napi_consume_skb(skb, in_napi);
+			break;
 
-			if (is_orphan_skb(ptr)) {
-				stats->packets++;
-				stats->bytes += skb->len;
-			} else {
-				stats->napi_packets++;
-				stats->napi_bytes += skb->len;
-			}
+		case VIRTNET_XMIT_TYPE_ORPHAN:
+			skb = ptr;
+
+			stats->packets++;
+			stats->bytes += skb->len;
 			napi_consume_skb(skb, in_napi);
-		} else {
-			struct xdp_frame *frame = ptr_to_xdp(ptr);
+			break;
+
+		case VIRTNET_XMIT_TYPE_XDP:
+			frame = ptr;
 
 			stats->packets++;
 			stats->bytes += xdp_get_frame_len(frame);
 			xdp_return_frame(frame);
+			break;
 		}
 	}
 	netdev_tx_completed_queue(txq, stats->napi_packets, stats->napi_bytes);
@@ -1421,8 +1427,7 @@ static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
 			    skb_frag_size(frag), skb_frag_off(frag));
 	}
 
-	err = virtqueue_add_outbuf(sq->vq, sq->sg, nr_frags + 1,
-				   xdp_to_ptr(xdpf), GFP_ATOMIC);
+	err = virtnet_add_outbuf(sq, nr_frags + 1, xdpf, VIRTNET_XMIT_TYPE_XDP);
 	if (unlikely(err))
 		return -ENOSPC; /* Caller handle free/refcnt */
 
@@ -3028,8 +3033,9 @@ static int xmit_skb(struct send_queue *sq, struct sk_buff *skb, bool orphan)
 			return num_sg;
 		num_sg++;
 	}
-	return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg,
-				    skb_to_ptr(skb, orphan), GFP_ATOMIC);
+
+	return virtnet_add_outbuf(sq, num_sg, skb,
+				  orphan ? VIRTNET_XMIT_TYPE_ORPHAN : VIRTNET_XMIT_TYPE_SKB);
 }
 
 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
@@ -5906,10 +5912,16 @@ static void free_receive_page_frags(struct virtnet_info *vi)
 
 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf)
 {
-	if (!is_xdp_frame(buf))
+	switch (virtnet_xmit_ptr_strip(&buf)) {
+	case VIRTNET_XMIT_TYPE_SKB:
+	case VIRTNET_XMIT_TYPE_ORPHAN:
 		dev_kfree_skb(buf);
-	else
-		xdp_return_frame(ptr_to_xdp(buf));
+		break;
+
+	case VIRTNET_XMIT_TYPE_XDP:
+		xdp_return_frame(buf);
+		break;
+	}
 }
 
 static void free_unused_bufs(struct virtnet_info *vi)
Signature · 1 line
2.32.0.3.g01195cf9f
#19 lore raw

Re: [PATCH net-next 07/13] virtio_net: refactor the xmit type

Quoted 7 lines · Xuan Zhuo

Because the af-xdp will introduce a new xmit type, so I refactor the xmit type mechanism first.

We use the last two bits of the pointer to distinguish the xmit type, so we can distinguish four xmit types. Now we have three types: skb, orphan and xdp.

And if I was not wrong, we do not anymore use bitmasks. If yes, let's explain the reason here.

Quoted 21 lines
---
 drivers/net/virtio_net.c | 90 +++++++++++++++++++++++-----------------
 1 file changed, 51 insertions(+), 39 deletions(-)
drivers/net/virtio_net.c +0 -3
index 41aaea3b90fd..96abee36738b 100644
@@ -45,9 +45,6 @@ module_param(napi_tx, bool, 0644);
 #define VIRTIO_XDP_TX          BIT(0)
 #define VIRTIO_XDP_REDIR       BIT(1)
 
-#define VIRTIO_XDP_FLAG                BIT(0)
-#define VIRTIO_ORPHAN_FLAG     BIT(1)
-
 /* RX packet size EWMA. The average packet size is used to determine the packet
  * buffer size when refilling RX rings. As the entire RX ring may be refilled
  * at once, the weight is chosen so that the EWMA will be insensitive to short-

drivers/net/virtio_net.c static struct sk_buff *virtnet_skb_append_frag(struct sk_buff *head_skb, · line 511

Quoted 10 lines
drivers/net/virtio_net.c +3 -4
@@ -509,34 +506,35 @@ static struct sk_buff *virtnet_skb_append_frag(struct sk_buff *head_skb,
                                               struct page *page, void *buf,
                                               int len, int truesize);
 
-static bool is_xdp_frame(void *ptr)
-{
-       return (unsigned long)ptr & VIRTIO_XDP_FLAG;
-}
+enum virtnet_xmit_type {
+       VIRTNET_XMIT_TYPE_SKB,
+       VIRTNET_XMIT_TYPE_ORPHAN,

Let's rename this to SKB_ORPHAN?

drivers/net/virtio_net.c static struct sk_buff *virtnet_skb_append_frag(struct sk_buff *head_skb, · line 516

Quoted 9 lines
drivers/net/virtio_net.c +4 -4
+       VIRTNET_XMIT_TYPE_XDP,
+};
 
-static void *xdp_to_ptr(struct xdp_frame *ptr)
-{
-       return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG);
-}
+#define VIRTNET_XMIT_TYPE_MASK (VIRTNET_XMIT_TYPE_SKB | VIRTNET_XMIT_TYPE_ORPHAN \
+                               | VIRTNET_XMIT_TYPE_XDP)

I may miss something but it seems not a correct bitmask definition as each member is not a bit actually?

Quoted 113 lines
drivers/net/virtio_net.c +38 -26
-static struct xdp_frame *ptr_to_xdp(void *ptr)
+static enum virtnet_xmit_type virtnet_xmit_ptr_strip(void **ptr)
 {
-       return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
-}
+       unsigned long p = (unsigned long)*ptr;
 
-static bool is_orphan_skb(void *ptr)
-{
-       return (unsigned long)ptr & VIRTIO_ORPHAN_FLAG;
+       *ptr = (void *)(p & ~VIRTNET_XMIT_TYPE_MASK);
+
+       return p & VIRTNET_XMIT_TYPE_MASK;
 }
 
-static void *skb_to_ptr(struct sk_buff *skb, bool orphan)
+static void *virtnet_xmit_ptr_mix(void *ptr, enum virtnet_xmit_type type)
 {
-       return (void *)((unsigned long)skb | (orphan ? VIRTIO_ORPHAN_FLAG : 0));
+       return (void *)((unsigned long)ptr | type);
 }
 
-static struct sk_buff *ptr_to_skb(void *ptr)
+static int virtnet_add_outbuf(struct send_queue *sq, int num, void *data,
+                             enum virtnet_xmit_type type)
 {
-       return (struct sk_buff *)((unsigned long)ptr & ~VIRTIO_ORPHAN_FLAG);
+       return virtqueue_add_outbuf(sq->vq, sq->sg, num,
+                                   virtnet_xmit_ptr_mix(data, type),
+                                   GFP_ATOMIC);
 }
 
 static void sg_fill_dma(struct scatterlist *sg, dma_addr_t addr, u32 len)
@@ -549,29 +547,37 @@ static void sg_fill_dma(struct scatterlist *sg, dma_addr_t addr, u32 len)
 static void __free_old_xmit(struct send_queue *sq, struct netdev_queue *txq,
                            bool in_napi, struct virtnet_sq_free_stats *stats)
 {
+       struct xdp_frame *frame;
+       struct sk_buff *skb;
        unsigned int len;
        void *ptr;
 
        while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
-               if (!is_xdp_frame(ptr)) {
-                       struct sk_buff *skb = ptr_to_skb(ptr);
+               switch (virtnet_xmit_ptr_strip(&ptr)) {
+               case VIRTNET_XMIT_TYPE_SKB:
+                       skb = ptr;
 
                        pr_debug("Sent skb %p\n", skb);
+                       stats->napi_packets++;
+                       stats->napi_bytes += skb->len;
+                       napi_consume_skb(skb, in_napi);
+                       break;
 
-                       if (is_orphan_skb(ptr)) {
-                               stats->packets++;
-                               stats->bytes += skb->len;
-                       } else {
-                               stats->napi_packets++;
-                               stats->napi_bytes += skb->len;
-                       }
+               case VIRTNET_XMIT_TYPE_ORPHAN:
+                       skb = ptr;
+
+                       stats->packets++;
+                       stats->bytes += skb->len;
                        napi_consume_skb(skb, in_napi);
-               } else {
-                       struct xdp_frame *frame = ptr_to_xdp(ptr);
+                       break;
+
+               case VIRTNET_XMIT_TYPE_XDP:
+                       frame = ptr;
 
                        stats->packets++;
                        stats->bytes += xdp_get_frame_len(frame);
                        xdp_return_frame(frame);
+                       break;
                }
        }
        netdev_tx_completed_queue(txq, stats->napi_packets, stats->napi_bytes);
@@ -1421,8 +1427,7 @@ static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
                            skb_frag_size(frag), skb_frag_off(frag));
        }
 
-       err = virtqueue_add_outbuf(sq->vq, sq->sg, nr_frags + 1,
-                                  xdp_to_ptr(xdpf), GFP_ATOMIC);
+       err = virtnet_add_outbuf(sq, nr_frags + 1, xdpf, VIRTNET_XMIT_TYPE_XDP);
        if (unlikely(err))
                return -ENOSPC; /* Caller handle free/refcnt */
 
@@ -3028,8 +3033,9 @@ static int xmit_skb(struct send_queue *sq, struct sk_buff *skb, bool orphan)
                        return num_sg;
                num_sg++;
        }
-       return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg,
-                                   skb_to_ptr(skb, orphan), GFP_ATOMIC);
+
+       return virtnet_add_outbuf(sq, num_sg, skb,
+                                 orphan ? VIRTNET_XMIT_TYPE_ORPHAN : VIRTNET_XMIT_TYPE_SKB);
 }
 
 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
@@ -5906,10 +5912,16 @@ static void free_receive_page_frags(struct virtnet_info *vi)
 
 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf)
 {
-       if (!is_xdp_frame(buf))
+       switch (virtnet_xmit_ptr_strip(&buf)) {
+       case VIRTNET_XMIT_TYPE_SKB:
+       case VIRTNET_XMIT_TYPE_ORPHAN:

drivers/net/virtio_net.c static void free_receive_page_frags(struct virtnet_info *vi) · line 5925

Quoted 10 lines
drivers/net/virtio_net.c +6 -2
                dev_kfree_skb(buf);
-       else
-               xdp_return_frame(ptr_to_xdp(buf));
+               break;
+
+       case VIRTNET_XMIT_TYPE_XDP:
+               xdp_return_frame(buf);
+               break;
+       }
 }

Others look fine.

Thanks

Quoted 5 lines
drivers/net/virtio_net.c +0 -1
 static void free_unused_bufs(struct virtnet_info *vi)
--

2.32.0.3.g01195cf9f

author reviewer
#20 lore raw

Re: [PATCH net-next 07/13] virtio_net: refactor the xmit type

Quoted 11 lines · Jason Wang
Quoted 7 lines · Xuan Zhuo

Because the af-xdp will introduce a new xmit type, so I refactor the xmit type mechanism first.

We use the last two bits of the pointer to distinguish the xmit type, so we can distinguish four xmit types. Now we have three types: skb, orphan and xdp.

And if I was not wrong, we do not anymore use bitmasks. If yes, let's explain the reason here.

In general, pointers are aligned to 4 or 8 bytes. If it is aligned to 4 bytes, then only two bits are free for a pointer. So we can only use two bits.

But there are 4 types here, so we can't use bits to distinguish them.

b00 for skb
b01 for SKB_ORPHAN
b10 for XDP
b11 for af-xdp tx

Quoted 38 lines
Quoted 22 lines
---
 drivers/net/virtio_net.c | 90 +++++++++++++++++++++++-----------------
 1 file changed, 51 insertions(+), 39 deletions(-)
drivers/net/virtio_net.c +0 -3
index 41aaea3b90fd..96abee36738b 100644
@@ -45,9 +45,6 @@ module_param(napi_tx, bool, 0644);
 #define VIRTIO_XDP_TX          BIT(0)
 #define VIRTIO_XDP_REDIR       BIT(1)
 
-#define VIRTIO_XDP_FLAG                BIT(0)
-#define VIRTIO_ORPHAN_FLAG     BIT(1)
-
 /* RX packet size EWMA. The average packet size is used to determine the packet
  * buffer size when refilling RX rings. As the entire RX ring may be refilled
  * at once, the weight is chosen so that the EWMA will be insensitive to short-

drivers/net/virtio_net.c static struct sk_buff *virtnet_skb_append_frag(struct sk_buff *head_skb, · line 511

Quoted 10 lines
drivers/net/virtio_net.c +3 -4
@@ -509,34 +506,35 @@ static struct sk_buff *virtnet_skb_append_frag(struct sk_buff *head_skb,
                                               struct page *page, void *buf,
                                               int len, int truesize);
 
-static bool is_xdp_frame(void *ptr)
-{
-       return (unsigned long)ptr & VIRTIO_XDP_FLAG;
-}
+enum virtnet_xmit_type {
+       VIRTNET_XMIT_TYPE_SKB,
+       VIRTNET_XMIT_TYPE_ORPHAN,

Let's rename this to SKB_ORPHAN?

Quoted 3 lines
drivers/net/virtio_net.c +2 -0
+       VIRTNET_XMIT_TYPE_XDP,
+};
 
Quoted 7 lines

drivers/net/virtio_net.c static struct sk_buff *virtnet_skb_append_frag(struct sk_buff *head_skb, · line 516

Quoted 6 lines
drivers/net/virtio_net.c +2 -4
-static void *xdp_to_ptr(struct xdp_frame *ptr)
-{
-       return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG);
-}
+#define VIRTNET_XMIT_TYPE_MASK (VIRTNET_XMIT_TYPE_SKB | VIRTNET_XMIT_TYPE_ORPHAN \
+                               | VIRTNET_XMIT_TYPE_XDP)

Maybe I should define VIRTNET_XMIT_TYPE_MASK to 0x3 directly with some comments.

Thanks.

Quoted 137 lines

I may miss something but it seems not a correct bitmask definition as each member is not a bit actually?

Quoted 113 lines
drivers/net/virtio_net.c +38 -26
-static struct xdp_frame *ptr_to_xdp(void *ptr)
+static enum virtnet_xmit_type virtnet_xmit_ptr_strip(void **ptr)
 {
-       return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
-}
+       unsigned long p = (unsigned long)*ptr;
 
-static bool is_orphan_skb(void *ptr)
-{
-       return (unsigned long)ptr & VIRTIO_ORPHAN_FLAG;
+       *ptr = (void *)(p & ~VIRTNET_XMIT_TYPE_MASK);
+
+       return p & VIRTNET_XMIT_TYPE_MASK;
 }
 
-static void *skb_to_ptr(struct sk_buff *skb, bool orphan)
+static void *virtnet_xmit_ptr_mix(void *ptr, enum virtnet_xmit_type type)
 {
-       return (void *)((unsigned long)skb | (orphan ? VIRTIO_ORPHAN_FLAG : 0));
+       return (void *)((unsigned long)ptr | type);
 }
 
-static struct sk_buff *ptr_to_skb(void *ptr)
+static int virtnet_add_outbuf(struct send_queue *sq, int num, void *data,
+                             enum virtnet_xmit_type type)
 {
-       return (struct sk_buff *)((unsigned long)ptr & ~VIRTIO_ORPHAN_FLAG);
+       return virtqueue_add_outbuf(sq->vq, sq->sg, num,
+                                   virtnet_xmit_ptr_mix(data, type),
+                                   GFP_ATOMIC);
 }
 
 static void sg_fill_dma(struct scatterlist *sg, dma_addr_t addr, u32 len)
@@ -549,29 +547,37 @@ static void sg_fill_dma(struct scatterlist *sg, dma_addr_t addr, u32 len)
 static void __free_old_xmit(struct send_queue *sq, struct netdev_queue *txq,
                            bool in_napi, struct virtnet_sq_free_stats *stats)
 {
+       struct xdp_frame *frame;
+       struct sk_buff *skb;
        unsigned int len;
        void *ptr;
 
        while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
-               if (!is_xdp_frame(ptr)) {
-                       struct sk_buff *skb = ptr_to_skb(ptr);
+               switch (virtnet_xmit_ptr_strip(&ptr)) {
+               case VIRTNET_XMIT_TYPE_SKB:
+                       skb = ptr;
 
                        pr_debug("Sent skb %p\n", skb);
+                       stats->napi_packets++;
+                       stats->napi_bytes += skb->len;
+                       napi_consume_skb(skb, in_napi);
+                       break;
 
-                       if (is_orphan_skb(ptr)) {
-                               stats->packets++;
-                               stats->bytes += skb->len;
-                       } else {
-                               stats->napi_packets++;
-                               stats->napi_bytes += skb->len;
-                       }
+               case VIRTNET_XMIT_TYPE_ORPHAN:
+                       skb = ptr;
+
+                       stats->packets++;
+                       stats->bytes += skb->len;
                        napi_consume_skb(skb, in_napi);
-               } else {
-                       struct xdp_frame *frame = ptr_to_xdp(ptr);
+                       break;
+
+               case VIRTNET_XMIT_TYPE_XDP:
+                       frame = ptr;
 
                        stats->packets++;
                        stats->bytes += xdp_get_frame_len(frame);
                        xdp_return_frame(frame);
+                       break;
                }
        }
        netdev_tx_completed_queue(txq, stats->napi_packets, stats->napi_bytes);
@@ -1421,8 +1427,7 @@ static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
                            skb_frag_size(frag), skb_frag_off(frag));
        }
 
-       err = virtqueue_add_outbuf(sq->vq, sq->sg, nr_frags + 1,
-                                  xdp_to_ptr(xdpf), GFP_ATOMIC);
+       err = virtnet_add_outbuf(sq, nr_frags + 1, xdpf, VIRTNET_XMIT_TYPE_XDP);
        if (unlikely(err))
                return -ENOSPC; /* Caller handle free/refcnt */
 
@@ -3028,8 +3033,9 @@ static int xmit_skb(struct send_queue *sq, struct sk_buff *skb, bool orphan)
                        return num_sg;
                num_sg++;
        }
-       return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg,
-                                   skb_to_ptr(skb, orphan), GFP_ATOMIC);
+
+       return virtnet_add_outbuf(sq, num_sg, skb,
+                                 orphan ? VIRTNET_XMIT_TYPE_ORPHAN : VIRTNET_XMIT_TYPE_SKB);
 }
 
 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
@@ -5906,10 +5912,16 @@ static void free_receive_page_frags(struct virtnet_info *vi)
 
 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf)
 {
-       if (!is_xdp_frame(buf))
+       switch (virtnet_xmit_ptr_strip(&buf)) {
+       case VIRTNET_XMIT_TYPE_SKB:
+       case VIRTNET_XMIT_TYPE_ORPHAN:

drivers/net/virtio_net.c static void free_receive_page_frags(struct virtnet_info *vi) · line 5925

Quoted 10 lines
drivers/net/virtio_net.c +6 -2
                dev_kfree_skb(buf);
-       else
-               xdp_return_frame(ptr_to_xdp(buf));
+               break;
+
+       case VIRTNET_XMIT_TYPE_XDP:
+               xdp_return_frame(buf);
+               break;
+       }
 }

Others look fine.

Thanks

Quoted 5 lines
drivers/net/virtio_net.c +0 -1
 static void free_unused_bufs(struct virtnet_info *vi)
--

2.32.0.3.g01195cf9f

#21 lore raw

Re: [PATCH net-next 07/13] virtio_net: refactor the xmit type

Quoted 17 lines · Xuan Zhuo
Quoted 11 lines · Jason Wang
Quoted 7 lines · Xuan Zhuo

Because the af-xdp will introduce a new xmit type, so I refactor the xmit type mechanism first.

We use the last two bits of the pointer to distinguish the xmit type, so we can distinguish four xmit types. Now we have three types: skb, orphan and xdp.

And if I was not wrong, we do not anymore use bitmasks. If yes, let's explain the reason here.

In general, pointers are aligned to 4 or 8 bytes. If it is aligned to 4 bytes, then only two bits are free for a pointer. So we can only use two bits.

Quoted 6 lines

But there are 4 types here, so we can't use bits to distinguish them.

b00 for skb
b01 for SKB_ORPHAN
b10 for XDP
b11 for af-xdp tx

Let's add these in the changelog.

Thanks

author reviewer
#22 lore raw

PATCH 8/13 virtio_net: xsk: bind/unbind xsk for tx

In reply to Xuan Zhuo

This patch implement the logic of bind/unbind xsk pool to sq and rq.

---
 drivers/net/virtio_net.c | 54 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
drivers/net/virtio_net.c +54 -0
index 96abee36738b..6a36a204e967 100644
@@ -295,6 +295,10 @@ struct send_queue {
 
 	/* Record whether sq is in reset state. */
 	bool reset;
+
+	struct xsk_buff_pool *xsk_pool;
+
+	dma_addr_t xsk_hdr_dma_addr;
 };
 
 /* Internal representation of a receive virtqueue */
@@ -494,6 +498,8 @@ struct virtio_net_common_hdr {
 	};
 };
 
+static struct virtio_net_common_hdr xsk_hdr;
+
 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf);
 static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp,
 			       struct net_device *dev,
@@ -5476,6 +5482,29 @@ static int virtnet_rq_bind_xsk_pool(struct virtnet_info *vi, struct receive_queu
 	return err;
 }
 
+static int virtnet_sq_bind_xsk_pool(struct virtnet_info *vi,
+				    struct send_queue *sq,
+				    struct xsk_buff_pool *pool)
+{
+	int err, qindex;
+
+	qindex = sq - vi->sq;
+
+	virtnet_tx_pause(vi, sq);
+
+	err = virtqueue_reset(sq->vq, virtnet_sq_free_unused_buf);
+	if (err) {
+		netdev_err(vi->dev, "reset tx fail: tx queue index: %d err: %d\n", qindex, err);
+		pool = NULL;
+	}
+
+	sq->xsk_pool = pool;
+
+	virtnet_tx_resume(vi, sq);
+
+	return err;
+}
+
 static int virtnet_xsk_pool_enable(struct net_device *dev,
 				   struct xsk_buff_pool *pool,
 				   u16 qid)
@@ -5484,6 +5513,7 @@ static int virtnet_xsk_pool_enable(struct net_device *dev,
 	struct receive_queue *rq;
 	struct device *dma_dev;
 	struct send_queue *sq;
+	dma_addr_t hdr_dma;
 	int err, size;
 
 	if (vi->hdr_len > xsk_pool_get_headroom(pool))
@@ -5521,6 +5551,10 @@ static int virtnet_xsk_pool_enable(struct net_device *dev,
 	if (!rq->xsk_buffs)
 		return -ENOMEM;
 
+	hdr_dma = dma_map_single(dma_dev, &xsk_hdr, vi->hdr_len, DMA_TO_DEVICE);
+	if (dma_mapping_error(dma_dev, hdr_dma))
+		return -ENOMEM;
+
 	err = xsk_pool_dma_map(pool, dma_dev, 0);
 	if (err)
 		goto err_xsk_map;
@@ -5529,11 +5563,23 @@ static int virtnet_xsk_pool_enable(struct net_device *dev,
 	if (err)
 		goto err_rq;
 
+	err = virtnet_sq_bind_xsk_pool(vi, sq, pool);
+	if (err)
+		goto err_sq;
+
+	/* Now, we do not support tx offset, so all the tx virtnet hdr is zero.
+	 * So all the tx packets can share a single hdr.
+	 */
+	sq->xsk_hdr_dma_addr = hdr_dma;
+
 	return 0;
 
+err_sq:
+	virtnet_rq_bind_xsk_pool(vi, rq, NULL);
 err_rq:
 	xsk_pool_dma_unmap(pool, 0);
 err_xsk_map:
+	dma_unmap_single(dma_dev, hdr_dma, vi->hdr_len, DMA_TO_DEVICE);
 	return err;
 }
 
@@ -5542,19 +5588,27 @@ static int virtnet_xsk_pool_disable(struct net_device *dev, u16 qid)
 	struct virtnet_info *vi = netdev_priv(dev);
 	struct xsk_buff_pool *pool;
 	struct receive_queue *rq;
+	struct device *dma_dev;
+	struct send_queue *sq;
 	int err;
 
 	if (qid >= vi->curr_queue_pairs)
 		return -EINVAL;
 
+	sq = &vi->sq[qid];
 	rq = &vi->rq[qid];
 
 	pool = rq->xsk_pool;
 
 	err = virtnet_rq_bind_xsk_pool(vi, rq, NULL);
+	err |= virtnet_sq_bind_xsk_pool(vi, sq, NULL);
 
 	xsk_pool_dma_unmap(pool, 0);
 
+	dma_dev = virtqueue_dma_dev(sq->vq);
+
+	dma_unmap_single(dma_dev, sq->xsk_hdr_dma_addr, vi->hdr_len, DMA_TO_DEVICE);
+
 	kvfree(rq->xsk_buffs);
 
 	return err;
Signature · 1 line
2.32.0.3.g01195cf9f
#23 lore raw

Re: [PATCH net-next 08/13] virtio_net: xsk: bind/unbind xsk for tx

Quoted 71 lines · Xuan Zhuo

This patch implement the logic of bind/unbind xsk pool to sq and rq.

---
 drivers/net/virtio_net.c | 54 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
drivers/net/virtio_net.c +30 -0
index 96abee36738b..6a36a204e967 100644
@@ -295,6 +295,10 @@ struct send_queue {
 
        /* Record whether sq is in reset state. */
        bool reset;
+
+       struct xsk_buff_pool *xsk_pool;
+
+       dma_addr_t xsk_hdr_dma_addr;
 };
 
 /* Internal representation of a receive virtqueue */
@@ -494,6 +498,8 @@ struct virtio_net_common_hdr {
        };
 };
 
+static struct virtio_net_common_hdr xsk_hdr;
+
 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf);
 static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp,
                               struct net_device *dev,
@@ -5476,6 +5482,29 @@ static int virtnet_rq_bind_xsk_pool(struct virtnet_info *vi, struct receive_queu
        return err;
 }
 
+static int virtnet_sq_bind_xsk_pool(struct virtnet_info *vi,
+                                   struct send_queue *sq,
+                                   struct xsk_buff_pool *pool)
+{
+       int err, qindex;
+
+       qindex = sq - vi->sq;
+
+       virtnet_tx_pause(vi, sq);
+
+       err = virtqueue_reset(sq->vq, virtnet_sq_free_unused_buf);
+       if (err) {
+               netdev_err(vi->dev, "reset tx fail: tx queue index: %d err: %d\n", qindex, err);
+               pool = NULL;
+       }
+
+       sq->xsk_pool = pool;
+
+       virtnet_tx_resume(vi, sq);
+
+       return err;
+}
+
 static int virtnet_xsk_pool_enable(struct net_device *dev,
                                   struct xsk_buff_pool *pool,
                                   u16 qid)
@@ -5484,6 +5513,7 @@ static int virtnet_xsk_pool_enable(struct net_device *dev,
        struct receive_queue *rq;
        struct device *dma_dev;
        struct send_queue *sq;
+       dma_addr_t hdr_dma;
        int err, size;
 
        if (vi->hdr_len > xsk_pool_get_headroom(pool))

drivers/net/virtio_net.c static int virtnet_xsk_pool_enable(struct net_device *dev, · line 5554

Quoted 4 lines
drivers/net/virtio_net.c +1 -0
@@ -5521,6 +5551,10 @@ static int virtnet_xsk_pool_enable(struct net_device *dev,
        if (!rq->xsk_buffs)
                return -ENOMEM;
 
+       hdr_dma = dma_map_single(dma_dev, &xsk_hdr, vi->hdr_len, DMA_TO_DEVICE);

Let's use the virtqueue_dma_xxx() wrappers here.

Quoted 44 lines
drivers/net/virtio_net.c +18 -0
+       if (dma_mapping_error(dma_dev, hdr_dma))
+               return -ENOMEM;
+
        err = xsk_pool_dma_map(pool, dma_dev, 0);
        if (err)
                goto err_xsk_map;
@@ -5529,11 +5563,23 @@ static int virtnet_xsk_pool_enable(struct net_device *dev,
        if (err)
                goto err_rq;
 
+       err = virtnet_sq_bind_xsk_pool(vi, sq, pool);
+       if (err)
+               goto err_sq;
+
+       /* Now, we do not support tx offset, so all the tx virtnet hdr is zero.
+        * So all the tx packets can share a single hdr.
+        */
+       sq->xsk_hdr_dma_addr = hdr_dma;
+
        return 0;
 
+err_sq:
+       virtnet_rq_bind_xsk_pool(vi, rq, NULL);
 err_rq:
        xsk_pool_dma_unmap(pool, 0);
 err_xsk_map:
+       dma_unmap_single(dma_dev, hdr_dma, vi->hdr_len, DMA_TO_DEVICE);
        return err;
 }
 
@@ -5542,19 +5588,27 @@ static int virtnet_xsk_pool_disable(struct net_device *dev, u16 qid)
        struct virtnet_info *vi = netdev_priv(dev);
        struct xsk_buff_pool *pool;
        struct receive_queue *rq;
+       struct device *dma_dev;
+       struct send_queue *sq;
        int err;
 
        if (qid >= vi->curr_queue_pairs)
                return -EINVAL;
 
+       sq = &vi->sq[qid];
        rq = &vi->rq[qid];
 

drivers/net/virtio_net.c static int virtnet_xsk_pool_disable(struct net_device *dev, u16 qid) · line 5610

Quoted 10 lines
drivers/net/virtio_net.c +4 -0
        pool = rq->xsk_pool;
 
        err = virtnet_rq_bind_xsk_pool(vi, rq, NULL);
+       err |= virtnet_sq_bind_xsk_pool(vi, sq, NULL);
 
        xsk_pool_dma_unmap(pool, 0);
 
+       dma_dev = virtqueue_dma_dev(sq->vq);
+
+       dma_unmap_single(dma_dev, sq->xsk_hdr_dma_addr, vi->hdr_len, DMA_TO_DEVICE);

And here.

Thanks

Quoted 7 lines
drivers/net/virtio_net.c +1 -1
+
        kvfree(rq->xsk_buffs);
 
        return err;
--

2.32.0.3.g01195cf9f

author reviewer
#24 lore raw

Re: [PATCH net-next 08/13] virtio_net: xsk: bind/unbind xsk for tx

Quoted 70 lines · Jason Wang
Quoted 69 lines · Xuan Zhuo

This patch implement the logic of bind/unbind xsk pool to sq and rq.

---
 drivers/net/virtio_net.c | 54 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
drivers/net/virtio_net.c +30 -0
index 96abee36738b..6a36a204e967 100644
@@ -295,6 +295,10 @@ struct send_queue {
 
        /* Record whether sq is in reset state. */
        bool reset;
+
+       struct xsk_buff_pool *xsk_pool;
+
+       dma_addr_t xsk_hdr_dma_addr;
 };
 
 /* Internal representation of a receive virtqueue */
@@ -494,6 +498,8 @@ struct virtio_net_common_hdr {
        };
 };
 
+static struct virtio_net_common_hdr xsk_hdr;
+
 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf);
 static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp,
                               struct net_device *dev,
@@ -5476,6 +5482,29 @@ static int virtnet_rq_bind_xsk_pool(struct virtnet_info *vi, struct receive_queu
        return err;
 }
 
+static int virtnet_sq_bind_xsk_pool(struct virtnet_info *vi,
+                                   struct send_queue *sq,
+                                   struct xsk_buff_pool *pool)
+{
+       int err, qindex;
+
+       qindex = sq - vi->sq;
+
+       virtnet_tx_pause(vi, sq);
+
+       err = virtqueue_reset(sq->vq, virtnet_sq_free_unused_buf);
+       if (err) {
+               netdev_err(vi->dev, "reset tx fail: tx queue index: %d err: %d\n", qindex, err);
+               pool = NULL;
+       }
+
+       sq->xsk_pool = pool;
+
+       virtnet_tx_resume(vi, sq);
+
+       return err;
+}
+
 static int virtnet_xsk_pool_enable(struct net_device *dev,
                                   struct xsk_buff_pool *pool,
                                   u16 qid)
@@ -5484,6 +5513,7 @@ static int virtnet_xsk_pool_enable(struct net_device *dev,
        struct receive_queue *rq;
        struct device *dma_dev;
        struct send_queue *sq;
+       dma_addr_t hdr_dma;
        int err, size;
 
Quoted 8 lines

drivers/net/virtio_net.c static int virtnet_xsk_pool_enable(struct net_device *dev, · line 5554

Quoted 6 lines
drivers/net/virtio_net.c +1 -0
        if (vi->hdr_len > xsk_pool_get_headroom(pool))
@@ -5521,6 +5551,10 @@ static int virtnet_xsk_pool_enable(struct net_device *dev,
        if (!rq->xsk_buffs)
                return -ENOMEM;
 
+       hdr_dma = dma_map_single(dma_dev, &xsk_hdr, vi->hdr_len, DMA_TO_DEVICE);

Let's use the virtqueue_dma_xxx() wrappers here.

Will fix.

Thanks.

Quoted 68 lines
Quoted 45 lines
drivers/net/virtio_net.c +18 -0
+       if (dma_mapping_error(dma_dev, hdr_dma))
+               return -ENOMEM;
+
        err = xsk_pool_dma_map(pool, dma_dev, 0);
        if (err)
                goto err_xsk_map;
@@ -5529,11 +5563,23 @@ static int virtnet_xsk_pool_enable(struct net_device *dev,
        if (err)
                goto err_rq;
 
+       err = virtnet_sq_bind_xsk_pool(vi, sq, pool);
+       if (err)
+               goto err_sq;
+
+       /* Now, we do not support tx offset, so all the tx virtnet hdr is zero.
+        * So all the tx packets can share a single hdr.
+        */
+       sq->xsk_hdr_dma_addr = hdr_dma;
+
        return 0;
 
+err_sq:
+       virtnet_rq_bind_xsk_pool(vi, rq, NULL);
 err_rq:
        xsk_pool_dma_unmap(pool, 0);
 err_xsk_map:
+       dma_unmap_single(dma_dev, hdr_dma, vi->hdr_len, DMA_TO_DEVICE);
        return err;
 }
 
@@ -5542,19 +5588,27 @@ static int virtnet_xsk_pool_disable(struct net_device *dev, u16 qid)
        struct virtnet_info *vi = netdev_priv(dev);
        struct xsk_buff_pool *pool;
        struct receive_queue *rq;
+       struct device *dma_dev;
+       struct send_queue *sq;
        int err;
 
        if (qid >= vi->curr_queue_pairs)
                return -EINVAL;
 
+       sq = &vi->sq[qid];
        rq = &vi->rq[qid];
 

drivers/net/virtio_net.c static int virtnet_xsk_pool_disable(struct net_device *dev, u16 qid) · line 5610

Quoted 10 lines
drivers/net/virtio_net.c +4 -0
        pool = rq->xsk_pool;
 
        err = virtnet_rq_bind_xsk_pool(vi, rq, NULL);
+       err |= virtnet_sq_bind_xsk_pool(vi, sq, NULL);
 
        xsk_pool_dma_unmap(pool, 0);
 
+       dma_dev = virtqueue_dma_dev(sq->vq);
+
+       dma_unmap_single(dma_dev, sq->xsk_hdr_dma_addr, vi->hdr_len, DMA_TO_DEVICE);

And here.

Thanks

Quoted 7 lines
drivers/net/virtio_net.c +1 -1
+
        kvfree(rq->xsk_buffs);
 
        return err;
--

2.32.0.3.g01195cf9f

author reviewer
#25 lore raw

PATCH 9/13 virtio_net: xsk: prevent disable tx napi

In reply to Xuan Zhuo

Since xsk's TX queue is consumed by TX NAPI, if sq is bound to xsk, then we must stop tx napi from being disabled.

---
 drivers/net/virtio_net.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +9 -1
index 6a36a204e967..221681926d23 100644
@@ -5005,7 +5005,7 @@ static int virtnet_set_coalesce(struct net_device *dev,
 				struct netlink_ext_ack *extack)
 {
 	struct virtnet_info *vi = netdev_priv(dev);
-	int ret, queue_number, napi_weight;
+	int ret, queue_number, napi_weight, i;
 	bool update_napi = false;
 
 	/* Can't change NAPI weight if the link is up */
@@ -5034,6 +5034,14 @@ static int virtnet_set_coalesce(struct net_device *dev,
 		return ret;
 
 	if (update_napi) {
+		/* xsk xmit depends on the tx napi. So if xsk is active,
+		 * prevent modifications to tx napi.
+		 */
+		for (i = queue_number; i < vi->max_queue_pairs; i++) {
+			if (vi->sq[i].xsk_pool)
+				return -EBUSY;
+		}
+
 		for (; queue_number < vi->max_queue_pairs; queue_number++)
 			vi->sq[queue_number].napi.weight = napi_weight;
 	}
Signature · 1 line
2.32.0.3.g01195cf9f
author reviewer
#26 lore raw

PATCH 10/13 virtio_net: xsk: tx: support xmit xsk buffer

In reply to Xuan Zhuo

The driver's tx napi is very important for XSK. It is responsible for obtaining data from the XSK queue and sending it out.

At the beginning, we need to trigger tx napi.

---
 drivers/net/virtio_net.c | 127 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 125 insertions(+), 2 deletions(-)
drivers/net/virtio_net.c +125 -2
index 221681926d23..3743694d3c3b 100644
@@ -516,10 +516,13 @@ enum virtnet_xmit_type {
 	VIRTNET_XMIT_TYPE_SKB,
 	VIRTNET_XMIT_TYPE_ORPHAN,
 	VIRTNET_XMIT_TYPE_XDP,
+	VIRTNET_XMIT_TYPE_XSK,
 };
 
 #define VIRTNET_XMIT_TYPE_MASK (VIRTNET_XMIT_TYPE_SKB | VIRTNET_XMIT_TYPE_ORPHAN \
-				| VIRTNET_XMIT_TYPE_XDP)
+				| VIRTNET_XMIT_TYPE_XDP | VIRTNET_XMIT_TYPE_XSK)
+
+#define VIRTIO_XSK_FLAG_OFFSET 4
 
 static enum virtnet_xmit_type virtnet_xmit_ptr_strip(void **ptr)
 {
@@ -543,6 +546,11 @@ static int virtnet_add_outbuf(struct send_queue *sq, int num, void *data,
 				    GFP_ATOMIC);
 }
 
+static u32 virtnet_ptr_to_xsk(void *ptr)
+{
+	return ((unsigned long)ptr) >> VIRTIO_XSK_FLAG_OFFSET;
+}
+
 static void sg_fill_dma(struct scatterlist *sg, dma_addr_t addr, u32 len)
 {
 	sg_assign_page(sg, NULL);
@@ -584,6 +592,10 @@ static void __free_old_xmit(struct send_queue *sq, struct netdev_queue *txq,
 			stats->bytes += xdp_get_frame_len(frame);
 			xdp_return_frame(frame);
 			break;
+
+		case VIRTNET_XMIT_TYPE_XSK:
+			stats->bytes += virtnet_ptr_to_xsk(ptr);
+			break;
 		}
 	}
 	netdev_tx_completed_queue(txq, stats->napi_packets, stats->napi_bytes);
@@ -1393,6 +1405,97 @@ static int virtnet_xsk_wakeup(struct net_device *dev, u32 qid, u32 flag)
 	return 0;
 }
 
+static void *virtnet_xsk_to_ptr(u32 len)
+{
+	unsigned long p;
+
+	p = len << VIRTIO_XSK_FLAG_OFFSET;
+
+	return virtnet_xmit_ptr_mix((void *)p, VIRTNET_XMIT_TYPE_XSK);
+}
+
+static int virtnet_xsk_xmit_one(struct send_queue *sq,
+				struct xsk_buff_pool *pool,
+				struct xdp_desc *desc)
+{
+	struct virtnet_info *vi;
+	dma_addr_t addr;
+
+	vi = sq->vq->vdev->priv;
+
+	addr = xsk_buff_raw_get_dma(pool, desc->addr);
+	xsk_buff_raw_dma_sync_for_device(pool, addr, desc->len);
+
+	sg_init_table(sq->sg, 2);
+
+	sg_fill_dma(sq->sg, sq->xsk_hdr_dma_addr, vi->hdr_len);
+	sg_fill_dma(sq->sg + 1, addr, desc->len);
+
+	return virtqueue_add_outbuf(sq->vq, sq->sg, 2,
+				    virtnet_xsk_to_ptr(desc->len), GFP_ATOMIC);
+}
+
+static int virtnet_xsk_xmit_batch(struct send_queue *sq,
+				  struct xsk_buff_pool *pool,
+				  unsigned int budget,
+				  u64 *kicks)
+{
+	struct xdp_desc *descs = pool->tx_descs;
+	bool kick = false;
+	u32 nb_pkts, i;
+	int err;
+
+	budget = min_t(u32, budget, sq->vq->num_free);
+
+	nb_pkts = xsk_tx_peek_release_desc_batch(pool, budget);
+	if (!nb_pkts)
+		return 0;
+
+	for (i = 0; i < nb_pkts; i++) {
+		err = virtnet_xsk_xmit_one(sq, pool, &descs[i]);
+		if (unlikely(err)) {
+			xsk_tx_completed(sq->xsk_pool, nb_pkts - i);
+			break;
+		}
+
+		kick = true;
+	}
+
+	if (kick && virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
+		(*kicks)++;
+
+	return i;
+}
+
+static bool virtnet_xsk_xmit(struct send_queue *sq, struct xsk_buff_pool *pool,
+			     int budget)
+{
+	struct virtnet_info *vi = sq->vq->vdev->priv;
+	struct virtnet_sq_free_stats stats = {};
+	struct net_device *dev = vi->dev;
+	u64 kicks = 0;
+	int sent;
+
+	__free_old_xmit(sq, netdev_get_tx_queue(dev, sq - vi->sq), true, &stats);
+
+	sent = virtnet_xsk_xmit_batch(sq, pool, budget, &kicks);
+
+	if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq))
+		check_sq_full_and_disable(vi, vi->dev, sq);
+
+	u64_stats_update_begin(&sq->stats.syncp);
+	u64_stats_add(&sq->stats.packets, stats.packets);
+	u64_stats_add(&sq->stats.bytes,   stats.bytes);
+	u64_stats_add(&sq->stats.kicks,   kicks);
+	u64_stats_add(&sq->stats.xdp_tx,  sent);
+	u64_stats_update_end(&sq->stats.syncp);
+
+	if (xsk_uses_need_wakeup(pool))
+		xsk_set_tx_need_wakeup(pool);
+
+	return sent == budget;
+}
+
 static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
 				   struct send_queue *sq,
 				   struct xdp_frame *xdpf)
@@ -2949,6 +3052,7 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
 	struct virtnet_info *vi = sq->vq->vdev->priv;
 	unsigned int index = vq2txq(sq->vq);
 	struct netdev_queue *txq;
+	bool xsk_busy = false;
 	int opaque;
 	bool done;
 
@@ -2961,7 +3065,11 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
 	txq = netdev_get_tx_queue(vi->dev, index);
 	__netif_tx_lock(txq, raw_smp_processor_id());
 	virtqueue_disable_cb(sq->vq);
-	free_old_xmit(sq, txq, !!budget);
+
+	if (sq->xsk_pool)
+		xsk_busy = virtnet_xsk_xmit(sq, sq->xsk_pool, budget);
+	else
+		free_old_xmit(sq, txq, !!budget);
 
 	if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) {
 		if (netif_tx_queue_stopped(txq)) {
@@ -2972,6 +3080,11 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
 		netif_tx_wake_queue(txq);
 	}
 
+	if (xsk_busy) {
+		__netif_tx_unlock(txq);
+		return budget;
+	}
+
 	opaque = virtqueue_enable_cb_prepare(sq->vq);
 
 	done = napi_complete_done(napi, 0);
@@ -5974,6 +6087,12 @@ static void free_receive_page_frags(struct virtnet_info *vi)
 
 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf)
 {
+	struct virtnet_info *vi = vq->vdev->priv;
+	struct send_queue *sq;
+	int i = vq2rxq(vq);
+
+	sq = &vi->sq[i];
+
 	switch (virtnet_xmit_ptr_strip(&buf)) {
 	case VIRTNET_XMIT_TYPE_SKB:
 	case VIRTNET_XMIT_TYPE_ORPHAN:
@@ -5983,6 +6102,10 @@ static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf)
 	case VIRTNET_XMIT_TYPE_XDP:
 		xdp_return_frame(buf);
 		break;
+
+	case VIRTNET_XMIT_TYPE_XSK:
+		xsk_tx_completed(sq->xsk_pool, 1);
+		break;
 	}
 }
 
Signature · 1 line
2.32.0.3.g01195cf9f
#27 lore raw

Re: [PATCH net-next 10/13] virtio_net: xsk: tx: support xmit xsk buffer

Quoted 31 lines · Xuan Zhuo

The driver's tx napi is very important for XSK. It is responsible for obtaining data from the XSK queue and sending it out.

At the beginning, we need to trigger tx napi.

---
 drivers/net/virtio_net.c | 127 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 125 insertions(+), 2 deletions(-)
drivers/net/virtio_net.c +4 -1
index 221681926d23..3743694d3c3b 100644
@@ -516,10 +516,13 @@ enum virtnet_xmit_type {
        VIRTNET_XMIT_TYPE_SKB,
        VIRTNET_XMIT_TYPE_ORPHAN,
        VIRTNET_XMIT_TYPE_XDP,
+       VIRTNET_XMIT_TYPE_XSK,
 };
 
 #define VIRTNET_XMIT_TYPE_MASK (VIRTNET_XMIT_TYPE_SKB | VIRTNET_XMIT_TYPE_ORPHAN \
-                               | VIRTNET_XMIT_TYPE_XDP)
+                               | VIRTNET_XMIT_TYPE_XDP | VIRTNET_XMIT_TYPE_XSK)
+
+#define VIRTIO_XSK_FLAG_OFFSET 4
 
 static enum virtnet_xmit_type virtnet_xmit_ptr_strip(void **ptr)
 {

drivers/net/virtio_net.c static int virtnet_add_outbuf(struct send_queue *sq, int num, void *data, · line 553

Quoted 8 lines
drivers/net/virtio_net.c +5 -0
@@ -543,6 +546,11 @@ static int virtnet_add_outbuf(struct send_queue *sq, int num, void *data,
                                    GFP_ATOMIC);
 }
 
+static u32 virtnet_ptr_to_xsk(void *ptr)
+{
+       return ((unsigned long)ptr) >> VIRTIO_XSK_FLAG_OFFSET;
+}
+

This needs a better name, otherwise readers might be confused.

E.g something like virtnet_ptr_to_xsk_buff_len()?

drivers/net/virtio_net.c static void __free_old_xmit(struct send_queue *sq, struct netdev_queue *txq, · line 598

Quoted 11 lines
drivers/net/virtio_net.c +4 -0
 static void sg_fill_dma(struct scatterlist *sg, dma_addr_t addr, u32 len)
 {
        sg_assign_page(sg, NULL);
@@ -584,6 +592,10 @@ static void __free_old_xmit(struct send_queue *sq, struct netdev_queue *txq,
                        stats->bytes += xdp_get_frame_len(frame);
                        xdp_return_frame(frame);
                        break;
+
+               case VIRTNET_XMIT_TYPE_XSK:
+                       stats->bytes += virtnet_ptr_to_xsk(ptr);
+                       break;

Do we miss xsk_tx_completed() here?

Quoted 47 lines
drivers/net/virtio_net.c +40 -0
                }
        }
        netdev_tx_completed_queue(txq, stats->napi_packets, stats->napi_bytes);
@@ -1393,6 +1405,97 @@ static int virtnet_xsk_wakeup(struct net_device *dev, u32 qid, u32 flag)
        return 0;
 }
 
+static void *virtnet_xsk_to_ptr(u32 len)
+{
+       unsigned long p;
+
+       p = len << VIRTIO_XSK_FLAG_OFFSET;
+
+       return virtnet_xmit_ptr_mix((void *)p, VIRTNET_XMIT_TYPE_XSK);
+}
+
+static int virtnet_xsk_xmit_one(struct send_queue *sq,
+                               struct xsk_buff_pool *pool,
+                               struct xdp_desc *desc)
+{
+       struct virtnet_info *vi;
+       dma_addr_t addr;
+
+       vi = sq->vq->vdev->priv;
+
+       addr = xsk_buff_raw_get_dma(pool, desc->addr);
+       xsk_buff_raw_dma_sync_for_device(pool, addr, desc->len);
+
+       sg_init_table(sq->sg, 2);
+
+       sg_fill_dma(sq->sg, sq->xsk_hdr_dma_addr, vi->hdr_len);
+       sg_fill_dma(sq->sg + 1, addr, desc->len);
+
+       return virtqueue_add_outbuf(sq->vq, sq->sg, 2,
+                                   virtnet_xsk_to_ptr(desc->len), GFP_ATOMIC);
+}
+
+static int virtnet_xsk_xmit_batch(struct send_queue *sq,
+                                 struct xsk_buff_pool *pool,
+                                 unsigned int budget,
+                                 u64 *kicks)
+{
+       struct xdp_desc *descs = pool->tx_descs;
+       bool kick = false;
+       u32 nb_pkts, i;
+       int err;
+

drivers/net/virtio_net.c static int virtnet_xsk_wakeup(struct net_device *dev, u32 qid, u32 flag) · line 1457

Quoted 10 lines
drivers/net/virtio_net.c +10 -0
+       budget = min_t(u32, budget, sq->vq->num_free);
+
+       nb_pkts = xsk_tx_peek_release_desc_batch(pool, budget);
+       if (!nb_pkts)
+               return 0;
+
+       for (i = 0; i < nb_pkts; i++) {
+               err = virtnet_xsk_xmit_one(sq, pool, &descs[i]);
+               if (unlikely(err)) {
+                       xsk_tx_completed(sq->xsk_pool, nb_pkts - i);

Should we kick in this condition?

drivers/net/virtio_net.c static int virtnet_xsk_wakeup(struct net_device *dev, u32 qid, u32 flag) · line 1464

Quoted 7 lines
drivers/net/virtio_net.c +7 -0
+                       break;
+               }
+
+               kick = true;
+       }
+
+       if (kick && virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))

Can we simply use virtqueue_kick() here?

Quoted 5 lines
drivers/net/virtio_net.c +5 -0
+               (*kicks)++;
+
+       return i;
+}
+

drivers/net/virtio_net.c static int virtnet_xsk_wakeup(struct net_device *dev, u32 qid, u32 flag) · line 1479

Quoted 10 lines
drivers/net/virtio_net.c +10 -0
+static bool virtnet_xsk_xmit(struct send_queue *sq, struct xsk_buff_pool *pool,
+                            int budget)
+{
+       struct virtnet_info *vi = sq->vq->vdev->priv;
+       struct virtnet_sq_free_stats stats = {};
+       struct net_device *dev = vi->dev;
+       u64 kicks = 0;
+       int sent;
+
+       __free_old_xmit(sq, netdev_get_tx_queue(dev, sq - vi->sq), true, &stats);

I haven't checked in depth, but I wonder if we have some side effects when using non NAPI tx mode:

        if (napi->weight)
                virtqueue_napi_schedule(napi, vq);
        else
                /* We were probably waiting for more output buffers. */
                netif_wake_subqueue(vi->dev, vq2txq(vq));

Does this mean xsk will suffer the same issue like when there's no xsk xmit request, we could end up with no way to reclaim xmitted xsk buffers? (Or should we disallow AF_XDP to be used for non TX-NAPI mode?)

Quoted 80 lines
drivers/net/virtio_net.c +40 -2
+
+       sent = virtnet_xsk_xmit_batch(sq, pool, budget, &kicks);
+
+       if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq))
+               check_sq_full_and_disable(vi, vi->dev, sq);
+
+       u64_stats_update_begin(&sq->stats.syncp);
+       u64_stats_add(&sq->stats.packets, stats.packets);
+       u64_stats_add(&sq->stats.bytes,   stats.bytes);
+       u64_stats_add(&sq->stats.kicks,   kicks);
+       u64_stats_add(&sq->stats.xdp_tx,  sent);
+       u64_stats_update_end(&sq->stats.syncp);
+
+       if (xsk_uses_need_wakeup(pool))
+               xsk_set_tx_need_wakeup(pool);
+
+       return sent == budget;
+}
+
 static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
                                   struct send_queue *sq,
                                   struct xdp_frame *xdpf)
@@ -2949,6 +3052,7 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
        struct virtnet_info *vi = sq->vq->vdev->priv;
        unsigned int index = vq2txq(sq->vq);
        struct netdev_queue *txq;
+       bool xsk_busy = false;
        int opaque;
        bool done;
 
@@ -2961,7 +3065,11 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
        txq = netdev_get_tx_queue(vi->dev, index);
        __netif_tx_lock(txq, raw_smp_processor_id());
        virtqueue_disable_cb(sq->vq);
-       free_old_xmit(sq, txq, !!budget);
+
+       if (sq->xsk_pool)
+               xsk_busy = virtnet_xsk_xmit(sq, sq->xsk_pool, budget);
+       else
+               free_old_xmit(sq, txq, !!budget);
 
        if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) {
                if (netif_tx_queue_stopped(txq)) {
@@ -2972,6 +3080,11 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
                netif_tx_wake_queue(txq);
        }
 
+       if (xsk_busy) {
+               __netif_tx_unlock(txq);
+               return budget;
+       }
+
        opaque = virtqueue_enable_cb_prepare(sq->vq);
 
        done = napi_complete_done(napi, 0);
@@ -5974,6 +6087,12 @@ static void free_receive_page_frags(struct virtnet_info *vi)
 
 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf)
 {
+       struct virtnet_info *vi = vq->vdev->priv;
+       struct send_queue *sq;
+       int i = vq2rxq(vq);
+
+       sq = &vi->sq[i];
+
        switch (virtnet_xmit_ptr_strip(&buf)) {
        case VIRTNET_XMIT_TYPE_SKB:
        case VIRTNET_XMIT_TYPE_ORPHAN:
@@ -5983,6 +6102,10 @@ static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf)
        case VIRTNET_XMIT_TYPE_XDP:
                xdp_return_frame(buf);
                break;
+
+       case VIRTNET_XMIT_TYPE_XSK:
+               xsk_tx_completed(sq->xsk_pool, 1);
+               break;
        }
 }
 
--
Quoted 2 lines

2.32.0.3.g01195cf9f

Thanks

author reviewer
#28 lore raw

Re: [PATCH net-next 10/13] virtio_net: xsk: tx: support xmit xsk buffer

Quoted 154 lines · Jason Wang
Quoted 31 lines · Xuan Zhuo

The driver's tx napi is very important for XSK. It is responsible for obtaining data from the XSK queue and sending it out.

At the beginning, we need to trigger tx napi.

---
 drivers/net/virtio_net.c | 127 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 125 insertions(+), 2 deletions(-)
drivers/net/virtio_net.c +4 -1
index 221681926d23..3743694d3c3b 100644
@@ -516,10 +516,13 @@ enum virtnet_xmit_type {
        VIRTNET_XMIT_TYPE_SKB,
        VIRTNET_XMIT_TYPE_ORPHAN,
        VIRTNET_XMIT_TYPE_XDP,
+       VIRTNET_XMIT_TYPE_XSK,
 };
 
 #define VIRTNET_XMIT_TYPE_MASK (VIRTNET_XMIT_TYPE_SKB | VIRTNET_XMIT_TYPE_ORPHAN \
-                               | VIRTNET_XMIT_TYPE_XDP)
+                               | VIRTNET_XMIT_TYPE_XDP | VIRTNET_XMIT_TYPE_XSK)
+
+#define VIRTIO_XSK_FLAG_OFFSET 4
 
 static enum virtnet_xmit_type virtnet_xmit_ptr_strip(void **ptr)
 {

drivers/net/virtio_net.c static int virtnet_add_outbuf(struct send_queue *sq, int num, void *data, · line 553

Quoted 8 lines
drivers/net/virtio_net.c +5 -0
@@ -543,6 +546,11 @@ static int virtnet_add_outbuf(struct send_queue *sq, int num, void *data,
                                    GFP_ATOMIC);
 }
 
+static u32 virtnet_ptr_to_xsk(void *ptr)
+{
+       return ((unsigned long)ptr) >> VIRTIO_XSK_FLAG_OFFSET;
+}
+

This needs a better name, otherwise readers might be confused.

E.g something like virtnet_ptr_to_xsk_buff_len()?

drivers/net/virtio_net.c static void __free_old_xmit(struct send_queue *sq, struct netdev_queue *txq, · line 598

Quoted 11 lines
drivers/net/virtio_net.c +4 -0
 static void sg_fill_dma(struct scatterlist *sg, dma_addr_t addr, u32 len)
 {
        sg_assign_page(sg, NULL);
@@ -584,6 +592,10 @@ static void __free_old_xmit(struct send_queue *sq, struct netdev_queue *txq,
                        stats->bytes += xdp_get_frame_len(frame);
                        xdp_return_frame(frame);
                        break;
+
+               case VIRTNET_XMIT_TYPE_XSK:
+                       stats->bytes += virtnet_ptr_to_xsk(ptr);
+                       break;

Do we miss xsk_tx_completed() here?

Quoted 47 lines
drivers/net/virtio_net.c +40 -0
                }
        }
        netdev_tx_completed_queue(txq, stats->napi_packets, stats->napi_bytes);
@@ -1393,6 +1405,97 @@ static int virtnet_xsk_wakeup(struct net_device *dev, u32 qid, u32 flag)
        return 0;
 }
 
+static void *virtnet_xsk_to_ptr(u32 len)
+{
+       unsigned long p;
+
+       p = len << VIRTIO_XSK_FLAG_OFFSET;
+
+       return virtnet_xmit_ptr_mix((void *)p, VIRTNET_XMIT_TYPE_XSK);
+}
+
+static int virtnet_xsk_xmit_one(struct send_queue *sq,
+                               struct xsk_buff_pool *pool,
+                               struct xdp_desc *desc)
+{
+       struct virtnet_info *vi;
+       dma_addr_t addr;
+
+       vi = sq->vq->vdev->priv;
+
+       addr = xsk_buff_raw_get_dma(pool, desc->addr);
+       xsk_buff_raw_dma_sync_for_device(pool, addr, desc->len);
+
+       sg_init_table(sq->sg, 2);
+
+       sg_fill_dma(sq->sg, sq->xsk_hdr_dma_addr, vi->hdr_len);
+       sg_fill_dma(sq->sg + 1, addr, desc->len);
+
+       return virtqueue_add_outbuf(sq->vq, sq->sg, 2,
+                                   virtnet_xsk_to_ptr(desc->len), GFP_ATOMIC);
+}
+
+static int virtnet_xsk_xmit_batch(struct send_queue *sq,
+                                 struct xsk_buff_pool *pool,
+                                 unsigned int budget,
+                                 u64 *kicks)
+{
+       struct xdp_desc *descs = pool->tx_descs;
+       bool kick = false;
+       u32 nb_pkts, i;
+       int err;
+

drivers/net/virtio_net.c static int virtnet_xsk_wakeup(struct net_device *dev, u32 qid, u32 flag) · line 1457

Quoted 10 lines
drivers/net/virtio_net.c +10 -0
+       budget = min_t(u32, budget, sq->vq->num_free);
+
+       nb_pkts = xsk_tx_peek_release_desc_batch(pool, budget);
+       if (!nb_pkts)
+               return 0;
+
+       for (i = 0; i < nb_pkts; i++) {
+               err = virtnet_xsk_xmit_one(sq, pool, &descs[i]);
+               if (unlikely(err)) {
+                       xsk_tx_completed(sq->xsk_pool, nb_pkts - i);

Should we kick in this condition?

drivers/net/virtio_net.c static int virtnet_xsk_wakeup(struct net_device *dev, u32 qid, u32 flag) · line 1464

Quoted 7 lines
drivers/net/virtio_net.c +7 -0
+                       break;
+               }
+
+               kick = true;
+       }
+
+       if (kick && virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))

Can we simply use virtqueue_kick() here?

Quoted 5 lines
drivers/net/virtio_net.c +5 -0
+               (*kicks)++;
+
+       return i;
+}
+

drivers/net/virtio_net.c static int virtnet_xsk_wakeup(struct net_device *dev, u32 qid, u32 flag) · line 1479

Quoted 10 lines
drivers/net/virtio_net.c +10 -0
+static bool virtnet_xsk_xmit(struct send_queue *sq, struct xsk_buff_pool *pool,
+                            int budget)
+{
+       struct virtnet_info *vi = sq->vq->vdev->priv;
+       struct virtnet_sq_free_stats stats = {};
+       struct net_device *dev = vi->dev;
+       u64 kicks = 0;
+       int sent;
+
+       __free_old_xmit(sq, netdev_get_tx_queue(dev, sq - vi->sq), true, &stats);

I haven't checked in depth, but I wonder if we have some side effects when using non NAPI tx mode:

        if (napi->weight)
                virtqueue_napi_schedule(napi, vq);
        else
                /* We were probably waiting for more output buffers. */
                netif_wake_subqueue(vi->dev, vq2txq(vq));
Quoted 4 lines

Does this mean xsk will suffer the same issue like when there's no xsk xmit request, we could end up with no way to reclaim xmitted xsk buffers? (Or should we disallow AF_XDP to be used for non TX-NAPI mode?)

Disallow AF_XDP to be used for non TX-NAPI mode.

The last patch #9 does this.

#9 [PATCH net-next 09/13] virtio_net: xsk: prevent disable tx napi

Thanks.

Quoted 86 lines
Quoted 81 lines
drivers/net/virtio_net.c +40 -2
+
+       sent = virtnet_xsk_xmit_batch(sq, pool, budget, &kicks);
+
+       if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq))
+               check_sq_full_and_disable(vi, vi->dev, sq);
+
+       u64_stats_update_begin(&sq->stats.syncp);
+       u64_stats_add(&sq->stats.packets, stats.packets);
+       u64_stats_add(&sq->stats.bytes,   stats.bytes);
+       u64_stats_add(&sq->stats.kicks,   kicks);
+       u64_stats_add(&sq->stats.xdp_tx,  sent);
+       u64_stats_update_end(&sq->stats.syncp);
+
+       if (xsk_uses_need_wakeup(pool))
+               xsk_set_tx_need_wakeup(pool);
+
+       return sent == budget;
+}
+
 static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
                                   struct send_queue *sq,
                                   struct xdp_frame *xdpf)
@@ -2949,6 +3052,7 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
        struct virtnet_info *vi = sq->vq->vdev->priv;
        unsigned int index = vq2txq(sq->vq);
        struct netdev_queue *txq;
+       bool xsk_busy = false;
        int opaque;
        bool done;
 
@@ -2961,7 +3065,11 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
        txq = netdev_get_tx_queue(vi->dev, index);
        __netif_tx_lock(txq, raw_smp_processor_id());
        virtqueue_disable_cb(sq->vq);
-       free_old_xmit(sq, txq, !!budget);
+
+       if (sq->xsk_pool)
+               xsk_busy = virtnet_xsk_xmit(sq, sq->xsk_pool, budget);
+       else
+               free_old_xmit(sq, txq, !!budget);
 
        if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) {
                if (netif_tx_queue_stopped(txq)) {
@@ -2972,6 +3080,11 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
                netif_tx_wake_queue(txq);
        }
 
+       if (xsk_busy) {
+               __netif_tx_unlock(txq);
+               return budget;
+       }
+
        opaque = virtqueue_enable_cb_prepare(sq->vq);
 
        done = napi_complete_done(napi, 0);
@@ -5974,6 +6087,12 @@ static void free_receive_page_frags(struct virtnet_info *vi)
 
 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf)
 {
+       struct virtnet_info *vi = vq->vdev->priv;
+       struct send_queue *sq;
+       int i = vq2rxq(vq);
+
+       sq = &vi->sq[i];
+
        switch (virtnet_xmit_ptr_strip(&buf)) {
        case VIRTNET_XMIT_TYPE_SKB:
        case VIRTNET_XMIT_TYPE_ORPHAN:
@@ -5983,6 +6102,10 @@ static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf)
        case VIRTNET_XMIT_TYPE_XDP:
                xdp_return_frame(buf);
                break;
+
+       case VIRTNET_XMIT_TYPE_XSK:
+               xsk_tx_completed(sq->xsk_pool, 1);
+               break;
        }
 }
 
--
Quoted 2 lines

2.32.0.3.g01195cf9f

Thanks

#29 lore raw

Re: [PATCH net-next 10/13] virtio_net: xsk: tx: support xmit xsk buffer

Quoted 161 lines · Xuan Zhuo
Quoted 154 lines · Jason Wang
Quoted 31 lines · Xuan Zhuo

The driver's tx napi is very important for XSK. It is responsible for obtaining data from the XSK queue and sending it out.

At the beginning, we need to trigger tx napi.

---
 drivers/net/virtio_net.c | 127 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 125 insertions(+), 2 deletions(-)
drivers/net/virtio_net.c +4 -1
index 221681926d23..3743694d3c3b 100644
@@ -516,10 +516,13 @@ enum virtnet_xmit_type {
        VIRTNET_XMIT_TYPE_SKB,
        VIRTNET_XMIT_TYPE_ORPHAN,
        VIRTNET_XMIT_TYPE_XDP,
+       VIRTNET_XMIT_TYPE_XSK,
 };
 
 #define VIRTNET_XMIT_TYPE_MASK (VIRTNET_XMIT_TYPE_SKB | VIRTNET_XMIT_TYPE_ORPHAN \
-                               | VIRTNET_XMIT_TYPE_XDP)
+                               | VIRTNET_XMIT_TYPE_XDP | VIRTNET_XMIT_TYPE_XSK)
+
+#define VIRTIO_XSK_FLAG_OFFSET 4
 
 static enum virtnet_xmit_type virtnet_xmit_ptr_strip(void **ptr)
 {

drivers/net/virtio_net.c static int virtnet_add_outbuf(struct send_queue *sq, int num, void *data, · line 553

Quoted 8 lines
drivers/net/virtio_net.c +5 -0
@@ -543,6 +546,11 @@ static int virtnet_add_outbuf(struct send_queue *sq, int num, void *data,
                                    GFP_ATOMIC);
 }
 
+static u32 virtnet_ptr_to_xsk(void *ptr)
+{
+       return ((unsigned long)ptr) >> VIRTIO_XSK_FLAG_OFFSET;
+}
+

This needs a better name, otherwise readers might be confused.

E.g something like virtnet_ptr_to_xsk_buff_len()?

drivers/net/virtio_net.c static void __free_old_xmit(struct send_queue *sq, struct netdev_queue *txq, · line 598

Quoted 11 lines
drivers/net/virtio_net.c +4 -0
 static void sg_fill_dma(struct scatterlist *sg, dma_addr_t addr, u32 len)
 {
        sg_assign_page(sg, NULL);
@@ -584,6 +592,10 @@ static void __free_old_xmit(struct send_queue *sq, struct netdev_queue *txq,
                        stats->bytes += xdp_get_frame_len(frame);
                        xdp_return_frame(frame);
                        break;
+
+               case VIRTNET_XMIT_TYPE_XSK:
+                       stats->bytes += virtnet_ptr_to_xsk(ptr);
+                       break;

Do we miss xsk_tx_completed() here?

Quoted 47 lines
drivers/net/virtio_net.c +40 -0
                }
        }
        netdev_tx_completed_queue(txq, stats->napi_packets, stats->napi_bytes);
@@ -1393,6 +1405,97 @@ static int virtnet_xsk_wakeup(struct net_device *dev, u32 qid, u32 flag)
        return 0;
 }
 
+static void *virtnet_xsk_to_ptr(u32 len)
+{
+       unsigned long p;
+
+       p = len << VIRTIO_XSK_FLAG_OFFSET;
+
+       return virtnet_xmit_ptr_mix((void *)p, VIRTNET_XMIT_TYPE_XSK);
+}
+
+static int virtnet_xsk_xmit_one(struct send_queue *sq,
+                               struct xsk_buff_pool *pool,
+                               struct xdp_desc *desc)
+{
+       struct virtnet_info *vi;
+       dma_addr_t addr;
+
+       vi = sq->vq->vdev->priv;
+
+       addr = xsk_buff_raw_get_dma(pool, desc->addr);
+       xsk_buff_raw_dma_sync_for_device(pool, addr, desc->len);
+
+       sg_init_table(sq->sg, 2);
+
+       sg_fill_dma(sq->sg, sq->xsk_hdr_dma_addr, vi->hdr_len);
+       sg_fill_dma(sq->sg + 1, addr, desc->len);
+
+       return virtqueue_add_outbuf(sq->vq, sq->sg, 2,
+                                   virtnet_xsk_to_ptr(desc->len), GFP_ATOMIC);
+}
+
+static int virtnet_xsk_xmit_batch(struct send_queue *sq,
+                                 struct xsk_buff_pool *pool,
+                                 unsigned int budget,
+                                 u64 *kicks)
+{
+       struct xdp_desc *descs = pool->tx_descs;
+       bool kick = false;
+       u32 nb_pkts, i;
+       int err;
+

drivers/net/virtio_net.c static int virtnet_xsk_wakeup(struct net_device *dev, u32 qid, u32 flag) · line 1457

Quoted 10 lines
drivers/net/virtio_net.c +10 -0
+       budget = min_t(u32, budget, sq->vq->num_free);
+
+       nb_pkts = xsk_tx_peek_release_desc_batch(pool, budget);
+       if (!nb_pkts)
+               return 0;
+
+       for (i = 0; i < nb_pkts; i++) {
+               err = virtnet_xsk_xmit_one(sq, pool, &descs[i]);
+               if (unlikely(err)) {
+                       xsk_tx_completed(sq->xsk_pool, nb_pkts - i);

Should we kick in this condition?

drivers/net/virtio_net.c static int virtnet_xsk_wakeup(struct net_device *dev, u32 qid, u32 flag) · line 1464

Quoted 7 lines
drivers/net/virtio_net.c +7 -0
+                       break;
+               }
+
+               kick = true;
+       }
+
+       if (kick && virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))

Can we simply use virtqueue_kick() here?

Quoted 5 lines
drivers/net/virtio_net.c +5 -0
+               (*kicks)++;
+
+       return i;
+}
+

drivers/net/virtio_net.c static int virtnet_xsk_wakeup(struct net_device *dev, u32 qid, u32 flag) · line 1479

Quoted 10 lines
drivers/net/virtio_net.c +10 -0
+static bool virtnet_xsk_xmit(struct send_queue *sq, struct xsk_buff_pool *pool,
+                            int budget)
+{
+       struct virtnet_info *vi = sq->vq->vdev->priv;
+       struct virtnet_sq_free_stats stats = {};
+       struct net_device *dev = vi->dev;
+       u64 kicks = 0;
+       int sent;
+
+       __free_old_xmit(sq, netdev_get_tx_queue(dev, sq - vi->sq), true, &stats);

I haven't checked in depth, but I wonder if we have some side effects when using non NAPI tx mode:

        if (napi->weight)
                virtqueue_napi_schedule(napi, vq);
        else
                /* We were probably waiting for more output buffers. */
                netif_wake_subqueue(vi->dev, vq2txq(vq));
Quoted 4 lines

Does this mean xsk will suffer the same issue like when there's no xsk xmit request, we could end up with no way to reclaim xmitted xsk buffers? (Or should we disallow AF_XDP to be used for non TX-NAPI mode?)

Quoted 5 lines

Disallow AF_XDP to be used for non TX-NAPI mode.

The last patch #9 does this.

#9 [PATCH net-next 09/13] virtio_net: xsk: prevent disable tx napi

Great, for some reason I miss that.

Thanks

Quoted 89 lines

Thanks.

Quoted 86 lines
Quoted 81 lines
drivers/net/virtio_net.c +40 -2
+
+       sent = virtnet_xsk_xmit_batch(sq, pool, budget, &kicks);
+
+       if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq))
+               check_sq_full_and_disable(vi, vi->dev, sq);
+
+       u64_stats_update_begin(&sq->stats.syncp);
+       u64_stats_add(&sq->stats.packets, stats.packets);
+       u64_stats_add(&sq->stats.bytes,   stats.bytes);
+       u64_stats_add(&sq->stats.kicks,   kicks);
+       u64_stats_add(&sq->stats.xdp_tx,  sent);
+       u64_stats_update_end(&sq->stats.syncp);
+
+       if (xsk_uses_need_wakeup(pool))
+               xsk_set_tx_need_wakeup(pool);
+
+       return sent == budget;
+}
+
 static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
                                   struct send_queue *sq,
                                   struct xdp_frame *xdpf)
@@ -2949,6 +3052,7 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
        struct virtnet_info *vi = sq->vq->vdev->priv;
        unsigned int index = vq2txq(sq->vq);
        struct netdev_queue *txq;
+       bool xsk_busy = false;
        int opaque;
        bool done;
 
@@ -2961,7 +3065,11 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
        txq = netdev_get_tx_queue(vi->dev, index);
        __netif_tx_lock(txq, raw_smp_processor_id());
        virtqueue_disable_cb(sq->vq);
-       free_old_xmit(sq, txq, !!budget);
+
+       if (sq->xsk_pool)
+               xsk_busy = virtnet_xsk_xmit(sq, sq->xsk_pool, budget);
+       else
+               free_old_xmit(sq, txq, !!budget);
 
        if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) {
                if (netif_tx_queue_stopped(txq)) {
@@ -2972,6 +3080,11 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
                netif_tx_wake_queue(txq);
        }
 
+       if (xsk_busy) {
+               __netif_tx_unlock(txq);
+               return budget;
+       }
+
        opaque = virtqueue_enable_cb_prepare(sq->vq);
 
        done = napi_complete_done(napi, 0);
@@ -5974,6 +6087,12 @@ static void free_receive_page_frags(struct virtnet_info *vi)
 
 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf)
 {
+       struct virtnet_info *vi = vq->vdev->priv;
+       struct send_queue *sq;
+       int i = vq2rxq(vq);
+
+       sq = &vi->sq[i];
+
        switch (virtnet_xmit_ptr_strip(&buf)) {
        case VIRTNET_XMIT_TYPE_SKB:
        case VIRTNET_XMIT_TYPE_ORPHAN:
@@ -5983,6 +6102,10 @@ static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf)
        case VIRTNET_XMIT_TYPE_XDP:
                xdp_return_frame(buf);
                break;
+
+       case VIRTNET_XMIT_TYPE_XSK:
+               xsk_tx_completed(sq->xsk_pool, 1);
+               break;
        }
 }
 
--
Quoted 2 lines

2.32.0.3.g01195cf9f

Thanks

author reviewer
#30 lore raw

PATCH 11/13 virtio_net: xsk: tx: handle the transmitted xsk buffer

In reply to Xuan Zhuo

virtnet_free_old_xmit distinguishes three type ptr(skb, xdp frame, xsk buffer) by the last bits of the pointer.

---
 drivers/net/virtio_net.c | 85 ++++++++++++++++++++++++++++------------
 1 file changed, 59 insertions(+), 26 deletions(-)
drivers/net/virtio_net.c +59 -26
index 3743694d3c3b..a898568bed5c 100644
@@ -83,6 +83,7 @@ struct virtnet_sq_free_stats {
 	u64 bytes;
 	u64 napi_packets;
 	u64 napi_bytes;
+	u64 xsk;
 };
 
 struct virtnet_sq_stats {
@@ -511,6 +512,7 @@ static struct sk_buff *virtnet_skb_append_frag(struct sk_buff *head_skb,
 					       struct sk_buff *curr_skb,
 					       struct page *page, void *buf,
 					       int len, int truesize);
+static void virtnet_xsk_completed(struct send_queue *sq, int num);
 
 enum virtnet_xmit_type {
 	VIRTNET_XMIT_TYPE_SKB,
@@ -595,12 +597,24 @@ static void __free_old_xmit(struct send_queue *sq, struct netdev_queue *txq,
 
 		case VIRTNET_XMIT_TYPE_XSK:
 			stats->bytes += virtnet_ptr_to_xsk(ptr);
+			stats->xsk++;
 			break;
 		}
 	}
 	netdev_tx_completed_queue(txq, stats->napi_packets, stats->napi_bytes);
 }
 
+static void virtnet_free_old_xmit(struct send_queue *sq,
+				  struct netdev_queue *txq,
+				  bool in_napi,
+				  struct virtnet_sq_free_stats *stats)
+{
+	__free_old_xmit(sq, txq, in_napi, stats);
+
+	if (stats->xsk)
+		virtnet_xsk_completed(sq, stats->xsk);
+}
+
 /* Converting between virtqueue no. and kernel tx/rx queue no.
  * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
  */
@@ -1021,7 +1035,7 @@ static void free_old_xmit(struct send_queue *sq, struct netdev_queue *txq,
 {
 	struct virtnet_sq_free_stats stats = {0};
 
-	__free_old_xmit(sq, txq, in_napi, &stats);
+	virtnet_free_old_xmit(sq, txq, in_napi, &stats);
 
 	/* Avoid overhead when no packets have been processed
 	 * happens when called speculatively from start_xmit.
@@ -1382,29 +1396,6 @@ static int virtnet_add_recvbuf_xsk(struct virtnet_info *vi, struct receive_queue
 	return err;
 }
 
-static int virtnet_xsk_wakeup(struct net_device *dev, u32 qid, u32 flag)
-{
-	struct virtnet_info *vi = netdev_priv(dev);
-	struct send_queue *sq;
-
-	if (!netif_running(dev))
-		return -ENETDOWN;
-
-	if (qid >= vi->curr_queue_pairs)
-		return -EINVAL;
-
-	sq = &vi->sq[qid];
-
-	if (napi_if_scheduled_mark_missed(&sq->napi))
-		return 0;
-
-	local_bh_disable();
-	virtqueue_napi_schedule(&sq->napi, sq->vq);
-	local_bh_enable();
-
-	return 0;
-}
-
 static void *virtnet_xsk_to_ptr(u32 len)
 {
 	unsigned long p;
@@ -1476,8 +1467,12 @@ static bool virtnet_xsk_xmit(struct send_queue *sq, struct xsk_buff_pool *pool,
 	u64 kicks = 0;
 	int sent;
 
+	/* Avoid to wakeup napi meanless, so call __free_old_xmit. */
 	__free_old_xmit(sq, netdev_get_tx_queue(dev, sq - vi->sq), true, &stats);
 
+	if (stats.xsk)
+		xsk_tx_completed(sq->xsk_pool, stats.xsk);
+
 	sent = virtnet_xsk_xmit_batch(sq, pool, budget, &kicks);
 
 	if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq))
@@ -1496,6 +1491,44 @@ static bool virtnet_xsk_xmit(struct send_queue *sq, struct xsk_buff_pool *pool,
 	return sent == budget;
 }
 
+static void xsk_wakeup(struct send_queue *sq)
+{
+	if (napi_if_scheduled_mark_missed(&sq->napi))
+		return;
+
+	local_bh_disable();
+	virtqueue_napi_schedule(&sq->napi, sq->vq);
+	local_bh_enable();
+}
+
+static int virtnet_xsk_wakeup(struct net_device *dev, u32 qid, u32 flag)
+{
+	struct virtnet_info *vi = netdev_priv(dev);
+	struct send_queue *sq;
+
+	if (!netif_running(dev))
+		return -ENETDOWN;
+
+	if (qid >= vi->curr_queue_pairs)
+		return -EINVAL;
+
+	sq = &vi->sq[qid];
+
+	xsk_wakeup(sq);
+	return 0;
+}
+
+static void virtnet_xsk_completed(struct send_queue *sq, int num)
+{
+	xsk_tx_completed(sq->xsk_pool, num);
+
+	/* If this is called by rx poll, start_xmit and xdp xmit we should
+	 * wakeup the tx napi to consume the xsk tx queue, because the tx
+	 * interrupt may not be triggered.
+	 */
+	xsk_wakeup(sq);
+}
+
 static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
 				   struct send_queue *sq,
 				   struct xdp_frame *xdpf)
@@ -1609,8 +1642,8 @@ static int virtnet_xdp_xmit(struct net_device *dev,
 	}
 
 	/* Free up any pending old buffers before queueing new ones. */
-	__free_old_xmit(sq, netdev_get_tx_queue(dev, sq - vi->sq),
-			false, &stats);
+	virtnet_free_old_xmit(sq, netdev_get_tx_queue(dev, sq - vi->sq),
+			      false, &stats);
 
 	for (i = 0; i < n; i++) {
 		struct xdp_frame *xdpf = frames[i];
Signature · 1 line
2.32.0.3.g01195cf9f
#31 lore raw

Re: [PATCH net-next 11/13] virtio_net: xsk: tx: handle the transmitted xsk buffer

Quoted 5 lines · Xuan Zhuo

virtnet_free_old_xmit distinguishes three type ptr(skb, xdp frame, xsk buffer) by the last bits of the pointer.

I suggest squashing this into the previous patch which looks more logical and complete.

Thanks

author reviewer
#32 lore raw

Re: [PATCH net-next 11/13] virtio_net: xsk: tx: handle the transmitted xsk buffer

Quoted 9 lines · Jason Wang
Quoted 5 lines · Xuan Zhuo

virtnet_free_old_xmit distinguishes three type ptr(skb, xdp frame, xsk buffer) by the last bits of the pointer.

I suggest squashing this into the previous patch which looks more logical and complete.

OK.

Thanks.

Quoted 4 lines

Thanks

author reviewer
#33 lore raw

PATCH 12/13 virtio_net: update tx timeout record

In reply to Xuan Zhuo

If send queue sent some packets, we update the tx timeout record to prevent the tx timeout.

---
 drivers/net/virtio_net.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/net/virtio_net.c +7 -0
index a898568bed5c..28c5f9e77fa3 100644
@@ -1478,6 +1478,13 @@ static bool virtnet_xsk_xmit(struct send_queue *sq, struct xsk_buff_pool *pool,
 	if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq))
 		check_sq_full_and_disable(vi, vi->dev, sq);
 
+	if (stats.packets) {
+		struct netdev_queue *txq;
+
+		txq = netdev_get_tx_queue(vi->dev, sq - vi->sq);
+		txq_trans_cond_update(txq);
+	}
+
 	u64_stats_update_begin(&sq->stats.syncp);
 	u64_stats_add(&sq->stats.packets, stats.packets);
 	u64_stats_add(&sq->stats.bytes,   stats.bytes);
Signature · 1 line
2.32.0.3.g01195cf9f
author reviewer
#34 lore raw

PATCH 13/13 virtio_net: xdp_features add NETDEV_XDP_ACT_XSK_ZEROCOPY

In reply to Xuan Zhuo

Now, we supported AF_XDP(xsk). Add NETDEV_XDP_ACT_XSK_ZEROCOPY to xdp_features.

---
 drivers/net/virtio_net.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +2 -1
index 28c5f9e77fa3..8e4578ec1937 100644
@@ -6594,7 +6594,8 @@ static int virtnet_probe(struct virtio_device *vdev)
 		dev->hw_features |= NETIF_F_GRO_HW;
 
 	dev->vlan_features = dev->features;
-	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT;
+	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
+		NETDEV_XDP_ACT_XSK_ZEROCOPY;
 
 	/* MTU range: 68 - 65535 */
 	dev->min_mtu = MIN_MTU;
Signature · 1 line
2.32.0.3.g01195cf9f