vhost: add missing barriers, move prefetching
v2 cover vhost: add missing barriers, move prefetching
This series adds missing read barriers after reading avail index for split ring and desc flags for packed ring.
Also, it turns out that some descriptors prefetching are either badly placed, or useless, last part of the series fixes that.
With the series applied, I get between 0 and 4% gain depending on the benchmark (testpmd txonly/rxonly/io).
Thanks to Jason for reporting the missing read barriers.
Changes since v1:
=================
- Drop volatile removal patch (Ilya)
- Improve commit messages for RMB patches (Ilya)
Maxime Coquelin (4):
vhost: enforce avail index and desc read ordering
vhost: enforce desc flags and content read ordering
vhost: prefetch descriptor after the read barrier
vhost: remove useless prefetch for packed ring descriptor
lib/librte_vhost/virtio_net.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-)
Signature · 1 line
2.17.2
PATCH v2 1/4 vhost: enforce avail index and desc read ordering
A read barrier is required to ensure the ordering between available index and the descriptor reads is enforced.
1. read avail_head = avail->idx
2. read cur_idx = last_avail_idx
if (cur_idx != avail_head) {
3. read idx = avail->ring[cur_idx]
4. read desc[idx]
}
There is a control dependency between step 1 and steps 3 & 4, 3 could be speculatively executed before 1, which could result in 'idx' to not being updated yet.
--- lib/librte_vhost/virtio_net.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
lib/librte_vhost/virtio_net.c +12 -0
@@ -752,6 +752,12 @@ virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,rte_prefetch0 (&vq->avail->ring[vq->last_avail_idx & (vq->size -1 )]); avail_head = *((volatile uint16_t *)&vq->avail->idx); +/* +* The ordering between avail index and +* desc reads needs to be enforced. +*/ +rte_smp_rmb (); +for (pkt_idx =0 ; pkt_idx < count; pkt_idx++) {uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;uint16_t nr_vec =0 ;
@@ -1334,6 +1340,12 @@ virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,if (free_entries ==0 )return 0 ; +/* +* The ordering between avail index and +* desc reads needs to be enforced. +*/ +rte_smp_rmb (); +VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); count =RTE_MIN (count,MAX_PKT_BURST );
Signature · 1 line
2.17.2
PATCH v2 2/4 vhost: enforce desc flags and content read ordering
In reply to Maxime CoquelinA read barrier is required to ensure that the ordering between descriptor's flags and content reads is enforced.
1. read flags = desc->flags
if (flags & AVAIL_BIT)
2. read desc->id
There is a control dependency between steps 1 and step 2. 2 could be speculatively executed before 1, which could result in 'id' to not be updated yet.
--- lib/librte_vhost/virtio_net.c | 6 ++++++ 1 file changed, 6 insertions(+)
lib/librte_vhost/virtio_net.c +6 -0
@@ -481,6 +481,12 @@ fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,if (unlikely (!desc_is_avail (&descs[avail_idx], wrap_counter)))return -1 ; +/* +* The ordering between desc flags and desc +* content reads need to be enforced. +*/ +rte_smp_rmb (); + *desc_count =0 ; *len =0 ;
Signature · 1 line
2.17.2
PATCH v2 3/4 vhost: prefetch descriptor after the read barrier
In reply to Maxime CoquelinThis patch moves the prefetch after the available index is read to avoid prefetching a descriptor not available yet.
--- lib/librte_vhost/virtio_net.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
lib/librte_vhost/virtio_net.c +4 -3
@@ -755,7 +755,6 @@ virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,struct buf_vector buf_vec[BUF_VECTOR_MAX ];uint16_t avail_head; -rte_prefetch0 (&vq->avail->ring[vq->last_avail_idx & (vq->size -1 )]); avail_head = *((volatile uint16_t *)&vq->avail->idx);/*
@@ -764,6 +763,8 @@ virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,*/ rte_smp_rmb (); +rte_prefetch0 (&vq->avail->ring[vq->last_avail_idx & (vq->size -1 )]); +for (pkt_idx =0 ; pkt_idx < count; pkt_idx++) {uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;uint16_t nr_vec =0 ;
@@ -1339,8 +1340,6 @@ virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq, } } -rte_prefetch0 (&vq->avail->ring[vq->last_avail_idx & (vq->size -1 )]); - free_entries = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;if (free_entries ==0 )
@@ -1352,6 +1351,8 @@ virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,*/ rte_smp_rmb (); +rte_prefetch0 (&vq->avail->ring[vq->last_avail_idx & (vq->size -1 )]); +VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); count =RTE_MIN (count,MAX_PKT_BURST );
Signature · 1 line
2.17.2
PATCH v2 4/4 vhost: remove useless prefetch for packed ring descriptor
In reply to Maxime CoquelinThis prefetch does not show any performance improvement.
--- lib/librte_vhost/virtio_net.c | 2 -- 1 file changed, 2 deletions(-)
lib/librte_vhost/virtio_net.c +0 -2
@@ -1437,8 +1437,6 @@ virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, {uint16_t i; -rte_prefetch0 (&vq->desc_packed[vq->last_avail_idx]); -if (unlikely (dev->dequeue_zero_copy)) {struct zcopy_mbuf *zmbuf, *next;
Signature · 1 line
2.17.2
Re: [PATCH v2 1/4] vhost: enforce avail index and desc read ordering
In reply to Maxime CoquelinQuoted 14 lines · Maxime Coquelin
A read barrier is required to ensure the ordering between available index and the descriptor reads is enforced.
1. read
avail_head=avail->idx
2. readcur_idx=last_avail_idx
if (cur_idx!=avail_head) {
3. read idx = avail->ring[cur_idx]
4. read desc[idx]
}There is a control dependency between step 1 and steps 3 & 4, 3 could be speculatively executed before 1, which could result in 'idx' to not being updated yet.
Quoted 6 lines
Fixes 4796ad63ba1f ("examples/vhost: import userspace vhost application")Reported-by Jason Wang <[email protected]>Signed-off-by Maxime Coquelin <[email protected]>Acked-by Ilya Maximets <[email protected]>
BTW Ilya do you see a performance degradation from RMBs with these patches?
Quoted 9 lines
--- lib/librte_vhost/virtio_net.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)lib/librte_vhost/virtio_net.c +0
-0
lib/librte_vhost/virtio_net.c virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 760
Quoted 9 lines
lib/librte_vhost/virtio_net.c +6
-0@@ -752,6 +752,12 @@ virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,rte_prefetch0 (&vq->avail->ring[vq->last_avail_idx & (vq->size -1 )]); avail_head = *((volatile uint16_t *)&vq->avail->idx); +/* +* The ordering between avail index and +* desc reads needs to be enforced. +*/ +rte_smp_rmb (); +
I'd guess you want to put the RMB before the prefetch. No? Otherwise I think you are either stalling until that completes or discard the prefetch ...
Quoted 18 lines
lib/librte_vhost/virtio_net.c +6
-0for (pkt_idx =0 ; pkt_idx < count; pkt_idx++) {uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;uint16_t nr_vec =0 ;@@ -1334,6 +1340,12 @@ virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,if (free_entries ==0 )return 0 ; +/* +* The ordering between avail index and +* desc reads needs to be enforced. +*/ +rte_smp_rmb (); +VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); count =RTE_MIN (count,MAX_PKT_BURST );Signature · 1 line
2.17.2
Re: [PATCH v2 4/4] vhost: remove useless prefetch for packed ring descriptor
In reply to Maxime CoquelinQuoted 3 lines · Maxime Coquelin
This prefetch does not show any performance improvement.
Signed-off-by Maxime Coquelin <[email protected]>
Likely because of the RMB.
Try prefetching the *next* descriptor maybe?
Quoted 19 lines
--- lib/librte_vhost/virtio_net.c | 2 -- 1 file changed, 2 deletions(-)lib/librte_vhost/virtio_net.c +0
-2@@ -1437,8 +1437,6 @@ virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, {uint16_t i; -rte_prefetch0 (&vq->desc_packed[vq->last_avail_idx]); -if (unlikely (dev->dequeue_zero_copy)) {struct zcopy_mbuf *zmbuf, *next;Signature · 1 line
2.17.2
Re: [PATCH v2 1/4] vhost: enforce avail index and desc read ordering
In reply to Michael S. TsirkinQuoted 39 lines · Michael S. Tsirkin
Quoted 14 lines · Maxime Coquelin
A read barrier is required to ensure the ordering between available index and the descriptor reads is enforced.
1. read
avail_head=avail->idx
2. readcur_idx=last_avail_idx
if (cur_idx!=avail_head) {
3. read idx = avail->ring[cur_idx]
4. read desc[idx]
}There is a control dependency between step 1 and steps 3 & 4, 3 could be speculatively executed before 1, which could result in 'idx' to not being updated yet.
Quoted 6 lines
Fixes 4796ad63ba1f ("examples/vhost: import userspace vhost application")Reported-by Jason Wang <[email protected]>Signed-off-by Maxime Coquelin <[email protected]>Acked-by Ilya Maximets <[email protected]>BTW Ilya do you see a performance degradation from RMBs with these patches?
Quoted 12 lines
--- lib/librte_vhost/virtio_net.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)lib/librte_vhost/virtio_net.c +0
-0@@ -752,6 +752,12 @@ virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,rte_prefetch0 (&vq->avail->ring[vq->last_avail_idx & (vq->size -1 )]); avail_head = *((volatile uint16_t *)&vq->avail->idx);
Quoted 10 lines
lib/librte_vhost/virtio_net.c virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 760
Quoted 6 lines
lib/librte_vhost/virtio_net.c +6
-0+/* +* The ordering between avail index and +* desc reads needs to be enforced. +*/ +rte_smp_rmb (); +I'd guess you want to put the RMB before the prefetch. No? Otherwise I think you are either stalling until that completes or discard the prefetch ...
Yeah, this is actually done in patch 3.
Thanks,
Maxime
Quoted 18 lines
Quoted 18 lines
lib/librte_vhost/virtio_net.c +6
-0for (pkt_idx =0 ; pkt_idx < count; pkt_idx++) {uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;uint16_t nr_vec =0 ;@@ -1334,6 +1340,12 @@ virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,if (free_entries ==0 )return 0 ; +/* +* The ordering between avail index and +* desc reads needs to be enforced. +*/ +rte_smp_rmb (); +VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); count =RTE_MIN (count,MAX_PKT_BURST );Signature · 1 line
2.17.2
Quoted 10 lines · Maxime Coquelin
This series adds missing read barriers after reading avail index for split ring and desc flags for packed ring.
Also, it turns out that some descriptors prefetching are either badly placed, or useless, last part of the series fixes that.
With the series applied, I get between 0 and 4% gain depending on the benchmark (testpmd txonly/rxonly/io).
Thanks to Jason for reporting the missing read barriers.
But I wonder what effect this has on ARM where RMB isn't a NOP. Ilya do you happen to have any data?
Quoted 16 lines
Changes since v1:
=================
- Drop volatile removal patch (Ilya)
- Improve commit messages for RMB patches (Ilya)Maxime Coquelin (4):
vhost: enforce avail index and desc read ordering
vhost: enforce desc flags and content read ordering
vhost: prefetch descriptor after the read barrier
vhost: remove useless prefetch for packed ring descriptorlib/librte_vhost/virtio_net.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-)Signature · 1 line
2.17.2
Quoted 10 lines · Michael S. Tsirkin
Quoted 9 lines · Maxime Coquelin
This series adds missing read barriers after reading avail index for split ring and desc flags for packed ring.
Also, it turns out that some descriptors prefetching are either badly placed, or useless, last part of the series fixes that.
With the series applied, I get between 0 and 4% gain depending on the benchmark (testpmd txonly/rxonly/io).
Quoted 6 lines
Quoted 1 line
Thanks to Jason for reporting the missing read barriers.
But I wonder what effect this has on ARM where RMB isn't a NOP. Ilya do you happen to have any data?
Hi.
My rough testing shows no significant performance difference on ARMv8.
Quoted 19 lines
Quoted 17 lines
Changes since v1:
=================
- Drop volatile removal patch (Ilya)
- Improve commit messages for RMB patches (Ilya)Maxime Coquelin (4):
vhost: enforce avail index and desc read ordering
vhost: enforce desc flags and content read ordering
vhost: prefetch descriptor after the read barrier
vhost: remove useless prefetch for packed ring descriptorlib/librte_vhost/virtio_net.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-)Signature · 1 line
2.17.2
Quoted 16 lines · Maxime Coquelin
This series adds missing read barriers after reading avail index for split ring and desc flags for packed ring.
Also, it turns out that some descriptors prefetching are either badly placed, or useless, last part of the series fixes that.
With the series applied, I get between 0 and 4% gain depending on the benchmark (testpmd txonly/rxonly/io).
Thanks to Jason for reporting the missing read barriers.
Changes since v1:
=================
- Drop volatile removal patch (Ilya)
- Improve commit messages for RMB patches (Ilya)
Quoted 8 lines
Maxime Coquelin (4):
vhost: enforce avail index and desc read ordering
vhost: enforce desc flags and content read ordering
vhost: prefetch descriptor after the read barrier
vhost: remove useless prefetch for packed ring descriptorlib/librte_vhost/virtio_net.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-)
Maxime Coquelin
AppliedQuoted 16 lines · Maxime Coquelin
This series adds missing read barriers after reading avail index for split ring and desc flags for packed ring.
Also, it turns out that some descriptors prefetching are either badly placed, or useless, last part of the series fixes that.
With the series applied, I get between 0 and 4% gain depending on the benchmark (testpmd txonly/rxonly/io).
Thanks to Jason for reporting the missing read barriers.
Changes since v1:
=================
- Drop volatile removal patch (Ilya)
- Improve commit messages for RMB patches (Ilya)
Quoted 9 lines
Maxime Coquelin (4):
vhost: enforce avail index and desc read ordering
vhost: enforce desc flags and content read ordering
vhost: prefetch descriptor after the read barrier
vhost: remove useless prefetch for packed ring descriptorlib/librte_vhost/virtio_net.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-)
Applied to dpdk-next-virtio.
Maxime