Skip to contentSkip to conversation

vhost: Fix last queue index of devices with no cvq

PATCH Superseded Eugenio Pérez 3 participants 13 messages Oct 29, 2021 – Nov 2, 2021

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vhost: Fix last queue index of devices with no cvq

The -1 assumes that all devices with no cvq have an spare vq allocated for them, but with no offer of VIRTIO_NET_F_CTRL_VQ. This may not be the case, and the device may have a pair number of queues.

To fix this, just resort to the lower even number of queues.

Fixes 049eb15b5fc9 ("vhost: record the last virtqueue index for the virtio device")
---
 hw/net/vhost_net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/net/vhost_net.c +1 -1
index 0d888f29a6..edf56a597f 100644
@@ -330,7 +330,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
     NetClientState *peer;
 
     if (!cvq) {
-        last_index -= 1;
+        last_index &= ~1ULL;
     }
 
     if (!k->set_guest_notifiers) {
Signature · 1 line
2.27.0
#1 lore raw
Quoted 17 lines · Eugenio Pérez

The -1 assumes that all devices with no cvq have an spare vq allocated for them, but with no offer of VIRTIO_NET_F_CTRL_VQ. This may not be the case, and the device may have a pair number of queues.

To fix this, just resort to the lower even number of queues.

Fixes 049eb15b5fc9 ("vhost: record the last virtqueue index for the virtio device")
---
 hw/net/vhost_net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/net/vhost_net.c +0 -0
index 0d888f29a6..edf56a597f 100644

hw/net/vhost_net.c int vhost_net_start(VirtIODevice *dev, NetClientState *ncs, · line 336

Quoted 8 lines
hw/net/vhost_net.c +1 -1
@@ -330,7 +330,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
     NetClientState *peer;
 
     if (!cvq) {
-        last_index -= 1;
+        last_index &= ~1ULL;
     }
 
     if (!k->set_guest_notifiers) {

could this code be made clearer?

Quoted 2 lines
Signature · 1 line
2.27.0
#2 lore raw
Quoted 21 lines · Michael S. Tsirkin
Quoted 19 lines · Eugenio Pérez

The -1 assumes that all devices with no cvq have an spare vq allocated for them, but with no offer of VIRTIO_NET_F_CTRL_VQ. This may not be the case, and the device may have a pair number of queues.

To fix this, just resort to the lower even number of queues.

Fixes 049eb15b5fc9 ("vhost: record the last virtqueue index for the virtio device")
---
 hw/net/vhost_net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/net/vhost_net.c +0 -0
index 0d888f29a6..edf56a597f 100644
@@ -330,7 +330,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
     NetClientState *peer;
 
Quoted 9 lines

hw/net/vhost_net.c int vhost_net_start(VirtIODevice *dev, NetClientState *ncs, · line 336

Quoted 6 lines
hw/net/vhost_net.c +1 -1
     if (!cvq) {
-        last_index -= 1;
+        last_index &= ~1ULL;
     }
 
     if (!k->set_guest_notifiers) {

could this code be made clearer?

I can expand both for sure, but do you mean clearer because the operation is obscure (to remove the last bit to make last_index even) or because the need of that operation is not clear enough?

Thanks!

Quoted 3 lines
Quoted 2 lines

--
2.27.0

#3 lore raw
Quoted 32 lines · Eugenio Perez Martin
Quoted 21 lines · Michael S. Tsirkin
Quoted 19 lines · Eugenio Pérez

The -1 assumes that all devices with no cvq have an spare vq allocated for them, but with no offer of VIRTIO_NET_F_CTRL_VQ. This may not be the case, and the device may have a pair number of queues.

To fix this, just resort to the lower even number of queues.

Fixes 049eb15b5fc9 ("vhost: record the last virtqueue index for the virtio device")
---
 hw/net/vhost_net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/net/vhost_net.c +0 -0
index 0d888f29a6..edf56a597f 100644
@@ -330,7 +330,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
     NetClientState *peer;
 
Quoted 9 lines

hw/net/vhost_net.c int vhost_net_start(VirtIODevice *dev, NetClientState *ncs, · line 336

Quoted 6 lines
hw/net/vhost_net.c +1 -1
     if (!cvq) {
-        last_index -= 1;
+        last_index &= ~1ULL;
     }
 
     if (!k->set_guest_notifiers) {

could this code be made clearer?

Quoted 5 lines

I can expand both for sure, but do you mean clearer because the operation is obscure (to remove the last bit to make last_index even) or because the need of that operation is not clear enough?

Thanks!

I'm not sure ... both?

Quoted 3 lines
Quoted 3 lines
Quoted 2 lines

--
2.27.0

#4 lore raw
In reply to Eugenio Pérez
Quoted 18 lines · Eugenio Pérez

The -1 assumes that all devices with no cvq have an spare vq allocated for them, but with no offer of VIRTIO_NET_F_CTRL_VQ. This may not be the case, and the device may have a pair number of queues.

To fix this, just resort to the lower even number of queues.

Fixes 049eb15b5fc9 ("vhost: record the last virtqueue index for the virtio device")
---
 hw/net/vhost_net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/net/vhost_net.c +0 -0
index 0d888f29a6..edf56a597f 100644

hw/net/vhost_net.c int vhost_net_start(VirtIODevice *dev, NetClientState *ncs, · line 334

Quoted 6 lines
hw/net/vhost_net.c +1 -1
@@ -330,7 +330,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
     NetClientState *peer;
 
     if (!cvq) {
-        last_index -= 1;
+        last_index &= ~1ULL;
     }

The math here looks correct but we need to fix vhost_vdpa_dev_start() instead?

if (dev->vq_index + dev->nvqs - 1 != dev->last_index) {
...
}

Thanks

Quoted 5 lines
hw/net/vhost_net.c +0 -1
     if (!k->set_guest_notifiers) {
--

2.27.0

#5 lore raw
Quoted 29 lines · Jason Wang
Quoted 18 lines · Eugenio Pérez

The -1 assumes that all devices with no cvq have an spare vq allocated for them, but with no offer of VIRTIO_NET_F_CTRL_VQ. This may not be the case, and the device may have a pair number of queues.

To fix this, just resort to the lower even number of queues.

Fixes 049eb15b5fc9 ("vhost: record the last virtqueue index for the virtio device")
---
 hw/net/vhost_net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/net/vhost_net.c +0 -0
index 0d888f29a6..edf56a597f 100644

hw/net/vhost_net.c int vhost_net_start(VirtIODevice *dev, NetClientState *ncs, · line 334

Quoted 6 lines
hw/net/vhost_net.c +1 -1
@@ -330,7 +330,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
     NetClientState *peer;
 
     if (!cvq) {
-        last_index -= 1;
+        last_index &= ~1ULL;
     }

The math here looks correct but we need to fix vhost_vdpa_dev_start() instead?

Quoted 4 lines

if (dev->vq_index + dev->nvqs - 1 != dev->last_index) {
...
}

If we just do that, devices that offer an odd number of queues but do not offer ctrl vq would never enable the last vq pair, isn't it?

Also, I would say that the right place for the solution of this problem should not be virtio/vhost-vdpa: This is highly dependent on having cvq, and this implies a knowledge about the use of each virtqueue. Another kind of device could have an odd number of virtqueues naturally, and that (-1) would not work for them, isn't it?

Thanks!

Quoted 8 lines

Thanks

Quoted 5 lines
hw/net/vhost_net.c +0 -1
     if (!k->set_guest_notifiers) {
--

2.27.0

#6 lore raw
Quoted 31 lines · Eugenio Perez Martin
Quoted 29 lines · Jason Wang
Quoted 18 lines · Eugenio Pérez

The -1 assumes that all devices with no cvq have an spare vq allocated for them, but with no offer of VIRTIO_NET_F_CTRL_VQ. This may not be the case, and the device may have a pair number of queues.

To fix this, just resort to the lower even number of queues.

Fixes 049eb15b5fc9 ("vhost: record the last virtqueue index for the virtio device")
---
 hw/net/vhost_net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/net/vhost_net.c +0 -0
index 0d888f29a6..edf56a597f 100644

hw/net/vhost_net.c int vhost_net_start(VirtIODevice *dev, NetClientState *ncs, · line 334

Quoted 6 lines
hw/net/vhost_net.c +1 -1
@@ -330,7 +330,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
     NetClientState *peer;
 
     if (!cvq) {
-        last_index -= 1;
+        last_index &= ~1ULL;
     }

The math here looks correct but we need to fix vhost_vdpa_dev_start() instead?

Quoted 8 lines
Quoted 4 lines

if (dev->vq_index + dev->nvqs - 1 != dev->last_index) {
...
}

If we just do that, devices that offer an odd number of queues but do not offer ctrl vq would never enable the last vq pair, isn't it?

To expand the issue,

With that condition it is not possible to make vp_vdpa work on devices with no cvq. If I set the L0 guest's device with no cvq (with -device virtio-net-pci,...,ctrl_vq=off,mq=off). The nested VM will enter that conditional in vhost_net_start, and will mark last_index=1, making it impossible to start a vhost_vdpa device.

However, re-reading the standard:

controlq only exists if VIRTIO_NET_F_CTRL_VQ set.

So the code is actually handling an invalid device: The device set VIRTIO_NET_F_CTRL_VQ but offered an odd number of VQs.

Do we have an example of such a device? It's not the case on qemu virtio-net, with or without vhost-net in L0 device. The operation &= ~1ULL is an intended noop in case the queues are already even. I'm fine to keep making last_index even, so we have that safety net, with further clarifications as MST said, just in case the device is not behaving well. But maybe it's even better just to delete that conditional entirely?

Thanks!

Quoted 16 lines

Also, I would say that the right place for the solution of this problem should not be virtio/vhost-vdpa: This is highly dependent on having cvq, and this implies a knowledge about the use of each virtqueue. Another kind of device could have an odd number of virtqueues naturally, and that (-1) would not work for them, isn't it?

Thanks!

Quoted 8 lines

Thanks

Quoted 5 lines
hw/net/vhost_net.c +0 -1
     if (!k->set_guest_notifiers) {
--

2.27.0

#7 lore raw
Quoted 64 lines · Eugenio Perez Martin
Quoted 31 lines · Eugenio Perez Martin
Quoted 29 lines · Jason Wang
Quoted 18 lines · Eugenio Pérez

The -1 assumes that all devices with no cvq have an spare vq allocated for them, but with no offer of VIRTIO_NET_F_CTRL_VQ. This may not be the case, and the device may have a pair number of queues.

To fix this, just resort to the lower even number of queues.

Fixes 049eb15b5fc9 ("vhost: record the last virtqueue index for the virtio device")
---
 hw/net/vhost_net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/net/vhost_net.c +0 -0
index 0d888f29a6..edf56a597f 100644

hw/net/vhost_net.c int vhost_net_start(VirtIODevice *dev, NetClientState *ncs, · line 334

Quoted 6 lines
hw/net/vhost_net.c +1 -1
@@ -330,7 +330,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
     NetClientState *peer;
 
     if (!cvq) {
-        last_index -= 1;
+        last_index &= ~1ULL;
     }

The math here looks correct but we need to fix vhost_vdpa_dev_start() instead?

Quoted 8 lines
Quoted 4 lines

if (dev->vq_index + dev->nvqs - 1 != dev->last_index) {
...
}

If we just do that, devices that offer an odd number of queues but do not offer ctrl vq would never enable the last vq pair, isn't it?

To expand the issue,

With that condition it is not possible to make vp_vdpa work on devices with no cvq. If I set the L0 guest's device with no cvq (with -device virtio-net-pci,...,ctrl_vq=off,mq=off). The nested VM will enter that conditional in vhost_net_start, and will mark last_index=1, making it impossible to start a vhost_vdpa device.

However, re-reading the standard:

controlq only exists if VIRTIO_NET_F_CTRL_VQ set.

So the code is actually handling an invalid device: The device set VIRTIO_NET_F_CTRL_VQ but offered an odd number of VQs.

Do we have an example of such a device? It's not the case on qemu virtio-net, with or without vhost-net in L0 device. The operation &= ~1ULL is an intended noop in case the queues are already even. I'm fine to keep making last_index even, so we have that safety net, with further clarifications as MST said, just in case the device is not behaving well. But maybe it's even better just to delete that conditional entirely?

Quoted 2 lines

Thanks!

For sure, no need to handle an invalid configuration. Do you have a patch in mind? It'd be easier to discuss things with a specific patch rather than theoretically.

Quoted 18 lines
Quoted 18 lines

Also, I would say that the right place for the solution of this problem should not be virtio/vhost-vdpa: This is highly dependent on having cvq, and this implies a knowledge about the use of each virtqueue. Another kind of device could have an odd number of virtqueues naturally, and that (-1) would not work for them, isn't it?

Thanks!

Quoted 8 lines

Thanks

Quoted 5 lines
hw/net/vhost_net.c +0 -1
     if (!k->set_guest_notifiers) {
--

2.27.0

#8 lore raw
Quoted 66 lines · Michael S. Tsirkin
Quoted 64 lines · Eugenio Perez Martin
Quoted 31 lines · Eugenio Perez Martin
Quoted 29 lines · Jason Wang
Quoted 18 lines · Eugenio Pérez

The -1 assumes that all devices with no cvq have an spare vq allocated for them, but with no offer of VIRTIO_NET_F_CTRL_VQ. This may not be the case, and the device may have a pair number of queues.

To fix this, just resort to the lower even number of queues.

Fixes 049eb15b5fc9 ("vhost: record the last virtqueue index for the virtio device")
---
 hw/net/vhost_net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/net/vhost_net.c +0 -0
index 0d888f29a6..edf56a597f 100644

hw/net/vhost_net.c int vhost_net_start(VirtIODevice *dev, NetClientState *ncs, · line 334

Quoted 6 lines
hw/net/vhost_net.c +1 -1
@@ -330,7 +330,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
     NetClientState *peer;
 
     if (!cvq) {
-        last_index -= 1;
+        last_index &= ~1ULL;
     }

The math here looks correct but we need to fix vhost_vdpa_dev_start() instead?

Quoted 8 lines
Quoted 4 lines

if (dev->vq_index + dev->nvqs - 1 != dev->last_index) {
...
}

If we just do that, devices that offer an odd number of queues but do not offer ctrl vq would never enable the last vq pair, isn't it?

To expand the issue,

With that condition it is not possible to make vp_vdpa work on devices with no cvq. If I set the L0 guest's device with no cvq (with -device virtio-net-pci,...,ctrl_vq=off,mq=off). The nested VM will enter that conditional in vhost_net_start, and will mark last_index=1, making it impossible to start a vhost_vdpa device.

However, re-reading the standard:

controlq only exists if VIRTIO_NET_F_CTRL_VQ set.

So the code is actually handling an invalid device: The device set VIRTIO_NET_F_CTRL_VQ but offered an odd number of VQs.

Do we have an example of such a device? It's not the case on qemu virtio-net, with or without vhost-net in L0 device. The operation &= ~1ULL is an intended noop in case the queues are already even. I'm fine to keep making last_index even, so we have that safety net, with further clarifications as MST said, just in case the device is not behaving well. But maybe it's even better just to delete that conditional entirely?

Quoted 7 lines
Quoted 2 lines

Thanks!

For sure, no need to handle an invalid configuration. Do you have a patch in mind? It'd be easier to discuss things with a specific patch rather than theoretically.

I meant to totally delete the check, since it's assuming a device has an odd number of virtqueues but cvq feature flag enabled. And this is invalid in -net kind of devices.

I can send a patch with that for sure.

Quoted 19 lines
Quoted 18 lines
Quoted 18 lines

Also, I would say that the right place for the solution of this problem should not be virtio/vhost-vdpa: This is highly dependent on having cvq, and this implies a knowledge about the use of each virtqueue. Another kind of device could have an odd number of virtqueues naturally, and that (-1) would not work for them, isn't it?

Thanks!

Quoted 8 lines

Thanks

Quoted 5 lines
hw/net/vhost_net.c +0 -1
     if (!k->set_guest_notifiers) {
--

2.27.0

#9 lore raw
In reply to Eugenio Perez Martin
Quoted 31 lines · Eugenio Perez Martin
Quoted 29 lines · Jason Wang
Quoted 18 lines · Eugenio Pérez

The -1 assumes that all devices with no cvq have an spare vq allocated for them, but with no offer of VIRTIO_NET_F_CTRL_VQ. This may not be the case, and the device may have a pair number of queues.

To fix this, just resort to the lower even number of queues.

Fixes 049eb15b5fc9 ("vhost: record the last virtqueue index for the virtio device")
---
 hw/net/vhost_net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/net/vhost_net.c +0 -0
index 0d888f29a6..edf56a597f 100644

hw/net/vhost_net.c int vhost_net_start(VirtIODevice *dev, NetClientState *ncs, · line 334

Quoted 6 lines
hw/net/vhost_net.c +1 -1
@@ -330,7 +330,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
     NetClientState *peer;
 
     if (!cvq) {
-        last_index -= 1;
+        last_index &= ~1ULL;
     }

The math here looks correct but we need to fix vhost_vdpa_dev_start() instead?

Quoted 7 lines
Quoted 4 lines

if (dev->vq_index + dev->nvqs - 1 != dev->last_index) {
...
}

If we just do that, devices that offer an odd number of queues but do not offer ctrl vq would never enable the last vq pair, isn't it?

For vq pair, you assume that it's a networking device, so the device you described here violates the spec.

Quoted 6 lines

Also, I would say that the right place for the solution of this problem should not be virtio/vhost-vdpa: This is highly dependent on having cvq, and this implies a knowledge about the use of each virtqueue. Another kind of device could have an odd number of virtqueues naturally, and that (-1) would not work for them, isn't it?

It actually depends on how multiqueue is modeled for each specific type of device. They need to initialize the vq_index and nvqs correctly:

E.g if we had a device with 3 queues, we could model it with the following:

vhost_dev 1, vq_index = 0, nvqs = 2
vhost_dev 2, vq_index = 2, nvqs = 1

In this case the last_index should be initialized to 2, then we know all the vhost_dev is initialized and we can start the hardware.

Thanks

Quoted 12 lines

Thanks!

Quoted 8 lines

Thanks

Quoted 5 lines
hw/net/vhost_net.c +0 -1
     if (!k->set_guest_notifiers) {
--

2.27.0

#10 lore raw
Quoted 55 lines · Jason Wang
Quoted 31 lines · Eugenio Perez Martin
Quoted 29 lines · Jason Wang
Quoted 18 lines · Eugenio Pérez

The -1 assumes that all devices with no cvq have an spare vq allocated for them, but with no offer of VIRTIO_NET_F_CTRL_VQ. This may not be the case, and the device may have a pair number of queues.

To fix this, just resort to the lower even number of queues.

Fixes 049eb15b5fc9 ("vhost: record the last virtqueue index for the virtio device")
---
 hw/net/vhost_net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/net/vhost_net.c +0 -0
index 0d888f29a6..edf56a597f 100644

hw/net/vhost_net.c int vhost_net_start(VirtIODevice *dev, NetClientState *ncs, · line 334

Quoted 6 lines
hw/net/vhost_net.c +1 -1
@@ -330,7 +330,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
     NetClientState *peer;
 
     if (!cvq) {
-        last_index -= 1;
+        last_index &= ~1ULL;
     }

The math here looks correct but we need to fix vhost_vdpa_dev_start() instead?

Quoted 7 lines
Quoted 4 lines

if (dev->vq_index + dev->nvqs - 1 != dev->last_index) {
...
}

If we just do that, devices that offer an odd number of queues but do not offer ctrl vq would never enable the last vq pair, isn't it?

For vq pair, you assume that it's a networking device, so the device you described here violates the spec.

Quoted 6 lines

Also, I would say that the right place for the solution of this problem should not be virtio/vhost-vdpa: This is highly dependent on having cvq, and this implies a knowledge about the use of each virtqueue. Another kind of device could have an odd number of virtqueues naturally, and that (-1) would not work for them, isn't it?

It actually depends on how multiqueue is modeled for each specific type of device. They need to initialize the vq_index and nvqs correctly:

Quoted 8 lines

E.g if we had a device with 3 queues, we could model it with the following:

vhost_dev 1, vq_index = 0, nvqs = 2
vhost_dev 2, vq_index = 2, nvqs = 1

In this case the last_index should be initialized to 2, then we know all the vhost_dev is initialized and we can start the hardware.

Right, but in that case, cvq == true, and we never enter the conditional if (!cvq).

If cvq is false at that moment, your vhost_dev 2 *must* not exist and the last index will be even, so we must not subtract 1 to last_index. The subtraction is the cause the device never starts.

Given all of the above, I think we can skip the conditional entirely.

Thanks!

Quoted 15 lines

Thanks

Quoted 12 lines

Thanks!

Quoted 8 lines

Thanks

Quoted 5 lines
hw/net/vhost_net.c +0 -1
     if (!k->set_guest_notifiers) {
--

2.27.0

#11 lore raw
Quoted 66 lines · Eugenio Perez Martin
Quoted 55 lines · Jason Wang
Quoted 31 lines · Eugenio Perez Martin
Quoted 29 lines · Jason Wang
Quoted 18 lines · Eugenio Pérez

The -1 assumes that all devices with no cvq have an spare vq allocated for them, but with no offer of VIRTIO_NET_F_CTRL_VQ. This may not be the case, and the device may have a pair number of queues.

To fix this, just resort to the lower even number of queues.

Fixes 049eb15b5fc9 ("vhost: record the last virtqueue index for the virtio device")
---
 hw/net/vhost_net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/net/vhost_net.c +0 -0
index 0d888f29a6..edf56a597f 100644

hw/net/vhost_net.c int vhost_net_start(VirtIODevice *dev, NetClientState *ncs, · line 334

Quoted 6 lines
hw/net/vhost_net.c +1 -1
@@ -330,7 +330,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
     NetClientState *peer;
 
     if (!cvq) {
-        last_index -= 1;
+        last_index &= ~1ULL;
     }

The math here looks correct but we need to fix vhost_vdpa_dev_start() instead?

Quoted 7 lines
Quoted 4 lines

if (dev->vq_index + dev->nvqs - 1 != dev->last_index) {
...
}

If we just do that, devices that offer an odd number of queues but do not offer ctrl vq would never enable the last vq pair, isn't it?

For vq pair, you assume that it's a networking device, so the device you described here violates the spec.

Quoted 6 lines

Also, I would say that the right place for the solution of this problem should not be virtio/vhost-vdpa: This is highly dependent on having cvq, and this implies a knowledge about the use of each virtqueue. Another kind of device could have an odd number of virtqueues naturally, and that (-1) would not work for them, isn't it?

It actually depends on how multiqueue is modeled for each specific type of device. They need to initialize the vq_index and nvqs correctly:

Quoted 8 lines

E.g if we had a device with 3 queues, we could model it with the following:

vhost_dev 1, vq_index = 0, nvqs = 2
vhost_dev 2, vq_index = 2, nvqs = 1

In this case the last_index should be initialized to 2, then we know all the vhost_dev is initialized and we can start the hardware.

Quoted 6 lines

Right, but in that case, cvq == true, and we never enter the conditional if (!cvq).

If cvq is false at that moment, your vhost_dev 2 *must* not exist and the last index will be even, so we must not subtract 1 to last_index. The subtraction is the cause the device never starts.

The last_index will be 1, so the device will be started after vhost_dev 1 is initialized?

Thanks

Quoted 21 lines

Given all of the above, I think we can skip the conditional entirely.

Thanks!

Quoted 15 lines

Thanks

Quoted 12 lines

Thanks!

Quoted 8 lines

Thanks

Quoted 5 lines
hw/net/vhost_net.c +0 -1
     if (!k->set_guest_notifiers) {
--

2.27.0

#12 lore raw
Quoted 68 lines · Jason Wang
Quoted 66 lines · Eugenio Perez Martin
Quoted 63 lines · Jason Wang
Quoted 31 lines · Eugenio Perez Martin
Quoted 29 lines · Jason Wang
Quoted 18 lines · Eugenio Pérez

The -1 assumes that all devices with no cvq have an spare vq allocated for them, but with no offer of VIRTIO_NET_F_CTRL_VQ. This may not be the case, and the device may have a pair number of queues.

To fix this, just resort to the lower even number of queues.

Fixes 049eb15b5fc9 ("vhost: record the last virtqueue index for the virtio device")
---
 hw/net/vhost_net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/net/vhost_net.c +0 -0
index 0d888f29a6..edf56a597f 100644

hw/net/vhost_net.c int vhost_net_start(VirtIODevice *dev, NetClientState *ncs, · line 334

Quoted 6 lines
hw/net/vhost_net.c +1 -1
@@ -330,7 +330,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
     NetClientState *peer;
 
     if (!cvq) {
-        last_index -= 1;
+        last_index &= ~1ULL;
     }

The math here looks correct but we need to fix vhost_vdpa_dev_start() instead?

Quoted 7 lines
Quoted 4 lines

if (dev->vq_index + dev->nvqs - 1 != dev->last_index) {
...
}

If we just do that, devices that offer an odd number of queues but do not offer ctrl vq would never enable the last vq pair, isn't it?

For vq pair, you assume that it's a networking device, so the device you described here violates the spec.

Quoted 6 lines

Also, I would say that the right place for the solution of this problem should not be virtio/vhost-vdpa: This is highly dependent on having cvq, and this implies a knowledge about the use of each virtqueue. Another kind of device could have an odd number of virtqueues naturally, and that (-1) would not work for them, isn't it?

It actually depends on how multiqueue is modeled for each specific type of device. They need to initialize the vq_index and nvqs correctly:

E.g if we had a device with 3 queues, we could model it with the following:

vhost_dev 1, vq_index = 0, nvqs = 2
vhost_dev 2, vq_index = 2, nvqs = 1

In this case the last_index should be initialized to 2, then we know all the vhost_dev is initialized and we can start the hardware.

Quoted 6 lines
Quoted 6 lines

Right, but in that case, cvq == true, and we never enter the conditional if (!cvq).

If cvq is false at that moment, your vhost_dev 2 *must* not exist and the last index will be even, so we must not subtract 1 to last_index. The subtraction is the cause the device never starts.

Clarification: I meant networking here :).

Quoted 4 lines

The last_index will be 1, so the device will be started after vhost_dev 1 is initialized?

In !cvq !mq case, last_index is the number of virtqueues. It is initialized as:

int ... last_index = data_queue_pairs * 2;

and data queue pairs comes from caller function virtio_net_vhost_status:

int queue_pairs = n->multiqueue ? n->max_queue_pairs : 1;

so last_index ends up being 2.

I didn't check a MQ device with !cvq, but I think it will be the same.

Thanks!

Quoted 24 lines

Thanks

Quoted 21 lines

Given all of the above, I think we can skip the conditional entirely.

Thanks!

Quoted 15 lines

Thanks

Quoted 12 lines

Thanks!

Quoted 8 lines

Thanks

Quoted 5 lines
hw/net/vhost_net.c +0 -1
     if (!k->set_guest_notifiers) {
--

2.27.0