virtio: fix idx in used ring retrieved only once
PATCH 1/1 virtio: fix idx in used ring retrieved only once
In the following loop:
while (vq->vq_used_cons_idx != vq->vq_ring.used->idx) {
...
}There is no external function call or any explict memory barrier in the loop, the re-read of used->idx would be optimized and would only be retrieved once.
use of voaltile normally should be prohibited, and access_once is Linux kernel's style to handle this issue; Once we have that macro in DPDK, we could change to that style.
virtio_recv_mergable_pkts might have the same issue, so will be fixed.
--- drivers/net/virtio/virtio_ethdev.c | 4 ++-- drivers/net/virtio/virtio_ring.h | 2 +- drivers/net/virtio/virtio_rxtx_simple.c | 3 +-- 3 files changed, 4 insertions(+), 5 deletions(-)
drivers/net/virtio/virtio_ethdev.c +2 -2
@@ -219,12 +219,12 @@ virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl,virtqueue_notify (vq);rte_rmb (); -while (vq->vq_used_cons_idx == vq->vq_ring.used->idx) { +while (VIRTQUEUE_NUSED (vq) ==0 ) {rte_rmb ();usleep (100 ); } -while (vq->vq_used_cons_idx != vq->vq_ring.used->idx) { +while (VIRTQUEUE_NUSED (vq)) {uint32_t idx, desc_idx, used_idx;struct vring_used_elem *uep;
drivers/net/virtio/virtio_ring.h +1 -1
@@ -79,7 +79,7 @@ struct vring_used_elem {struct vring_used {uint16_t flags; -uint16_t idx; +volatile uint16_t idx;struct vring_used_elem ring[0 ]; };
drivers/net/virtio/virtio_rxtx_simple.c +1 -2
@@ -184,8 +184,7 @@ virtio_recv_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts,if (unlikely (nb_pkts <RTE_VIRTIO_DESC_PER_LOOP ))return 0 ; - nb_used = *(volatile uint16_t *)&vq->vq_ring.used->idx - - vq->vq_used_cons_idx; + nb_used = vq->vq_ring.used->idx - vq->vq_used_cons_idx;rte_compiler_barrier ();
Signature · 1 line
1.8.1.4
Quoted 23 lines · Huawei Xie
drivers/net/virtio/virtio_ethdev.c +1
-1-while (vq->vq_used_cons_idx != vq->vq_ring.used->idx) { +while (VIRTQUEUE_NUSED (vq)) {uint32_t idx, desc_idx, used_idx;struct vring_used_elem *uep;drivers/net/virtio/virtio_ring.h +1
-1@@ -79,7 +79,7 @@ struct vring_used_elem {struct vring_used {uint16_t flags; -uint16_t idx; +volatile uint16_t idx;struct vring_used_elem ring[0 ]; };drivers/net/virtio/virtio_rxtx_simple.c +0
-0
drivers/net/virtio/virtio_rxtx_simple.c virtio_recv_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts, · line 187
Quoted 6 lines
drivers/net/virtio/virtio_rxtx_simple.c +1
-2@@ -184,8 +184,7 @@ virtio_recv_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts,if (unlikely (nb_pkts <RTE_VIRTIO_DESC_PER_LOOP ))return 0 ; - nb_used = *(volatile uint16_t *)&vq->vq_ring.used->idx - - vq->vq_used_cons_idx; + nb_used = vq->vq_ring.used->idx - vq->vq_used_cons_idx;
How about replacing it with VIRTQUEUE_NUSED here?
--yliu
PATCH v2 1/1 virtio: fix idx in used ring retrieved only once
In reply to Huawei XieIn the following loop:
while (vq->vq_used_cons_idx != vq->vq_ring.used->idx) {
...
}There is no external function call or any explict memory barrier in the loop, the re-read of used->idx would be optimized and would only be retrieved once.
use of voaltile normally should be prohibited, and access_once is Linux kernel's style to handle this issue; Once we have that macro in DPDK, we could change to that style.
virtio_recv_mergable_pkts might have the same issue, so will be fixed.
---
v2: use VIRTQUEUE_NUSED
--- drivers/net/virtio/virtio_ethdev.c | 4 ++-- drivers/net/virtio/virtio_ring.h | 2 +- drivers/net/virtio/virtio_rxtx_simple.c | 3 +-- 3 files changed, 4 insertions(+), 5 deletions(-)
drivers/net/virtio/virtio_ethdev.c +2 -2
@@ -219,12 +219,12 @@ virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl,virtqueue_notify (vq);rte_rmb (); -while (vq->vq_used_cons_idx == vq->vq_ring.used->idx) { +while (VIRTQUEUE_NUSED (vq) ==0 ) {rte_rmb ();usleep (100 ); } -while (vq->vq_used_cons_idx != vq->vq_ring.used->idx) { +while (VIRTQUEUE_NUSED (vq)) {uint32_t idx, desc_idx, used_idx;struct vring_used_elem *uep;
drivers/net/virtio/virtio_ring.h +1 -1
@@ -79,7 +79,7 @@ struct vring_used_elem {struct vring_used {uint16_t flags; -uint16_t idx; +volatile uint16_t idx;struct vring_used_elem ring[0 ]; };
drivers/net/virtio/virtio_rxtx_simple.c +1 -2
@@ -184,8 +184,7 @@ virtio_recv_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts,if (unlikely (nb_pkts <RTE_VIRTIO_DESC_PER_LOOP ))return 0 ; - nb_used = *(volatile uint16_t *)&vq->vq_ring.used->idx - - vq->vq_used_cons_idx; + nb_used =VIRTQUEUE_NUSED (vq);rte_compiler_barrier ();
Signature · 1 line
1.8.1.4
Yuanhan Liu
AppliedQuoted 12 lines · Huawei Xie
In the following loop:
while (vq->vq_used_cons_idx != vq->vq_ring.used->idx) { ... }There is no external function call or any explict memory barrier in the loop, the re-read of
used->idxwould be optimized and would only be retrieved once.use of voaltile normally should be prohibited, and
access_onceis Linux kernel's style to handle this issue; Once we have that macro in DPDK, we could change to that style.
Quoted 6 lines
virtio_recv_mergable_pktsmight have the same issue, so will be fixed.Fixes 823ad647950a ("virtio: support multiple queues")Fixes 13ce5e7eb94f ("virtio: mergeable buffers")Signed-off-by Huawei Xie <[email protected]>
Applied to dpdk-next-virtio.
Thanks.
--yliu