Skip to contentSkip to conversation

vhost: fix some async vhost index calculation issues

PATCH Superseded Cheng Jiang 4 participants 16 messages Aug 22, 2022 – Oct 26, 2022

lore
Share & tools
lore
#0 lore raw

cover vhost: fix some async vhost index calculation issues

Fix some async vhost index calculation issues.

Cheng Jiang (2):
vhost: fix descs count in async vhost packed ring
vhost: fix slot index calculation in async vhost

 lib/vhost/virtio_net.c | 40 +++++++++++++++++++++++++++++-----------
 1 file changed, 29 insertions(+), 11 deletions(-)

--
2.35.1

#1 lore raw

PATCH 1/2 vhost: fix descs count in async vhost packed ring

When vhost receive packets from the front-end using packed virtqueue, it might use multiple descriptors for one packet, so we need calculate and record the descriptor number for each packet to update available descriptor counter and used descriptor counter, and rollback when DMA ring is full.

---
 lib/vhost/virtio_net.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
lib/vhost/virtio_net.c +15 -9
index 35fa4670fd..bfc6d65b7c 100644
@@ -3553,14 +3553,15 @@ virtio_dev_tx_async_split_compliant(struct virtio_net *dev,
 }
 
 static __rte_always_inline void
-vhost_async_shadow_dequeue_single_packed(struct vhost_virtqueue *vq, uint16_t buf_id)
+vhost_async_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,
+				uint16_t buf_id, uint16_t count)
 {
 	struct vhost_async *async = vq->async;
 	uint16_t idx = async->buffer_idx_packed;
 
 	async->buffers_packed[idx].id = buf_id;
 	async->buffers_packed[idx].len = 0;
-	async->buffers_packed[idx].count = 1;
+	async->buffers_packed[idx].count = count;
 
 	async->buffer_idx_packed++;
 	if (async->buffer_idx_packed >= vq->size)
@@ -3581,6 +3582,8 @@ virtio_dev_tx_async_single_packed(struct virtio_net *dev,
 	uint16_t nr_vec = 0;
 	uint32_t buf_len;
 	struct buf_vector buf_vec[BUF_VECTOR_MAX];
+	struct vhost_async *async = vq->async;
+	struct async_inflight_info *pkts_info = async->pkts_info;
 	static bool allocerr_warned;
 
 	if (unlikely(fill_vec_buf_packed(dev, vq, vq->last_avail_idx, &desc_count,
@@ -3609,8 +3612,12 @@ virtio_dev_tx_async_single_packed(struct virtio_net *dev,
 		return -1;
 	}
 
+	pkts_info[slot_idx].descs = desc_count;
+
 	/* update async shadow packed ring */
-	vhost_async_shadow_dequeue_single_packed(vq, buf_id);
+	vhost_async_shadow_dequeue_single_packed(vq, buf_id, desc_count);
+
+	vq_inc_last_avail_packed(vq, desc_count);
 
 	return err;
 }
@@ -3649,9 +3656,6 @@ virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
 		}
 
 		pkts_info[slot_idx].mbuf = pkt;
-
-		vq_inc_last_avail_packed(vq, 1);
-
 	}
 
 	n_xfer = vhost_async_dma_transfer(dev, vq, dma_id, vchan_id, async->pkts_idx,
@@ -3662,6 +3666,8 @@ virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
 	pkt_err = pkt_idx - n_xfer;
 
 	if (unlikely(pkt_err)) {
+		uint16_t descs_err = 0;
+
 		pkt_idx -= pkt_err;
 
 		/**
@@ -3678,10 +3684,10 @@ virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
 		}
 
 		/* recover available ring */
-		if (vq->last_avail_idx >= pkt_err) {
-			vq->last_avail_idx -= pkt_err;
+		if (vq->last_avail_idx >= descs_err) {
+			vq->last_avail_idx -= descs_err;
 		} else {
-			vq->last_avail_idx += vq->size - pkt_err;
+			vq->last_avail_idx += vq->size - descs_err;
 			vq->avail_wrap_counter ^= 1;
 		}
 	}
Signature · 1 line
2.35.1
#2 lore raw

Re: [PATH 1/2] vhost: fix descs count in async vhost packed ring

Quoted 1 line · Cheng Jiang

When vhost receive packets from the front-end using packed virtqueue, it

receives*

Quoted 1 line

might use multiple descriptors for one packet, so we need calculate and

so we need to*

Quoted 3 lines

record the descriptor number for each packet to update available descriptor counter and used descriptor counter, and rollback when DMA ring is full.

This is a fix, so the Fixes tag should be present, and [email protected] cc'ed.

Quoted 73 lines
---
  lib/vhost/virtio_net.c | 24 +++++++++++++++---------
  1 file changed, 15 insertions(+), 9 deletions(-)
lib/vhost/virtio_net.c +12 -6
index 35fa4670fd..bfc6d65b7c 100644
@@ -3553,14 +3553,15 @@ virtio_dev_tx_async_split_compliant(struct virtio_net *dev,
  }
 
  static __rte_always_inline void
-vhost_async_shadow_dequeue_single_packed(struct vhost_virtqueue *vq, uint16_t buf_id)
+vhost_async_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,
+				uint16_t buf_id, uint16_t count)
  {
  	struct vhost_async *async = vq->async;
  	uint16_t idx = async->buffer_idx_packed;
 
  	async->buffers_packed[idx].id = buf_id;
  	async->buffers_packed[idx].len = 0;
-	async->buffers_packed[idx].count = 1;
+	async->buffers_packed[idx].count = count;
 
  	async->buffer_idx_packed++;
  	if (async->buffer_idx_packed >= vq->size)
@@ -3581,6 +3582,8 @@ virtio_dev_tx_async_single_packed(struct virtio_net *dev,
  	uint16_t nr_vec = 0;
  	uint32_t buf_len;
  	struct buf_vector buf_vec[BUF_VECTOR_MAX];
+	struct vhost_async *async = vq->async;
+	struct async_inflight_info *pkts_info = async->pkts_info;
  	static bool allocerr_warned;
 
  	if (unlikely(fill_vec_buf_packed(dev, vq, vq->last_avail_idx, &desc_count,
@@ -3609,8 +3612,12 @@ virtio_dev_tx_async_single_packed(struct virtio_net *dev,
  		return -1;
  	}
 
+	pkts_info[slot_idx].descs = desc_count;
+
  	/* update async shadow packed ring */
-	vhost_async_shadow_dequeue_single_packed(vq, buf_id);
+	vhost_async_shadow_dequeue_single_packed(vq, buf_id, desc_count);
+
+	vq_inc_last_avail_packed(vq, desc_count);
 
  	return err;
  }
@@ -3649,9 +3656,6 @@ virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
  		}
 
  		pkts_info[slot_idx].mbuf = pkt;
-
-		vq_inc_last_avail_packed(vq, 1);
-
  	}
 
  	n_xfer = vhost_async_dma_transfer(dev, vq, dma_id, vchan_id, async->pkts_idx,
@@ -3662,6 +3666,8 @@ virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
  	pkt_err = pkt_idx - n_xfer;
 
  	if (unlikely(pkt_err)) {
+		uint16_t descs_err = 0;
+
  		pkt_idx -= pkt_err;
 
  		/**
@@ -3678,10 +3684,10 @@ virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
  		}
 
  		/* recover available ring */

lib/vhost/virtio_net.c virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 3693

Quoted 10 lines
lib/vhost/virtio_net.c +3 -3
-		if (vq->last_avail_idx >= pkt_err) {
-			vq->last_avail_idx -= pkt_err;
+		if (vq->last_avail_idx >= descs_err) {
+			vq->last_avail_idx -= descs_err;
  		} else {
-			vq->last_avail_idx += vq->size - pkt_err;
+			vq->last_avail_idx += vq->size - descs_err;
  			vq->avail_wrap_counter ^= 1;
  		}
  	}

I'm not sure to understand, isn't descs_err always 0 here?

Maxime

#3 lore raw

PATCH 2/2 vhost: fix slot index calculation in async vhost

In reply to Cheng Jiang

When the packet receiving failure and the DMA ring full occur simultaneously in the asynchronous vhost, the slot_idx needs to be reduced by 1. For packed virtqueue, the slot index should be ring_size - 1, if the slot_idx is currently 0, since the ring size is not necessarily the power of 2.

---
 lib/vhost/virtio_net.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
lib/vhost/virtio_net.c +14 -2
index bfc6d65b7c..f804bce0bd 100644
@@ -3462,6 +3462,7 @@ virtio_dev_tx_async_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
 				allocerr_warned = true;
 			}
 			dropped = true;
+			slot_idx--;
 			break;
 		}
 
@@ -3652,6 +3653,12 @@ virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
 		if (unlikely(virtio_dev_tx_async_single_packed(dev, vq, mbuf_pool, pkt,
 				slot_idx, legacy_ol_flags))) {
 			rte_pktmbuf_free_bulk(&pkts_prealloc[pkt_idx], count - pkt_idx);
+
+			if (slot_idx == 0)
+				slot_idx = vq->size - 1;
+			else
+				slot_idx--;
+
 			break;
 		}
 
@@ -3679,8 +3686,13 @@ virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
 			async->buffer_idx_packed += vq->size - pkt_err;
 
 		while (pkt_err-- > 0) {
-			rte_pktmbuf_free(pkts_info[slot_idx % vq->size].mbuf);
-			slot_idx--;
+			rte_pktmbuf_free(pkts_info[slot_idx].mbuf);
+			descs_err += pkts_info[slot_idx].descs;
+
+			if (slot_idx == 0)
+				slot_idx = vq->size - 1;
+			else
+				slot_idx--;
 		}
 
 		/* recover available ring */
Signature · 1 line
2.35.1
#4 lore raw

Re: [PATH 2/2] vhost: fix slot index calculation in async vhost

Quoted 3 lines · Cheng Jiang

When the packet receiving failure and the DMA ring full occur simultaneously in the asynchronous vhost, the slot_idx needs to be reduced by 1. For packed virtqueue, the slot index should be

s/reduced/decreased/

Quoted 2 lines

ring_size - 1, if the slot_idx is currently 0, since the ring size is not necessarily the power of 2.

This is a fix, so fixes tag and [email protected] is required.

Quoted 46 lines
---
  lib/vhost/virtio_net.c | 16 ++++++++++++++--
  1 file changed, 14 insertions(+), 2 deletions(-)
lib/vhost/virtio_net.c +14 -2
index bfc6d65b7c..f804bce0bd 100644
@@ -3462,6 +3462,7 @@ virtio_dev_tx_async_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
  				allocerr_warned = true;
  			}
  			dropped = true;
+			slot_idx--;
  			break;
  		}
 
@@ -3652,6 +3653,12 @@ virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
  		if (unlikely(virtio_dev_tx_async_single_packed(dev, vq, mbuf_pool, pkt,
  				slot_idx, legacy_ol_flags))) {
  			rte_pktmbuf_free_bulk(&pkts_prealloc[pkt_idx], count - pkt_idx);
+
+			if (slot_idx == 0)
+				slot_idx = vq->size - 1;
+			else
+				slot_idx--;
+
  			break;
  		}
 
@@ -3679,8 +3686,13 @@ virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
  			async->buffer_idx_packed += vq->size - pkt_err;
 
  		while (pkt_err-- > 0) {
-			rte_pktmbuf_free(pkts_info[slot_idx % vq->size].mbuf);
-			slot_idx--;
+			rte_pktmbuf_free(pkts_info[slot_idx].mbuf);
+			descs_err += pkts_info[slot_idx].descs;
+
+			if (slot_idx == 0)
+				slot_idx = vq->size - 1;
+			else
+				slot_idx--;
  		}
 
  		/* recover available ring */
#5 lore raw

RE: [PATH 2/2] vhost: fix slot index calculation in async vhost

Hi,

Quoted 13 lines

-----Original Message-----
From: Maxime Coquelin <[email protected]>
Sent: Monday, October 3, 2022 6:10 PM
To: Jiang, Cheng1 <[email protected]>; Xia, Chenbo <[email protected]>
Cc: [email protected]; Hu, Jiayu <[email protected]>; Ding, Xuan <[email protected]>; Ma, WenwuX <[email protected]>; Wang, YuanX <[email protected]>; Yang, YvonneX <[email protected]>; He, Xingguang <[email protected]>
Subject: Re: [PATH 2/2] vhost: fix slot index calculation in async vhost

Quoted 6 lines
Quoted 3 lines · Cheng Jiang

When the packet receiving failure and the DMA ring full occur simultaneously in the asynchronous vhost, the slot_idx needs to be reduced by 1. For packed virtqueue, the slot index should be

s/reduced/decreased/

Sure, thanks.

Quoted 5 lines
Quoted 3 lines

ring_size - 1, if the slot_idx is currently 0, since the ring size is not necessarily the power of 2.

This is a fix, so fixes tag and [email protected] is required.

Sorry for the miss. It will be fixed in the next version.

Thanks,
Cheng

Quoted 53 lines

lib/vhost/virtio_net.c virtio_dev_tx_async_split(struct virtio_net *dev,

Quoted 11 lines
---
  lib/vhost/virtio_net.c | 16 ++++++++++++++--
  1 file changed, 14 insertions(+), 2 deletions(-)
lib/vhost/virtio_net.c index renamed from lib/vhost/virtio_net.c +0 -0

bfc6d65b7c..f804bce0bd 100644

lib/vhost/virtio_net.c +0 -0
@@ -3462,6 +3462,7 @@ virtio_dev_tx_async_split(struct virtio_net *dev,

struct vhost_virtqueue *vq,

lib/vhost/virtio_net.c virtio_dev_tx_async_packed(struct virtio_net

Quoted 8 lines
lib/vhost/virtio_net.c +1 -0
  				allocerr_warned = true;
  			}
  			dropped = true;
+			slot_idx--;
  			break;
  		}
 
@@ -3652,6 +3653,12 @@ virtio_dev_tx_async_packed(struct virtio_net

*dev, struct vhost_virtqueue *vq,

lib/vhost/virtio_net.c virtio_dev_tx_async_packed(struct virtio_net · line 3653

Quoted 1 line
lib/vhost/virtio_net.c +0 -0
  		if (unlikely(virtio_dev_tx_async_single_packed(dev, vq,

mbuf_pool, pkt,

lib/vhost/virtio_net.c virtio_dev_tx_async_packed(struct virtio_net · line 3655

Quoted 2 lines
lib/vhost/virtio_net.c +0 -0
  				slot_idx, legacy_ol_flags))) {
  			rte_pktmbuf_free_bulk(&pkts_prealloc[pkt_idx],

count - pkt_idx);

lib/vhost/virtio_net.c virtio_dev_tx_async_packed(struct virtio_net

Quoted 10 lines
lib/vhost/virtio_net.c +6 -0
+
+			if (slot_idx == 0)
+				slot_idx = vq->size - 1;
+			else
+				slot_idx--;
+
  			break;
  		}
 
@@ -3679,8 +3686,13 @@ virtio_dev_tx_async_packed(struct virtio_net

*dev, struct vhost_virtqueue *vq,

Quoted 16 lines
lib/vhost/virtio_net.c +7 -2
  			async->buffer_idx_packed += vq->size - pkt_err;
 
  		while (pkt_err-- > 0) {
-			rte_pktmbuf_free(pkts_info[slot_idx % vq-
 size].mbuf);
-			slot_idx--;
+			rte_pktmbuf_free(pkts_info[slot_idx].mbuf);
+			descs_err += pkts_info[slot_idx].descs;
+
+			if (slot_idx == 0)
+				slot_idx = vq->size - 1;
+			else
+				slot_idx--;
  		}
 

/* recover available ring */

#6 lore raw

v2 cover vhost: fix some async vhost index calculation issues

In reply to Cheng Jiang

Fix some async vhost index calculation issues.

v2: fixed fixes tag and replaced some words in commit message.

Cheng Jiang (2):
vhost: fix descs count in async vhost packed ring
vhost: fix slot index calculation in async vhost

 lib/vhost/virtio_net.c | 40 +++++++++++++++++++++++++++++-----------
 1 file changed, 29 insertions(+), 11 deletions(-)

--
2.35.1

#7 lore raw

PATCH v2 1/2 vhost: fix descs count in async vhost packed ring

When vhost receive packets from the front-end using packed virtqueue, it might use multiple descriptors for one packet, so we need calculate and record the descriptor number for each packet to update available descriptor counter and used descriptor counter, and rollback when DMA ring is full.

Fixes fe8477ebbd94 ("vhost: support async packed ring dequeue")
---
 lib/vhost/virtio_net.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
lib/vhost/virtio_net.c +15 -9
index 8f4d0f0502..457ac2e92a 100644
@@ -3548,14 +3548,15 @@ virtio_dev_tx_async_split_compliant(struct virtio_net *dev,
 }
 
 static __rte_always_inline void
-vhost_async_shadow_dequeue_single_packed(struct vhost_virtqueue *vq, uint16_t buf_id)
+vhost_async_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,
+				uint16_t buf_id, uint16_t count)
 {
 	struct vhost_async *async = vq->async;
 	uint16_t idx = async->buffer_idx_packed;
 
 	async->buffers_packed[idx].id = buf_id;
 	async->buffers_packed[idx].len = 0;
-	async->buffers_packed[idx].count = 1;
+	async->buffers_packed[idx].count = count;
 
 	async->buffer_idx_packed++;
 	if (async->buffer_idx_packed >= vq->size)
@@ -3576,6 +3577,8 @@ virtio_dev_tx_async_single_packed(struct virtio_net *dev,
 	uint16_t nr_vec = 0;
 	uint32_t buf_len;
 	struct buf_vector buf_vec[BUF_VECTOR_MAX];
+	struct vhost_async *async = vq->async;
+	struct async_inflight_info *pkts_info = async->pkts_info;
 	static bool allocerr_warned;
 
 	if (unlikely(fill_vec_buf_packed(dev, vq, vq->last_avail_idx, &desc_count,
@@ -3604,8 +3607,12 @@ virtio_dev_tx_async_single_packed(struct virtio_net *dev,
 		return -1;
 	}
 
+	pkts_info[slot_idx].descs = desc_count;
+
 	/* update async shadow packed ring */
-	vhost_async_shadow_dequeue_single_packed(vq, buf_id);
+	vhost_async_shadow_dequeue_single_packed(vq, buf_id, desc_count);
+
+	vq_inc_last_avail_packed(vq, desc_count);
 
 	return err;
 }
@@ -3644,9 +3651,6 @@ virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
 		}
 
 		pkts_info[slot_idx].mbuf = pkt;
-
-		vq_inc_last_avail_packed(vq, 1);
-
 	}
 
 	n_xfer = vhost_async_dma_transfer(dev, vq, dma_id, vchan_id, async->pkts_idx,
@@ -3657,6 +3661,8 @@ virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
 	pkt_err = pkt_idx - n_xfer;
 
 	if (unlikely(pkt_err)) {
+		uint16_t descs_err = 0;
+
 		pkt_idx -= pkt_err;
 
 		/**
@@ -3673,10 +3679,10 @@ virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
 		}
 
 		/* recover available ring */
-		if (vq->last_avail_idx >= pkt_err) {
-			vq->last_avail_idx -= pkt_err;
+		if (vq->last_avail_idx >= descs_err) {
+			vq->last_avail_idx -= descs_err;
 		} else {
-			vq->last_avail_idx += vq->size - pkt_err;
+			vq->last_avail_idx += vq->size - descs_err;
 			vq->avail_wrap_counter ^= 1;
 		}
 	}
Signature · 1 line
2.35.1
Reviewed-by on v2 1/2
#8 lore raw

Re: [PATCH v2 1/2] vhost: fix descs count in async vhost packed ring

Quoted 1 line · Cheng Jiang

When vhost receive packets from the front-end using packed virtqueue, it

receives

Quoted 1 line

might use multiple descriptors for one packet, so we need calculate and

to calculate

Quoted 80 lines

record the descriptor number for each packet to update available descriptor counter and used descriptor counter, and rollback when DMA ring is full.

Fixes fe8477ebbd94 ("vhost: support async packed ring dequeue")
---
  lib/vhost/virtio_net.c | 24 +++++++++++++++---------
  1 file changed, 15 insertions(+), 9 deletions(-)
lib/vhost/virtio_net.c +12 -6
index 8f4d0f0502..457ac2e92a 100644
@@ -3548,14 +3548,15 @@ virtio_dev_tx_async_split_compliant(struct virtio_net *dev,
  }
 
  static __rte_always_inline void
-vhost_async_shadow_dequeue_single_packed(struct vhost_virtqueue *vq, uint16_t buf_id)
+vhost_async_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,
+				uint16_t buf_id, uint16_t count)
  {
  	struct vhost_async *async = vq->async;
  	uint16_t idx = async->buffer_idx_packed;
 
  	async->buffers_packed[idx].id = buf_id;
  	async->buffers_packed[idx].len = 0;
-	async->buffers_packed[idx].count = 1;
+	async->buffers_packed[idx].count = count;
 
  	async->buffer_idx_packed++;
  	if (async->buffer_idx_packed >= vq->size)
@@ -3576,6 +3577,8 @@ virtio_dev_tx_async_single_packed(struct virtio_net *dev,
  	uint16_t nr_vec = 0;
  	uint32_t buf_len;
  	struct buf_vector buf_vec[BUF_VECTOR_MAX];
+	struct vhost_async *async = vq->async;
+	struct async_inflight_info *pkts_info = async->pkts_info;
  	static bool allocerr_warned;
 
  	if (unlikely(fill_vec_buf_packed(dev, vq, vq->last_avail_idx, &desc_count,
@@ -3604,8 +3607,12 @@ virtio_dev_tx_async_single_packed(struct virtio_net *dev,
  		return -1;
  	}
 
+	pkts_info[slot_idx].descs = desc_count;
+
  	/* update async shadow packed ring */
-	vhost_async_shadow_dequeue_single_packed(vq, buf_id);
+	vhost_async_shadow_dequeue_single_packed(vq, buf_id, desc_count);
+
+	vq_inc_last_avail_packed(vq, desc_count);
 
  	return err;
  }
@@ -3644,9 +3651,6 @@ virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
  		}
 
  		pkts_info[slot_idx].mbuf = pkt;
-
-		vq_inc_last_avail_packed(vq, 1);
-
  	}
 
  	n_xfer = vhost_async_dma_transfer(dev, vq, dma_id, vchan_id, async->pkts_idx,
@@ -3657,6 +3661,8 @@ virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
  	pkt_err = pkt_idx - n_xfer;
 
  	if (unlikely(pkt_err)) {
+		uint16_t descs_err = 0;
+
  		pkt_idx -= pkt_err;
 
  		/**
@@ -3673,10 +3679,10 @@ virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
  		}
 
  		/* recover available ring */

lib/vhost/virtio_net.c virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 3688

Quoted 10 lines
lib/vhost/virtio_net.c +3 -3
-		if (vq->last_avail_idx >= pkt_err) {
-			vq->last_avail_idx -= pkt_err;
+		if (vq->last_avail_idx >= descs_err) {
+			vq->last_avail_idx -= descs_err;
  		} else {
-			vq->last_avail_idx += vq->size - pkt_err;
+			vq->last_avail_idx += vq->size - descs_err;
  			vq->avail_wrap_counter ^= 1;
  		}
  	}

If only the commit message typos need to be fixed, maybe no need to send a new version.

Thanks,
Maxime

#9 lore raw

RE: [PATCH v2 1/2] vhost: fix descs count in async vhost packed ring

Hi Maxime,

Quoted 114 lines

-----Original Message-----
From: Maxime Coquelin <[email protected]>
Sent: Friday, October 21, 2022 4:16 PM
To: Jiang, Cheng1 <[email protected]>; Xia, Chenbo <[email protected]>
Cc: [email protected]; Hu, Jiayu <[email protected]>; Ding, Xuan <[email protected]>; Ma, WenwuX <[email protected]>; Wang, YuanX <[email protected]>; Yang, YvonneX <[email protected]>; He, Xingguang <[email protected]>; [email protected]
Subject: Re: [PATCH v2 1/2] vhost: fix descs count in async vhost packed ring

Quoted 2 lines · Cheng Jiang

When vhost receive packets from the front-end using packed virtqueue, it

receives

Quoted 2 lines

might use multiple descriptors for one packet, so we need calculate and

to calculate

lib/vhost/virtio_net.c virtio_dev_tx_async_split_compliant(struct

Quoted 17 lines

record the descriptor number for each packet to update available descriptor counter and used descriptor counter, and rollback when DMA ring is full.

Fixes fe8477ebbd94 ("vhost: support async packed ring dequeue")
---
  lib/vhost/virtio_net.c | 24 +++++++++++++++---------
  1 file changed, 15 insertions(+), 9 deletions(-)
lib/vhost/virtio_net.c index renamed from lib/vhost/virtio_net.c +0 -0

8f4d0f0502..457ac2e92a 100644

lib/vhost/virtio_net.c +0 -0
@@ -3548,14 +3548,15 @@ virtio_dev_tx_async_split_compliant(struct

virtio_net *dev,

lib/vhost/virtio_net.c virtio_dev_tx_async_split_compliant(struct virtio_net *dev,

Quoted 4 lines
lib/vhost/virtio_net.c +0 -1
  }
 
  static __rte_always_inline void
-vhost_async_shadow_dequeue_single_packed(struct vhost_virtqueue

*vq,

lib/vhost/virtio_net.c virtio_dev_tx_async_split_compliant(struct · line 3552

Quoted 2 lines
lib/vhost/virtio_net.c +1 -0
 uint16_t buf_id)
+vhost_async_shadow_dequeue_single_packed(struct vhost_virtqueue

*vq,

Quoted 21 lines
lib/vhost/virtio_net.c +2 -1
+				uint16_t buf_id, uint16_t count)
  {
  	struct vhost_async *async = vq->async;
  	uint16_t idx = async->buffer_idx_packed;
 
  	async->buffers_packed[idx].id = buf_id;
  	async->buffers_packed[idx].len = 0;
-	async->buffers_packed[idx].count = 1;
+	async->buffers_packed[idx].count = count;
 
  	async->buffer_idx_packed++;

if (async->buffer_idx_packed >= vq->size) @@ -3576,6 +3577,8 @@
virtio_dev_tx_async_single_packed(struct virtio_net *dev,
uint16_t nr_vec = 0;
uint32_t buf_len;
struct buf_vector buf_vec[BUF_VECTOR_MAX];

lib/vhost/virtio_net.c +2 -0
+	struct vhost_async *async = vq->async;
+	struct async_inflight_info *pkts_info = async->pkts_info;
  	static bool allocerr_warned;
 
  	if (unlikely(fill_vec_buf_packed(dev, vq, vq->last_avail_idx,
Quoted 1 line

&desc_count, @@ -3604,8 +3607,12 @@

virtio_dev_tx_async_single_packed(struct virtio_net *dev,

lib/vhost/virtio_net.c virtio_dev_tx_async_single_packed(struct virtio_net *dev,

Quoted 8 lines
  		return -1;
  	}
lib/vhost/virtio_net.c +3 -1
+	pkts_info[slot_idx].descs = desc_count;
+
  	/* update async shadow packed ring */
-	vhost_async_shadow_dequeue_single_packed(vq, buf_id);
+	vhost_async_shadow_dequeue_single_packed(vq, buf_id,

desc_count);

lib/vhost/virtio_net.c virtio_dev_tx_async_packed(struct virtio_net

Quoted 6 lines
lib/vhost/virtio_net.c +2 -0
+
+	vq_inc_last_avail_packed(vq, desc_count);
 
  	return err;
  }
@@ -3644,9 +3651,6 @@ virtio_dev_tx_async_packed(struct virtio_net

*dev, struct vhost_virtqueue *vq,

Quoted 10 lines
lib/vhost/virtio_net.c +0 -3
  		}
 
  		pkts_info[slot_idx].mbuf = pkt;
-
-		vq_inc_last_avail_packed(vq, 1);
-
  	}
 
  	n_xfer = vhost_async_dma_transfer(dev, vq, dma_id, vchan_id,

async->pkts_idx, @@ -3657,6 +3661,8 @@

virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,

lib/vhost/virtio_net.c virtio_dev_tx_async_packed(struct virtio_net

Quoted 9 lines
  	pkt_err = pkt_idx - n_xfer;

  	if (unlikely(pkt_err)) {
lib/vhost/virtio_net.c +2 -0
+		uint16_t descs_err = 0;
+
  		pkt_idx -= pkt_err;
 
  		/**
@@ -3673,10 +3679,10 @@ virtio_dev_tx_async_packed(struct virtio_net

*dev, struct vhost_virtqueue *vq,

Quoted 2 lines
lib/vhost/virtio_net.c +0 -0
  		}
 
Quoted 14 lines

lib/vhost/virtio_net.c virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 3688

Quoted 11 lines
lib/vhost/virtio_net.c +3 -3
  		/* recover available ring */
-		if (vq->last_avail_idx >= pkt_err) {
-			vq->last_avail_idx -= pkt_err;
+		if (vq->last_avail_idx >= descs_err) {
+			vq->last_avail_idx -= descs_err;
  		} else {
-			vq->last_avail_idx += vq->size - pkt_err;
+			vq->last_avail_idx += vq->size - descs_err;
  			vq->avail_wrap_counter ^= 1;
  		}
  	}

If only the commit message typos need to be fixed, maybe no need to send a new version.

Sure, thanks a lot!
Cheng

Quoted 5 lines

Thanks,
Maxime

Reviewed-by
#10 lore raw

RE: [PATCH v2 1/2] vhost: fix descs count in async vhost packed ring

Quoted 114 lines

-----Original Message-----
From: Jiang, Cheng1 <[email protected]>
Sent: Monday, October 24, 2022 9:42 AM
To: Maxime Coquelin <[email protected]>; Xia, Chenbo <[email protected]>
Cc: [email protected]; Hu, Jiayu <[email protected]>; Ding, Xuan <[email protected]>; Ma, WenwuX <[email protected]>; Wang, YuanX <[email protected]>; Yang, YvonneX <[email protected]>; He, Xingguang <[email protected]>; [email protected]
Subject: RE: [PATCH v2 1/2] vhost: fix descs count in async vhost packed ring

Hi Maxime,

Quoted 11 lines

-----Original Message-----
From: Maxime Coquelin <[email protected]>
Sent: Friday, October 21, 2022 4:16 PM
To: Jiang, Cheng1 <[email protected]>; Xia, Chenbo <[email protected]>
Cc: [email protected]; Hu, Jiayu <[email protected]>; Ding, Xuan <[email protected]>; Ma, WenwuX <[email protected]>; Wang, YuanX <[email protected]>; Yang, YvonneX <[email protected]>; He, Xingguang <[email protected]>; [email protected]
Subject: Re: [PATCH v2 1/2] vhost: fix descs count in async vhost packed

ring

Quoted 45 lines · Cheng Jiang
Quoted 2 lines

When vhost receive packets from the front-end using packed virtqueue, it

receives

Quoted 2 lines

might use multiple descriptors for one packet, so we need calculate and

to calculate

lib/vhost/virtio_net.c virtio_dev_tx_async_split_compliant(struct

Quoted 17 lines

record the descriptor number for each packet to update available descriptor counter and used descriptor counter, and rollback when DMA ring is full.

Fixes fe8477ebbd94 ("vhost: support async packed ring dequeue")
---
  lib/vhost/virtio_net.c | 24 +++++++++++++++---------
  1 file changed, 15 insertions(+), 9 deletions(-)
lib/vhost/virtio_net.c index renamed from lib/vhost/virtio_net.c +0 -0

8f4d0f0502..457ac2e92a 100644

lib/vhost/virtio_net.c +0 -0
@@ -3548,14 +3548,15 @@ virtio_dev_tx_async_split_compliant(struct

virtio_net *dev,

lib/vhost/virtio_net.c virtio_dev_tx_async_split_compliant(struct virtio_net *dev,

Quoted 4 lines
lib/vhost/virtio_net.c +0 -1
  }
 
  static __rte_always_inline void
-vhost_async_shadow_dequeue_single_packed(struct vhost_virtqueue

*vq,

lib/vhost/virtio_net.c virtio_dev_tx_async_split_compliant(struct · line 3552

Quoted 2 lines
lib/vhost/virtio_net.c +1 -0
 uint16_t buf_id)
+vhost_async_shadow_dequeue_single_packed(struct vhost_virtqueue

*vq,

Quoted 5 lines
lib/vhost/virtio_net.c +1 -0
+				uint16_t buf_id, uint16_t count)
  {
  	struct vhost_async *async = vq->async;
  	uint16_t idx = async->buffer_idx_packed;
 
Quoted 7 lines
Quoted 7 lines
lib/vhost/virtio_net.c +1 -1
  	async->buffers_packed[idx].id = buf_id;
  	async->buffers_packed[idx].len = 0;
-	async->buffers_packed[idx].count = 1;
+	async->buffers_packed[idx].count = count;
 
  	async->buffer_idx_packed++;

if (async->buffer_idx_packed >= vq->size) @@ -3576,6 +3577,8

@@

Quoted 35 lines
Quoted 10 lines

virtio_dev_tx_async_single_packed(struct virtio_net *dev,
uint16_t nr_vec = 0;
uint32_t buf_len;
struct buf_vector buf_vec[BUF_VECTOR_MAX];

lib/vhost/virtio_net.c +2 -0
+	struct vhost_async *async = vq->async;
+	struct async_inflight_info *pkts_info = async->pkts_info;
  	static bool allocerr_warned;
 
  	if (unlikely(fill_vec_buf_packed(dev, vq, vq->last_avail_idx,

&desc_count, @@ -3604,8 +3607,12 @@

virtio_dev_tx_async_single_packed(struct virtio_net *dev,

lib/vhost/virtio_net.c virtio_dev_tx_async_single_packed(struct virtio_net *dev,

Quoted 8 lines
  		return -1;
  	}
lib/vhost/virtio_net.c +3 -1
+	pkts_info[slot_idx].descs = desc_count;
+
  	/* update async shadow packed ring */
-	vhost_async_shadow_dequeue_single_packed(vq, buf_id);
+	vhost_async_shadow_dequeue_single_packed(vq, buf_id,

desc_count);

lib/vhost/virtio_net.c virtio_dev_tx_async_packed(struct virtio_net

Quoted 6 lines
lib/vhost/virtio_net.c +2 -0
+
+	vq_inc_last_avail_packed(vq, desc_count);
 
  	return err;
  }
@@ -3644,9 +3651,6 @@ virtio_dev_tx_async_packed(struct virtio_net

*dev, struct vhost_virtqueue *vq,

Quoted 8 lines
lib/vhost/virtio_net.c +0 -3
  		}
 
  		pkts_info[slot_idx].mbuf = pkt;
-
-		vq_inc_last_avail_packed(vq, 1);
-
  	}
 
Quoted 31 lines
Quoted 3 lines
Quoted 2 lines
lib/vhost/virtio_net.c +0 -0
  	n_xfer = vhost_async_dma_transfer(dev, vq, dma_id, vchan_id,

async->pkts_idx, @@ -3657,6 +3661,8 @@

virtio_dev_tx_async_packed(struct virtio_net *dev, struct

vhost_virtqueue

Quoted 27 lines

*vq,

lib/vhost/virtio_net.c virtio_dev_tx_async_packed(struct virtio_net

Quoted 9 lines
  	pkt_err = pkt_idx - n_xfer;

  	if (unlikely(pkt_err)) {
lib/vhost/virtio_net.c +2 -0
+		uint16_t descs_err = 0;
+
  		pkt_idx -= pkt_err;
 
  		/**
@@ -3673,10 +3679,10 @@ virtio_dev_tx_async_packed(struct virtio_net

*dev, struct vhost_virtqueue *vq,

lib/vhost/virtio_net.c virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 3688

Quoted 13 lines
lib/vhost/virtio_net.c +3 -3
  		}
 
  		/* recover available ring */
-		if (vq->last_avail_idx >= pkt_err) {
-			vq->last_avail_idx -= pkt_err;
+		if (vq->last_avail_idx >= descs_err) {
+			vq->last_avail_idx -= descs_err;
  		} else {
-			vq->last_avail_idx += vq->size - pkt_err;
+			vq->last_avail_idx += vq->size - descs_err;
  			vq->avail_wrap_counter ^= 1;
  		}
  	}

If only the commit message typos need to be fixed, maybe no need to send a new version.

Will fix above typos when applying

Thanks,
Chenbo

Quoted 9 lines

Sure, thanks a lot!
Cheng

Quoted 5 lines

Thanks,
Maxime

#11 lore raw

PATCH v2 2/2 vhost: fix slot index calculation in async vhost

In reply to Cheng Jiang

When the packet receiving failure and the DMA ring full occur simultaneously in the asynchronous vhost, the slot_idx needs to be decreased by 1. For packed virtqueue, the slot index should be ring_size - 1, if the slot_idx is currently 0, since the ring size is not necessarily the power of 2.

Fixes 84d5204310d7 ("vhost: support async dequeue for split ring")
Fixes fe8477ebbd94 ("vhost: support async packed ring dequeue")
---
 lib/vhost/virtio_net.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
lib/vhost/virtio_net.c +14 -2
index 457ac2e92a..efebd063d7 100644
@@ -3457,6 +3457,7 @@ virtio_dev_tx_async_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
 				allocerr_warned = true;
 			}
 			dropped = true;
+			slot_idx--;
 			break;
 		}
 
@@ -3647,6 +3648,12 @@ virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
 		if (unlikely(virtio_dev_tx_async_single_packed(dev, vq, mbuf_pool, pkt,
 				slot_idx, legacy_ol_flags))) {
 			rte_pktmbuf_free_bulk(&pkts_prealloc[pkt_idx], count - pkt_idx);
+
+			if (slot_idx == 0)
+				slot_idx = vq->size - 1;
+			else
+				slot_idx--;
+
 			break;
 		}
 
@@ -3674,8 +3681,13 @@ virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
 			async->buffer_idx_packed += vq->size - pkt_err;
 
 		while (pkt_err-- > 0) {
-			rte_pktmbuf_free(pkts_info[slot_idx % vq->size].mbuf);
-			slot_idx--;
+			rte_pktmbuf_free(pkts_info[slot_idx].mbuf);
+			descs_err += pkts_info[slot_idx].descs;
+
+			if (slot_idx == 0)
+				slot_idx = vq->size - 1;
+			else
+				slot_idx--;
 		}
 
 		/* recover available ring */
Signature · 1 line
2.35.1
Tested-by on v2 2/2
#12 lore raw

RE: [PATCH v2 2/2] vhost: fix slot index calculation in async vhost

Quoted 16 lines

-----Original Message-----
From: Cheng Jiang <[email protected]>
Sent: Tuesday, October 11, 2022 11:08 AM
To: [email protected]; Xia, Chenbo <[email protected]>
Cc: [email protected]; Hu, Jiayu <[email protected]>; Ding, Xuan <[email protected]>; Ma, WenwuX <[email protected]>; Wang, YuanX <[email protected]>; Yang, YvonneX <[email protected]>; He, Xingguang <[email protected]>; Jiang, Cheng1 <[email protected]>; [email protected]
Subject: [PATCH v2 2/2] vhost: fix slot index calculation in async vhost

When the packet receiving failure and the DMA ring full occur simultaneously in the asynchronous vhost, the slot_idx needs to be decreased by 1. For packed virtqueue, the slot index should be ring_size - 1, if the slot_idx is currently 0, since the ring size is not necessarily the power of 2.

Quoted 6 lines
Fixes 84d5204310d7 ("vhost: support async dequeue for split ring")
Fixes fe8477ebbd94 ("vhost: support async packed ring dequeue")

---

Reviewed-by on v2 2/2
#13 lore raw

Re: [PATCH v2 2/2] vhost: fix slot index calculation in async vhost

In reply to Cheng Jiang
Quoted 46 lines · Cheng Jiang

When the packet receiving failure and the DMA ring full occur simultaneously in the asynchronous vhost, the slot_idx needs to be decreased by 1. For packed virtqueue, the slot index should be ring_size - 1, if the slot_idx is currently 0, since the ring size is not necessarily the power of 2.

Fixes 84d5204310d7 ("vhost: support async dequeue for split ring")
Fixes fe8477ebbd94 ("vhost: support async packed ring dequeue")
---
  lib/vhost/virtio_net.c | 16 ++++++++++++++--
  1 file changed, 14 insertions(+), 2 deletions(-)
lib/vhost/virtio_net.c +7 -2
index 457ac2e92a..efebd063d7 100644
@@ -3457,6 +3457,7 @@ virtio_dev_tx_async_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
  				allocerr_warned = true;
  			}
  			dropped = true;
+			slot_idx--;
  			break;
  		}
 
@@ -3647,6 +3648,12 @@ virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
  		if (unlikely(virtio_dev_tx_async_single_packed(dev, vq, mbuf_pool, pkt,
  				slot_idx, legacy_ol_flags))) {
  			rte_pktmbuf_free_bulk(&pkts_prealloc[pkt_idx], count - pkt_idx);
+
+			if (slot_idx == 0)
+				slot_idx = vq->size - 1;
+			else
+				slot_idx--;
+
  			break;
  		}
 
@@ -3674,8 +3681,13 @@ virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
  			async->buffer_idx_packed += vq->size - pkt_err;
 
  		while (pkt_err-- > 0) {
-			rte_pktmbuf_free(pkts_info[slot_idx % vq->size].mbuf);
-			slot_idx--;

lib/vhost/virtio_net.c virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 3693

Quoted 10 lines
lib/vhost/virtio_net.c +7 -0
+			rte_pktmbuf_free(pkts_info[slot_idx].mbuf);
+			descs_err += pkts_info[slot_idx].descs;
+
+			if (slot_idx == 0)
+				slot_idx = vq->size - 1;
+			else
+				slot_idx--;
  		}
 
  		/* recover available ring */

Thanks,
Maxime

Reviewed-by on v2 2/2
#14 lore raw

RE: [PATCH v2 2/2] vhost: fix slot index calculation in async vhost

In reply to Cheng Jiang
Quoted 71 lines

-----Original Message-----
From: Jiang, Cheng1 <[email protected]>
Sent: Tuesday, October 11, 2022 11:08 AM
To: [email protected]; Xia, Chenbo <[email protected]>
Cc: [email protected]; Hu, Jiayu <[email protected]>; Ding, Xuan <[email protected]>; Ma, WenwuX <[email protected]>; Wang, YuanX <[email protected]>; Yang, YvonneX <[email protected]>; He, Xingguang <[email protected]>; Jiang, Cheng1 <[email protected]>; [email protected]
Subject: [PATCH v2 2/2] vhost: fix slot index calculation in async vhost

When the packet receiving failure and the DMA ring full occur simultaneously in the asynchronous vhost, the slot_idx needs to be decreased by 1. For packed virtqueue, the slot index should be ring_size - 1, if the slot_idx is currently 0, since the ring size is not necessarily the power of 2.

Fixes 84d5204310d7 ("vhost: support async dequeue for split ring")
Fixes fe8477ebbd94 ("vhost: support async packed ring dequeue")
---
 lib/vhost/virtio_net.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
lib/vhost/virtio_net.c +7 -0
index 457ac2e92a..efebd063d7 100644
@@ -3457,6 +3457,7 @@ virtio_dev_tx_async_split(struct virtio_net *dev,
 struct vhost_virtqueue *vq,
 				allocerr_warned = true;
 			}
 			dropped = true;
+			slot_idx--;
 			break;
 		}
@@ -3647,6 +3648,12 @@ virtio_dev_tx_async_packed(struct virtio_net *dev,
 struct vhost_virtqueue *vq,
 		if (unlikely(virtio_dev_tx_async_single_packed(dev, vq,
 mbuf_pool, pkt,
 				slot_idx, legacy_ol_flags))) {
 			rte_pktmbuf_free_bulk(&pkts_prealloc[pkt_idx], count -
 pkt_idx);
+
+			if (slot_idx == 0)
+				slot_idx = vq->size - 1;
+			else
+				slot_idx--;
+
lib/vhost/virtio_net.c +7 -2
 			break;
 		}
@@ -3674,8 +3681,13 @@ virtio_dev_tx_async_packed(struct virtio_net *dev,
 struct vhost_virtqueue *vq,
 			async->buffer_idx_packed += vq->size - pkt_err;
 
 		while (pkt_err-- > 0) {
-			rte_pktmbuf_free(pkts_info[slot_idx % vq->size].mbuf);
-			slot_idx--;
+			rte_pktmbuf_free(pkts_info[slot_idx].mbuf);
+			descs_err += pkts_info[slot_idx].descs;
+
+			if (slot_idx == 0)
+				slot_idx = vq->size - 1;
+			else
+				slot_idx--;
 		}
 
Quoted 3 lines

/* recover available ring */
--
2.35.1

#15 lore raw
In reply to Cheng Jiang
Quoted 19 lines

-----Original Message-----
From: Jiang, Cheng1 <[email protected]>
Sent: Tuesday, October 11, 2022 11:08 AM
To: [email protected]; Xia, Chenbo <[email protected]>
Cc: [email protected]; Hu, Jiayu <[email protected]>; Ding, Xuan <[email protected]>; Ma, WenwuX <[email protected]>; Wang, YuanX <[email protected]>; Yang, YvonneX <[email protected]>; He, Xingguang <[email protected]>; Jiang, Cheng1 <[email protected]>
Subject: [PATCH v2 0/2] vhost: fix some async vhost index calculation issues

Fix some async vhost index calculation issues.

v2: fixed fixes tag and replaced some words in commit message.

Cheng Jiang (2):
vhost: fix descs count in async vhost packed ring
vhost: fix slot index calculation in async vhost

Quoted 5 lines
 lib/vhost/virtio_net.c | 40 +++++++++++++++++++++++++++++-----------
 1 file changed, 29 insertions(+), 11 deletions(-)

--
2.35.1

Series applied to next-virtio/main, thanks