few virtio/vhost fixes
cover few virtio/vhost fixes
---
Yuanhan Liu (3):
vhost: fix dead loop in enqueue path
vhost: fix long stall of vhost-user negotiation net/virtio: fix crash when number of virtio devices > 1
drivers/net/virtio/virtio_ethdev.c | 2 +- lib/librte_vhost/vhost_user.c | 3 ++- lib/librte_vhost/virtio_net.c | 4 +++- 3 files changed, 6 insertions(+), 3 deletions(-)
Signature · 1 line
1.9.0
PATCH 1/3 vhost: fix dead loop in enqueue path
If a malicious guest forges a dead loop desc chain (let desc->next point to itself) and desc->len is zero, this could lead to a dead loop in copy_mbuf_to_desc(following is a simplified code to show this issue clearly):
while (mbuf_is_not_totally_consumed) {
if (desc_avail == 0) {
desc = &descs[desc->next];
desc_avail = desc->len;
}
COPY(desc, mbuf, desc_avail);
}I have actually fixed a same issue before: a436f53ebfeb ("vhost: avoid dead loop chain"); it fixes the dequeue path though, leaving the enqueue path still vulnerable.
The fix is the same. Add a var nr_desc to avoid the dead loop.
--- lib/librte_vhost/virtio_net.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
lib/librte_vhost/virtio_net.c +3 -1
@@ -195,6 +195,8 @@ static inline int __attribute__((always_inline))struct vring_desc *desc;uint64_t desc_addr;struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0 ,0 ,0 ,0 ,0 ,0 },0 }; +/* A counter to avoid desc dead loop chain */ +uint16_t nr_desc =1 ; desc = &descs[desc_idx]; desc_addr =gpa_to_vva (dev, desc->addr);
@@ -233,7 +235,7 @@ static inline int __attribute__((always_inline))/* Room in vring buffer is not enough */ return -1 ; } -if (unlikely (desc->next >= size)) +if (unlikely (desc->next >= size || ++nr_desc > size))return -1 ; desc = &descs[desc->next];
Signature · 1 line
1.9.0
Re: [PATCH 1/3] vhost: fix dead loop in enqueue path
Quoted 20 lines · Yuanhan Liu
If a malicious guest forges a dead loop desc chain (let
desc->nextpoint to itself) anddesc->lenis zero, this could lead to a dead loop incopy_mbuf_to_desc(following is a simplified code to show this issue clearly):while (mbuf_is_not_totally_consumed) { if (desc_avail == 0) { desc = &descs[desc->next]; desc_avail = desc->len; } COPY(desc, mbuf, desc_avail); }I have actually fixed a same issue before:
a436f53ebfeb("vhost: avoid dead loop chain"); it fixes the dequeue path though, leaving the enqueue path still vulnerable.The fix is the same. Add a var
nr_descto avoid the dead loop.
Quoted 8 lines
Fixes f1a519ad981c ("vhost: fix enqueue/dequeue to handle chained vring descriptors")Reported-by Xieming Katty <[email protected]>Signed-off-by Yuanhan Liu <[email protected]>--- lib/librte_vhost/virtio_net.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
Thanks for the fix:
- Maxime
PATCH 2/3 vhost: fix long stall of vhost-user negotiation
In reply to Yuanhan LiuSetting up the mapping from GPA (guest physical address) to HPA (guest physical address) could be very time consuming when the guest memory is backened with small pages (4K). The bigger the guest memory, the longer it takes. This could lead a very long vhost-user negotiation.
Since the mapping is only needed in zero copy mode so far, we could avoid such time consuming settup when zero copy is turned off (which is the default case).
It's actually a workaround, a right fix might be to start a new thread, and hide the big latency there.
--- lib/librte_vhost/vhost_user.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/librte_vhost/vhost_user.c +2 -1
@@ -567,7 +567,8 @@ reg->host_user_addr = (uint64_t )(uintptr_t )mmap_addr + mmap_offset; -add_guest_pages (dev, reg, alignment); +if (dev->dequeue_zero_copy) +add_guest_pages (dev, reg, alignment);RTE_LOG (INFO ,VHOST_CONFIG , "guest memory region %u, size: 0x%" PRIx64 "\n"
Signature · 1 line
1.9.0
Re: [PATCH 2/3] vhost: fix long stall of vhost-user negotiation
Quoted 12 lines · Yuanhan Liu
Setting up the mapping from GPA (guest physical address) to HPA (guest physical address) could be very time consuming when the guest memory is backened with small pages (4K). The bigger the guest memory, the longer it takes. This could lead a very long vhost-user negotiation.
Since the mapping is only needed in zero copy mode so far, we could avoid such time consuming settup when zero copy is turned off (which is the default case).
It's actually a workaround, a right fix might be to start a new thread, and hide the big latency there.
Quoted 7 lines
Fixes e246896178e6 ("vhost: get guest/host physical address mappings")Signed-off-by Yuanhan Liu <[email protected]>--- lib/librte_vhost/vhost_user.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Thanks,
Maxime
PATCH 3/3 net/virtio: fix crash when number of virtio devices > 1
In reply to Yuanhan LiuThe vtpci_ops assignment needs the 'hw->port_id' as an input parameter. That said, we should set 'hw->port_id' firstly, then do the vtpci_ops assignment, while the code does reversely. That would result to a crash when more than one virtio devices are used, because we keep assigning proper vtpci_ops to virtio_hw_internal[0]->vtpci_ops, leaving the pointer for other ports being NULL.
Reverse the order fixes this issue.
--- drivers/net/virtio/virtio_ethdev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/virtio/virtio_ethdev.c +1 -1
@@ -1515,6 +1515,7 @@ static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,return -ENOMEM ; } + hw->port_id = eth_dev->data->port_id;/* For virtio_user case the hw->virtio_user_dev is populated by * virtio_user_eth_dev_alloc() before eth_virtio_dev_init() is called. */
@@ -1525,7 +1526,6 @@ static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,return ret; } - hw->port_id = eth_dev->data->port_id; eth_dev->data->dev_flags = dev_flags;/* reset device and negotiate default features */
Signature · 1 line
1.9.0
Re: [PATCH 3/3] net/virtio: fix crash when number of virtio devices > 1
Quoted 9 lines · Yuanhan Liu
The
vtpci_opsassignment needs the 'hw->port_id' as an input parameter. That said, we should set 'hw->port_id' firstly, then do thevtpci_opsassignment, while the code does reversely. That would result to a crash when more than one virtio devices are used, because we keep assigning propervtpci_opsto virtio_hw_internal[0]->vtpci_ops, leaving the pointer for other ports being NULL.Reverse the order fixes this issue.
Quoted 8 lines
Fixes 9470427c88e1 ("net/virtio: do not store PCI device pointer at shared memory")Reported-by Lei Yao <[email protected]>Signed-off-by Yuanhan Liu <[email protected]>--- drivers/net/virtio/virtio_ethdev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Thanks,
Maxime
Yuanhan Liu
AppliedQuoted 5 lines · Yuanhan Liu
---
Yuanhan Liu (3):
vhost: fix dead loop in enqueue path
vhost: fix long stall of vhost-user negotiation net/virtio: fix crash when number of virtio devices > 1
Applied to dpdk-next-virtio.
--yliu