Skip to contentSkip to conversation

vdpa: Avoid reset when stop device

PATCH In review [email protected] 6 participants 47 messages Mar 23, 2022 – Apr 11, 2022 6 messages not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vdpa: Avoid reset when stop device

From: Michael Qiu <[email protected]>

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset twice, this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_dev_stop(), qemu tries to stop the device, then stop the queue: vhost_virtqueue_stop().
In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and the driver finds
that the VQ is invalied, this is the root cause.

Actually, device reset will be called within func release()

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

---
 hw/virtio/vhost-vdpa.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
hw/virtio/vhost-vdpa.c +4 -4
index c5ed7a3..d858b4f 100644
@@ -719,14 +719,14 @@ static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx)
     return idx;
 }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, unsigned int ready)
 {
     int i;
     trace_vhost_vdpa_set_vring_ready(dev);
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_vring_state state = {
             .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
         };
         vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
     }
@@ -1088,8 +1088,9 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
         if (unlikely(!ok)) {
             return -1;
         }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
     } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
         ok = vhost_vdpa_svqs_stop(dev);
         if (unlikely(!ok)) {
             return -1;
@@ -1105,7 +1106,6 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
         memory_listener_register(&v->listener, &address_space_memory);
         return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
     } else {
-        vhost_vdpa_reset_device(dev);
         vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
                                    VIRTIO_CONFIG_S_DRIVER);
         memory_listener_unregister(&v->listener);
Signature · 1 line
1.8.3.1
Acked-by on v1 1/1
#1 lore raw

Adding Eugenio, and Ling Shan.

Quoted 15 lines · On Wed, Mar 23, 2022 at 4:58 PM <[email protected]> wrote:

From: Michael Qiu <[email protected]>

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset twice, this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_dev_stop(), qemu tries to stop the device, then stop the queue: vhost_virtqueue_stop().
In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and the driver finds
that the VQ is invalied, this is the root cause.

Quoted 4 lines

Actually, device reset will be called within func release()

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

This is an interesting issue. Do you see a real issue except for the above warnings.

The reason we "abuse" reset is that we don't have a stop uAPI for vhost. We plan to add a status bit to stop the whole device in the virtio spec, but considering it may take a while maybe we can first introduce a new uAPI/ioctl for that.

Note that the stop doesn't just work for virtqueue but others like, e.g config space. But considering we don't have config interrupt support right now, we're probably fine.

Checking the driver, it looks to me only the IFCVF's set_vq_ready() is problematic, Ling Shan, please have a check. And we probably need a workaround for vp_vdpa as well.

Anyhow, this seems to be better than reset. So for 7.1:

Quoted 49 lines
---
 hw/virtio/vhost-vdpa.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
hw/virtio/vhost-vdpa.c +4 -5
index c5ed7a3..d858b4f 100644
@@ -719,14 +719,14 @@ static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx)
     return idx;
 }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, unsigned int ready)
 {
     int i;
     trace_vhost_vdpa_set_vring_ready(dev);
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_vring_state state = {
             .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
         };
         vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
     }
@@ -1088,8 +1088,9 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
         if (unlikely(!ok)) {
             return -1;
         }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
     } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
         ok = vhost_vdpa_svqs_stop(dev);
         if (unlikely(!ok)) {
             return -1;
@@ -1105,7 +1106,6 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
         memory_listener_register(&v->listener, &address_space_memory);
         return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
     } else {
-        vhost_vdpa_reset_device(dev);
         vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
                                    VIRTIO_CONFIG_S_DRIVER);
         memory_listener_unregister(&v->listener);
--

1.8.3.1

#2 lore raw
Quoted 21 lines · Jason Wang

Adding Eugenio, and Ling Shan.

Quoted 14 lines · On Wed, Mar 23, 2022 at 4:58 PM <[email protected]> wrote:

From: Michael Qiu <[email protected]>

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset twice, this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_dev_stop(), qemu tries to stop the device, then stop the queue: vhost_virtqueue_stop().
In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and the driver finds
that the VQ is invalied, this is the root cause.

Quoted 4 lines

Actually, device reset will be called within func release()

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

Quoted 7 lines

This is an interesting issue. Do you see a real issue except for the above warnings.

The reason we "abuse" reset is that we don't have a stop uAPI for vhost. We plan to add a status bit to stop the whole device in the virtio spec, but considering it may take a while maybe we can first introduce a new uAPI/ioctl for that.

Yep. What was missing here is a vdpa specific uAPI for per-virtqueue stop/suspend rather than spec level amendment to stop the whole device (including both vq and config space). For now we can have vDPA specific means to control the vq, something vDPA hardware vendor must support for live migration, e.g. datapath switching to shadow vq. I believe the spec amendment may follow to define a bit for virtio feature negotiation later on if needed (FWIW virtio-vdpa already does set_vq_ready(..., 0) to stop the vq).

However, there's a flaw in this patch, see below.

Quoted 47 lines

Note that the stop doesn't just work for virtqueue but others like, e.g config space. But considering we don't have config interrupt support right now, we're probably fine.

Checking the driver, it looks to me only the IFCVF's set_vq_ready() is problematic, Ling Shan, please have a check. And we probably need a workaround for vp_vdpa as well.

Anyhow, this seems to be better than reset. So for 7.1:

Quoted 34 lines
---
  hw/virtio/vhost-vdpa.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
hw/virtio/vhost-vdpa.c +4 -3
index c5ed7a3..d858b4f 100644
@@ -719,14 +719,14 @@ static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx)
      return idx;
  }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, unsigned int ready)
  {
      int i;
      trace_vhost_vdpa_set_vring_ready(dev);
      for (i = 0; i < dev->nvqs; ++i) {
          struct vhost_vring_state state = {
              .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
          };
          vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
      }
@@ -1088,8 +1088,9 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
          if (unlikely(!ok)) {
              return -1;
          }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
      } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
Quoted 8 lines

hw/virtio/vhost-vdpa.c static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started) · line 1108

Quoted 8 lines
hw/virtio/vhost-vdpa.c +0 -1
          ok = vhost_vdpa_svqs_stop(dev);
          if (unlikely(!ok)) {
              return -1;
@@ -1105,7 +1106,6 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
          memory_listener_register(&v->listener, &address_space_memory);
          return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
      } else {
-        vhost_vdpa_reset_device(dev);

Unfortunately, the reset can't be be removed from here as this code path usually involves virtio reset or status change for e.g. invoked via virtio_net_set_status(... , 0). Ideally we should use the VhostOps.vhost_reset_device() to reset the vhost-vdpa device where status change is involved after vhost_dev_stop() is done, but this distinction is not there yet as of today in all of the virtio devices except vhost_user_scsi.

Alternatively we may be able to do something like below, stop the virtqueue in vhost_vdpa_get_vring_base() in the vhost_virtqueue_stop() context. Only until the hardware vq is stopped, svq can stop and unmap then vhost-vdpa would reset the device status. It kinda works, but not in a perfect way...

hw/virtio/vhost-vdpa.c +0 -0
@@ -564,14 +564,14 @@ static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx)

     return idx;
 }

-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, int enable) {
     int i;
     trace_vhost_vdpa_set_vring_ready(dev);
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_vring_state state = {
             .index = dev->vq_index + i,
-            .num = 1,
+            .num = enable,
         };
         vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
     }
@@ -641,7 +641,7 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)

     if (started) {
         vhost_vdpa_host_notifiers_init(dev);
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
     } else {
         vhost_vdpa_host_notifiers_uninit(dev, dev->nvqs);
     }
@@ -708,6 +708,9 @@ static int vhost_vdpa_get_vring_base(struct vhost_dev *dev, {
     int ret;

+    /* Deactivate the queue (best effort) */
+    vhost_vdpa_set_vring_ready(dev, 0);
+
     ret = vhost_vdpa_call(dev, VHOST_GET_VRING_BASE, ring); trace_vhost_vdpa_get_vring_base(dev, ring->index, ring->num); return ret;

hw/virtio/vhost.c +0 -0
index 437347a..2e917d8 100644
@@ -1832,15 +1832,15 @@ void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)

     /* should only be called after backend is connected */ assert(hdev->vhost_ops);

-    if (hdev->vhost_ops->vhost_dev_start) {
-        hdev->vhost_ops->vhost_dev_start(hdev, false); -    }
     for (i = 0; i < hdev->nvqs; ++i) { vhost_virtqueue_stop(hdev,
                              vdev,
                              hdev->vqs + i, hdev->vq_index + i); }
+    if (hdev->vhost_ops->vhost_dev_start) {
+        hdev->vhost_ops->vhost_dev_start(hdev, false); +    }

     if (vhost_dev_has_iommu(hdev)) {
         if (hdev->vhost_ops->vhost_set_iotlb_callback) {

Regards,
-Siwei

Quoted 7 lines
Quoted 6 lines
hw/virtio/vhost-vdpa.c +0 -1
          vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
                                     VIRTIO_CONFIG_S_DRIVER);
          memory_listener_unregister(&v->listener);
--

1.8.3.1

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw
Quoted 99 lines · Michael Qiu
Quoted 86 lines · Si-Wei Liu
Quoted 21 lines · Jason Wang

Adding Eugenio,  and Ling Shan.

Quoted 14 lines · On Wed, Mar 23, 2022 at 4:58 PM <[email protected]> wrote:

From: Michael Qiu <[email protected]>

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset twice, this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_dev_stop(), qemu tries to stop the device, then stop the queue: vhost_virtqueue_stop().
In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and the driver finds
that the VQ is invalied, this is the root cause.

Quoted 4 lines

Actually, device reset will be called within func release()

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

Quoted 7 lines

This is an interesting issue. Do you see a real issue except for the above warnings.

The reason we "abuse" reset is that we don't have a stop uAPI for vhost. We plan to add a status bit to stop the whole device in the virtio spec, but considering it may take a while maybe we can first introduce a new uAPI/ioctl for that.

Yep. What was missing here is a vdpa specific uAPI for per-virtqueue stop/suspend rather than spec level amendment to stop the whole device (including both vq and config space). For now we can have vDPA specific means to control the vq, something vDPA hardware vendor must support for live migration, e.g. datapath switching to shadow vq. I believe the spec amendment may follow to define a bit for virtio feature negotiation later on if needed (FWIW virtio-vdpa already does set_vq_ready(..., 0) to stop the vq).

However, there's a flaw in this patch, see below.

Quoted 52 lines

Note that the stop doesn't just work for virtqueue but others like, e.g config space. But considering we don't have config interrupt support right now, we're probably fine.

Checking the driver, it looks to me only the IFCVF's set_vq_ready() is problematic, Ling Shan, please have a check. And we probably need a workaround for vp_vdpa as well.

Anyhow, this seems to be better than reset. So for 7.1:

Quoted 39 lines

---
  hw/virtio/vhost-vdpa.c | 8 ++++----

  1 file changed, 4 insertions(+), 4 deletions(-)
hw/virtio/vhost-vdpa.c +2 -2
index c5ed7a3..d858b4f 100644
@@ -719,14 +719,14 @@ static int vhost_vdpa_get_vq_index(struct 
 vhost_dev *dev, int idx)
       return idx;
   }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, 
 unsigned int ready)
   {
       int i;
       trace_vhost_vdpa_set_vring_ready(dev);
       for (i = 0; i < dev->nvqs; ++i) {
           struct vhost_vring_state state = {
               .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
           };

          vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state); }

hw/virtio/vhost-vdpa.c +2 -1
@@ -1088,8 +1088,9 @@ static int vhost_vdpa_dev_start(struct 
 vhost_dev *dev, bool started)
           if (unlikely(!ok)) {
               return -1;
           }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
       } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
           ok = vhost_vdpa_svqs_stop(dev);
           if (unlikely(!ok)) {
Quoted 9 lines
Quoted 7 lines

hw/virtio/vhost-vdpa.c static int vhost_vdpa_dev_start(struct · line 1109

Quoted 7 lines

              return -1;

hw/virtio/vhost-vdpa.c +0 -1
@@ -1105,7 +1106,6 @@ static int vhost_vdpa_dev_start(struct 
 vhost_dev *dev, bool started)
           memory_listener_register(&v->listener, &address_space_memory);
           return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
       } else {
-        vhost_vdpa_reset_device(dev);

Unfortunately, the reset can't be be removed from here as this code path usually involves virtio reset or status change for e.g. invoked via virtio_net_set_status(... , 0). Ideally we should use the VhostOps.vhost_reset_device() to reset the vhost-vdpa device where status change is involved after vhost_dev_stop() is done, but this distinction is not there yet as of today in all of the virtio devices except vhost_user_scsi.

Quoted 1 line

Actually, we may not care about virtio_net_set_status(... , 0), because in virtio_net_device_unrealize() will finnally call qemu_del_nic(),

The reset is needed because guest can write 0 to the device status register to initiate device reset while VM is running, that's a very common scenario where virtio_net_set_status(... , 0) has to be invoked. Quoting the spec:

-----------------%<-----------------

2.1.2 Device Requirements: Device Status Field
The device MUST initialize device status to 0 upon reset.
...
device_status
The driver writes the device status here (see 2.1). Writing 0 into this field resets the device.

-----------------%<-----------------

That being said, remove vhost_vdpa_reset_device() will introduce severe regression to vdpa functionality, for e.g. you may see weird error or panic once guest is rebooted as the device state may have been messed up. As indicated earlier, to fix it in a clean way it would need to involve serious code refactoring to all callers of vhost_dev_stop, and converting those which require device reset to explicitly call VhostOps.vhost_reset_device().

Quoted 7 lines

see below:

qemu_del_nic() -->qemu_cleanup_net_client() -->cleanup/vhost_vdpa_cleanup() -->qemu_close(s->vhost_vdpa.device_fd)

Quoted 5 lines

In kernel space, close() action triggered release(), release()/vhost_vdpa_release()
    --> vhost_vdpa_reset()

So it will finnally do vdpa_reset, that's why I said reset will be called twice in current qemu code.

That's a minor problem as nobody cares about the extra reset while guest is being shut off.

Regards,
-Siwei

Quoted 86 lines

Thanks,
Michael

Quoted 81 lines

Alternatively we may be able to do something like below, stop the virtqueue in vhost_vdpa_get_vring_base() in the vhost_virtqueue_stop() context. Only until the hardware vq is stopped, svq can stop and unmap then vhost-vdpa would reset the device status. It kinda works, but not in a perfect way...

hw/virtio/vhost-vdpa.c +2 -2
@@ -564,14 +564,14 @@ static int vhost_vdpa_get_vq_index(struct 
 vhost_dev *dev, int idx)
       return idx;
   }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, int 
 enable)
   {
       int i;
       trace_vhost_vdpa_set_vring_ready(dev);
       for (i = 0; i < dev->nvqs; ++i) {
           struct vhost_vring_state state = {
               .index = dev->vq_index + i,
-            .num = 1,
+            .num = enable,
           };

          vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state); }

hw/virtio/vhost-vdpa.c +1 -1
@@ -641,7 +641,7 @@ static int vhost_vdpa_dev_start(struct vhost_dev 
 *dev, bool started)
 
       if (started) {
           vhost_vdpa_host_notifiers_init(dev);
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
       } else {
           vhost_vdpa_host_notifiers_uninit(dev, dev->nvqs);

      }

hw/virtio/vhost-vdpa.c +3 -0
@@ -708,6 +708,9 @@ static int vhost_vdpa_get_vring_base(struct 
 vhost_dev *dev,
   {
       int ret;
 
+    /* Deactivate the queue (best effort) */
+    vhost_vdpa_set_vring_ready(dev, 0);
+
       ret = vhost_vdpa_call(dev, VHOST_GET_VRING_BASE, ring);
       trace_vhost_vdpa_get_vring_base(dev, ring->index, ring->num);

      return ret;

hw/virtio/vhost.c +3 -3
index 437347a..2e917d8 100644
@@ -1832,15 +1832,15 @@ void vhost_dev_stop(struct vhost_dev *hdev, 
 VirtIODevice *vdev)
       /* should only be called after backend is connected */
       assert(hdev->vhost_ops);
 
-    if (hdev->vhost_ops->vhost_dev_start) {
-        hdev->vhost_ops->vhost_dev_start(hdev, false);
-    }
       for (i = 0; i < hdev->nvqs; ++i) {
           vhost_virtqueue_stop(hdev,
                                vdev,
                                hdev->vqs + i,
                                hdev->vq_index + i);
       }
+    if (hdev->vhost_ops->vhost_dev_start) {
+        hdev->vhost_ops->vhost_dev_start(hdev, false);
+    }
 
       if (vhost_dev_has_iommu(hdev)) {

          if (hdev->vhost_ops->vhost_set_iotlb_callback) {

Regards,
-Siwei

Quoted 7 lines
Quoted 6 lines

          vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER); memory_listener_unregister(&v->listener);

Signature · 1 line
1.8.3.1

A message that is not in the archive (sent off-list or not yet received): [email protected]

#6 lore raw
Quoted 99 lines · Michael Qiu

On 2022/3/25 14:32, Si-Wei Liu Wrote:

Quoted 96 lines · Jason Wang
Quoted 21 lines

Adding Eugenio,  and Ling Shan.

Quoted 14 lines · On Wed, Mar 23, 2022 at 4:58 PM <[email protected]> wrote:

From: Michael Qiu <[email protected]>

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset twice, this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_dev_stop(), qemu tries to stop the device, then stop the queue: vhost_virtqueue_stop().
In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and the driver finds
that the VQ is invalied, this is the root cause.

Quoted 4 lines

Actually, device reset will be called within func release()

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

Quoted 7 lines

This is an interesting issue. Do you see a real issue except for the above warnings.

The reason we "abuse" reset is that we don't have a stop uAPI for vhost. We plan to add a status bit to stop the whole device in the virtio spec, but considering it may take a while maybe we can first introduce a new uAPI/ioctl for that.

Yep. What was missing here is a vdpa specific uAPI for per-virtqueue stop/suspend rather than spec level amendment to stop the whole device (including both vq and config space). For now we can have vDPA specific means to control the vq, something vDPA hardware vendor must support for live migration, e.g. datapath switching to shadow vq. I believe the spec amendment may follow to define a bit for virtio feature negotiation later on if needed (FWIW virtio-vdpa already does set_vq_ready(..., 0) to stop the vq).

However, there's a flaw in this patch, see below.

Quoted 51 lines

Note that the stop doesn't just work for virtqueue but others like, e.g config space. But considering we don't have config interrupt support right now, we're probably fine.

Checking the driver, it looks to me only the IFCVF's set_vq_ready() is problematic, Ling Shan, please have a check. And we probably need a workaround for vp_vdpa as well.

Anyhow, this seems to be better than reset. So for 7.1:

Quoted 38 lines

---
  hw/virtio/vhost-vdpa.c | 8 ++++----

  1 file changed, 4 insertions(+), 4 deletions(-)
hw/virtio/vhost-vdpa.c +2 -2
index c5ed7a3..d858b4f 100644
@@ -719,14 +719,14 @@ static int vhost_vdpa_get_vq_index(struct 
 vhost_dev *dev, int idx)
       return idx;
   }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, 
 unsigned int ready)
   {
       int i;
       trace_vhost_vdpa_set_vring_ready(dev);
       for (i = 0; i < dev->nvqs; ++i) {
           struct vhost_vring_state state = {
               .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
           };

          vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state); }

hw/virtio/vhost-vdpa.c +2 -1
@@ -1088,8 +1088,9 @@ static int vhost_vdpa_dev_start(struct 
 vhost_dev *dev, bool started)
           if (unlikely(!ok)) {
               return -1;
           }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
       } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
           ok = vhost_vdpa_svqs_stop(dev);
Quoted 8 lines

hw/virtio/vhost-vdpa.c static int vhost_vdpa_dev_start(struct · line 1109

Quoted 8 lines
hw/virtio/vhost-vdpa.c +0 -0
           if (unlikely(!ok)) {

              return -1;

hw/virtio/vhost-vdpa.c +0 -1
@@ -1105,7 +1106,6 @@ static int vhost_vdpa_dev_start(struct 
 vhost_dev *dev, bool started)
           memory_listener_register(&v->listener, &address_space_memory);
           return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
       } else {
-        vhost_vdpa_reset_device(dev);

Unfortunately, the reset can't be be removed from here as this code path usually involves virtio reset or status change for e.g. invoked via virtio_net_set_status(... , 0). Ideally we should use the VhostOps.vhost_reset_device() to reset the vhost-vdpa device where status change is involved after vhost_dev_stop() is done, but this distinction is not there yet as of today in all of the virtio devices except vhost_user_scsi.

Alternatively we may be able to do something like below, stop the virtqueue in vhost_vdpa_get_vring_base() in the vhost_virtqueue_stop() context. Only until the hardware vq is stopped, svq can stop and unmap then vhost-vdpa would reset the device status. It kinda works, but not in a perfect way...

As I indicated above, this is an less ideal way to address the issue you came across about, without losing functionality or introducing regression to the code. Ideally it'd be best to get fixed in a clean way, though that would a little more effort in code refactoring. Personally I feel that the error message you saw is somewhat benign and don't think it caused any real problem. Did you see trouble if living with the bogus error message for the moment?

Quoted 37 lines
Quoted 37 lines
hw/virtio/vhost-vdpa.c +2 -2
@@ -564,14 +564,14 @@ static int vhost_vdpa_get_vq_index(struct 
 vhost_dev *dev, int idx)
       return idx;
   }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, int 
 enable)
   {
       int i;
       trace_vhost_vdpa_set_vring_ready(dev);
       for (i = 0; i < dev->nvqs; ++i) {
           struct vhost_vring_state state = {
               .index = dev->vq_index + i,
-            .num = 1,
+            .num = enable,
           };

          vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state); }

hw/virtio/vhost-vdpa.c +1 -1
@@ -641,7 +641,7 @@ static int vhost_vdpa_dev_start(struct vhost_dev 
 *dev, bool started)
 
       if (started) {
           vhost_vdpa_host_notifiers_init(dev);
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
       } else {
           vhost_vdpa_host_notifiers_uninit(dev, dev->nvqs);

      }

hw/virtio/vhost-vdpa.c +0 -0
@@ -708,6 +708,9 @@ static int vhost_vdpa_get_vring_base(struct 
 vhost_dev *dev,
   {
       int ret;
 
Quoted 7 lines

hw/virtio/vhost-vdpa.c static int vhost_vdpa_get_vring_base(struct · line 714

Quoted 3 lines
hw/virtio/vhost-vdpa.c +3 -0
+    /* Deactivate the queue (best effort) */
+    vhost_vdpa_set_vring_ready(dev, 0);
+

I don't think it's a good idea to change the state in "get" function,

get means "read" not "write".

Well, if you look at the context of vhost_vdpa_get_vring_base(), the only caller is vhost_virtqueue_stop(). Without stopping the hardware ahead, it doesn't make sense to effectively get a used_index snapshot for resuming/restarting the vq. It might be more obvious and sensible to see that were to introduce another Vhost op to suspend the vq right before the get_vring_base() call, though I wouldn't bother doing it.

Quoted 29 lines
Quoted 16 lines
hw/virtio/vhost-vdpa.c +0 -0
       ret = vhost_vdpa_call(dev, VHOST_GET_VRING_BASE, ring);
       trace_vhost_vdpa_get_vring_base(dev, ring->index, ring->num);

      return ret;

hw/virtio/vhost.c +0 -3
index 437347a..2e917d8 100644
@@ -1832,15 +1832,15 @@ void vhost_dev_stop(struct vhost_dev *hdev, 
 VirtIODevice *vdev)
       /* should only be called after backend is connected */
       assert(hdev->vhost_ops);
 
-    if (hdev->vhost_ops->vhost_dev_start) {
-        hdev->vhost_ops->vhost_dev_start(hdev, false);
-    }

hw/virtio/vhost.c void vhost_dev_stop(struct vhost_dev *hdev, · line 1845

Quoted 10 lines
hw/virtio/vhost.c +3 -0
       for (i = 0; i < hdev->nvqs; ++i) {
           vhost_virtqueue_stop(hdev,
                                vdev,
                                hdev->vqs + i,
                                hdev->vq_index + i);
       }
+    if (hdev->vhost_ops->vhost_dev_start) {
+        hdev->vhost_ops->vhost_dev_start(hdev, false);
+    }
 

This first idea comes to me is just like this, but at last I don't choose this solution.

Quoted 3 lines

When we start a device, first we start the virtqueue then vhost_ops->vhost_dev_start.

So in stop stage, in my opinion, we should just do the opposite, do as the orignal code do. Change the sequential is a dangerous action.

I don't see any danger yet, would you please elaborate the specific problem you see? I think this sequence is as expected:

1. suspend each individual vq i.e. stop processing upcoming request, and possibly complete inflight requests  -> get_vring_base()
2. tear down virtio resources from VMM for e.g. unmap guest memory mappings, remove host notifiers, and et al
3. reset device -> vhost_vdpa_reset_device()

Regards,
-Siwei

Quoted 18 lines

Thanks,
Michael

Quoted 15 lines
hw/virtio/vhost.c +0 -0
       if (vhost_dev_has_iommu(hdev)) {

          if (hdev->vhost_ops->vhost_set_iotlb_callback) {

Regards,
-Siwei

Quoted 7 lines
Quoted 6 lines

          vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER); memory_listener_unregister(&v->listener);

Signature · 1 line
1.8.3.1
#7 lore raw
Quoted 122 lines · Si-Wei Liu
Quoted 118 lines · Michael Qiu

On 2022/3/25 14:32, Si-Wei Liu Wrote:

Quoted 115 lines · Jason Wang
Quoted 21 lines

Adding Eugenio, and Ling Shan.

Quoted 14 lines · On Wed, Mar 23, 2022 at 4:58 PM <[email protected]> wrote:

From: Michael Qiu <[email protected]>

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset twice, this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_dev_stop(), qemu tries to stop the device, then stop the queue: vhost_virtqueue_stop().
In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and the driver finds
that the VQ is invalied, this is the root cause.

Quoted 4 lines

Actually, device reset will be called within func release()

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

Quoted 7 lines

This is an interesting issue. Do you see a real issue except for the above warnings.

The reason we "abuse" reset is that we don't have a stop uAPI for vhost. We plan to add a status bit to stop the whole device in the virtio spec, but considering it may take a while maybe we can first introduce a new uAPI/ioctl for that.

Yep. What was missing here is a vdpa specific uAPI for per-virtqueue stop/suspend rather than spec level amendment to stop the whole device (including both vq and config space). For now we can have vDPA specific means to control the vq, something vDPA hardware vendor must support for live migration, e.g. datapath switching to shadow vq. I believe the spec amendment may follow to define a bit for virtio feature negotiation later on if needed (FWIW virtio-vdpa already does set_vq_ready(..., 0) to stop the vq).

However, there's a flaw in this patch, see below.

Quoted 53 lines

Note that the stop doesn't just work for virtqueue but others like, e.g config space. But considering we don't have config interrupt support right now, we're probably fine.

Checking the driver, it looks to me only the IFCVF's set_vq_ready() is problematic, Ling Shan, please have a check. And we probably need a workaround for vp_vdpa as well.

Anyhow, this seems to be better than reset. So for 7.1:

Quoted 40 lines
---
  hw/virtio/vhost-vdpa.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
hw/virtio/vhost-vdpa.c +2 -2
index c5ed7a3..d858b4f 100644
@@ -719,14 +719,14 @@ static int vhost_vdpa_get_vq_index(struct
 vhost_dev *dev, int idx)
      return idx;
  }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev,
 unsigned int ready)
  {
      int i;
      trace_vhost_vdpa_set_vring_ready(dev);
      for (i = 0; i < dev->nvqs; ++i) {
          struct vhost_vring_state state = {
              .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
          };
          vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
      }
hw/virtio/vhost-vdpa.c +2 -1
@@ -1088,8 +1088,9 @@ static int vhost_vdpa_dev_start(struct
 vhost_dev *dev, bool started)
          if (unlikely(!ok)) {
              return -1;
          }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
      } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
          ok = vhost_vdpa_svqs_stop(dev);
          if (unlikely(!ok)) {
              return -1;
Quoted 8 lines

hw/virtio/vhost-vdpa.c static int vhost_vdpa_dev_start(struct · line 1111

Quoted 8 lines
hw/virtio/vhost-vdpa.c +0 -1
@@ -1105,7 +1106,6 @@ static int vhost_vdpa_dev_start(struct
 vhost_dev *dev, bool started)
          memory_listener_register(&v->listener,
 &address_space_memory);
          return vhost_vdpa_add_status(dev,
 VIRTIO_CONFIG_S_DRIVER_OK);
      } else {
-        vhost_vdpa_reset_device(dev);

Unfortunately, the reset can't be be removed from here as this code path usually involves virtio reset or status change for e.g. invoked via virtio_net_set_status(... , 0). Ideally we should use the VhostOps.vhost_reset_device() to reset the vhost-vdpa device where status change is involved after vhost_dev_stop() is done, but this distinction is not there yet as of today in all of the virtio devices except vhost_user_scsi.

Alternatively we may be able to do something like below, stop the virtqueue in vhost_vdpa_get_vring_base() in the vhost_virtqueue_stop() context. Only until the hardware vq is stopped, svq can stop and unmap then vhost-vdpa would reset the device status. It kinda works, but not in a perfect way...

Quoted 7 lines

As I indicated above, this is an less ideal way to address the issue you came across about, without losing functionality or introducing regression to the code. Ideally it'd be best to get fixed in a clean way, though that would a little more effort in code refactoring. Personally I feel that the error message you saw is somewhat benign and don't think it caused any real problem. Did you see trouble if living with the bogus error message for the moment?

Should be fine for networking devices at least since we don't care about duplicated packets (restore last_avail_idx as used_idx).

But it might be problematic for types of devices.

Thanks

Quoted 118 lines
Quoted 38 lines
Quoted 38 lines
hw/virtio/vhost-vdpa.c +2 -2
@@ -564,14 +564,14 @@ static int vhost_vdpa_get_vq_index(struct
 vhost_dev *dev, int idx)
      return idx;
  }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, int
 enable)
  {
      int i;
      trace_vhost_vdpa_set_vring_ready(dev);
      for (i = 0; i < dev->nvqs; ++i) {
          struct vhost_vring_state state = {
              .index = dev->vq_index + i,
-            .num = 1,
+            .num = enable,
          };
          vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
      }
hw/virtio/vhost-vdpa.c +1 -1
@@ -641,7 +641,7 @@ static int vhost_vdpa_dev_start(struct vhost_dev
 *dev, bool started)
 
      if (started) {
          vhost_vdpa_host_notifiers_init(dev);
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
      } else {
          vhost_vdpa_host_notifiers_uninit(dev, dev->nvqs);
      }
hw/virtio/vhost-vdpa.c +0 -0
@@ -708,6 +708,9 @@ static int vhost_vdpa_get_vring_base(struct
 vhost_dev *dev,
  {
      int ret;
 
Quoted 7 lines

hw/virtio/vhost-vdpa.c static int vhost_vdpa_get_vring_base(struct · line 714

Quoted 3 lines
hw/virtio/vhost-vdpa.c +3 -0
+    /* Deactivate the queue (best effort) */
+    vhost_vdpa_set_vring_ready(dev, 0);
+

I don't think it's a good idea to change the state in "get" function,

get means "read" not "write".

Well, if you look at the context of vhost_vdpa_get_vring_base(), the only caller is vhost_virtqueue_stop(). Without stopping the hardware ahead, it doesn't make sense to effectively get a used_index snapshot for resuming/restarting the vq. It might be more obvious and sensible to see that were to introduce another Vhost op to suspend the vq right before the get_vring_base() call, though I wouldn't bother doing it.

Quoted 30 lines
Quoted 16 lines
hw/virtio/vhost-vdpa.c +0 -0
      ret = vhost_vdpa_call(dev, VHOST_GET_VRING_BASE, ring);
      trace_vhost_vdpa_get_vring_base(dev, ring->index, ring->num);
      return ret;
hw/virtio/vhost.c +0 -3
index 437347a..2e917d8 100644
@@ -1832,15 +1832,15 @@ void vhost_dev_stop(struct vhost_dev *hdev,
 VirtIODevice *vdev)
      /* should only be called after backend is connected */
      assert(hdev->vhost_ops);
 
-    if (hdev->vhost_ops->vhost_dev_start) {
-        hdev->vhost_ops->vhost_dev_start(hdev, false);
-    }

hw/virtio/vhost.c void vhost_dev_stop(struct vhost_dev *hdev, · line 1845

Quoted 10 lines
hw/virtio/vhost.c +3 -0
      for (i = 0; i < hdev->nvqs; ++i) {
          vhost_virtqueue_stop(hdev,
                               vdev,
                               hdev->vqs + i,
                               hdev->vq_index + i);
      }
+    if (hdev->vhost_ops->vhost_dev_start) {
+        hdev->vhost_ops->vhost_dev_start(hdev, false);
+    }
 

This first idea comes to me is just like this, but at last I don't choose this solution.

Quoted 5 lines

When we start a device, first we start the virtqueue then vhost_ops->vhost_dev_start.

So in stop stage, in my opinion, we should just do the opposite, do as the orignal code do. Change the sequential is a dangerous action.

I don't see any danger yet, would you please elaborate the specific problem you see? I think this sequence is as expected:

1. suspend each individual vq i.e. stop processing upcoming request, and possibly complete inflight requests -> get_vring_base()
2. tear down virtio resources from VMM for e.g. unmap guest memory mappings, remove host notifiers, and et al
3. reset device -> vhost_vdpa_reset_device()

Regards,
-Siwei

Quoted 18 lines

Thanks,
Michael

Quoted 15 lines
hw/virtio/vhost.c +0 -0
      if (vhost_dev_has_iommu(hdev)) {
          if (hdev->vhost_ops->vhost_set_iotlb_callback) {

Regards,
-Siwei

Quoted 7 lines
Quoted 6 lines

vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |

VIRTIO_CONFIG_S_DRIVER);
          memory_listener_unregister(&v->listener);

--
1.8.3.1

#8 lore raw
Quoted 131 lines · Jason Wang
Quoted 122 lines · Si-Wei Liu
Quoted 118 lines · Michael Qiu

On 2022/3/25 14:32, Si-Wei Liu Wrote:

Quoted 115 lines · Jason Wang
Quoted 21 lines

Adding Eugenio, and Ling Shan.

Quoted 14 lines · On Wed, Mar 23, 2022 at 4:58 PM <[email protected]> wrote:

From: Michael Qiu <[email protected]>

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset twice, this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_dev_stop(), qemu tries to stop the device, then stop the queue: vhost_virtqueue_stop().
In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and the driver finds
that the VQ is invalied, this is the root cause.

Quoted 4 lines

Actually, device reset will be called within func release()

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

Quoted 7 lines

This is an interesting issue. Do you see a real issue except for the above warnings.

The reason we "abuse" reset is that we don't have a stop uAPI for vhost. We plan to add a status bit to stop the whole device in the virtio spec, but considering it may take a while maybe we can first introduce a new uAPI/ioctl for that.

Yep. What was missing here is a vdpa specific uAPI for per-virtqueue stop/suspend rather than spec level amendment to stop the whole device (including both vq and config space). For now we can have vDPA specific means to control the vq, something vDPA hardware vendor must support for live migration, e.g. datapath switching to shadow vq. I believe the spec amendment may follow to define a bit for virtio feature negotiation later on if needed (FWIW virtio-vdpa already does set_vq_ready(..., 0) to stop the vq).

However, there's a flaw in this patch, see below.

Quoted 53 lines

Note that the stop doesn't just work for virtqueue but others like, e.g config space. But considering we don't have config interrupt support right now, we're probably fine.

Checking the driver, it looks to me only the IFCVF's set_vq_ready() is problematic, Ling Shan, please have a check. And we probably need a workaround for vp_vdpa as well.

Anyhow, this seems to be better than reset. So for 7.1:

Quoted 40 lines
---
   hw/virtio/vhost-vdpa.c | 8 ++++----
   1 file changed, 4 insertions(+), 4 deletions(-)
hw/virtio/vhost-vdpa.c +2 -2
index c5ed7a3..d858b4f 100644
@@ -719,14 +719,14 @@ static int vhost_vdpa_get_vq_index(struct
 vhost_dev *dev, int idx)
       return idx;
   }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev,
 unsigned int ready)
   {
       int i;
       trace_vhost_vdpa_set_vring_ready(dev);
       for (i = 0; i < dev->nvqs; ++i) {
           struct vhost_vring_state state = {
               .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
           };
           vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
       }
hw/virtio/vhost-vdpa.c +2 -1
@@ -1088,8 +1088,9 @@ static int vhost_vdpa_dev_start(struct
 vhost_dev *dev, bool started)
           if (unlikely(!ok)) {
               return -1;
           }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
       } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
           ok = vhost_vdpa_svqs_stop(dev);
           if (unlikely(!ok)) {
               return -1;
Quoted 8 lines

hw/virtio/vhost-vdpa.c static int vhost_vdpa_dev_start(struct · line 1111

Quoted 8 lines
hw/virtio/vhost-vdpa.c +0 -1
@@ -1105,7 +1106,6 @@ static int vhost_vdpa_dev_start(struct
 vhost_dev *dev, bool started)
           memory_listener_register(&v->listener,
 &address_space_memory);
           return vhost_vdpa_add_status(dev,
 VIRTIO_CONFIG_S_DRIVER_OK);
       } else {
-        vhost_vdpa_reset_device(dev);

Unfortunately, the reset can't be be removed from here as this code path usually involves virtio reset or status change for e.g. invoked via virtio_net_set_status(... , 0). Ideally we should use the VhostOps.vhost_reset_device() to reset the vhost-vdpa device where status change is involved after vhost_dev_stop() is done, but this distinction is not there yet as of today in all of the virtio devices except vhost_user_scsi.

Alternatively we may be able to do something like below, stop the virtqueue in vhost_vdpa_get_vring_base() in the vhost_virtqueue_stop() context. Only until the hardware vq is stopped, svq can stop and unmap then vhost-vdpa would reset the device status. It kinda works, but not in a perfect way...

Quoted 7 lines

As I indicated above, this is an less ideal way to address the issue you came across about, without losing functionality or introducing regression to the code. Ideally it'd be best to get fixed in a clean way, though that would a little more effort in code refactoring. Personally I feel that the error message you saw is somewhat benign and don't think it caused any real problem. Did you see trouble if living with the bogus error message for the moment?

Quoted 7 lines

Should be fine for networking devices at least since we don't care about duplicated packets (restore last_avail_idx as used_idx).

But it might be problematic for types of devices.

Thanks

I find that the second reset does not triggered by device qemu_del_nic, although it will trigger device reset, the rootcause is that,
we try to stop the vhost devices in each queue pair and control queue, each queue pair or control queue needs one vhost device. And all vhost devices share one kernel vdpa device.

In my case, only enable 1 datapath queue pair and one control queue.

the first time vhost_net_stop_one is for datapath queue pair, but at this time the backend kernel vdpa device has been reset, in the second loop, when stop control queue backend vhost device, the queue already disappeared.

I will send V2 to fix the issue, to reset the device in the last vhost device stop stage.

Quoted 121 lines
Quoted 119 lines
Quoted 39 lines
Quoted 39 lines
hw/virtio/vhost-vdpa.c +2 -2
@@ -564,14 +564,14 @@ static int vhost_vdpa_get_vq_index(struct
 vhost_dev *dev, int idx)
       return idx;
   }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, int
 enable)
   {
       int i;
       trace_vhost_vdpa_set_vring_ready(dev);
       for (i = 0; i < dev->nvqs; ++i) {
           struct vhost_vring_state state = {
               .index = dev->vq_index + i,
-            .num = 1,
+            .num = enable,
           };
           vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
       }
hw/virtio/vhost-vdpa.c +1 -1
@@ -641,7 +641,7 @@ static int vhost_vdpa_dev_start(struct vhost_dev
 *dev, bool started)
 
       if (started) {
           vhost_vdpa_host_notifiers_init(dev);
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
       } else {
           vhost_vdpa_host_notifiers_uninit(dev, dev->nvqs);
       }
hw/virtio/vhost-vdpa.c +0 -0
@@ -708,6 +708,9 @@ static int vhost_vdpa_get_vring_base(struct
 vhost_dev *dev,
   {
       int ret;
 
Quoted 7 lines

hw/virtio/vhost-vdpa.c static int vhost_vdpa_get_vring_base(struct · line 714

Quoted 3 lines
hw/virtio/vhost-vdpa.c +3 -0
+    /* Deactivate the queue (best effort) */
+    vhost_vdpa_set_vring_ready(dev, 0);
+

I don't think it's a good idea to change the state in "get" function,

get means "read" not "write".

Well, if you look at the context of vhost_vdpa_get_vring_base(), the only caller is vhost_virtqueue_stop(). Without stopping the hardware ahead, it doesn't make sense to effectively get a used_index snapshot for resuming/restarting the vq. It might be more obvious and sensible to see that were to introduce another Vhost op to suspend the vq right before the get_vring_base() call, though I wouldn't bother doing it.

Quoted 30 lines
Quoted 16 lines
hw/virtio/vhost-vdpa.c +0 -0
       ret = vhost_vdpa_call(dev, VHOST_GET_VRING_BASE, ring);
       trace_vhost_vdpa_get_vring_base(dev, ring->index, ring->num);
       return ret;
hw/virtio/vhost.c +0 -3
index 437347a..2e917d8 100644
@@ -1832,15 +1832,15 @@ void vhost_dev_stop(struct vhost_dev *hdev,
 VirtIODevice *vdev)
       /* should only be called after backend is connected */
       assert(hdev->vhost_ops);
 
-    if (hdev->vhost_ops->vhost_dev_start) {
-        hdev->vhost_ops->vhost_dev_start(hdev, false);
-    }

hw/virtio/vhost.c void vhost_dev_stop(struct vhost_dev *hdev, · line 1845

Quoted 10 lines
hw/virtio/vhost.c +3 -0
       for (i = 0; i < hdev->nvqs; ++i) {
           vhost_virtqueue_stop(hdev,
                                vdev,
                                hdev->vqs + i,
                                hdev->vq_index + i);
       }
+    if (hdev->vhost_ops->vhost_dev_start) {
+        hdev->vhost_ops->vhost_dev_start(hdev, false);
+    }
 

This first idea comes to me is just like this, but at last I don't choose this solution.

Quoted 5 lines

When we start a device, first we start the virtqueue then vhost_ops->vhost_dev_start.

So in stop stage, in my opinion, we should just do the opposite, do as the orignal code do. Change the sequential is a dangerous action.

I don't see any danger yet, would you please elaborate the specific problem you see? I think this sequence is as expected:

1. suspend each individual vq i.e. stop processing upcoming request, and possibly complete inflight requests -> get_vring_base()
2. tear down virtio resources from VMM for e.g. unmap guest memory mappings, remove host notifiers, and et al
3. reset device -> vhost_vdpa_reset_device()

Regards,
-Siwei

Quoted 18 lines

Thanks,
Michael

Quoted 15 lines
hw/virtio/vhost.c +0 -0
       if (vhost_dev_has_iommu(hdev)) {
           if (hdev->vhost_ops->vhost_set_iotlb_callback) {

Regards,
-Siwei

Quoted 7 lines
Quoted 6 lines

vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |

VIRTIO_CONFIG_S_DRIVER);
           memory_listener_unregister(&v->listener);

--
1.8.3.1

#9 lore raw

PATCH v2 1/1 vdpa: reset the backend device in stage of stop last vhost device

In reply to [email protected]

From: Michael Qiu <[email protected]>

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset many times(with 1 datapath queue pair and one control queue, triggered 3 times), this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_net_stop(), it will stop all vhost device bind to this virtio device, and in vhost_dev_stop(), qemu tries to stop the device , then stop the queue: vhost_virtqueue_stop().

In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and in next loop(stop other vhost backends), qemu try to stop the queue corresponding to the vhost backend,
the driver finds that the VQ is invalied, this is the root cause.

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

and implement a new function vhost_dev_reset, only reset backend device when the last vhost stop triggerd.

---
v2 --> v1:
implement a new function vhost_dev_reset,
reset the backend kernel device at last.

---
 hw/net/vhost_net.c        | 22 +++++++++++++++++++---
 hw/virtio/vhost-vdpa.c    |  8 ++++----
 hw/virtio/vhost.c         | 16 +++++++++++++++-
 include/hw/virtio/vhost.h |  1 +
 4 files changed, 39 insertions(+), 8 deletions(-)
hw/net/vhost_net.c +19 -3
index 30379d2..3cdf6a4 100644
@@ -299,7 +299,7 @@ fail_notifiers:
 }
 
 static void vhost_net_stop_one(struct vhost_net *net,
-                               VirtIODevice *dev)
+                               VirtIODevice *dev, bool reset)
 {
     struct vhost_vring_file file = { .fd = -1 };
 
@@ -313,6 +313,11 @@ static void vhost_net_stop_one(struct vhost_net *net,
         net->nc->info->poll(net->nc, true);
     }
     vhost_dev_stop(&net->dev, dev);
+
+    if (reset) {
+        vhost_dev_reset(&net->dev);
+    }
+
     vhost_dev_disable_notifiers(&net->dev, dev);
 }
 
@@ -391,7 +396,12 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
 err_start:
     while (--i >= 0) {
         peer = qemu_get_peer(ncs , i);
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        if (i == 0) {
+            vhost_net_stop_one(get_vhost_net(peer), dev, true);
+        } else {
+            vhost_net_stop_one(get_vhost_net(peer), dev, false);
+        }
     }
     e = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
     if (e < 0) {
@@ -420,7 +430,13 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
         } else {
             peer = qemu_get_peer(ncs, n->max_queue_pairs);
         }
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        /* We only reset backend device during the last vhost */
+        if (i == nvhosts - 1) {
+            vhost_net_stop_one(get_vhost_net(peer), dev, true);
+        } else {
+            vhost_net_stop_one(get_vhost_net(peer), dev, false);
+        }
     }
 
     r = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
hw/virtio/vhost-vdpa.c +4 -4
index c5ed7a3..d858b4f 100644
@@ -719,14 +719,14 @@ static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx)
     return idx;
 }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, unsigned int ready)
 {
     int i;
     trace_vhost_vdpa_set_vring_ready(dev);
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_vring_state state = {
             .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
         };
         vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
     }
@@ -1088,8 +1088,9 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
         if (unlikely(!ok)) {
             return -1;
         }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
     } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
         ok = vhost_vdpa_svqs_stop(dev);
         if (unlikely(!ok)) {
             return -1;
@@ -1105,7 +1106,6 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
         memory_listener_register(&v->listener, &address_space_memory);
         return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
     } else {
-        vhost_vdpa_reset_device(dev);
         vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
                                    VIRTIO_CONFIG_S_DRIVER);
         memory_listener_unregister(&v->listener);
hw/virtio/vhost.c +15 -1
index b643f42..6d9b4a3 100644
@@ -1820,7 +1820,7 @@ fail_features:
 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
 {
     int i;
-
+    printf("vhost_dev_stop test\n");
     /* should only be called after backend is connected */
     assert(hdev->vhost_ops);
 
@@ -1854,3 +1854,17 @@ int vhost_net_set_backend(struct vhost_dev *hdev,
 
     return -ENOSYS;
 }
+
+int vhost_dev_reset(struct vhost_dev *hdev)
+{
+    int ret = 0;
+
+    /* should only be called after backend is connected */
+    assert(hdev->vhost_ops);
+
+    if (hdev->vhost_ops->vhost_reset_device) {
+        ret = hdev->vhost_ops->vhost_reset_device(hdev);
+    }
+
+    return ret;
+}
include/hw/virtio/vhost.h +1 -0
index 58a73e7..b8b7c20 100644
@@ -114,6 +114,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
 void vhost_dev_cleanup(struct vhost_dev *hdev);
 int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
+int vhost_dev_reset(struct vhost_dev *hdev);
 int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
 void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
 
Signature · 1 line
1.8.3.1
#10 lore raw

Re: [PATCH v2] vdpa: reset the backend device in stage of stop last vhost device

On Wed, Mar 30, 2022 at 06:02:41AM -0400, [email protected] wrote:

It's an empty patch.

Signature · 1 line
MST
Acked-by Jason Wang
#11 lore raw

Re: [PATCH v2] vdpa: reset the backend device in stage of stop last vhost device

Michael,

Others has already received the patch, don't know why. Anyway, I will repost another version(V3).

Here is the V2 patch, see below:

From: Michael Qiu <[email protected]>

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset many times(with 1 datapath queue pair and one control queue, triggered 3 times), this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_net_stop(), it will stop all vhost device bind to this virtio device, and in vhost_dev_stop(), qemu tries to stop the device , then stop the queue: vhost_virtqueue_stop().

In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and in next loop(stop other vhost backends), qemu try to stop the queue corresponding to the vhost backend,
the driver finds that the VQ is invalied, this is the root cause.

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

and implement a new function vhost_dev_reset, only reset backend device when the last vhost stop triggerd.

---
v2 --> v1:
implement a new function vhost_dev_reset,
reset the backend kernel device at last.

---
 hw/net/vhost_net.c        | 22 +++++++++++++++++++---
 hw/virtio/vhost-vdpa.c    |  8 ++++----
 hw/virtio/vhost.c         | 16 +++++++++++++++-
 include/hw/virtio/vhost.h |  1 +
 4 files changed, 39 insertions(+), 8 deletions(-)
hw/net/vhost_net.c +19 -3
index 30379d2..3cdf6a4 100644
@@ -299,7 +299,7 @@ fail_notifiers:
 }
 
 static void vhost_net_stop_one(struct vhost_net *net,
-                               VirtIODevice *dev)
+                               VirtIODevice *dev, bool reset)
 {
     struct vhost_vring_file file = { .fd = -1 };
 
@@ -313,6 +313,11 @@ static void vhost_net_stop_one(struct vhost_net *net,
         net->nc->info->poll(net->nc, true);
     }
     vhost_dev_stop(&net->dev, dev);
+
+    if (reset) {
+        vhost_dev_reset(&net->dev);
+    }
+
     vhost_dev_disable_notifiers(&net->dev, dev);
 }
 
@@ -391,7 +396,12 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
 err_start:
     while (--i >= 0) {
         peer = qemu_get_peer(ncs , i);
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        if (i == 0) {
+            vhost_net_stop_one(get_vhost_net(peer), dev, true);
+        } else {
+            vhost_net_stop_one(get_vhost_net(peer), dev, false);
+        }
     }
     e = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
     if (e < 0) {
@@ -420,7 +430,13 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
         } else {
             peer = qemu_get_peer(ncs, n->max_queue_pairs);
         }
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        /* We only reset backend device during the last vhost */
+        if (i == nvhosts - 1) {
+            vhost_net_stop_one(get_vhost_net(peer), dev, true);
+        } else {
+            vhost_net_stop_one(get_vhost_net(peer), dev, false);
+        }
     }
 
     r = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
hw/virtio/vhost-vdpa.c +4 -4
index c5ed7a3..d858b4f 100644
@@ -719,14 +719,14 @@ static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx)
     return idx;
 }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, unsigned int ready)
 {
     int i;
     trace_vhost_vdpa_set_vring_ready(dev);
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_vring_state state = {
             .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
         };
         vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
     }
@@ -1088,8 +1088,9 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
         if (unlikely(!ok)) {
             return -1;
         }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
     } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
         ok = vhost_vdpa_svqs_stop(dev);
         if (unlikely(!ok)) {
             return -1;
@@ -1105,7 +1106,6 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
         memory_listener_register(&v->listener, &address_space_memory);
         return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
     } else {
-        vhost_vdpa_reset_device(dev);
         vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
                                    VIRTIO_CONFIG_S_DRIVER);
         memory_listener_unregister(&v->listener);
hw/virtio/vhost.c +15 -1
index b643f42..6d9b4a3 100644
@@ -1820,7 +1820,7 @@ fail_features:
 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
 {
     int i;
-
+    printf("vhost_dev_stop test\n");
     /* should only be called after backend is connected */
     assert(hdev->vhost_ops);
 
@@ -1854,3 +1854,17 @@ int vhost_net_set_backend(struct vhost_dev *hdev,
 
     return -ENOSYS;
 }
+
+int vhost_dev_reset(struct vhost_dev *hdev)
+{
+    int ret = 0;
+
+    /* should only be called after backend is connected */
+    assert(hdev->vhost_ops);
+
+    if (hdev->vhost_ops->vhost_reset_device) {
+        ret = hdev->vhost_ops->vhost_reset_device(hdev);
+    }
+
+    return ret;
+}
include/hw/virtio/vhost.h +1 -0
index 58a73e7..b8b7c20 100644
@@ -114,6 +114,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
 void vhost_dev_cleanup(struct vhost_dev *hdev);
 int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
+int vhost_dev_reset(struct vhost_dev *hdev);
 int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice 

*vdev);
void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice
*vdev);

Signature · 1 line
1.8.3.1
Quoted 4 lines · Michael S. Tsirkin

On Wed, Mar 30, 2022 at 06:02:41AM -0400, [email protected] wrote:

It's an empty patch.

#12 lore raw

Re: [PATCH v2] vdpa: reset the backend device in stage of stop last vhost device

In reply to [email protected]
Quoted 78 lines · On 3/30/2022 3:02 AM, [email protected] wrote:

From: Michael Qiu <[email protected]>

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset many times(with 1 datapath queue pair and one control queue, triggered 3 times), this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_net_stop(), it will stop all vhost device bind to this virtio device, and in vhost_dev_stop(), qemu tries to stop the device , then stop the queue: vhost_virtqueue_stop().

In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and in next loop(stop other vhost backends), qemu try to stop the queue corresponding to the vhost backend,
the driver finds that the VQ is invalied, this is the root cause.

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

and implement a new function vhost_dev_reset, only reset backend device when the last vhost stop triggerd.

---
v2 --> v1:
implement a new function vhost_dev_reset,
reset the backend kernel device at last.

---
  hw/net/vhost_net.c        | 22 +++++++++++++++++++---
  hw/virtio/vhost-vdpa.c    |  8 ++++----
  hw/virtio/vhost.c         | 16 +++++++++++++++-
  include/hw/virtio/vhost.h |  1 +
  4 files changed, 39 insertions(+), 8 deletions(-)
hw/net/vhost_net.c +12 -2
index 30379d2..3cdf6a4 100644
@@ -299,7 +299,7 @@ fail_notifiers:
  }
 
  static void vhost_net_stop_one(struct vhost_net *net,
-                               VirtIODevice *dev)
+                               VirtIODevice *dev, bool reset)
  {
      struct vhost_vring_file file = { .fd = -1 };
 
@@ -313,6 +313,11 @@ static void vhost_net_stop_one(struct vhost_net *net,
          net->nc->info->poll(net->nc, true);
      }
      vhost_dev_stop(&net->dev, dev);
+
+    if (reset) {
+        vhost_dev_reset(&net->dev);
+    }
+
      vhost_dev_disable_notifiers(&net->dev, dev);
  }
 
@@ -391,7 +396,12 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
  err_start:
      while (--i >= 0) {
          peer = qemu_get_peer(ncs , i);
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        if (i == 0) {
+            vhost_net_stop_one(get_vhost_net(peer), dev, true);
+        } else {
+            vhost_net_stop_one(get_vhost_net(peer), dev, false);
+        }
      }
      e = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
      if (e < 0) {

hw/net/vhost_net.c void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs, · line 435

Quoted 7 lines
hw/net/vhost_net.c +3 -1
@@ -420,7 +430,13 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
          } else {
              peer = qemu_get_peer(ncs, n->max_queue_pairs);
          }
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        /* We only reset backend device during the last vhost */
+        if (i == nvhosts - 1) {

I wonder if there's any specific reason to position device reset in the for loop given that there's no virtqueue level reset? Wouldn't it be cleaner to reset the device at the end of vhost_net_stop() before return? you may use qemu_get_peer(ncs, 0) without hassle? Noted the vhost_ops->vhost_reset_device op is per device rather per vq.

Quoted 40 lines
hw/net/vhost_net.c +4 -0
+            vhost_net_stop_one(get_vhost_net(peer), dev, true);
+        } else {
+            vhost_net_stop_one(get_vhost_net(peer), dev, false);
+        }
      }
 
      r = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
hw/virtio/vhost-vdpa.c +4 -3
index c5ed7a3..d858b4f 100644
@@ -719,14 +719,14 @@ static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx)
      return idx;
  }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, unsigned int ready)
  {
      int i;
      trace_vhost_vdpa_set_vring_ready(dev);
      for (i = 0; i < dev->nvqs; ++i) {
          struct vhost_vring_state state = {
              .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
          };
          vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
      }
@@ -1088,8 +1088,9 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
          if (unlikely(!ok)) {
              return -1;
          }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
      } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
          ok = vhost_vdpa_svqs_stop(dev);
          if (unlikely(!ok)) {
              return -1;

hw/virtio/vhost-vdpa.c static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started) · line 1110

Quoted 6 lines
hw/virtio/vhost-vdpa.c +0 -1
@@ -1105,7 +1106,6 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
          memory_listener_register(&v->listener, &address_space_memory);
          return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
      } else {
-        vhost_vdpa_reset_device(dev);
          vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
                                     VIRTIO_CONFIG_S_DRIVER);

Here's another issue (regression) got to address - the added S_ACKNOWLEDGE | S_DRIVER bits will be cleared right away by the follow-up reset in vhost_net_stop_one(... , true), which in turn will cause virtio fail to initialize e.g. vhost_vdpa_set_features() will fail to set VIRTIO_CONFIG_S_FEATURES_OK.

Ideally the status bit should be set whenever the corresponding status bit is set by virtio_net from virtio_net_vhost_status(), or practically it can be done at the very beginning of vhost_dev_start(), for e.g. the first call before vhost_dev_set_features(). For this purpose, you may consider adding another vhost_init_device op, which is symmetric to vhost_ops->vhost_reset_device in the vhost_net_stop() path.

Thanks,
-Siwei

Quoted 44 lines
hw/virtio/vhost-vdpa.c +0 -0
          memory_listener_unregister(&v->listener);
hw/virtio/vhost.c +15 -1
index b643f42..6d9b4a3 100644
@@ -1820,7 +1820,7 @@ fail_features:
  void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
  {
      int i;
-
+    printf("vhost_dev_stop test\n");
      /* should only be called after backend is connected */
      assert(hdev->vhost_ops);
 
@@ -1854,3 +1854,17 @@ int vhost_net_set_backend(struct vhost_dev *hdev,
 
      return -ENOSYS;
  }
+
+int vhost_dev_reset(struct vhost_dev *hdev)
+{
+    int ret = 0;
+
+    /* should only be called after backend is connected */
+    assert(hdev->vhost_ops);
+
+    if (hdev->vhost_ops->vhost_reset_device) {
+        ret = hdev->vhost_ops->vhost_reset_device(hdev);
+    }
+
+    return ret;
+}
include/hw/virtio/vhost.h +1 -0
index 58a73e7..b8b7c20 100644
@@ -114,6 +114,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
  void vhost_dev_cleanup(struct vhost_dev *hdev);
  int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
  void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
+int vhost_dev_reset(struct vhost_dev *hdev);
  int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
  void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
 
#13 lore raw

Re: [PATCH v2] vdpa: reset the backend device in stage of stop last vhost device

Quoted 81 lines · Si-Wei Liu
Quoted 78 lines · On 3/30/2022 3:02 AM, [email protected] wrote:

From: Michael Qiu <[email protected]>

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset many times(with 1 datapath queue pair and one control queue, triggered 3 times), this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_net_stop(), it will stop all vhost device bind to this virtio device, and in vhost_dev_stop(), qemu tries to stop the device , then stop the queue: vhost_virtqueue_stop().

In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and in next loop(stop other vhost backends), qemu try to stop the queue corresponding to the vhost backend, the driver finds that the VQ is invalied, this is the root cause.

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

and implement a new function vhost_dev_reset, only reset backend device when the last vhost stop triggerd.

---
v2 --> v1:
    implement a new function vhost_dev_reset,
    reset the backend kernel device at last.

---
  hw/net/vhost_net.c        | 22 +++++++++++++++++++--- hw/virtio/vhost-vdpa.c    |  8 ++++---- hw/virtio/vhost.c         | 16 +++++++++++++++- include/hw/virtio/vhost.h |  1 +

  4 files changed, 39 insertions(+), 8 deletions(-)
hw/net/vhost_net.c +6 -1
index 30379d2..3cdf6a4 100644
@@ -299,7 +299,7 @@ fail_notifiers:
   }
   static void vhost_net_stop_one(struct vhost_net *net,
-                               VirtIODevice *dev)
+                               VirtIODevice *dev, bool reset)
   {
       struct vhost_vring_file file = { .fd = -1 };
@@ -313,6 +313,11 @@ static void vhost_net_stop_one(struct vhost_net 
 *net,
           net->nc->info->poll(net->nc, true);
       }
       vhost_dev_stop(&net->dev, dev);
+
+    if (reset) {
+        vhost_dev_reset(&net->dev);
+    }
+
       vhost_dev_disable_notifiers(&net->dev, dev);
   }
@@ -391,7 +396,12 @@ int vhost_net_start(VirtIODevice *dev, 
 NetClientState *ncs,

  err_start:
      while (--i >= 0) {
          peer = qemu_get_peer(ncs , i);
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        if (i == 0) {
+            vhost_net_stop_one(get_vhost_net(peer), dev, true); +        } else {
+            vhost_net_stop_one(get_vhost_net(peer), dev, false); +        }
      }
      e = k->set_guest_notifiers(qbus->parent, total_notifiers, false); if (e < 0) {

hw/net/vhost_net.c +0 -0
@@ -420,7 +430,13 @@ void vhost_net_stop(VirtIODevice *dev, 
 NetClientState *ncs,
Quoted 8 lines

hw/net/vhost_net.c void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs, · line 435

Quoted 7 lines
hw/net/vhost_net.c +3 -1
           } else {
               peer = qemu_get_peer(ncs, n->max_queue_pairs);
           }
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        /* We only reset backend device during the last vhost */
+        if (i == nvhosts - 1) {

I wonder if there's any specific reason to position device reset in the for loop given that there's no virtqueue level reset? Wouldn't it be cleaner to reset the device at the end of vhost_net_stop() before return? you may use qemu_get_peer(ncs, 0) without hassle? Noted the vhost_ops->vhost_reset_device op is per device rather per vq.

OK, it's a good way to do reset at the end of vhost_net_stop(), I will change it in next version.

Quoted 51 lines
Quoted 47 lines
hw/net/vhost_net.c +4 -0
+            vhost_net_stop_one(get_vhost_net(peer), dev, true);
+        } else {
+            vhost_net_stop_one(get_vhost_net(peer), dev, false);
+        }
       }
       r = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
hw/virtio/vhost-vdpa.c +2 -2
index c5ed7a3..d858b4f 100644
@@ -719,14 +719,14 @@ static int vhost_vdpa_get_vq_index(struct 
 vhost_dev *dev, int idx)
       return idx;
   }
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, unsigned 
 int ready)
   {
       int i;
       trace_vhost_vdpa_set_vring_ready(dev);
       for (i = 0; i < dev->nvqs; ++i) {
           struct vhost_vring_state state = {
               .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
           };
           vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);

      }

hw/virtio/vhost-vdpa.c +2 -1
@@ -1088,8 +1088,9 @@ static int vhost_vdpa_dev_start(struct vhost_dev 
 *dev, bool started)
           if (unlikely(!ok)) {
               return -1;
           }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
       } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
           ok = vhost_vdpa_svqs_stop(dev);
           if (unlikely(!ok)) {

              return -1;

hw/virtio/vhost-vdpa.c +0 -1
@@ -1105,7 +1106,6 @@ static int vhost_vdpa_dev_start(struct vhost_dev 
 *dev, bool started)
           memory_listener_register(&v->listener, &address_space_memory);
           return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
       } else {
-        vhost_vdpa_reset_device(dev);
Quoted 2 lines

          vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER);

Here's another issue (regression) got to address - the added S_ACKNOWLEDGE | S_DRIVER bits will be cleared right away by the follow-up reset in vhost_net_stop_one(... , true), which in turn will cause virtio fail to initialize e.g. vhost_vdpa_set_features() will fail to set VIRTIO_CONFIG_S_FEATURES_OK

Quoted 2 lines

Ideally the status bit should be set whenever the corresponding status bit is set by virtio_net from virtio_net_vhost_status(), or practically it can be done at the very beginning of vhost_dev_start(), for e.g. the first call before vhost_dev_set_features(). For this purpose, you may consider adding another vhost_init_device op, which is symmetric to vhost_ops->vhost_reset_device in the vhost_net_stop() path.

Seems only vdpa device need reset after stop, although virtio spec said need reset, but kernel doesn't reset, and if reset it has issue to reprobe virtio-net in guest, So we probely only add it after reset if it is VDPA device, for kernel and other datapath we just keep the same as before.

Thanks,
Michael

Quoted 47 lines

Thanks,
-Siwei

Quoted 42 lines

          memory_listener_unregister(&v->listener);

hw/virtio/vhost.c +15 -1
index b643f42..6d9b4a3 100644
@@ -1820,7 +1820,7 @@ fail_features:
   void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
   {
       int i;
-
+    printf("vhost_dev_stop test\n");
       /* should only be called after backend is connected */
       assert(hdev->vhost_ops);
@@ -1854,3 +1854,17 @@ int vhost_net_set_backend(struct vhost_dev *hdev,
       return -ENOSYS;
   }
+
+int vhost_dev_reset(struct vhost_dev *hdev)
+{
+    int ret = 0;
+
+    /* should only be called after backend is connected */
+    assert(hdev->vhost_ops);
+
+    if (hdev->vhost_ops->vhost_reset_device) {
+        ret = hdev->vhost_ops->vhost_reset_device(hdev);
+    }
+
+    return ret;
+}
include/hw/virtio/vhost.h +1 -0
index 58a73e7..b8b7c20 100644
@@ -114,6 +114,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void 
 *opaque,
   void vhost_dev_cleanup(struct vhost_dev *hdev);
   int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
   void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
+int vhost_dev_reset(struct vhost_dev *hdev);
   int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
   void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
#14 lore raw

Re: [PATCH v2] vdpa: reset the backend device in stage of stop last vhost device

In reply to Si-Wei Liu
Quoted 81 lines · Si-Wei Liu
Quoted 78 lines · On 3/30/2022 3:02 AM, [email protected] wrote:

From: Michael Qiu <[email protected]>

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset many times(with 1 datapath queue pair and one control queue, triggered 3 times), this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_net_stop(), it will stop all vhost device bind to this virtio device, and in vhost_dev_stop(), qemu tries to stop the device , then stop the queue: vhost_virtqueue_stop().

In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and in next loop(stop other vhost backends), qemu try to stop the queue corresponding to the vhost backend, the driver finds that the VQ is invalied, this is the root cause.

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

and implement a new function vhost_dev_reset, only reset backend device when the last vhost stop triggerd.

---
v2 --> v1:
    implement a new function vhost_dev_reset,
    reset the backend kernel device at last.

---
  hw/net/vhost_net.c        | 22 +++++++++++++++++++--- hw/virtio/vhost-vdpa.c    |  8 ++++---- hw/virtio/vhost.c         | 16 +++++++++++++++- include/hw/virtio/vhost.h |  1 +

  4 files changed, 39 insertions(+), 8 deletions(-)
hw/net/vhost_net.c +6 -1
index 30379d2..3cdf6a4 100644
@@ -299,7 +299,7 @@ fail_notifiers:
   }
   static void vhost_net_stop_one(struct vhost_net *net,
-                               VirtIODevice *dev)
+                               VirtIODevice *dev, bool reset)
   {
       struct vhost_vring_file file = { .fd = -1 };
@@ -313,6 +313,11 @@ static void vhost_net_stop_one(struct vhost_net 
 *net,
           net->nc->info->poll(net->nc, true);
       }
       vhost_dev_stop(&net->dev, dev);
+
+    if (reset) {
+        vhost_dev_reset(&net->dev);
+    }
+
       vhost_dev_disable_notifiers(&net->dev, dev);
   }
@@ -391,7 +396,12 @@ int vhost_net_start(VirtIODevice *dev, 
 NetClientState *ncs,

  err_start:
      while (--i >= 0) {
          peer = qemu_get_peer(ncs , i);
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        if (i == 0) {
+            vhost_net_stop_one(get_vhost_net(peer), dev, true); +        } else {
+            vhost_net_stop_one(get_vhost_net(peer), dev, false); +        }
      }
      e = k->set_guest_notifiers(qbus->parent, total_notifiers, false); if (e < 0) {

hw/net/vhost_net.c +0 -0
@@ -420,7 +430,13 @@ void vhost_net_stop(VirtIODevice *dev, 
 NetClientState *ncs,
Quoted 8 lines

hw/net/vhost_net.c void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs, · line 435

Quoted 7 lines
hw/net/vhost_net.c +3 -1
           } else {
               peer = qemu_get_peer(ncs, n->max_queue_pairs);
           }
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        /* We only reset backend device during the last vhost */
+        if (i == nvhosts - 1) {

I wonder if there's any specific reason to position device reset in the for loop given that there's no virtqueue level reset? Wouldn't it be cleaner to reset the device at the end of vhost_net_stop() before return? you may use qemu_get_peer(ncs, 0) without hassle? Noted the vhost_ops->vhost_reset_device op is per device rather per vq.

OK, it's a good way to do reset at the end of vhost_net_stop(), I will change it in next version.

Quoted 51 lines
Quoted 47 lines
hw/net/vhost_net.c +4 -0
+            vhost_net_stop_one(get_vhost_net(peer), dev, true);
+        } else {
+            vhost_net_stop_one(get_vhost_net(peer), dev, false);
+        }
       }
       r = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
hw/virtio/vhost-vdpa.c +2 -2
index c5ed7a3..d858b4f 100644
@@ -719,14 +719,14 @@ static int vhost_vdpa_get_vq_index(struct 
 vhost_dev *dev, int idx)
       return idx;
   }
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, unsigned 
 int ready)
   {
       int i;
       trace_vhost_vdpa_set_vring_ready(dev);
       for (i = 0; i < dev->nvqs; ++i) {
           struct vhost_vring_state state = {
               .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
           };
           vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);

      }

hw/virtio/vhost-vdpa.c +2 -1
@@ -1088,8 +1088,9 @@ static int vhost_vdpa_dev_start(struct vhost_dev 
 *dev, bool started)
           if (unlikely(!ok)) {
               return -1;
           }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
       } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
           ok = vhost_vdpa_svqs_stop(dev);
           if (unlikely(!ok)) {

              return -1;

hw/virtio/vhost-vdpa.c +0 -1
@@ -1105,7 +1106,6 @@ static int vhost_vdpa_dev_start(struct vhost_dev 
 *dev, bool started)
           memory_listener_register(&v->listener, &address_space_memory);
           return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
       } else {
-        vhost_vdpa_reset_device(dev);
Quoted 2 lines

          vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER);

Here's another issue (regression) got to address - the added S_ACKNOWLEDGE | S_DRIVER bits will be cleared right away by the follow-up reset in vhost_net_stop_one(... , true), which in turn will cause virtio fail to initialize e.g. vhost_vdpa_set_features() will fail to set VIRTIO_CONFIG_S_FEATURES_OK

Quoted 2 lines

Ideally the status bit should be set whenever the corresponding status bit is set by virtio_net from virtio_net_vhost_status(), or practically it can be done at the very beginning of vhost_dev_start(), for e.g. the first call before vhost_dev_set_features(). For this purpose, you may consider adding another vhost_init_device op, which is symmetric to vhost_ops->vhost_reset_device in the vhost_net_stop() path.

Seems only vdpa device need reset after stop, although virtio spec said need reset, but kernel doesn't reset, and if reset it has issue to reprobe virtio-net in guest, So we probely only add it after reset if it is VDPA device, for kernel and other datapath we just keep the same as before.

Thanks,
Michael

Quoted 47 lines

Thanks,
-Siwei

Quoted 42 lines

          memory_listener_unregister(&v->listener);

hw/virtio/vhost.c +15 -1
index b643f42..6d9b4a3 100644
@@ -1820,7 +1820,7 @@ fail_features:
   void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
   {
       int i;
-
+    printf("vhost_dev_stop test\n");
       /* should only be called after backend is connected */
       assert(hdev->vhost_ops);
@@ -1854,3 +1854,17 @@ int vhost_net_set_backend(struct vhost_dev *hdev,
       return -ENOSYS;
   }
+
+int vhost_dev_reset(struct vhost_dev *hdev)
+{
+    int ret = 0;
+
+    /* should only be called after backend is connected */
+    assert(hdev->vhost_ops);
+
+    if (hdev->vhost_ops->vhost_reset_device) {
+        ret = hdev->vhost_ops->vhost_reset_device(hdev);
+    }
+
+    return ret;
+}
include/hw/virtio/vhost.h +1 -0
index 58a73e7..b8b7c20 100644
@@ -114,6 +114,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void 
 *opaque,
   void vhost_dev_cleanup(struct vhost_dev *hdev);
   int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
   void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
+int vhost_dev_reset(struct vhost_dev *hdev);
   int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
   void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
#15 lore raw

PATCH v3 1/1 vdpa: reset the backend device in the end of vhost_net_stop()

In reply to [email protected]

From: Michael Qiu <[email protected]>

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset many times(with 1 datapath queue pair and one control queue, triggered 3 times), this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_net_stop(), it will stop all vhost device bind to this virtio device, and in vhost_dev_stop(), qemu tries to stop the device , then stop the queue: vhost_virtqueue_stop().

In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and in next loop(stop other vhost backends), qemu try to stop the queue corresponding to the vhost backend,
the driver finds that the VQ is invalied, this is the root cause.

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

and implement a new function vhost_dev_reset, only reset backend device after all vhost(per-queue) stoped.

---
v3 --> v2:
Call vhost_dev_reset() at the end of vhost_net_stop().

Since the vDPA device need re-add the status bit VIRTIO_CONFIG_S_ACKNOWLEDGE and VIRTIO_CONFIG_S_DRIVER, simply, add them inside vhost_vdpa_reset_device, and the only way calling vhost_vdpa_reset_device is in vhost_net_stop(), so it keeps the same behavior as before.

v2 --> v1:
Implement a new function vhost_dev_reset,
reset the backend kernel device at last.

---
 hw/net/vhost_net.c        | 24 +++++++++++++++++++++---
 hw/virtio/vhost-vdpa.c    | 15 +++++++++------
 hw/virtio/vhost.c         | 15 ++++++++++++++-
 include/hw/virtio/vhost.h |  1 +
 4 files changed, 45 insertions(+), 10 deletions(-)
hw/net/vhost_net.c +21 -3
index 30379d2..422c9bf 100644
@@ -325,7 +325,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
     int total_notifiers = data_queue_pairs * 2 + cvq;
     VirtIONet *n = VIRTIO_NET(dev);
     int nvhosts = data_queue_pairs + cvq;
-    struct vhost_net *net;
+    struct vhost_net *net = NULL;
     int r, e, i, index_end = data_queue_pairs * 2;
     NetClientState *peer;
 
@@ -391,8 +391,17 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
 err_start:
     while (--i >= 0) {
         peer = qemu_get_peer(ncs , i);
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
     }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
+    }
+
     e = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
     if (e < 0) {
         fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
@@ -410,6 +419,7 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
     VirtIONet *n = VIRTIO_NET(dev);
     NetClientState *peer;
+    struct vhost_net *net = NULL;
     int total_notifiers = data_queue_pairs * 2 + cvq;
     int nvhosts = data_queue_pairs + cvq;
     int i, r;
@@ -420,7 +430,15 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
         } else {
             peer = qemu_get_peer(ncs, n->max_queue_pairs);
         }
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
+    }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
     }
 
     r = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
hw/virtio/vhost-vdpa.c +9 -6
index c5ed7a3..3ef0199 100644
@@ -708,6 +708,11 @@ static int vhost_vdpa_reset_device(struct vhost_dev *dev)
 
     ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &status);
     trace_vhost_vdpa_reset_device(dev, status);
+
+    /* Add back this status, so that the device could work next time*/
+    vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
+                               VIRTIO_CONFIG_S_DRIVER);
+
     return ret;
 }
 
@@ -719,14 +724,14 @@ static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx)
     return idx;
 }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, unsigned int ready)
 {
     int i;
     trace_vhost_vdpa_set_vring_ready(dev);
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_vring_state state = {
             .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
         };
         vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
     }
@@ -1088,8 +1093,9 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
         if (unlikely(!ok)) {
             return -1;
         }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
     } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
         ok = vhost_vdpa_svqs_stop(dev);
         if (unlikely(!ok)) {
             return -1;
@@ -1105,9 +1111,6 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
         memory_listener_register(&v->listener, &address_space_memory);
         return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
     } else {
-        vhost_vdpa_reset_device(dev);
-        vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
-                                   VIRTIO_CONFIG_S_DRIVER);
         memory_listener_unregister(&v->listener);
 
         return 0;
hw/virtio/vhost.c +14 -1
index b643f42..7e0cdb6 100644
@@ -1820,7 +1820,6 @@ fail_features:
 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
 {
     int i;
-
     /* should only be called after backend is connected */
     assert(hdev->vhost_ops);
 
@@ -1854,3 +1853,17 @@ int vhost_net_set_backend(struct vhost_dev *hdev,
 
     return -ENOSYS;
 }
+
+int vhost_dev_reset(struct vhost_dev *hdev)
+{
+    int ret = 0;
+
+    /* should only be called after backend is connected */
+    assert(hdev->vhost_ops);
+
+    if (hdev->vhost_ops->vhost_reset_device) {
+        ret = hdev->vhost_ops->vhost_reset_device(hdev);
+    }
+
+    return ret;
+}
include/hw/virtio/vhost.h +1 -0
index 58a73e7..b8b7c20 100644
@@ -114,6 +114,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
 void vhost_dev_cleanup(struct vhost_dev *hdev);
 int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
+int vhost_dev_reset(struct vhost_dev *hdev);
 int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
 void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
 
Signature · 1 line
1.8.3.1
#16 lore raw

Re: [PATCH v3] vdpa: reset the backend device in the end of vhost_net_stop()

On Thu, Mar 31, 2022 at 1:20 PM <[email protected]> wrote:

Hi:

For some reason, I see the patch as an attachment.

Thanks

#17 lore raw

Re: [PATCH v3] vdpa: reset the backend device in the end of vhost_net_stop()

Hi,

Quoted 5 lines · Jason Wang

On Thu, Mar 31, 2022 at 1:20 PM <[email protected]> wrote:

Hi:

For some reason, I see the patch as an attachment.

We are starting to see this more and more since yesterday on DPDK mailing list. It seems like an issue with mimecast, when the From: tag is different from the sender.

Maxime

Quoted 3 lines

Thanks

#18 lore raw

Re: [PATCH v3] vdpa: reset the backend device in the end of vhost_net_stop()

Hi, all

To avoid tigger the issue of mimecast, I will re-post the V3 patch use the mail address "Michael Qiu <[email protected]>" as a workaround.

Thanks,
Michael

Quoted 20 lines · Maxime Coquelin

Hi,

Quoted 5 lines · Jason Wang

On Thu, Mar 31, 2022 at 1:20 PM <[email protected]> wrote:

Hi:

For some reason, I see the patch as an attachment.

We are starting to see this more and more since yesterday on DPDK mailing list. It seems like an issue with mimecast, when the From: tag is different from the sender.

Maxime

Quoted 3 lines

Thanks

#19 lore raw

Re: [PATCH v3] vdpa: reset the backend device in the end of vhost_net_stop()

In reply to Maxime Coquelin
Quoted 8 lines · Maxime Coquelin

Hi,

Quoted 4 lines · Jason Wang

On Thu, Mar 31, 2022 at 1:20 PM <[email protected]> wrote:

Hi:

Quoted 7 lines
Quoted 1 line

For some reason, I see the patch as an attachment.

We are starting to see this more and more since yesterday on DPDK mailing list. It seems like an issue with mimecast, when the From: tag is different from the sender.

Maxime

I see. Thanks

Quoted 5 lines
Quoted 4 lines

Thanks

#20 lore raw

PATCH RESEND v3 1/1 vdpa: reset the backend device in the end of vhost_net_stop()

In reply to [email protected]

From: Michael Qiu <[email protected]>

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset many times(with 1 datapath queue pair and one control queue, triggered 3 times), this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_net_stop(), it will stop all vhost device bind to this virtio device, and in vhost_dev_stop(), qemu tries to stop the device , then stop the queue: vhost_virtqueue_stop().

In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and in next loop(stop other vhost backends), qemu try to stop the queue corresponding to the vhost backend,
the driver finds that the VQ is invalied, this is the root cause.

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

and implement a new function vhost_dev_reset, only reset backend device after all vhost(per-queue) stoped.

---
v3 --> v2:
Call vhost_dev_reset() at the end of vhost_net_stop().

Since the vDPA device need re-add the status bit VIRTIO_CONFIG_S_ACKNOWLEDGE and VIRTIO_CONFIG_S_DRIVER, simply, add them inside vhost_vdpa_reset_device, and the only way calling vhost_vdpa_reset_device is in vhost_net_stop(), so it keeps the same behavior as before.

v2 --> v1:
Implement a new function vhost_dev_reset,
reset the backend kernel device at last.

---
 hw/net/vhost_net.c        | 24 +++++++++++++++++++++---
 hw/virtio/vhost-vdpa.c    | 15 +++++++++------
 hw/virtio/vhost.c         | 15 ++++++++++++++-
 include/hw/virtio/vhost.h |  1 +
 4 files changed, 45 insertions(+), 10 deletions(-)
hw/net/vhost_net.c +21 -3
index 30379d2..422c9bf 100644
@@ -325,7 +325,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
     int total_notifiers = data_queue_pairs * 2 + cvq;
     VirtIONet *n = VIRTIO_NET(dev);
     int nvhosts = data_queue_pairs + cvq;
-    struct vhost_net *net;
+    struct vhost_net *net = NULL;
     int r, e, i, index_end = data_queue_pairs * 2;
     NetClientState *peer;
 
@@ -391,8 +391,17 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
 err_start:
     while (--i >= 0) {
         peer = qemu_get_peer(ncs , i);
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
     }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
+    }
+
     e = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
     if (e < 0) {
         fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
@@ -410,6 +419,7 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
     VirtIONet *n = VIRTIO_NET(dev);
     NetClientState *peer;
+    struct vhost_net *net = NULL;
     int total_notifiers = data_queue_pairs * 2 + cvq;
     int nvhosts = data_queue_pairs + cvq;
     int i, r;
@@ -420,7 +430,15 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
         } else {
             peer = qemu_get_peer(ncs, n->max_queue_pairs);
         }
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
+    }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
     }
 
     r = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
hw/virtio/vhost-vdpa.c +9 -6
index c5ed7a3..3ef0199 100644
@@ -708,6 +708,11 @@ static int vhost_vdpa_reset_device(struct vhost_dev *dev)
 
     ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &status);
     trace_vhost_vdpa_reset_device(dev, status);
+
+    /* Add back this status, so that the device could work next time*/
+    vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
+                               VIRTIO_CONFIG_S_DRIVER);
+
     return ret;
 }
 
@@ -719,14 +724,14 @@ static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx)
     return idx;
 }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, unsigned int ready)
 {
     int i;
     trace_vhost_vdpa_set_vring_ready(dev);
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_vring_state state = {
             .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
         };
         vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
     }
@@ -1088,8 +1093,9 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
         if (unlikely(!ok)) {
             return -1;
         }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
     } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
         ok = vhost_vdpa_svqs_stop(dev);
         if (unlikely(!ok)) {
             return -1;
@@ -1105,9 +1111,6 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
         memory_listener_register(&v->listener, &address_space_memory);
         return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
     } else {
-        vhost_vdpa_reset_device(dev);
-        vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
-                                   VIRTIO_CONFIG_S_DRIVER);
         memory_listener_unregister(&v->listener);
 
         return 0;
hw/virtio/vhost.c +14 -1
index b643f42..7e0cdb6 100644
@@ -1820,7 +1820,6 @@ fail_features:
 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
 {
     int i;
-
     /* should only be called after backend is connected */
     assert(hdev->vhost_ops);
 
@@ -1854,3 +1853,17 @@ int vhost_net_set_backend(struct vhost_dev *hdev,
 
     return -ENOSYS;
 }
+
+int vhost_dev_reset(struct vhost_dev *hdev)
+{
+    int ret = 0;
+
+    /* should only be called after backend is connected */
+    assert(hdev->vhost_ops);
+
+    if (hdev->vhost_ops->vhost_reset_device) {
+        ret = hdev->vhost_ops->vhost_reset_device(hdev);
+    }
+
+    return ret;
+}
include/hw/virtio/vhost.h +1 -0
index 58a73e7..b8b7c20 100644
@@ -114,6 +114,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
 void vhost_dev_cleanup(struct vhost_dev *hdev);
 int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
+int vhost_dev_reset(struct vhost_dev *hdev);
 int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
 void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
 
Signature · 1 line
1.8.3.1
#21 lore raw

Re: [PATCH RESEND v3] vdpa: reset the backend device in the end of vhost_net_stop()

Hi, Jason

Does it work this time?

Quoted 199 lines · On 2022/3/31 17:25, [email protected] wrote:

From: Michael Qiu <[email protected]>

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset many times(with 1 datapath queue pair and one control queue, triggered 3 times), this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_net_stop(), it will stop all vhost device bind to this virtio device, and in vhost_dev_stop(), qemu tries to stop the device , then stop the queue: vhost_virtqueue_stop().

In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and in next loop(stop other vhost backends), qemu try to stop the queue corresponding to the vhost backend,
the driver finds that the VQ is invalied, this is the root cause.

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

and implement a new function vhost_dev_reset, only reset backend device after all vhost(per-queue) stoped.

---
v3 --> v2:
Call vhost_dev_reset() at the end of vhost_net_stop().

Since the vDPA device need re-add the status bit VIRTIO_CONFIG_S_ACKNOWLEDGE and VIRTIO_CONFIG_S_DRIVER, simply, add them inside vhost_vdpa_reset_device, and the only way calling vhost_vdpa_reset_device is in vhost_net_stop(), so it keeps the same behavior as before.

v2 --> v1:
Implement a new function vhost_dev_reset,
reset the backend kernel device at last.

---
  hw/net/vhost_net.c        | 24 +++++++++++++++++++++---
  hw/virtio/vhost-vdpa.c    | 15 +++++++++------
  hw/virtio/vhost.c         | 15 ++++++++++++++-
  include/hw/virtio/vhost.h |  1 +
  4 files changed, 45 insertions(+), 10 deletions(-)
hw/net/vhost_net.c +21 -3
index 30379d2..422c9bf 100644
@@ -325,7 +325,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
      int total_notifiers = data_queue_pairs * 2 + cvq;
      VirtIONet *n = VIRTIO_NET(dev);
      int nvhosts = data_queue_pairs + cvq;
-    struct vhost_net *net;
+    struct vhost_net *net = NULL;
      int r, e, i, index_end = data_queue_pairs * 2;
      NetClientState *peer;
 
@@ -391,8 +391,17 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
  err_start:
      while (--i >= 0) {
          peer = qemu_get_peer(ncs , i);
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
      }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
+    }
+
      e = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
      if (e < 0) {
          fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
@@ -410,6 +419,7 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
      VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
      VirtIONet *n = VIRTIO_NET(dev);
      NetClientState *peer;
+    struct vhost_net *net = NULL;
      int total_notifiers = data_queue_pairs * 2 + cvq;
      int nvhosts = data_queue_pairs + cvq;
      int i, r;
@@ -420,7 +430,15 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
          } else {
              peer = qemu_get_peer(ncs, n->max_queue_pairs);
          }
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
+    }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
      }
 
      r = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
hw/virtio/vhost-vdpa.c +9 -6
index c5ed7a3..3ef0199 100644
@@ -708,6 +708,11 @@ static int vhost_vdpa_reset_device(struct vhost_dev *dev)
 
      ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &status);
      trace_vhost_vdpa_reset_device(dev, status);
+
+    /* Add back this status, so that the device could work next time*/
+    vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
+                               VIRTIO_CONFIG_S_DRIVER);
+
      return ret;
  }
 
@@ -719,14 +724,14 @@ static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx)
      return idx;
  }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, unsigned int ready)
  {
      int i;
      trace_vhost_vdpa_set_vring_ready(dev);
      for (i = 0; i < dev->nvqs; ++i) {
          struct vhost_vring_state state = {
              .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
          };
          vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
      }
@@ -1088,8 +1093,9 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
          if (unlikely(!ok)) {
              return -1;
          }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
      } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
          ok = vhost_vdpa_svqs_stop(dev);
          if (unlikely(!ok)) {
              return -1;
@@ -1105,9 +1111,6 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
          memory_listener_register(&v->listener, &address_space_memory);
          return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
      } else {
-        vhost_vdpa_reset_device(dev);
-        vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
-                                   VIRTIO_CONFIG_S_DRIVER);
          memory_listener_unregister(&v->listener);
 
          return 0;
hw/virtio/vhost.c +14 -1
index b643f42..7e0cdb6 100644
@@ -1820,7 +1820,6 @@ fail_features:
  void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
  {
      int i;
-
      /* should only be called after backend is connected */
      assert(hdev->vhost_ops);
 
@@ -1854,3 +1853,17 @@ int vhost_net_set_backend(struct vhost_dev *hdev,
 
      return -ENOSYS;
  }
+
+int vhost_dev_reset(struct vhost_dev *hdev)
+{
+    int ret = 0;
+
+    /* should only be called after backend is connected */
+    assert(hdev->vhost_ops);
+
+    if (hdev->vhost_ops->vhost_reset_device) {
+        ret = hdev->vhost_ops->vhost_reset_device(hdev);
+    }
+
+    return ret;
+}
include/hw/virtio/vhost.h +1 -0
index 58a73e7..b8b7c20 100644
@@ -114,6 +114,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
  void vhost_dev_cleanup(struct vhost_dev *hdev);
  int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
  void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
+int vhost_dev_reset(struct vhost_dev *hdev);
  int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
  void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
 

A message that is not in the archive (sent off-list or not yet received): [email protected]

#23 lore raw

Re: [PATCH RESEND v3] vdpa: reset the backend device in the end of vhost_net_stop()

Quoted 3 lines · Michael Qiu

Hi, Jason

Does it work this time?

Nope. Just use git-send-email.

Signature · 1 line
MST
#24 lore raw

Re: [PATCH RESEND v3] vdpa: reset the backend device in the end of vhost_net_stop()

In reply to [email protected]
Quoted 62 lines · On 3/31/2022 2:25 AM, [email protected] wrote:

From: Michael Qiu <[email protected]>

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset many times(with 1 datapath queue pair and one control queue, triggered 3 times), this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_net_stop(), it will stop all vhost device bind to this virtio device, and in vhost_dev_stop(), qemu tries to stop the device , then stop the queue: vhost_virtqueue_stop().

In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and in next loop(stop other vhost backends), qemu try to stop the queue corresponding to the vhost backend,
the driver finds that the VQ is invalied, this is the root cause.

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

and implement a new function vhost_dev_reset, only reset backend device after all vhost(per-queue) stoped.

---
v3 --> v2:
Call vhost_dev_reset() at the end of vhost_net_stop().

Since the vDPA device need re-add the status bit VIRTIO_CONFIG_S_ACKNOWLEDGE and VIRTIO_CONFIG_S_DRIVER, simply, add them inside vhost_vdpa_reset_device, and the only way calling vhost_vdpa_reset_device is in vhost_net_stop(), so it keeps the same behavior as before.

v2 --> v1:
Implement a new function vhost_dev_reset,
reset the backend kernel device at last.

---
  hw/net/vhost_net.c        | 24 +++++++++++++++++++++---
  hw/virtio/vhost-vdpa.c    | 15 +++++++++------
  hw/virtio/vhost.c         | 15 ++++++++++++++-
  include/hw/virtio/vhost.h |  1 +
  4 files changed, 45 insertions(+), 10 deletions(-)
hw/net/vhost_net.c +1 -1
index 30379d2..422c9bf 100644
@@ -325,7 +325,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
      int total_notifiers = data_queue_pairs * 2 + cvq;
      VirtIONet *n = VIRTIO_NET(dev);
      int nvhosts = data_queue_pairs + cvq;
-    struct vhost_net *net;
+    struct vhost_net *net = NULL;
      int r, e, i, index_end = data_queue_pairs * 2;
      NetClientState *peer;
 
@@ -391,8 +391,17 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
  err_start:
      while (--i >= 0) {

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

Quoted 10 lines
hw/net/vhost_net.c +7 -1
          peer = qemu_get_peer(ncs , i);
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
      }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {

I would reset the device anyway regardless the first vhost_dev. Some ioctl calls may have well changed device state in vhost_dev_start() that has no way to get back than reset.

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

Quoted 1 line
hw/net/vhost_net.c +1 -0
+        vhost_dev_reset(&net->dev);

I would move this to the end as it's more sensible to reset the device after guest notifier is disabled.

Quoted 16 lines
hw/net/vhost_net.c +3 -0
+    }
+
      e = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
      if (e < 0) {
          fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
@@ -410,6 +419,7 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
      VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
      VirtIONet *n = VIRTIO_NET(dev);
      NetClientState *peer;
+    struct vhost_net *net = NULL;
      int total_notifiers = data_queue_pairs * 2 + cvq;
      int nvhosts = data_queue_pairs + cvq;
      int i, r;
@@ -420,7 +430,15 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
          } else {
              peer = qemu_get_peer(ncs, n->max_queue_pairs);

hw/net/vhost_net.c void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs, · line 440

Quoted 10 lines
hw/net/vhost_net.c +8 -1
          }
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
+    }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {

Yikes, I think it needs some code refactoring here without having to check VHOST_BACKEND_TYPE_VDPA explicitly. Historically the .vhost_reset_device() op was misnamed: it was initially meant for RESET_OWNER but never got used. Could you add a new .vhost_reset_owner() op to VhostOps (via another patch) and rename properly, e.g. from vhost_kernel_reset_device() to vhost_kernel_reset_owner()? For vhost_user_reset_device(), you can safely factor out the VHOST_USER_RESET_OWNER case to a new vhost_user_reset_owner() function, and only reset the device in vhost_user_reset_device() depending on the VHOST_USER_PROTOCOL_F_RESET_DEVICE protocol feature.

With this change, vhost_reset_device will be effectively a no-op on vhost_kernel (NULL) and vhost_user (only applicable to vhost-user-scsi backend which supports VHOST_USER_PROTOCOL_F_RESET_DEVICE).

hw/net/vhost_net.c void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs, · line 441

Quoted 1 line
hw/net/vhost_net.c +1 -0
+        vhost_dev_reset(&net->dev);

I would move this to the end as it's more sensible to reset the device after guest notifier is disabled.

Quoted 8 lines
hw/net/vhost_net.c +0 -0
      }
 
      r = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
hw/virtio/vhost-vdpa.c +0 -0
index c5ed7a3..3ef0199 100644

hw/virtio/vhost-vdpa.c static int vhost_vdpa_reset_device(struct vhost_dev *dev) · line 715

Quoted 8 lines
hw/virtio/vhost-vdpa.c +5 -0
@@ -708,6 +708,11 @@ static int vhost_vdpa_reset_device(struct vhost_dev *dev)
 
      ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &status);
      trace_vhost_vdpa_reset_device(dev, status);
+
+    /* Add back this status, so that the device could work next time*/
+    vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
+                               VIRTIO_CONFIG_S_DRIVER);
+

Hmmm, this might not be the ideal place, but I'm fine to leave it as-is. It would need some more future work in code refactoring for e.g. live migration and error recovery.

Thanks,
-Siwei

Quoted 83 lines
hw/virtio/vhost-vdpa.c +4 -6
      return ret;
  }
 
@@ -719,14 +724,14 @@ static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx)
      return idx;
  }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, unsigned int ready)
  {
      int i;
      trace_vhost_vdpa_set_vring_ready(dev);
      for (i = 0; i < dev->nvqs; ++i) {
          struct vhost_vring_state state = {
              .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
          };
          vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
      }
@@ -1088,8 +1093,9 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
          if (unlikely(!ok)) {
              return -1;
          }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
      } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
          ok = vhost_vdpa_svqs_stop(dev);
          if (unlikely(!ok)) {
              return -1;
@@ -1105,9 +1111,6 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
          memory_listener_register(&v->listener, &address_space_memory);
          return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
      } else {
-        vhost_vdpa_reset_device(dev);
-        vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
-                                   VIRTIO_CONFIG_S_DRIVER);
          memory_listener_unregister(&v->listener);
 
          return 0;
hw/virtio/vhost.c +14 -1
index b643f42..7e0cdb6 100644
@@ -1820,7 +1820,6 @@ fail_features:
  void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
  {
      int i;
-
      /* should only be called after backend is connected */
      assert(hdev->vhost_ops);
 
@@ -1854,3 +1853,17 @@ int vhost_net_set_backend(struct vhost_dev *hdev,
 
      return -ENOSYS;
  }
+
+int vhost_dev_reset(struct vhost_dev *hdev)
+{
+    int ret = 0;
+
+    /* should only be called after backend is connected */
+    assert(hdev->vhost_ops);
+
+    if (hdev->vhost_ops->vhost_reset_device) {
+        ret = hdev->vhost_ops->vhost_reset_device(hdev);
+    }
+
+    return ret;
+}
include/hw/virtio/vhost.h +1 -0
index 58a73e7..b8b7c20 100644
@@ -114,6 +114,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
  void vhost_dev_cleanup(struct vhost_dev *hdev);
  int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
  void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
+int vhost_dev_reset(struct vhost_dev *hdev);
  int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
  void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
 
#25 lore raw

Re: [PATCH RESEND v3] vdpa: reset the backend device in the end of vhost_net_stop()

On 2022/4/1 9:12, Si-Wei Liu worte:

Quoted 70 lines · On 3/31/2022 2:25 AM, [email protected] wrote:
Quoted 67 lines

From: Michael Qiu <[email protected]>

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset many times(with 1 datapath queue pair and one control queue, triggered 3 times), this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_net_stop(), it will stop all vhost device bind to this virtio device, and in vhost_dev_stop(), qemu tries to stop the device , then stop the queue: vhost_virtqueue_stop().

In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and in next loop(stop other vhost backends), qemu try to stop the queue corresponding to the vhost backend, the driver finds that the VQ is invalied, this is the root cause.

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

and implement a new function vhost_dev_reset, only reset backend device after all vhost(per-queue) stoped.

---
v3 --> v2:
     Call vhost_dev_reset() at the end of vhost_net_stop().

     Since the vDPA device need re-add the status bit VIRTIO_CONFIG_S_ACKNOWLEDGE and VIRTIO_CONFIG_S_DRIVER, simply, add them inside vhost_vdpa_reset_device, and the only way calling vhost_vdpa_reset_device is in vhost_net_stop(), so it keeps the same behavior as before.

v2 --> v1:
    Implement a new function vhost_dev_reset, reset the backend kernel device at last.
---
  hw/net/vhost_net.c        | 24 +++++++++++++++++++++--- hw/virtio/vhost-vdpa.c    | 15 +++++++++------ hw/virtio/vhost.c         | 15 ++++++++++++++- include/hw/virtio/vhost.h |  1 +

  4 files changed, 45 insertions(+), 10 deletions(-)
hw/net/vhost_net.c +1 -1
index 30379d2..422c9bf 100644
@@ -325,7 +325,7 @@ int vhost_net_start(VirtIODevice *dev, 
 NetClientState *ncs,
       int total_notifiers = data_queue_pairs * 2 + cvq;
       VirtIONet *n = VIRTIO_NET(dev);
       int nvhosts = data_queue_pairs + cvq;
-    struct vhost_net *net;
+    struct vhost_net *net = NULL;
       int r, e, i, index_end = data_queue_pairs * 2;
       NetClientState *peer;
@@ -391,8 +391,17 @@ int vhost_net_start(VirtIODevice *dev, 
 NetClientState *ncs,

  err_start:
      while (--i >= 0) {
          peer = qemu_get_peer(ncs , i);
-        vhost_net_stop_one(get_vhost_net(peer), dev); +
+        net = get_vhost_net(peer);

Quoted 9 lines
Quoted 7 lines

+
+        vhost_net_stop_one(net, dev);
      }
+
+    /* We only reset backend vdpa device */ +    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {

I would reset the device anyway regardless the first vhost_dev. Some ioctl calls may have well changed device state in vhost_dev_start() that has no way to get back than reset.

Here I just use the first vhost_dev as nothing is different in each vhost_dev, reset just set 0 to the vhost-vdpa socket FD. In all vhost_dev, FD is the same.

Quoted 2 lines
Quoted 1 line

+        vhost_dev_reset(&net->dev);

I would move this to the end as it's more sensible to reset the device after guest notifier is disabled.

I will move it in next patch

Quoted 31 lines
Quoted 19 lines

+    }
+
      e = k->set_guest_notifiers(qbus->parent, total_notifiers, false); if (e < 0) {
          fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);

hw/net/vhost_net.c +1 -0
@@ -410,6 +419,7 @@ void vhost_net_stop(VirtIODevice *dev, 
 NetClientState *ncs,
       VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
       VirtIONet *n = VIRTIO_NET(dev);
       NetClientState *peer;
+    struct vhost_net *net = NULL;
       int total_notifiers = data_queue_pairs * 2 + cvq;
       int nvhosts = data_queue_pairs + cvq;

      int i, r;

hw/net/vhost_net.c +0 -0
@@ -420,7 +430,15 @@ void vhost_net_stop(VirtIODevice *dev, 
 NetClientState *ncs,
           } else {
               peer = qemu_get_peer(ncs, n->max_queue_pairs);
           }

hw/net/vhost_net.c void vhost_net_stop(VirtIODevice *dev, · line 442

Quoted 10 lines
hw/net/vhost_net.c +8 -1
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
+    }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == 
 VHOST_BACKEND_TYPE_VDPA) {

Yikes, I think it needs some code refactoring here without having to check VHOST_BACKEND_TYPE_VDPA explicitly. Historically the .vhost_reset_device() op was misnamed: it was initially meant for RESET_OWNER but never got used. Could you add a new .vhost_reset_owner() op to VhostOps (via another patch) and rename properly, e.g. from vhost_kernel_reset_device() to vhost_kernel_reset_owner()? For vhost_user_reset_device(), you can safely factor out the VHOST_USER_RESET_OWNER case to a new vhost_user_reset_owner() function, and only reset the device in vhost_user_reset_device() depending on the VHOST_USER_PROTOCOL_F_RESET_DEVICE protocol feature.

Quoted 1 line

With this change, vhost_reset_device will be effectively a no-op on vhost_kernel (NULL) and vhost_user (only applicable to vhost-user-scsi backend which supports VHOST_USER_PROTOCOL_F_RESET_DEVICE).

OK, I will make a patch to do that.

Quoted 107 lines

hw/net/vhost_net.c void vhost_net_stop(VirtIODevice *dev, · line 443

Quoted 1 line
hw/net/vhost_net.c +1 -0
+        vhost_dev_reset(&net->dev);

I would move this to the end as it's more sensible to reset the device after guest notifier is disabled.

Quoted 7 lines
hw/net/vhost_net.c +0 -0
       }

      r = k->set_guest_notifiers(qbus->parent, total_notifiers, false);

hw/virtio/vhost-vdpa.c +0 -0
index c5ed7a3..3ef0199 100644

hw/virtio/vhost-vdpa.c static int vhost_vdpa_reset_device(struct · line 715

Quoted 8 lines
hw/virtio/vhost-vdpa.c +5 -0
@@ -708,6 +708,11 @@ static int vhost_vdpa_reset_device(struct 
 vhost_dev *dev)
       ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &status);
       trace_vhost_vdpa_reset_device(dev, status);
+
+    /* Add back this status, so that the device could work next time*/
+    vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
+                               VIRTIO_CONFIG_S_DRIVER);
+

Hmmm, this might not be the ideal place, but I'm fine to leave it as-is. It would need some more future work in code refactoring for e.g. live migration and error recovery.

Thanks,
-Siwei

Quoted 82 lines
hw/virtio/vhost-vdpa.c +2 -2
       return ret;
   }
@@ -719,14 +724,14 @@ static int vhost_vdpa_get_vq_index(struct 
 vhost_dev *dev, int idx)
       return idx;
   }
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, unsigned 
 int ready)
   {
       int i;
       trace_vhost_vdpa_set_vring_ready(dev);
       for (i = 0; i < dev->nvqs; ++i) {
           struct vhost_vring_state state = {
               .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
           };
           vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);

      }

hw/virtio/vhost-vdpa.c +2 -1
@@ -1088,8 +1093,9 @@ static int vhost_vdpa_dev_start(struct vhost_dev 
 *dev, bool started)
           if (unlikely(!ok)) {
               return -1;
           }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
       } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
           ok = vhost_vdpa_svqs_stop(dev);
           if (unlikely(!ok)) {

              return -1;

hw/virtio/vhost-vdpa.c +0 -3
@@ -1105,9 +1111,6 @@ static int vhost_vdpa_dev_start(struct vhost_dev 
 *dev, bool started)
           memory_listener_register(&v->listener, &address_space_memory);
           return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
       } else {
-        vhost_vdpa_reset_device(dev);
-        vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
-                                   VIRTIO_CONFIG_S_DRIVER);
           memory_listener_unregister(&v->listener);
           return 0;
hw/virtio/vhost.c +14 -1
index b643f42..7e0cdb6 100644
@@ -1820,7 +1820,6 @@ fail_features:
   void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
   {
       int i;
-
       /* should only be called after backend is connected */
       assert(hdev->vhost_ops);
@@ -1854,3 +1853,17 @@ int vhost_net_set_backend(struct vhost_dev *hdev,
       return -ENOSYS;
   }
+
+int vhost_dev_reset(struct vhost_dev *hdev)
+{
+    int ret = 0;
+
+    /* should only be called after backend is connected */
+    assert(hdev->vhost_ops);
+
+    if (hdev->vhost_ops->vhost_reset_device) {
+        ret = hdev->vhost_ops->vhost_reset_device(hdev);
+    }
+
+    return ret;
+}
include/hw/virtio/vhost.h +1 -0
index 58a73e7..b8b7c20 100644
@@ -114,6 +114,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void 
 *opaque,
   void vhost_dev_cleanup(struct vhost_dev *hdev);
   int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
   void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
+int vhost_dev_reset(struct vhost_dev *hdev);
   int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
   void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
#26 lore raw

PATCH v4 1/1 vdpa: reset the backend device in the end of vhost_net_stop()

In reply to [email protected]

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset many times(with 1 datapath queue pair and one control queue, triggered 3 times), this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_net_stop(), it will stop all vhost device bind to this virtio device, and in vhost_dev_stop(), qemu tries to stop the device , then stop the queue: vhost_virtqueue_stop().

In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and in next loop(stop other vhost backends), qemu try to stop the queue corresponding to the vhost backend,
the driver finds that the VQ is invalied, this is the root cause.

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

and implement a new function vhost_dev_reset, only reset backend device after all vhost(per-queue) stoped.

---
v4 --> v3
Nothing changed, becasue of issue with mimecast, when the From: tag is different from the sender, the some mail client will take the patch as an attachment, RESEND v3 does not work, So resend the patch as v4

v3 --> v2:
Call vhost_dev_reset() at the end of vhost_net_stop().

Since the vDPA device need re-add the status bit VIRTIO_CONFIG_S_ACKNOWLEDGE and VIRTIO_CONFIG_S_DRIVER, simply, add them inside vhost_vdpa_reset_device, and the only way calling vhost_vdpa_reset_device is in vhost_net_stop(), so it keeps the same behavior as before.

v2 --> v1:
Implement a new function vhost_dev_reset,
reset the backend kernel device at last.

---
 hw/net/vhost_net.c        | 24 +++++++++++++++++++++---
 hw/virtio/vhost-vdpa.c    | 15 +++++++++------
 hw/virtio/vhost.c         | 15 ++++++++++++++-
 include/hw/virtio/vhost.h |  1 +
 4 files changed, 45 insertions(+), 10 deletions(-)
hw/net/vhost_net.c +21 -3
index 30379d2..422c9bf 100644
@@ -325,7 +325,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
     int total_notifiers = data_queue_pairs * 2 + cvq;
     VirtIONet *n = VIRTIO_NET(dev);
     int nvhosts = data_queue_pairs + cvq;
-    struct vhost_net *net;
+    struct vhost_net *net = NULL;
     int r, e, i, index_end = data_queue_pairs * 2;
     NetClientState *peer;
 
@@ -391,8 +391,17 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
 err_start:
     while (--i >= 0) {
         peer = qemu_get_peer(ncs , i);
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
     }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
+    }
+
     e = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
     if (e < 0) {
         fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
@@ -410,6 +419,7 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
     VirtIONet *n = VIRTIO_NET(dev);
     NetClientState *peer;
+    struct vhost_net *net = NULL;
     int total_notifiers = data_queue_pairs * 2 + cvq;
     int nvhosts = data_queue_pairs + cvq;
     int i, r;
@@ -420,7 +430,15 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
         } else {
             peer = qemu_get_peer(ncs, n->max_queue_pairs);
         }
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
+    }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
     }
 
     r = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
hw/virtio/vhost-vdpa.c +9 -6
index c5ed7a3..3ef0199 100644
@@ -708,6 +708,11 @@ static int vhost_vdpa_reset_device(struct vhost_dev *dev)
 
     ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &status);
     trace_vhost_vdpa_reset_device(dev, status);
+
+    /* Add back this status, so that the device could work next time*/
+    vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
+                               VIRTIO_CONFIG_S_DRIVER);
+
     return ret;
 }
 
@@ -719,14 +724,14 @@ static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx)
     return idx;
 }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, unsigned int ready)
 {
     int i;
     trace_vhost_vdpa_set_vring_ready(dev);
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_vring_state state = {
             .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
         };
         vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
     }
@@ -1088,8 +1093,9 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
         if (unlikely(!ok)) {
             return -1;
         }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
     } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
         ok = vhost_vdpa_svqs_stop(dev);
         if (unlikely(!ok)) {
             return -1;
@@ -1105,9 +1111,6 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
         memory_listener_register(&v->listener, &address_space_memory);
         return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
     } else {
-        vhost_vdpa_reset_device(dev);
-        vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
-                                   VIRTIO_CONFIG_S_DRIVER);
         memory_listener_unregister(&v->listener);
 
         return 0;
hw/virtio/vhost.c +14 -1
index b643f42..7e0cdb6 100644
@@ -1820,7 +1820,6 @@ fail_features:
 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
 {
     int i;
-
     /* should only be called after backend is connected */
     assert(hdev->vhost_ops);
 
@@ -1854,3 +1853,17 @@ int vhost_net_set_backend(struct vhost_dev *hdev,
 
     return -ENOSYS;
 }
+
+int vhost_dev_reset(struct vhost_dev *hdev)
+{
+    int ret = 0;
+
+    /* should only be called after backend is connected */
+    assert(hdev->vhost_ops);
+
+    if (hdev->vhost_ops->vhost_reset_device) {
+        ret = hdev->vhost_ops->vhost_reset_device(hdev);
+    }
+
+    return ret;
+}
include/hw/virtio/vhost.h +1 -0
index 58a73e7..b8b7c20 100644
@@ -114,6 +114,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
 void vhost_dev_cleanup(struct vhost_dev *hdev);
 int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
+int vhost_dev_reset(struct vhost_dev *hdev);
 int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
 void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
 
Signature · 1 line
1.8.3.1
#27 lore raw

Re: [PATCH v4] vdpa: reset the backend device in the end of vhost_net_stop()

Quoted 17 lines · Michael Qiu

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset many times(with 1 datapath queue pair and one control queue, triggered 3 times), this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_net_stop(), it will stop all vhost device bind to this virtio device, and in vhost_dev_stop(), qemu tries to stop the device , then stop the queue: vhost_virtqueue_stop().

In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and in next loop(stop other vhost backends), qemu try to stop the queue corresponding to the vhost backend,
the driver finds that the VQ is invalied, this is the root cause.

Quoted 5 lines

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

and implement a new function vhost_dev_reset, only reset backend device after all vhost(per-queue) stoped.

Typo.

Quoted 3 lines

Rethink this patch, consider there're devices that don't support set_vq_ready(). I wonder if we need

1) uAPI to tell the user space whether or not it supports set_vq_ready()
2) userspace will call SET_VRING_ENABLE() when the device supports otherwise it will use RESET.

And for safety, I suggest tagging this as 7.1.

Quoted 72 lines

---
v4 --> v3
Nothing changed, becasue of issue with mimecast, when the From: tag is different from the sender, the some mail client will take the patch as an attachment, RESEND v3 does not work, So resend the patch as v4

v3 --> v2:
Call vhost_dev_reset() at the end of vhost_net_stop().

Since the vDPA device need re-add the status bit VIRTIO_CONFIG_S_ACKNOWLEDGE and VIRTIO_CONFIG_S_DRIVER, simply, add them inside vhost_vdpa_reset_device, and the only way calling vhost_vdpa_reset_device is in vhost_net_stop(), so it keeps the same behavior as before.

v2 --> v1:
Implement a new function vhost_dev_reset,
reset the backend kernel device at last.

---
 hw/net/vhost_net.c        | 24 +++++++++++++++++++++---
 hw/virtio/vhost-vdpa.c    | 15 +++++++++------
 hw/virtio/vhost.c         | 15 ++++++++++++++-
 include/hw/virtio/vhost.h |  1 +
 4 files changed, 45 insertions(+), 10 deletions(-)
hw/net/vhost_net.c +12 -3
index 30379d2..422c9bf 100644
@@ -325,7 +325,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
     int total_notifiers = data_queue_pairs * 2 + cvq;
     VirtIONet *n = VIRTIO_NET(dev);
     int nvhosts = data_queue_pairs + cvq;
-    struct vhost_net *net;
+    struct vhost_net *net = NULL;
     int r, e, i, index_end = data_queue_pairs * 2;
     NetClientState *peer;
 
@@ -391,8 +391,17 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
 err_start:
     while (--i >= 0) {
         peer = qemu_get_peer(ncs , i);
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
     }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
+    }
+
     e = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
     if (e < 0) {
         fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
@@ -410,6 +419,7 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
     VirtIONet *n = VIRTIO_NET(dev);
     NetClientState *peer;
+    struct vhost_net *net = NULL;
     int total_notifiers = data_queue_pairs * 2 + cvq;
     int nvhosts = data_queue_pairs + cvq;
     int i, r;
@@ -420,7 +430,15 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
         } else {
             peer = qemu_get_peer(ncs, n->max_queue_pairs);
         }
-        vhost_net_stop_one(get_vhost_net(peer), dev);

hw/net/vhost_net.c void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs, · line 442

Quoted 10 lines
hw/net/vhost_net.c +9 -0
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
+    }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
     }

So we've already reset the device in vhost_vdpa_dev_start(), any reason we need to do it again here?

Quoted 7 lines
hw/net/vhost_net.c +0 -0
     r = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
hw/virtio/vhost-vdpa.c +0 -0
index c5ed7a3..3ef0199 100644

hw/virtio/vhost-vdpa.c static int vhost_vdpa_reset_device(struct vhost_dev *dev) · line 714

Quoted 7 lines
hw/virtio/vhost-vdpa.c +4 -0
@@ -708,6 +708,11 @@ static int vhost_vdpa_reset_device(struct vhost_dev *dev)
 
     ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &status);
     trace_vhost_vdpa_reset_device(dev, status);
+
+    /* Add back this status, so that the device could work next time*/
+    vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
+                               VIRTIO_CONFIG_S_DRIVER);

This seems to contradict the semantic of reset.

Quoted 47 lines
hw/virtio/vhost-vdpa.c +5 -6
+
     return ret;
 }
 
@@ -719,14 +724,14 @@ static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx)
     return idx;
 }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, unsigned int ready)
 {
     int i;
     trace_vhost_vdpa_set_vring_ready(dev);
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_vring_state state = {
             .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
         };
         vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
     }
@@ -1088,8 +1093,9 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
         if (unlikely(!ok)) {
             return -1;
         }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
     } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
         ok = vhost_vdpa_svqs_stop(dev);
         if (unlikely(!ok)) {
             return -1;
@@ -1105,9 +1111,6 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
         memory_listener_register(&v->listener, &address_space_memory);
         return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
     } else {
-        vhost_vdpa_reset_device(dev);
-        vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
-                                   VIRTIO_CONFIG_S_DRIVER);
         memory_listener_unregister(&v->listener);
 
         return 0;
hw/virtio/vhost.c +0 -0
index b643f42..7e0cdb6 100644

hw/virtio/vhost.c fail_features: · line 1823

Quoted 4 lines
hw/virtio/vhost.c +0 -1
@@ -1820,7 +1820,6 @@ fail_features:
 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
 {
     int i;
-

Unnecessary changes.

hw/virtio/vhost.c int vhost_net_set_backend(struct vhost_dev *hdev, · line 1858

Quoted 10 lines
hw/virtio/vhost.c +3 -0
     /* should only be called after backend is connected */
     assert(hdev->vhost_ops);
 
@@ -1854,3 +1853,17 @@ int vhost_net_set_backend(struct vhost_dev *hdev,
 
     return -ENOSYS;
 }
+
+int vhost_dev_reset(struct vhost_dev *hdev)
+{

Let's use a separate patch for this.

Thanks

Quoted 28 lines
hw/virtio/vhost.c +11 -0
+    int ret = 0;
+
+    /* should only be called after backend is connected */
+    assert(hdev->vhost_ops);
+
+    if (hdev->vhost_ops->vhost_reset_device) {
+        ret = hdev->vhost_ops->vhost_reset_device(hdev);
+    }
+
+    return ret;
+}
include/hw/virtio/vhost.h +1 -1
index 58a73e7..b8b7c20 100644
@@ -114,6 +114,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
 void vhost_dev_cleanup(struct vhost_dev *hdev);
 int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
+int vhost_dev_reset(struct vhost_dev *hdev);
 int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
 void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
 
--

1.8.3.1

#28 lore raw

Re: [PATCH v4] vdpa: reset the backend device in the end of vhost_net_stop()

Quoted 30 lines · Jason Wang
Quoted 17 lines · Michael Qiu

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset many times(with 1 datapath queue pair and one control queue, triggered 3 times), this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_net_stop(), it will stop all vhost device bind to this virtio device, and in vhost_dev_stop(), qemu tries to stop the device , then stop the queue: vhost_virtqueue_stop().

In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and in next loop(stop other vhost backends), qemu try to stop the queue corresponding to the vhost backend,
the driver finds that the VQ is invalied, this is the root cause.

Quoted 5 lines

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

and implement a new function vhost_dev_reset, only reset backend device after all vhost(per-queue) stoped.

Typo.

Quoted 3 lines
Quoted 6 lines

Rethink this patch, consider there're devices that don't support set_vq_ready(). I wonder if we need

1) uAPI to tell the user space whether or not it supports set_vq_ready()
2) userspace will call SET_VRING_ENABLE() when the device supports otherwise it will use RESET.

if the device does not support set_vq_ready() in kernel, it will trigger kernel oops, at least in current kernel, it does not check where set_vq_ready has been implemented.

And I checked all vdpa driver in kernel, all drivers has implemented this ops.

So I think it is OK to call set_vq_ready without check.

Quoted 80 lines

And for safety, I suggest tagging this as 7.1.

Quoted 77 lines

---
v4 --> v3
Nothing changed, becasue of issue with mimecast, when the From: tag is different from the sender, the some mail client will take the patch as an attachment, RESEND v3 does not work, So resend the patch as v4

v3 --> v2:
Call vhost_dev_reset() at the end of vhost_net_stop().

Since the vDPA device need re-add the status bit VIRTIO_CONFIG_S_ACKNOWLEDGE and VIRTIO_CONFIG_S_DRIVER, simply, add them inside vhost_vdpa_reset_device, and the only way calling vhost_vdpa_reset_device is in vhost_net_stop(), so it keeps the same behavior as before.

v2 --> v1:
Implement a new function vhost_dev_reset,
reset the backend kernel device at last.

---
  hw/net/vhost_net.c        | 24 +++++++++++++++++++++---
  hw/virtio/vhost-vdpa.c    | 15 +++++++++------
  hw/virtio/vhost.c         | 15 ++++++++++++++-
  include/hw/virtio/vhost.h |  1 +
  4 files changed, 45 insertions(+), 10 deletions(-)
hw/net/vhost_net.c +17 -3
index 30379d2..422c9bf 100644
@@ -325,7 +325,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
      int total_notifiers = data_queue_pairs * 2 + cvq;
      VirtIONet *n = VIRTIO_NET(dev);
      int nvhosts = data_queue_pairs + cvq;
-    struct vhost_net *net;
+    struct vhost_net *net = NULL;
      int r, e, i, index_end = data_queue_pairs * 2;
      NetClientState *peer;
 
@@ -391,8 +391,17 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
  err_start:
      while (--i >= 0) {
          peer = qemu_get_peer(ncs , i);
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
      }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
+    }
+
      e = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
      if (e < 0) {
          fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
@@ -410,6 +419,7 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
      VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
      VirtIONet *n = VIRTIO_NET(dev);
      NetClientState *peer;
+    struct vhost_net *net = NULL;
      int total_notifiers = data_queue_pairs * 2 + cvq;
      int nvhosts = data_queue_pairs + cvq;
      int i, r;
@@ -420,7 +430,15 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
          } else {
              peer = qemu_get_peer(ncs, n->max_queue_pairs);
          }
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
+    }
Quoted 8 lines

hw/net/vhost_net.c void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs, · line 442

Quoted 5 lines
hw/net/vhost_net.c +4 -0
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
      }

So we've already reset the device in vhost_vdpa_dev_start(), any reason we need to do it again here?

reset device in vhost_vdpa_dev_start if there is some error with start.

Quoted 9 lines
Quoted 9 lines
hw/net/vhost_net.c +0 -0
      r = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
hw/virtio/vhost-vdpa.c +0 -0
index c5ed7a3..3ef0199 100644
@@ -708,6 +708,11 @@ static int vhost_vdpa_reset_device(struct vhost_dev *dev)
 
Quoted 8 lines

hw/virtio/vhost-vdpa.c static int vhost_vdpa_reset_device(struct vhost_dev *dev) · line 714

Quoted 6 lines
hw/virtio/vhost-vdpa.c +4 -0
      ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &status);
      trace_vhost_vdpa_reset_device(dev, status);
+
+    /* Add back this status, so that the device could work next time*/
+    vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
+                               VIRTIO_CONFIG_S_DRIVER);

This seems to contradict the semantic of reset

Yes, but it's hard to put it in other place, seems only vhost-vdpa need it, and for VM shutdown, qemu_del_nic() will do cleanup this like close vhost fds, which will call reset in kernel space without set those features.

So at last I put it here with no other inpact.

Thanks,
Michael

Quoted 101 lines
Quoted 48 lines
hw/virtio/vhost-vdpa.c +5 -6
+
      return ret;
  }
 
@@ -719,14 +724,14 @@ static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx)
      return idx;
  }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, unsigned int ready)
  {
      int i;
      trace_vhost_vdpa_set_vring_ready(dev);
      for (i = 0; i < dev->nvqs; ++i) {
          struct vhost_vring_state state = {
              .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
          };
          vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
      }
@@ -1088,8 +1093,9 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
          if (unlikely(!ok)) {
              return -1;
          }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
      } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
          ok = vhost_vdpa_svqs_stop(dev);
          if (unlikely(!ok)) {
              return -1;
@@ -1105,9 +1111,6 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
          memory_listener_register(&v->listener, &address_space_memory);
          return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
      } else {
-        vhost_vdpa_reset_device(dev);
-        vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
-                                   VIRTIO_CONFIG_S_DRIVER);
          memory_listener_unregister(&v->listener);
 
          return 0;
hw/virtio/vhost.c +0 -0
index b643f42..7e0cdb6 100644

hw/virtio/vhost.c fail_features: · line 1823

Quoted 4 lines
hw/virtio/vhost.c +0 -1
@@ -1820,7 +1820,6 @@ fail_features:
  void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
  {
      int i;
-

Unnecessary changes.

hw/virtio/vhost.c int vhost_net_set_backend(struct vhost_dev *hdev, · line 1858

Quoted 10 lines
hw/virtio/vhost.c +3 -0
      /* should only be called after backend is connected */
      assert(hdev->vhost_ops);
 
@@ -1854,3 +1853,17 @@ int vhost_net_set_backend(struct vhost_dev *hdev,
 
      return -ENOSYS;
  }
+
+int vhost_dev_reset(struct vhost_dev *hdev)
+{

Let's use a separate patch for this.

Thanks

Quoted 28 lines
hw/virtio/vhost.c +11 -0
+    int ret = 0;
+
+    /* should only be called after backend is connected */
+    assert(hdev->vhost_ops);
+
+    if (hdev->vhost_ops->vhost_reset_device) {
+        ret = hdev->vhost_ops->vhost_reset_device(hdev);
+    }
+
+    return ret;
+}
include/hw/virtio/vhost.h +1 -1
index 58a73e7..b8b7c20 100644
@@ -114,6 +114,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
  void vhost_dev_cleanup(struct vhost_dev *hdev);
  int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
  void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
+int vhost_dev_reset(struct vhost_dev *hdev);
  int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
  void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
 
--

1.8.3.1

#29 lore raw

Re: [PATCH v4] vdpa: reset the backend device in the end of vhost_net_stop()

In reply to Jason Wang
Quoted 22 lines · Jason Wang
Quoted 16 lines · Michael Qiu

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset many times(with 1 datapath queue pair and one control queue, triggered 3 times), this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_net_stop(), it will stop all vhost device bind to this virtio device, and in vhost_dev_stop(), qemu tries to stop the device , then stop the queue: vhost_virtqueue_stop().

In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and in next loop(stop other vhost backends), qemu try to stop the queue corresponding to the vhost backend,
the driver finds that the VQ is invalied, this is the root cause.

Quoted 5 lines

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

and implement a new function vhost_dev_reset, only reset backend device after all vhost(per-queue) stoped.

Quoted 8 lines

Typo.

Quoted 2 lines

Rethink this patch, consider there're devices that don't support set_vq_ready(). I wonder if we need

1) uAPI to tell the user space whether or not it supports set_vq_ready()

I guess what's more relevant here is to define the uAPI semantics for unready i.e. set_vq_ready(0) for resuming/stopping virtqueue processing, as starting vq is comparatively less ambiguous. Considering the likelihood that this interface may be used for live migration, it would be nice to come up with variants such as 1) discard inflight request v.s. 2) waiting for inflight processing to be done, and 3) timeout in waiting.

Quoted 2 lines

2) userspace will call SET_VRING_ENABLE() when the device supports otherwise it will use RESET.

Are you looking to making virtqueue resume-able through the new SET_VRING_ENABLE() uAPI?

I think RESET is inevitable in some case, i.e. when guest initiates device reset by writing 0 to the status register. For suspend/resume and live migration use cases, indeed RESET can be substituted with SET_VRING_ENABLE. Again, it'd need quite some code refactoring to accommodate this change. Although I'm all for it, it'd be the best to lay out the plan for multiple phases rather than overload this single patch too much. You can count my time on this endeavor if you don't mind. :)

Quoted 2 lines

And for safety, I suggest tagging this as 7.1.

+1

Regards,
-Siwei

Quoted 196 lines
Quoted 73 lines

---
v4 --> v3
Nothing changed, becasue of issue with mimecast, when the From: tag is different from the sender, the some mail client will take the patch as an attachment, RESEND v3 does not work, So resend the patch as v4

v3 --> v2:
Call vhost_dev_reset() at the end of vhost_net_stop().

Since the vDPA device need re-add the status bit VIRTIO_CONFIG_S_ACKNOWLEDGE and VIRTIO_CONFIG_S_DRIVER, simply, add them inside vhost_vdpa_reset_device, and the only way calling vhost_vdpa_reset_device is in vhost_net_stop(), so it keeps the same behavior as before.

v2 --> v1:
Implement a new function vhost_dev_reset,
reset the backend kernel device at last.

---
  hw/net/vhost_net.c        | 24 +++++++++++++++++++++---
  hw/virtio/vhost-vdpa.c    | 15 +++++++++------
  hw/virtio/vhost.c         | 15 ++++++++++++++-
  include/hw/virtio/vhost.h |  1 +
  4 files changed, 45 insertions(+), 10 deletions(-)
hw/net/vhost_net.c +12 -3
index 30379d2..422c9bf 100644
@@ -325,7 +325,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
      int total_notifiers = data_queue_pairs * 2 + cvq;
      VirtIONet *n = VIRTIO_NET(dev);
      int nvhosts = data_queue_pairs + cvq;
-    struct vhost_net *net;
+    struct vhost_net *net = NULL;
      int r, e, i, index_end = data_queue_pairs * 2;
      NetClientState *peer;
 
@@ -391,8 +391,17 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
  err_start:
      while (--i >= 0) {
          peer = qemu_get_peer(ncs , i);
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
      }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
+    }
+
      e = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
      if (e < 0) {
          fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
@@ -410,6 +419,7 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
      VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
      VirtIONet *n = VIRTIO_NET(dev);
      NetClientState *peer;
+    struct vhost_net *net = NULL;
      int total_notifiers = data_queue_pairs * 2 + cvq;
      int nvhosts = data_queue_pairs + cvq;
      int i, r;
@@ -420,7 +430,15 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
          } else {
              peer = qemu_get_peer(ncs, n->max_queue_pairs);
          }
-        vhost_net_stop_one(get_vhost_net(peer), dev);

hw/net/vhost_net.c void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs, · line 442

Quoted 10 lines
hw/net/vhost_net.c +9 -0
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
+    }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
      }

So we've already reset the device in vhost_vdpa_dev_start(), any reason we need to do it again here?

hw/virtio/vhost-vdpa.c static int vhost_vdpa_reset_device(struct vhost_dev *dev) · line 714

Quoted 13 lines
hw/net/vhost_net.c +0 -0
      r = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
hw/virtio/vhost-vdpa.c +4 -0
index c5ed7a3..3ef0199 100644
@@ -708,6 +708,11 @@ static int vhost_vdpa_reset_device(struct vhost_dev *dev)
 
      ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &status);
      trace_vhost_vdpa_reset_device(dev, status);
+
+    /* Add back this status, so that the device could work next time*/
+    vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
+                               VIRTIO_CONFIG_S_DRIVER);

This seems to contradict the semantic of reset.

Quoted 47 lines
hw/virtio/vhost-vdpa.c +5 -6
+
      return ret;
  }
 
@@ -719,14 +724,14 @@ static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx)
      return idx;
  }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, unsigned int ready)
  {
      int i;
      trace_vhost_vdpa_set_vring_ready(dev);
      for (i = 0; i < dev->nvqs; ++i) {
          struct vhost_vring_state state = {
              .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
          };
          vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
      }
@@ -1088,8 +1093,9 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
          if (unlikely(!ok)) {
              return -1;
          }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
      } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
          ok = vhost_vdpa_svqs_stop(dev);
          if (unlikely(!ok)) {
              return -1;
@@ -1105,9 +1111,6 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
          memory_listener_register(&v->listener, &address_space_memory);
          return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
      } else {
-        vhost_vdpa_reset_device(dev);
-        vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
-                                   VIRTIO_CONFIG_S_DRIVER);
          memory_listener_unregister(&v->listener);
 
          return 0;
hw/virtio/vhost.c +0 -0
index b643f42..7e0cdb6 100644

hw/virtio/vhost.c fail_features: · line 1823

Quoted 4 lines
hw/virtio/vhost.c +0 -1
@@ -1820,7 +1820,6 @@ fail_features:
  void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
  {
      int i;
-

Unnecessary changes.

hw/virtio/vhost.c int vhost_net_set_backend(struct vhost_dev *hdev, · line 1858

Quoted 10 lines
hw/virtio/vhost.c +3 -0
      /* should only be called after backend is connected */
      assert(hdev->vhost_ops);
 
@@ -1854,3 +1853,17 @@ int vhost_net_set_backend(struct vhost_dev *hdev,
 
      return -ENOSYS;
  }
+
+int vhost_dev_reset(struct vhost_dev *hdev)
+{

Let's use a separate patch for this.

Thanks

Quoted 28 lines
hw/virtio/vhost.c +11 -0
+    int ret = 0;
+
+    /* should only be called after backend is connected */
+    assert(hdev->vhost_ops);
+
+    if (hdev->vhost_ops->vhost_reset_device) {
+        ret = hdev->vhost_ops->vhost_reset_device(hdev);
+    }
+
+    return ret;
+}
include/hw/virtio/vhost.h +1 -1
index 58a73e7..b8b7c20 100644
@@ -114,6 +114,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
  void vhost_dev_cleanup(struct vhost_dev *hdev);
  int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
  void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
+int vhost_dev_reset(struct vhost_dev *hdev);
  int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
  void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
 
--

1.8.3.1

#30 lore raw

Re: [PATCH v4] vdpa: reset the backend device in the end of vhost_net_stop()

Adding Michael.

Quoted 30 lines · Si-Wei Liu
Quoted 26 lines · Jason Wang
Quoted 16 lines · Michael Qiu

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset many times(with 1 datapath queue pair and one control queue, triggered 3 times), this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_net_stop(), it will stop all vhost device bind to this virtio device, and in vhost_dev_stop(), qemu tries to stop the device , then stop the queue: vhost_virtqueue_stop().

In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and in next loop(stop other vhost backends), qemu try to stop the queue corresponding to the vhost backend,
the driver finds that the VQ is invalied, this is the root cause.

Quoted 5 lines

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

and implement a new function vhost_dev_reset, only reset backend device after all vhost(per-queue) stoped.

Typo.

Quoted 2 lines
Quoted 7 lines
Quoted 4 lines

Rethink this patch, consider there're devices that don't support set_vq_ready(). I wonder if we need

1) uAPI to tell the user space whether or not it supports set_vq_ready()

I guess what's more relevant here is to define the uAPI semantics for unready i.e. set_vq_ready(0) for resuming/stopping virtqueue processing, as starting vq is comparatively less ambiguous.

Yes.

Quoted 4 lines

Considering the
likelihood that this interface may be used for live migration, it would be nice to come up with variants such as 1) discard inflight request v.s. 2) waiting for inflight processing to be done,

Or inflight descriptor reporting (which seems to be tricky). But we can start from net that a discarding may just work.

Quoted 2 lines

and 3) timeout in
waiting.

Actually, that's the plan and Eugenio is proposing something like this via virtio spec:

https://lists.oasis-open.org/archives/virtio-dev/202111/msg00020.html

Quoted 8 lines
Quoted 3 lines

2) userspace will call SET_VRING_ENABLE() when the device supports otherwise it will use RESET.

Are you looking to making virtqueue resume-able through the new SET_VRING_ENABLE() uAPI?

I think RESET is inevitable in some case, i.e. when guest initiates device reset by writing 0 to the status register.

Yes, that's all my plan.

Quoted 6 lines

For suspend/resume and
live migration use cases, indeed RESET can be substituted with SET_VRING_ENABLE. Again, it'd need quite some code refactoring to accommodate this change. Although I'm all for it, it'd be the best to lay out the plan for multiple phases rather than overload this single patch too much. You can count my time on this endeavor if you don't mind. :)

You're welcome, I agree we should choose a way to go first:

1) manage to use SET_VRING_ENABLE (more like a workaround anyway)
2) go with virtio-spec (may take a while)
3) don't wait for the spec, have a vDPA specific uAPI first. Note that I've chatted with most of the vendors and they seem to be fine with the _S_STOP. If we go this way, we can still provide the forward compatibility of _S_STOP
4) or do them all (in parallel)

Any thoughts?

Thanks

Quoted 205 lines
Quoted 3 lines

And for safety, I suggest tagging this as 7.1.

+1

Regards,
-Siwei

Quoted 196 lines
Quoted 73 lines

---
v4 --> v3
Nothing changed, becasue of issue with mimecast, when the From: tag is different from the sender, the some mail client will take the patch as an attachment, RESEND v3 does not work, So resend the patch as v4

v3 --> v2:
Call vhost_dev_reset() at the end of vhost_net_stop().

Since the vDPA device need re-add the status bit VIRTIO_CONFIG_S_ACKNOWLEDGE and VIRTIO_CONFIG_S_DRIVER, simply, add them inside vhost_vdpa_reset_device, and the only way calling vhost_vdpa_reset_device is in vhost_net_stop(), so it keeps the same behavior as before.

v2 --> v1:
Implement a new function vhost_dev_reset,
reset the backend kernel device at last.

---
  hw/net/vhost_net.c        | 24 +++++++++++++++++++++---
  hw/virtio/vhost-vdpa.c    | 15 +++++++++------
  hw/virtio/vhost.c         | 15 ++++++++++++++-
  include/hw/virtio/vhost.h |  1 +
  4 files changed, 45 insertions(+), 10 deletions(-)
hw/net/vhost_net.c +12 -3
index 30379d2..422c9bf 100644
@@ -325,7 +325,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
      int total_notifiers = data_queue_pairs * 2 + cvq;
      VirtIONet *n = VIRTIO_NET(dev);
      int nvhosts = data_queue_pairs + cvq;
-    struct vhost_net *net;
+    struct vhost_net *net = NULL;
      int r, e, i, index_end = data_queue_pairs * 2;
      NetClientState *peer;
 
@@ -391,8 +391,17 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
  err_start:
      while (--i >= 0) {
          peer = qemu_get_peer(ncs , i);
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
      }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
+    }
+
      e = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
      if (e < 0) {
          fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
@@ -410,6 +419,7 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
      VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
      VirtIONet *n = VIRTIO_NET(dev);
      NetClientState *peer;
+    struct vhost_net *net = NULL;
      int total_notifiers = data_queue_pairs * 2 + cvq;
      int nvhosts = data_queue_pairs + cvq;
      int i, r;
@@ -420,7 +430,15 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
          } else {
              peer = qemu_get_peer(ncs, n->max_queue_pairs);
          }
-        vhost_net_stop_one(get_vhost_net(peer), dev);

hw/net/vhost_net.c void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs, · line 442

Quoted 10 lines
hw/net/vhost_net.c +9 -0
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
+    }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
      }

So we've already reset the device in vhost_vdpa_dev_start(), any reason we need to do it again here?

hw/virtio/vhost-vdpa.c static int vhost_vdpa_reset_device(struct vhost_dev *dev) · line 714

Quoted 13 lines
hw/net/vhost_net.c +0 -0
      r = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
hw/virtio/vhost-vdpa.c +4 -0
index c5ed7a3..3ef0199 100644
@@ -708,6 +708,11 @@ static int vhost_vdpa_reset_device(struct vhost_dev *dev)
 
      ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &status);
      trace_vhost_vdpa_reset_device(dev, status);
+
+    /* Add back this status, so that the device could work next time*/
+    vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
+                               VIRTIO_CONFIG_S_DRIVER);

This seems to contradict the semantic of reset.

Quoted 47 lines
hw/virtio/vhost-vdpa.c +5 -6
+
      return ret;
  }
 
@@ -719,14 +724,14 @@ static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx)
      return idx;
  }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, unsigned int ready)
  {
      int i;
      trace_vhost_vdpa_set_vring_ready(dev);
      for (i = 0; i < dev->nvqs; ++i) {
          struct vhost_vring_state state = {
              .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
          };
          vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
      }
@@ -1088,8 +1093,9 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
          if (unlikely(!ok)) {
              return -1;
          }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
      } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
          ok = vhost_vdpa_svqs_stop(dev);
          if (unlikely(!ok)) {
              return -1;
@@ -1105,9 +1111,6 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
          memory_listener_register(&v->listener, &address_space_memory);
          return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
      } else {
-        vhost_vdpa_reset_device(dev);
-        vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
-                                   VIRTIO_CONFIG_S_DRIVER);
          memory_listener_unregister(&v->listener);
 
          return 0;
hw/virtio/vhost.c +0 -0
index b643f42..7e0cdb6 100644

hw/virtio/vhost.c fail_features: · line 1823

Quoted 4 lines
hw/virtio/vhost.c +0 -1
@@ -1820,7 +1820,6 @@ fail_features:
  void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
  {
      int i;
-

Unnecessary changes.

hw/virtio/vhost.c int vhost_net_set_backend(struct vhost_dev *hdev, · line 1858

Quoted 10 lines
hw/virtio/vhost.c +3 -0
      /* should only be called after backend is connected */
      assert(hdev->vhost_ops);
 
@@ -1854,3 +1853,17 @@ int vhost_net_set_backend(struct vhost_dev *hdev,
 
      return -ENOSYS;
  }
+
+int vhost_dev_reset(struct vhost_dev *hdev)
+{

Let's use a separate patch for this.

Thanks

Quoted 28 lines
hw/virtio/vhost.c +11 -0
+    int ret = 0;
+
+    /* should only be called after backend is connected */
+    assert(hdev->vhost_ops);
+
+    if (hdev->vhost_ops->vhost_reset_device) {
+        ret = hdev->vhost_ops->vhost_reset_device(hdev);
+    }
+
+    return ret;
+}
include/hw/virtio/vhost.h +1 -1
index 58a73e7..b8b7c20 100644
@@ -114,6 +114,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
  void vhost_dev_cleanup(struct vhost_dev *hdev);
  int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
  void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
+int vhost_dev_reset(struct vhost_dev *hdev);
  int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
  void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
 
--

1.8.3.1

#31 lore raw

Re: [PATCH v4] vdpa: reset the backend device in the end of vhost_net_stop()

Quoted 87 lines · Jason Wang

Adding Michael.

Quoted 30 lines · Si-Wei Liu
Quoted 26 lines · Jason Wang
Quoted 16 lines · Michael Qiu

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset many times(with 1 datapath queue pair and one control queue, triggered 3 times), this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_net_stop(), it will stop all vhost device bind to this virtio device, and in vhost_dev_stop(), qemu tries to stop the device , then stop the queue: vhost_virtqueue_stop().

In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and in next loop(stop other vhost backends), qemu try to stop the queue corresponding to the vhost backend,
the driver finds that the VQ is invalied, this is the root cause.

Quoted 5 lines

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

and implement a new function vhost_dev_reset, only reset backend device after all vhost(per-queue) stoped.

Typo.

Quoted 2 lines
Quoted 7 lines
Quoted 4 lines

Rethink this patch, consider there're devices that don't support set_vq_ready(). I wonder if we need

1) uAPI to tell the user space whether or not it supports set_vq_ready()

I guess what's more relevant here is to define the uAPI semantics for unready i.e. set_vq_ready(0) for resuming/stopping virtqueue processing, as starting vq is comparatively less ambiguous.

Yes.

Quoted 4 lines

Considering the
likelihood that this interface may be used for live migration, it would be nice to come up with variants such as 1) discard inflight request v.s. 2) waiting for inflight processing to be done,

Or inflight descriptor reporting (which seems to be tricky). But we can start from net that a discarding may just work.

Quoted 2 lines

and 3) timeout in
waiting.

Actually, that's the plan and Eugenio is proposing something like this via virtio spec:

https://lists.oasis-open.org/archives/virtio-dev/202111/msg00020.html

Quoted 8 lines
Quoted 3 lines

2) userspace will call SET_VRING_ENABLE() when the device supports otherwise it will use RESET.

Are you looking to making virtqueue resume-able through the new SET_VRING_ENABLE() uAPI?

I think RESET is inevitable in some case, i.e. when guest initiates device reset by writing 0 to the status register.

Yes, that's all my plan.

Quoted 6 lines

For suspend/resume and
live migration use cases, indeed RESET can be substituted with SET_VRING_ENABLE. Again, it'd need quite some code refactoring to accommodate this change. Although I'm all for it, it'd be the best to lay out the plan for multiple phases rather than overload this single patch too much. You can count my time on this endeavor if you don't mind. :)

You're welcome, I agree we should choose a way to go first:

1) manage to use SET_VRING_ENABLE (more like a workaround anyway)
2) go with virtio-spec (may take a while)
3) don't wait for the spec, have a vDPA specific uAPI first. Note that I've chatted with most of the vendors and they seem to be fine with the _S_STOP. If we go this way, we can still provide the forward compatibility of _S_STOP
4) or do them all (in parallel)

Quoted 2 lines

Any thoughts?

virtio-spec should be long-term, not only because the spec goes very slowly, but also the hardware upgrade should be a problem.

For short-term, better to take the first one?

Thanks,
Michael

Quoted 209 lines

Thanks

Quoted 205 lines
Quoted 3 lines

And for safety, I suggest tagging this as 7.1.

+1

Regards,
-Siwei

Quoted 196 lines
Quoted 73 lines

---
v4 --> v3
Nothing changed, becasue of issue with mimecast, when the From: tag is different from the sender, the some mail client will take the patch as an attachment, RESEND v3 does not work, So resend the patch as v4

v3 --> v2:
Call vhost_dev_reset() at the end of vhost_net_stop().

Since the vDPA device need re-add the status bit VIRTIO_CONFIG_S_ACKNOWLEDGE and VIRTIO_CONFIG_S_DRIVER, simply, add them inside vhost_vdpa_reset_device, and the only way calling vhost_vdpa_reset_device is in vhost_net_stop(), so it keeps the same behavior as before.

v2 --> v1:
Implement a new function vhost_dev_reset,
reset the backend kernel device at last.

---
   hw/net/vhost_net.c        | 24 +++++++++++++++++++++---
   hw/virtio/vhost-vdpa.c    | 15 +++++++++------
   hw/virtio/vhost.c         | 15 ++++++++++++++-
   include/hw/virtio/vhost.h |  1 +
   4 files changed, 45 insertions(+), 10 deletions(-)
hw/net/vhost_net.c +12 -3
index 30379d2..422c9bf 100644
@@ -325,7 +325,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
       int total_notifiers = data_queue_pairs * 2 + cvq;
       VirtIONet *n = VIRTIO_NET(dev);
       int nvhosts = data_queue_pairs + cvq;
-    struct vhost_net *net;
+    struct vhost_net *net = NULL;
       int r, e, i, index_end = data_queue_pairs * 2;
       NetClientState *peer;
 
@@ -391,8 +391,17 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
   err_start:
       while (--i >= 0) {
           peer = qemu_get_peer(ncs , i);
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
       }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
+    }
+
       e = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
       if (e < 0) {
           fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
@@ -410,6 +419,7 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
       VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
       VirtIONet *n = VIRTIO_NET(dev);
       NetClientState *peer;
+    struct vhost_net *net = NULL;
       int total_notifiers = data_queue_pairs * 2 + cvq;
       int nvhosts = data_queue_pairs + cvq;
       int i, r;
@@ -420,7 +430,15 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
           } else {
               peer = qemu_get_peer(ncs, n->max_queue_pairs);
           }
-        vhost_net_stop_one(get_vhost_net(peer), dev);

hw/net/vhost_net.c void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs, · line 442

Quoted 10 lines
hw/net/vhost_net.c +9 -0
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
+    }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
       }

So we've already reset the device in vhost_vdpa_dev_start(), any reason we need to do it again here?

hw/virtio/vhost-vdpa.c static int vhost_vdpa_reset_device(struct vhost_dev *dev) · line 714

Quoted 13 lines
hw/net/vhost_net.c +0 -0
       r = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
hw/virtio/vhost-vdpa.c +4 -0
index c5ed7a3..3ef0199 100644
@@ -708,6 +708,11 @@ static int vhost_vdpa_reset_device(struct vhost_dev *dev)
 
       ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &status);
       trace_vhost_vdpa_reset_device(dev, status);
+
+    /* Add back this status, so that the device could work next time*/
+    vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
+                               VIRTIO_CONFIG_S_DRIVER);

This seems to contradict the semantic of reset.

Quoted 47 lines
hw/virtio/vhost-vdpa.c +5 -6
+
       return ret;
   }
 
@@ -719,14 +724,14 @@ static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx)
       return idx;
   }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, unsigned int ready)
   {
       int i;
       trace_vhost_vdpa_set_vring_ready(dev);
       for (i = 0; i < dev->nvqs; ++i) {
           struct vhost_vring_state state = {
               .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
           };
           vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
       }
@@ -1088,8 +1093,9 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
           if (unlikely(!ok)) {
               return -1;
           }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
       } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
           ok = vhost_vdpa_svqs_stop(dev);
           if (unlikely(!ok)) {
               return -1;
@@ -1105,9 +1111,6 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
           memory_listener_register(&v->listener, &address_space_memory);
           return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
       } else {
-        vhost_vdpa_reset_device(dev);
-        vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
-                                   VIRTIO_CONFIG_S_DRIVER);
           memory_listener_unregister(&v->listener);
 
           return 0;
hw/virtio/vhost.c +0 -0
index b643f42..7e0cdb6 100644

hw/virtio/vhost.c fail_features: · line 1823

Quoted 4 lines
hw/virtio/vhost.c +0 -1
@@ -1820,7 +1820,6 @@ fail_features:
   void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
   {
       int i;
-

Unnecessary changes.

hw/virtio/vhost.c int vhost_net_set_backend(struct vhost_dev *hdev, · line 1858

Quoted 10 lines
hw/virtio/vhost.c +3 -0
       /* should only be called after backend is connected */
       assert(hdev->vhost_ops);
 
@@ -1854,3 +1853,17 @@ int vhost_net_set_backend(struct vhost_dev *hdev,
 
       return -ENOSYS;
   }
+
+int vhost_dev_reset(struct vhost_dev *hdev)
+{

Let's use a separate patch for this.

Thanks

Quoted 28 lines
hw/virtio/vhost.c +11 -0
+    int ret = 0;
+
+    /* should only be called after backend is connected */
+    assert(hdev->vhost_ops);
+
+    if (hdev->vhost_ops->vhost_reset_device) {
+        ret = hdev->vhost_ops->vhost_reset_device(hdev);
+    }
+
+    return ret;
+}
include/hw/virtio/vhost.h +1 -1
index 58a73e7..b8b7c20 100644
@@ -114,6 +114,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
   void vhost_dev_cleanup(struct vhost_dev *hdev);
   int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
   void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
+int vhost_dev_reset(struct vhost_dev *hdev);
   int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
   void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
 
--

1.8.3.1

#32 lore raw

Re: [PATCH v4] vdpa: reset the backend device in the end of vhost_net_stop()

In reply to Jason Wang
Quoted 48 lines · Jason Wang

Adding Michael.

Quoted 29 lines · Si-Wei Liu
Quoted 26 lines · Jason Wang
Quoted 16 lines · Michael Qiu

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset many times(with 1 datapath queue pair and one control queue, triggered 3 times), this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_net_stop(), it will stop all vhost device bind to this virtio device, and in vhost_dev_stop(), qemu tries to stop the device , then stop the queue: vhost_virtqueue_stop().

In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and in next loop(stop other vhost backends), qemu try to stop the queue corresponding to the vhost backend,
the driver finds that the VQ is invalied, this is the root cause.

Quoted 5 lines

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

and implement a new function vhost_dev_reset, only reset backend device after all vhost(per-queue) stoped.

Typo.

Quoted 2 lines
Quoted 7 lines
Quoted 4 lines

Rethink this patch, consider there're devices that don't support set_vq_ready(). I wonder if we need

1) uAPI to tell the user space whether or not it supports set_vq_ready()

I guess what's more relevant here is to define the uAPI semantics for unready i.e. set_vq_ready(0) for resuming/stopping virtqueue processing, as starting vq is comparatively less ambiguous.

Yes.

Quoted 4 lines

Considering the
likelihood that this interface may be used for live migration, it would be nice to come up with variants such as 1) discard inflight request v.s. 2) waiting for inflight processing to be done,

Or inflight descriptor reporting (which seems to be tricky). But we can start from net that a discarding may just work.

Quoted 6 lines
Quoted 2 lines

and 3) timeout in
waiting.

Actually, that's the plan and Eugenio is proposing something like this via virtio spec:

https://urldefense.com/v3/__https://lists.oasis-open.org/archives/virtio-dev/202111/msg00020.html__;!!ACWV5N9M2RV99hQ!bcX6i6_atR-6Gcl-4q5Tekab_xDuXr7lDAMw2E1hilZ_1cZIX1c5mztQtvsnjiiy$

Thanks for the pointer, I seem to recall I saw it some time back though I wonder if there's follow-up for the v3? My impression was that this is still a work-in-progress spec proposal, while the semantics of various F_STOP scenario is unclear yet and not all of the requirements (ex: STOP_FAILED, rewind & !IN_ORDER) for live migration do seem to get accommodated?

Quoted 16 lines
Quoted 8 lines
Quoted 3 lines

2) userspace will call SET_VRING_ENABLE() when the device supports otherwise it will use RESET.

Are you looking to making virtqueue resume-able through the new SET_VRING_ENABLE() uAPI?

I think RESET is inevitable in some case, i.e. when guest initiates device reset by writing 0 to the status register.

Yes, that's all my plan.

Quoted 6 lines

For suspend/resume and
live migration use cases, indeed RESET can be substituted with SET_VRING_ENABLE. Again, it'd need quite some code refactoring to accommodate this change. Although I'm all for it, it'd be the best to lay out the plan for multiple phases rather than overload this single patch too much. You can count my time on this endeavor if you don't mind. :)

Quoted 3 lines

You're welcome, I agree we should choose a way to go first:

1) manage to use SET_VRING_ENABLE (more like a workaround anyway)

For networking device and the vq suspend/resume and live migration use cases to support, I thought it might suffice? We may drop inflight or unused ones for Ethernet... What other part do you think may limit its extension to become a general uAPI or add new uAPI to address similar VQ stop requirement if need be? Or we might well define subsystem specific uAPI to stop the virtqueue, for vdpa device specifically? I think the point here is given that we would like to avoid guest side modification to support live migration, we can define specific uAPI for specific live migration requirement without having to involve guest driver change. It'd be easy to get started this way and generalize them all to a full blown _S_STOP when things are eventually settled.

Quoted 1 line

2) go with virtio-spec (may take a while)

I feel it might be still quite early for now to get to a full blown _S_STOP spec level amendment that works for all types of virtio (vendor) devices. Generally there can be very specific subsystem-dependent ways to stop each type of virtio devices that satisfies the live migration of virtio subsystem devices. For now the discussion mostly concerns with vq index rewind, inflight handling, notification interrupt and configuration space such kind of virtio level things, but real device backend has implication on the other parts such as the order of IO/DMA quiescing and interrupt masking. If the subsystem virtio guest drivers today somehow don't support any of those _S_STOP new behaviors, I guess it's with little point to introduce the same or similar _S_STOP functionality to the guest driver to effectively support live migration.

Thanks,
-Siwei

Quoted 211 lines

3) don't wait for the spec, have a vDPA specific uAPI first. Note that I've chatted with most of the vendors and they seem to be fine with the _S_STOP. If we go this way, we can still provide the forward compatibility of _S_STOP
4) or do them all (in parallel)

Any thoughts?

Thanks

Quoted 201 lines
Quoted 1 line

And for safety, I suggest tagging this as 7.1.

+1

Regards,
-Siwei

Quoted 195 lines
Quoted 72 lines

---
v4 --> v3
Nothing changed, becasue of issue with mimecast, when the From: tag is different from the sender, the some mail client will take the patch as an attachment, RESEND v3 does not work, So resend the patch as v4

v3 --> v2:
Call vhost_dev_reset() at the end of vhost_net_stop().

Since the vDPA device need re-add the status bit VIRTIO_CONFIG_S_ACKNOWLEDGE and VIRTIO_CONFIG_S_DRIVER, simply, add them inside vhost_vdpa_reset_device, and the only way calling vhost_vdpa_reset_device is in vhost_net_stop(), so it keeps the same behavior as before.

v2 --> v1:
Implement a new function vhost_dev_reset,
reset the backend kernel device at last.

---
   hw/net/vhost_net.c        | 24 +++++++++++++++++++++---
   hw/virtio/vhost-vdpa.c    | 15 +++++++++------
   hw/virtio/vhost.c         | 15 ++++++++++++++-
   include/hw/virtio/vhost.h |  1 +
   4 files changed, 45 insertions(+), 10 deletions(-)
hw/net/vhost_net.c +12 -3
index 30379d2..422c9bf 100644
@@ -325,7 +325,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
       int total_notifiers = data_queue_pairs * 2 + cvq;
       VirtIONet *n = VIRTIO_NET(dev);
       int nvhosts = data_queue_pairs + cvq;
-    struct vhost_net *net;
+    struct vhost_net *net = NULL;
       int r, e, i, index_end = data_queue_pairs * 2;
       NetClientState *peer;
 
@@ -391,8 +391,17 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
   err_start:
       while (--i >= 0) {
           peer = qemu_get_peer(ncs , i);
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
       }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
+    }
+
       e = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
       if (e < 0) {
           fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
@@ -410,6 +419,7 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
       VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
       VirtIONet *n = VIRTIO_NET(dev);
       NetClientState *peer;
+    struct vhost_net *net = NULL;
       int total_notifiers = data_queue_pairs * 2 + cvq;
       int nvhosts = data_queue_pairs + cvq;
       int i, r;
@@ -420,7 +430,15 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
           } else {
               peer = qemu_get_peer(ncs, n->max_queue_pairs);
           }
-        vhost_net_stop_one(get_vhost_net(peer), dev);

hw/net/vhost_net.c void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs, · line 442

Quoted 10 lines
hw/net/vhost_net.c +9 -0
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
+    }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
       }

So we've already reset the device in vhost_vdpa_dev_start(), any reason we need to do it again here?

hw/virtio/vhost-vdpa.c static int vhost_vdpa_reset_device(struct vhost_dev *dev) · line 714

Quoted 13 lines
hw/net/vhost_net.c +0 -0
       r = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
hw/virtio/vhost-vdpa.c +4 -0
index c5ed7a3..3ef0199 100644
@@ -708,6 +708,11 @@ static int vhost_vdpa_reset_device(struct vhost_dev *dev)
 
       ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &status);
       trace_vhost_vdpa_reset_device(dev, status);
+
+    /* Add back this status, so that the device could work next time*/
+    vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
+                               VIRTIO_CONFIG_S_DRIVER);

This seems to contradict the semantic of reset.

Quoted 47 lines
hw/virtio/vhost-vdpa.c +5 -6
+
       return ret;
   }
 
@@ -719,14 +724,14 @@ static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx)
       return idx;
   }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, unsigned int ready)
   {
       int i;
       trace_vhost_vdpa_set_vring_ready(dev);
       for (i = 0; i < dev->nvqs; ++i) {
           struct vhost_vring_state state = {
               .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
           };
           vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
       }
@@ -1088,8 +1093,9 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
           if (unlikely(!ok)) {
               return -1;
           }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
       } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
           ok = vhost_vdpa_svqs_stop(dev);
           if (unlikely(!ok)) {
               return -1;
@@ -1105,9 +1111,6 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
           memory_listener_register(&v->listener, &address_space_memory);
           return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
       } else {
-        vhost_vdpa_reset_device(dev);
-        vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
-                                   VIRTIO_CONFIG_S_DRIVER);
           memory_listener_unregister(&v->listener);
 
           return 0;
hw/virtio/vhost.c +0 -0
index b643f42..7e0cdb6 100644

hw/virtio/vhost.c fail_features: · line 1823

Quoted 4 lines
hw/virtio/vhost.c +0 -1
@@ -1820,7 +1820,6 @@ fail_features:
   void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
   {
       int i;
-

Unnecessary changes.

hw/virtio/vhost.c int vhost_net_set_backend(struct vhost_dev *hdev, · line 1858

Quoted 10 lines
hw/virtio/vhost.c +3 -0
       /* should only be called after backend is connected */
       assert(hdev->vhost_ops);
 
@@ -1854,3 +1853,17 @@ int vhost_net_set_backend(struct vhost_dev *hdev,
 
       return -ENOSYS;
   }
+
+int vhost_dev_reset(struct vhost_dev *hdev)
+{

Let's use a separate patch for this.

Thanks

Quoted 28 lines
hw/virtio/vhost.c +11 -0
+    int ret = 0;
+
+    /* should only be called after backend is connected */
+    assert(hdev->vhost_ops);
+
+    if (hdev->vhost_ops->vhost_reset_device) {
+        ret = hdev->vhost_ops->vhost_reset_device(hdev);
+    }
+
+    return ret;
+}
include/hw/virtio/vhost.h +1 -1
index 58a73e7..b8b7c20 100644
@@ -114,6 +114,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
   void vhost_dev_cleanup(struct vhost_dev *hdev);
   int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
   void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
+int vhost_dev_reset(struct vhost_dev *hdev);
   int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
   void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
 
--

1.8.3.1

#33 lore raw

Re: [PATCH v4] vdpa: reset the backend device in the end of vhost_net_stop()

Quoted 51 lines · Si-Wei Liu
Quoted 48 lines · Jason Wang

Adding Michael.

Quoted 29 lines · Si-Wei Liu
Quoted 26 lines · Jason Wang
Quoted 16 lines · Michael Qiu

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset many times(with 1 datapath queue pair and one control queue, triggered 3 times), this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_net_stop(), it will stop all vhost device bind to this virtio device, and in vhost_dev_stop(), qemu tries to stop the device , then stop the queue: vhost_virtqueue_stop().

In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and in next loop(stop other vhost backends), qemu try to stop the queue corresponding to the vhost backend, the driver finds that the VQ is invalied, this is the root cause.

Quoted 5 lines

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

and implement a new function vhost_dev_reset, only reset backend device after all vhost(per-queue) stoped.

Typo.

Quoted 2 lines
Quoted 7 lines
Quoted 4 lines

Rethink this patch, consider there're devices that don't support set_vq_ready(). I wonder if we need

1) uAPI to tell the user space whether or not it supports set_vq_ready()

I guess what's more relevant here is to define the uAPI semantics for unready i.e. set_vq_ready(0) for resuming/stopping virtqueue processing, as starting vq is comparatively less ambiguous.

Yes.

Quoted 4 lines

Considering the
likelihood that this interface may be used for live migration, it would be nice to come up with variants such as 1) discard inflight request v.s. 2) waiting for inflight processing to be done,

Or inflight descriptor reporting (which seems to be tricky). But we can start from net that a discarding may just work.

Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

and 3) timeout in
waiting.

Actually, that's the plan and Eugenio is proposing something like this via virtio spec:

https://urldefense.com/v3/__https://lists.oasis-open.org/archives/virtio-dev/202111/msg00020.html__;!!ACWV5N9M2RV99hQ!bcX6i6_atR-6Gcl-4q5Tekab_xDuXr7lDAMw2E1hilZ_1cZIX1c5mztQtvsnjiiy$

Thanks for the pointer, I seem to recall I saw it some time back though I wonder if there's follow-up for the v3? My impression was that this is still a work-in-progress spec proposal, while the semantics of various F_STOP scenario is unclear yet and not all of the requirements (ex: STOP_FAILED, rewind & !IN_ORDER) for live migration do seem to get accommodated?

My understanding is that, the reason for STOP_FAILED and IN_ORDER is because we don't have a way to report inflight descriptors. We will try to overcome those by allow the device to report inflight descriptors in the next version.

Quoted 17 lines
Quoted 17 lines
Quoted 9 lines
Quoted 4 lines

2) userspace will call SET_VRING_ENABLE() when the device supports otherwise it will use RESET.

Are you looking to making virtqueue resume-able through the new SET_VRING_ENABLE() uAPI?

I think RESET is inevitable in some case, i.e. when guest initiates device reset by writing 0 to the status register.

Yes, that's all my plan.

Quoted 6 lines

For suspend/resume and
live migration use cases, indeed RESET can be substituted with SET_VRING_ENABLE. Again, it'd need quite some code refactoring to accommodate this change. Although I'm all for it, it'd be the best to lay out the plan for multiple phases rather than overload this single patch too much. You can count my time on this endeavor if you don't mind. :)

Quoted 4 lines
Quoted 3 lines

You're welcome, I agree we should choose a way to go first:

1) manage to use SET_VRING_ENABLE (more like a workaround anyway)

For networking device and the vq suspend/resume and live migration use cases to support, I thought it might suffice?

Without config space change it would be sufficient. And anyhow the vDPA parent can prevent the config change if all the virtqueue is disabled.

Quoted 1 line

We may drop inflight or unused ones for Ethernet...

Yes.

Quoted 1 line

What other part do you think may limit its extension to become a general uAPI or add new uAPI to address similar VQ stop requirement if need be?

For networking, we don't need other.

Quoted 1 line

Or we might well define subsystem specific uAPI to stop the virtqueue, for vdpa device specifically?

Anyhow we need a uAPI consider we have some parent that doesn't support virtqueue stop. So this could be another way to go.

But if we decide to bother with new uAPI, I would rather go with a new uAPI for stop the device. It can help for the config space change as well.

Quoted 1 line

I think the point here is given that we would like to avoid guest side modification to support live migration, we can define specific uAPI for specific live migration requirement without having to involve guest driver change. It'd be easy to get started this way and generalize them all to a full blown _S_STOP when things are eventually settled.

Yes, note that the status seen by guest is mediated by the hypervisor. So the hypervisor can choose not to hide the _S_STOP from the guest to keep the migration work without modifications in the guest driver.

Quoted 3 lines
Quoted 2 lines

2) go with virtio-spec (may take a while)

I feel it might be still quite early for now to get to a full blown _S_STOP spec level amendment that works for all types of virtio (vendor) devices. Generally there can be very specific subsystem-dependent ways to stop each type of virtio devices that satisfies the live migration of virtio subsystem devices.

Yes.

Quoted 1 line

For now the discussion mostly concerns with vq index rewind, inflight handling, notification interrupt and configuration space such kind of virtio level things, but real device backend has implication on the other parts such as the order of IO/DMA quiescing and interrupt masking.

It's the charge of the vDPA parent to perform any necessary quiescing to satisfy the semantic of _S_STOP, it's an implementation detail which is out of the scope of the spec.

Quoted 1 line

If the subsystem virtio guest drivers today somehow don't support any of those _S_STOP new behaviors, I guess it's with little point to introduce the same or similar _S_STOP functionality to the guest driver to effectively support live migration.

See above, the live migration is transparent to the guest. For the driver that doesn't support _S_STOP, we can still live migrate it. The only interesting part is nesting: if we want to live migrate a nested guest we need to the guest driver must support _S_STOP.

Thanks

Quoted 230 lines

Thanks,
-Siwei

Quoted 225 lines

3) don't wait for the spec, have a vDPA specific uAPI first. Note that I've chatted with most of the vendors and they seem to be fine with the _S_STOP. If we go this way, we can still provide the forward compatibility of _S_STOP
4) or do them all (in parallel)

Any thoughts?

Thanks

Quoted 215 lines
Quoted 1 line

And for safety, I suggest tagging this as 7.1.

+1

Regards,
-Siwei

Quoted 209 lines
Quoted 78 lines

---
v4 --> v3
      Nothing changed, becasue of issue with mimecast, when the From: tag is different from the sender, the some mail client will take the patch as an attachment, RESEND v3 does not work, So resend the patch as v4

v3 --> v2:
      Call vhost_dev_reset() at the end of vhost_net_stop().

      Since the vDPA device need re-add the status bit VIRTIO_CONFIG_S_ACKNOWLEDGE and VIRTIO_CONFIG_S_DRIVER, simply, add them inside vhost_vdpa_reset_device, and the only way calling vhost_vdpa_reset_device is in vhost_net_stop(), so it keeps the same behavior as before.

v2 --> v1:
     Implement a new function vhost_dev_reset, reset the backend kernel device at last.
---
   hw/net/vhost_net.c        | 24 +++++++++++++++++++++--- hw/virtio/vhost-vdpa.c    | 15 +++++++++------ hw/virtio/vhost.c         | 15 ++++++++++++++- include/hw/virtio/vhost.h |  1 +

   4 files changed, 45 insertions(+), 10 deletions(-)
hw/net/vhost_net.c +1 -1
index 30379d2..422c9bf 100644
@@ -325,7 +325,7 @@ int vhost_net_start(VirtIODevice *dev, 
 NetClientState *ncs,
        int total_notifiers = data_queue_pairs * 2 + cvq;
        VirtIONet *n = VIRTIO_NET(dev);
        int nvhosts = data_queue_pairs + cvq;
-    struct vhost_net *net;
+    struct vhost_net *net = NULL;
        int r, e, i, index_end = data_queue_pairs * 2;
        NetClientState *peer;
@@ -391,8 +391,17 @@ int vhost_net_start(VirtIODevice *dev, 
 NetClientState *ncs,

   err_start:
       while (--i >= 0) {
           peer = qemu_get_peer(ncs , i);
-        vhost_net_stop_one(get_vhost_net(peer), dev);

hw/net/vhost_net.c +4 -0
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);

       }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
+    }
+
       e = k->set_guest_notifiers(qbus->parent, total_notifiers, false); if (e < 0) {
           fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);

hw/net/vhost_net.c +1 -0
@@ -410,6 +419,7 @@ void vhost_net_stop(VirtIODevice *dev, 
 NetClientState *ncs,
        VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
        VirtIONet *n = VIRTIO_NET(dev);
        NetClientState *peer;
+    struct vhost_net *net = NULL;
        int total_notifiers = data_queue_pairs * 2 + cvq;
        int nvhosts = data_queue_pairs + cvq;

       int i, r;

hw/net/vhost_net.c +1 -1
@@ -420,7 +430,15 @@ void vhost_net_stop(VirtIODevice *dev, 
 NetClientState *ncs,
            } else {
                peer = qemu_get_peer(ncs, n->max_queue_pairs);
            }
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+

hw/net/vhost_net.c void vhost_net_stop(VirtIODevice *dev, · line 444

Quoted 10 lines
hw/net/vhost_net.c +8 -0
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
+    }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == 
 VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
        }

So we've already reset the device in vhost_vdpa_dev_start(), any reason we need to do it again here?

Quoted 12 lines

       r = k->set_guest_notifiers(qbus->parent, total_notifiers, false);

hw/virtio/vhost-vdpa.c +2 -0
index c5ed7a3..3ef0199 100644
@@ -708,6 +708,11 @@ static int vhost_vdpa_reset_device(struct 
 vhost_dev *dev)
 
        ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &status);
        trace_vhost_vdpa_reset_device(dev, status);
+
+    /* Add back this status, so that the device could work next 
Quoted 3 lines

time*/
+    vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE | +                               VIRTIO_CONFIG_S_DRIVER);

This seems to contradict the semantic of reset.

Quoted 51 lines

+
       return ret;
   }

hw/virtio/vhost-vdpa.c +2 -2
@@ -719,14 +724,14 @@ static int vhost_vdpa_get_vq_index(struct 
 vhost_dev *dev, int idx)
        return idx;
    }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, 
 unsigned int ready)
    {
        int i;
        trace_vhost_vdpa_set_vring_ready(dev);
        for (i = 0; i < dev->nvqs; ++i) {
            struct vhost_vring_state state = {
                .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
            };

           vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state); }

hw/virtio/vhost-vdpa.c +2 -1
@@ -1088,8 +1093,9 @@ static int vhost_vdpa_dev_start(struct 
 vhost_dev *dev, bool started)
            if (unlikely(!ok)) {
                return -1;
            }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
        } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
            ok = vhost_vdpa_svqs_stop(dev);
            if (unlikely(!ok)) {

               return -1;

hw/virtio/vhost-vdpa.c +0 -3
@@ -1105,9 +1111,6 @@ static int vhost_vdpa_dev_start(struct 
 vhost_dev *dev, bool started)
            memory_listener_register(&v->listener, &address_space_memory);
            return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
        } else {
-        vhost_vdpa_reset_device(dev);
-        vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
- VIRTIO_CONFIG_S_DRIVER);
 memory_listener_unregister(&v->listener);
 

           return 0;

hw/virtio/vhost.c +0 -0
index b643f42..7e0cdb6 100644

hw/virtio/vhost.c fail_features: · line 1823

Quoted 4 lines
hw/virtio/vhost.c +0 -1
@@ -1820,7 +1820,6 @@ fail_features:
    void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
    {
        int i;
-

Unnecessary changes.

hw/virtio/vhost.c int vhost_net_set_backend(struct vhost_dev *hdev, · line 1858

Quoted 11 lines
hw/virtio/vhost.c +0 -0
        /* should only be called after backend is connected */
        assert(hdev->vhost_ops);
 
@@ -1854,3 +1853,17 @@ int vhost_net_set_backend(struct vhost_dev 
 *hdev,
 
        return -ENOSYS;

   }

hw/virtio/vhost.c +3 -0
+
+int vhost_dev_reset(struct vhost_dev *hdev)
+{

Let's use a separate patch for this.

Thanks

Quoted 29 lines

+    int ret = 0;

hw/virtio/vhost.c +10 -0
+
+    /* should only be called after backend is connected */
+    assert(hdev->vhost_ops);
+
+    if (hdev->vhost_ops->vhost_reset_device) {
+        ret = hdev->vhost_ops->vhost_reset_device(hdev);
+    }
+
+    return ret;
+}
include/hw/virtio/vhost.h +1 -0
index 58a73e7..b8b7c20 100644
@@ -114,6 +114,7 @@ int vhost_dev_init(struct vhost_dev *hdev, 
 void *opaque,
    void vhost_dev_cleanup(struct vhost_dev *hdev);
    int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
    void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
+int vhost_dev_reset(struct vhost_dev *hdev);
    int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
    void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
Signature · 2 lines
-- 
1.8.3.1

A message that is not in the archive (sent off-list or not yet received): [email protected]

#35 lore raw

Re: [PATCH v4] vdpa: reset the backend device in the end of vhost_net_stop()

Quoted 90 lines · Michael Qiu
Quoted 87 lines · Jason Wang

Adding Michael.

Quoted 30 lines · Si-Wei Liu
Quoted 26 lines · Jason Wang
Quoted 16 lines · Michael Qiu

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset many times(with 1 datapath queue pair and one control queue, triggered 3 times), this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_net_stop(), it will stop all vhost device bind to this virtio device, and in vhost_dev_stop(), qemu tries to stop the device , then stop the queue: vhost_virtqueue_stop().

In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and in next loop(stop other vhost backends), qemu try to stop the queue corresponding to the vhost backend, the driver finds that the VQ is invalied, this is the root cause.

Quoted 5 lines

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

and implement a new function vhost_dev_reset, only reset backend device after all vhost(per-queue) stoped.

Typo.

Quoted 2 lines
Quoted 7 lines
Quoted 4 lines

Rethink this patch, consider there're devices that don't support set_vq_ready(). I wonder if we need

1) uAPI to tell the user space whether or not it supports set_vq_ready()

I guess what's more relevant here is to define the uAPI semantics for unready i.e. set_vq_ready(0) for resuming/stopping virtqueue processing, as starting vq is comparatively less ambiguous.

Yes.

Quoted 4 lines

Considering the
likelihood that this interface may be used for live migration, it would be nice to come up with variants such as 1) discard inflight request v.s. 2) waiting for inflight processing to be done,

Or inflight descriptor reporting (which seems to be tricky). But we can start from net that a discarding may just work.

Quoted 2 lines

and 3) timeout in
waiting.

Actually, that's the plan and Eugenio is proposing something like this via virtio spec:

https://lists.oasis-open.org/archives/virtio-dev/202111/msg00020.html

Quoted 8 lines
Quoted 3 lines

2) userspace will call SET_VRING_ENABLE() when the device supports otherwise it will use RESET.

Are you looking to making virtqueue resume-able through the new SET_VRING_ENABLE() uAPI?

I think RESET is inevitable in some case, i.e. when guest initiates device reset by writing 0 to the status register.

Yes, that's all my plan.

Quoted 6 lines

For suspend/resume and
live migration use cases, indeed RESET can be substituted with SET_VRING_ENABLE. Again, it'd need quite some code refactoring to accommodate this change. Although I'm all for it, it'd be the best to lay out the plan for multiple phases rather than overload this single patch too much. You can count my time on this endeavor if you don't mind. :)

You're welcome, I agree we should choose a way to go first:

1) manage to use SET_VRING_ENABLE (more like a workaround anyway)
2) go with virtio-spec (may take a while)
3) don't wait for the spec, have a vDPA specific uAPI first. Note that I've chatted with most of the vendors and they seem to be fine with the _S_STOP. If we go this way, we can still provide the forward compatibility of _S_STOP
4) or do them all (in parallel)

Quoted 6 lines
Quoted 2 lines

Any thoughts?

virtio-spec should be long-term, not only because the spec goes very slowly, but also the hardware upgrade should be a problem.

For short-term, better to take the first one?

Consider we need a new uAPI anyhow, I prefer for 2) but you can try 1) and see what people think.

Thanks

Quoted 228 lines

Thanks,
Michael

Quoted 223 lines

Thanks

Quoted 219 lines
Quoted 3 lines

And for safety, I suggest tagging this as 7.1.

+1

Regards,
-Siwei

Quoted 210 lines
Quoted 79 lines

---
v4 --> v3
      Nothing changed, becasue of issue with mimecast, when the From: tag is different from the sender, the some mail client will take the patch as an attachment, RESEND v3 does not work, So resend the patch as v4

v3 --> v2:
      Call vhost_dev_reset() at the end of vhost_net_stop().

      Since the vDPA device need re-add the status bit VIRTIO_CONFIG_S_ACKNOWLEDGE and VIRTIO_CONFIG_S_DRIVER, simply, add them inside vhost_vdpa_reset_device, and the only way calling vhost_vdpa_reset_device is in vhost_net_stop(), so it keeps the same behavior as before.

v2 --> v1:
     Implement a new function vhost_dev_reset, reset the backend kernel device at last.
---
   hw/net/vhost_net.c        | 24 +++++++++++++++++++++--- hw/virtio/vhost-vdpa.c    | 15 +++++++++------ hw/virtio/vhost.c         | 15 ++++++++++++++- include/hw/virtio/vhost.h |  1 +

   4 files changed, 45 insertions(+), 10 deletions(-)
hw/net/vhost_net.c +1 -1
index 30379d2..422c9bf 100644
@@ -325,7 +325,7 @@ int vhost_net_start(VirtIODevice *dev, 
 NetClientState *ncs,
        int total_notifiers = data_queue_pairs * 2 + cvq;
        VirtIONet *n = VIRTIO_NET(dev);
        int nvhosts = data_queue_pairs + cvq;
-    struct vhost_net *net;
+    struct vhost_net *net = NULL;
        int r, e, i, index_end = data_queue_pairs * 2;
        NetClientState *peer;
@@ -391,8 +391,17 @@ int vhost_net_start(VirtIODevice *dev, 
 NetClientState *ncs,

   err_start:
       while (--i >= 0) {
           peer = qemu_get_peer(ncs , i);
-        vhost_net_stop_one(get_vhost_net(peer), dev);

hw/net/vhost_net.c +4 -0
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);

       }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
+    }
+
       e = k->set_guest_notifiers(qbus->parent, total_notifiers, false); if (e < 0) {
           fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);

hw/net/vhost_net.c +1 -0
@@ -410,6 +419,7 @@ void vhost_net_stop(VirtIODevice *dev, 
 NetClientState *ncs,
        VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
        VirtIONet *n = VIRTIO_NET(dev);
        NetClientState *peer;
+    struct vhost_net *net = NULL;
        int total_notifiers = data_queue_pairs * 2 + cvq;
        int nvhosts = data_queue_pairs + cvq;

       int i, r;

hw/net/vhost_net.c +1 -1
@@ -420,7 +430,15 @@ void vhost_net_stop(VirtIODevice *dev, 
 NetClientState *ncs,
            } else {
                peer = qemu_get_peer(ncs, n->max_queue_pairs);
            }
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+

hw/net/vhost_net.c void vhost_net_stop(VirtIODevice *dev, · line 444

Quoted 10 lines
hw/net/vhost_net.c +8 -0
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
+    }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == 
 VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
        }

So we've already reset the device in vhost_vdpa_dev_start(), any reason we need to do it again here?

Quoted 12 lines

       r = k->set_guest_notifiers(qbus->parent, total_notifiers, false);

hw/virtio/vhost-vdpa.c +2 -0
index c5ed7a3..3ef0199 100644
@@ -708,6 +708,11 @@ static int vhost_vdpa_reset_device(struct 
 vhost_dev *dev)
 
        ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &status);
        trace_vhost_vdpa_reset_device(dev, status);
+
+    /* Add back this status, so that the device could work next 
Quoted 3 lines

time*/
+    vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE | +                               VIRTIO_CONFIG_S_DRIVER);

This seems to contradict the semantic of reset.

Quoted 51 lines

+
       return ret;
   }

hw/virtio/vhost-vdpa.c +2 -2
@@ -719,14 +724,14 @@ static int vhost_vdpa_get_vq_index(struct 
 vhost_dev *dev, int idx)
        return idx;
    }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, 
 unsigned int ready)
    {
        int i;
        trace_vhost_vdpa_set_vring_ready(dev);
        for (i = 0; i < dev->nvqs; ++i) {
            struct vhost_vring_state state = {
                .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
            };

           vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state); }

hw/virtio/vhost-vdpa.c +2 -1
@@ -1088,8 +1093,9 @@ static int vhost_vdpa_dev_start(struct 
 vhost_dev *dev, bool started)
            if (unlikely(!ok)) {
                return -1;
            }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
        } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
            ok = vhost_vdpa_svqs_stop(dev);
            if (unlikely(!ok)) {

               return -1;

hw/virtio/vhost-vdpa.c +0 -3
@@ -1105,9 +1111,6 @@ static int vhost_vdpa_dev_start(struct 
 vhost_dev *dev, bool started)
            memory_listener_register(&v->listener, &address_space_memory);
            return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
        } else {
-        vhost_vdpa_reset_device(dev);
-        vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
- VIRTIO_CONFIG_S_DRIVER);
 memory_listener_unregister(&v->listener);
 

           return 0;

hw/virtio/vhost.c +0 -0
index b643f42..7e0cdb6 100644

hw/virtio/vhost.c fail_features: · line 1823

Quoted 4 lines
hw/virtio/vhost.c +0 -1
@@ -1820,7 +1820,6 @@ fail_features:
    void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
    {
        int i;
-

Unnecessary changes.

hw/virtio/vhost.c int vhost_net_set_backend(struct vhost_dev *hdev, · line 1858

Quoted 11 lines
hw/virtio/vhost.c +0 -0
        /* should only be called after backend is connected */
        assert(hdev->vhost_ops);
 
@@ -1854,3 +1853,17 @@ int vhost_net_set_backend(struct vhost_dev 
 *hdev,
 
        return -ENOSYS;

   }

hw/virtio/vhost.c +3 -0
+
+int vhost_dev_reset(struct vhost_dev *hdev)
+{

Let's use a separate patch for this.

Thanks

Quoted 29 lines

+    int ret = 0;

hw/virtio/vhost.c +10 -0
+
+    /* should only be called after backend is connected */
+    assert(hdev->vhost_ops);
+
+    if (hdev->vhost_ops->vhost_reset_device) {
+        ret = hdev->vhost_ops->vhost_reset_device(hdev);
+    }
+
+    return ret;
+}
include/hw/virtio/vhost.h +1 -0
index 58a73e7..b8b7c20 100644
@@ -114,6 +114,7 @@ int vhost_dev_init(struct vhost_dev *hdev, 
 void *opaque,
    void vhost_dev_cleanup(struct vhost_dev *hdev);
    int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
    void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
+int vhost_dev_reset(struct vhost_dev *hdev);
    int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
    void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
Signature · 2 lines
-- 
1.8.3.1

A message that is not in the archive (sent off-list or not yet received): [email protected]

#37 lore raw

Re: [PATCH v4] vdpa: reset the backend device in the end of vhost_net_stop()

Quoted 41 lines · Michael Qiu
Quoted 30 lines · Jason Wang
Quoted 17 lines · Michael Qiu

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset many times(with 1 datapath queue pair and one control queue, triggered 3 times), this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_net_stop(), it will stop all vhost device bind to this virtio device, and in vhost_dev_stop(), qemu tries to stop the device , then stop the queue: vhost_virtqueue_stop().

In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and in next loop(stop other vhost backends), qemu try to stop the queue corresponding to the vhost backend,
the driver finds that the VQ is invalied, this is the root cause.

Quoted 5 lines

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

and implement a new function vhost_dev_reset, only reset backend device after all vhost(per-queue) stoped.

Typo.

Quoted 3 lines
Quoted 6 lines

Rethink this patch, consider there're devices that don't support set_vq_ready(). I wonder if we need

1) uAPI to tell the user space whether or not it supports set_vq_ready()
2) userspace will call SET_VRING_ENABLE() when the device supports otherwise it will use RESET.

Quoted 6 lines

if the device does not support set_vq_ready() in kernel, it will trigger kernel oops, at least in current kernel, it does not check where set_vq_ready has been implemented.

And I checked all vdpa driver in kernel, all drivers has implemented this ops.

Actually, it's not about whether or not the set_vq_ready() is implemented. It's about whether the parent supports it correctly:

The ability to suspend and resume a virtqueue is currently beyond the ability of some transport (e.g PCI).

For IFCVF:

static void ifcvf_vdpa_set_vq_ready(struct vdpa_device *vdpa_dev,
u16 qid, bool ready)

{
        struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);

        vf->vring[qid].ready = ready;
}

It seems to follow the PCI transport, so if you just set it to zero, it simply doesn't work at all. I can see some tricks that are used in the DPDK driver, maybe we can use the same to "fix" this.

For VDUSE, we are basically the same:

static void vduse_vdpa_set_vq_ready(struct vdpa_device *vdpa,
u16 idx, bool ready)

{
        struct vduse_dev *dev = vdpa_to_vduse(vdpa);
        struct vduse_virtqueue *vq = &dev->vqs[idx];

        vq->ready = ready;
}

It can't be stopped correctly if we just set it to zero.

For vp_vdpa, it basically wants to abuse the queue_enable, which may result in a warning in Qemu (and the device isn't stopped).

static void vp_vdpa_set_vq_ready(struct vdpa_device *vdpa,
u16 qid, bool ready)

{
        struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);

        vp_modern_set_queue_enable(mdev, qid, ready);
}

ENI did a trick in writing 0 to virtqueue address, so it works for stop but not the start.

static void eni_vdpa_set_vq_ready(struct vdpa_device *vdpa, u16 qid,
bool ready)

{
        struct virtio_pci_legacy_device *ldev = vdpa_to_ldev(vdpa);

        /* ENI is a legacy virtio-pci device. This is not supported
         * by specification. But we can disable virtqueue by setting
         * address to 0.
         */
        if (!ready)
                vp_legacy_set_queue_address(ldev, qid, 0);
}

mlx5 call suspend_vq() which should be fine.

Simulator is probably fine.

So I worry if we use the set_vq_ready(0) it won't work correctly and will have other issues. The idea is:

- advertise the suspend/resume capability via uAPI, then mlx5_vdpa and simulator can go with set_vq_ready()
- others can still go with reset(), and we can try to fix them gradually (and introduce this in the virtio spec).

Quoted 92 lines

So I think it is OK to call set_vq_ready without check.

Quoted 80 lines

And for safety, I suggest tagging this as 7.1.

Quoted 77 lines

---
v4 --> v3
Nothing changed, becasue of issue with mimecast, when the From: tag is different from the sender, the some mail client will take the patch as an attachment, RESEND v3 does not work, So resend the patch as v4

v3 --> v2:
Call vhost_dev_reset() at the end of vhost_net_stop().

Since the vDPA device need re-add the status bit VIRTIO_CONFIG_S_ACKNOWLEDGE and VIRTIO_CONFIG_S_DRIVER, simply, add them inside vhost_vdpa_reset_device, and the only way calling vhost_vdpa_reset_device is in vhost_net_stop(), so it keeps the same behavior as before.

v2 --> v1:
Implement a new function vhost_dev_reset,
reset the backend kernel device at last.

---
  hw/net/vhost_net.c        | 24 +++++++++++++++++++++---
  hw/virtio/vhost-vdpa.c    | 15 +++++++++------
  hw/virtio/vhost.c         | 15 ++++++++++++++-
  include/hw/virtio/vhost.h |  1 +
  4 files changed, 45 insertions(+), 10 deletions(-)
hw/net/vhost_net.c +17 -3
index 30379d2..422c9bf 100644
@@ -325,7 +325,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
      int total_notifiers = data_queue_pairs * 2 + cvq;
      VirtIONet *n = VIRTIO_NET(dev);
      int nvhosts = data_queue_pairs + cvq;
-    struct vhost_net *net;
+    struct vhost_net *net = NULL;
      int r, e, i, index_end = data_queue_pairs * 2;
      NetClientState *peer;
 
@@ -391,8 +391,17 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
  err_start:
      while (--i >= 0) {
          peer = qemu_get_peer(ncs , i);
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
      }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
+    }
+
      e = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
      if (e < 0) {
          fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
@@ -410,6 +419,7 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
      VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
      VirtIONet *n = VIRTIO_NET(dev);
      NetClientState *peer;
+    struct vhost_net *net = NULL;
      int total_notifiers = data_queue_pairs * 2 + cvq;
      int nvhosts = data_queue_pairs + cvq;
      int i, r;
@@ -420,7 +430,15 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
          } else {
              peer = qemu_get_peer(ncs, n->max_queue_pairs);
          }
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
+    }
Quoted 8 lines

hw/net/vhost_net.c void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs, · line 442

Quoted 5 lines
hw/net/vhost_net.c +4 -0
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
      }

So we've already reset the device in vhost_vdpa_dev_start(), any reason we need to do it again here?

Quoted 1 line

reset device in vhost_vdpa_dev_start if there is some error with start.

The rest should have been done in vhost_net_stop_one()?

Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
hw/net/vhost_net.c +0 -0
      r = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
hw/virtio/vhost-vdpa.c +0 -0
index c5ed7a3..3ef0199 100644
@@ -708,6 +708,11 @@ static int vhost_vdpa_reset_device(struct vhost_dev *dev)
 
Quoted 8 lines

hw/virtio/vhost-vdpa.c static int vhost_vdpa_reset_device(struct vhost_dev *dev) · line 714

Quoted 6 lines
hw/virtio/vhost-vdpa.c +4 -0
      ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &status);
      trace_vhost_vdpa_reset_device(dev, status);
+
+    /* Add back this status, so that the device could work next time*/
+    vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
+                               VIRTIO_CONFIG_S_DRIVER);

This seems to contradict the semantic of reset

Quoted 5 lines

Yes, but it's hard to put it in other place, seems only vhost-vdpa need it, and for VM shutdown, qemu_del_nic() will do cleanup this like close vhost fds, which will call reset in kernel space without set those features.

So at last I put it here with no other inpact.

Can we move this to the suitable caller of this function?

Thanks

Quoted 107 lines

Thanks,
Michael

Quoted 101 lines
Quoted 48 lines
hw/virtio/vhost-vdpa.c +5 -6
+
      return ret;
  }
 
@@ -719,14 +724,14 @@ static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx)
      return idx;
  }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, unsigned int ready)
  {
      int i;
      trace_vhost_vdpa_set_vring_ready(dev);
      for (i = 0; i < dev->nvqs; ++i) {
          struct vhost_vring_state state = {
              .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
          };
          vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
      }
@@ -1088,8 +1093,9 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
          if (unlikely(!ok)) {
              return -1;
          }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
      } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
          ok = vhost_vdpa_svqs_stop(dev);
          if (unlikely(!ok)) {
              return -1;
@@ -1105,9 +1111,6 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
          memory_listener_register(&v->listener, &address_space_memory);
          return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
      } else {
-        vhost_vdpa_reset_device(dev);
-        vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
-                                   VIRTIO_CONFIG_S_DRIVER);
          memory_listener_unregister(&v->listener);
 
          return 0;
hw/virtio/vhost.c +0 -0
index b643f42..7e0cdb6 100644

hw/virtio/vhost.c fail_features: · line 1823

Quoted 4 lines
hw/virtio/vhost.c +0 -1
@@ -1820,7 +1820,6 @@ fail_features:
  void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
  {
      int i;
-

Unnecessary changes.

hw/virtio/vhost.c int vhost_net_set_backend(struct vhost_dev *hdev, · line 1858

Quoted 10 lines
hw/virtio/vhost.c +3 -0
      /* should only be called after backend is connected */
      assert(hdev->vhost_ops);
 
@@ -1854,3 +1853,17 @@ int vhost_net_set_backend(struct vhost_dev *hdev,
 
      return -ENOSYS;
  }
+
+int vhost_dev_reset(struct vhost_dev *hdev)
+{

Let's use a separate patch for this.

Thanks

Quoted 28 lines
hw/virtio/vhost.c +11 -0
+    int ret = 0;
+
+    /* should only be called after backend is connected */
+    assert(hdev->vhost_ops);
+
+    if (hdev->vhost_ops->vhost_reset_device) {
+        ret = hdev->vhost_ops->vhost_reset_device(hdev);
+    }
+
+    return ret;
+}
include/hw/virtio/vhost.h +1 -1
index 58a73e7..b8b7c20 100644
@@ -114,6 +114,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
  void vhost_dev_cleanup(struct vhost_dev *hdev);
  int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
  void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
+int vhost_dev_reset(struct vhost_dev *hdev);
  int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
  void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
 
--

1.8.3.1

#38 lore raw

Re: [PATCH v4] vdpa: reset the backend device in the end of vhost_net_stop()

Quoted 46 lines · Jason Wang
Quoted 45 lines · Michael Qiu
Quoted 30 lines · Jason Wang
Quoted 17 lines · Michael Qiu

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset many times(with 1 datapath queue pair and one control queue, triggered 3 times), this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_net_stop(), it will stop all vhost device bind to this virtio device, and in vhost_dev_stop(), qemu tries to stop the device , then stop the queue: vhost_virtqueue_stop().

In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and in next loop(stop other vhost backends), qemu try to stop the queue corresponding to the vhost backend,
the driver finds that the VQ is invalied, this is the root cause.

Quoted 5 lines

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

and implement a new function vhost_dev_reset, only reset backend device after all vhost(per-queue) stoped.

Typo.

Quoted 3 lines
Quoted 6 lines

Rethink this patch, consider there're devices that don't support set_vq_ready(). I wonder if we need

1) uAPI to tell the user space whether or not it supports set_vq_ready()
2) userspace will call SET_VRING_ENABLE() when the device supports otherwise it will use RESET.

if the device does not support set_vq_ready() in kernel, it will trigger kernel oops, at least in current kernel, it does not check where set_vq_ready has been implemented.

Quoted 9 lines
Quoted 2 lines

And I checked all vdpa driver in kernel, all drivers has implemented this ops.

Actually, it's not about whether or not the set_vq_ready() is implemented. It's about whether the parent supports it correctly:

The ability to suspend and resume a virtqueue is currently beyond the ability of some transport (e.g PCI).

OK, Got it

Quoted 58 lines

For IFCVF:

static void ifcvf_vdpa_set_vq_ready(struct vdpa_device *vdpa_dev,
u16 qid, bool ready)

{
         struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);

         vf->vring[qid].ready = ready;
}

It seems to follow the PCI transport, so if you just set it to zero, it simply doesn't work at all. I can see some tricks that are used in the DPDK driver, maybe we can use the same to "fix" this.

For VDUSE, we are basically the same:

static void vduse_vdpa_set_vq_ready(struct vdpa_device *vdpa,
u16 idx, bool ready)

{
         struct vduse_dev *dev = vdpa_to_vduse(vdpa);
         struct vduse_virtqueue *vq = &dev->vqs[idx];

         vq->ready = ready;
}

It can't be stopped correctly if we just set it to zero.

For vp_vdpa, it basically wants to abuse the queue_enable, which may result in a warning in Qemu (and the device isn't stopped).

static void vp_vdpa_set_vq_ready(struct vdpa_device *vdpa,
u16 qid, bool ready)

{
         struct virtio_pci_modern_device *mdev = vdpa_to_mdev(vdpa);

         vp_modern_set_queue_enable(mdev, qid, ready);
}

ENI did a trick in writing 0 to virtqueue address, so it works for stop but not the start.

static void eni_vdpa_set_vq_ready(struct vdpa_device *vdpa, u16 qid,
bool ready)

{
         struct virtio_pci_legacy_device *ldev = vdpa_to_ldev(vdpa);

         /* ENI is a legacy virtio-pci device. This is not supported
          * by specification. But we can disable virtqueue by setting
          * address to 0.
          */
         if (!ready)
                 vp_legacy_set_queue_address(ldev, qid, 0);
}

mlx5 call suspend_vq() which should be fine.

Simulator is probably fine.

Quoted 8 lines

So I worry if we use the set_vq_ready(0) it won't work correctly and will have other issues. The idea is:

- advertise the suspend/resume capability via uAPI, then mlx5_vdpa and simulator can go with set_vq_ready()
- others can still go with reset(), and we can try to fix them gradually (and introduce this in the virtio spec).

Totally agreet.

Quoted 116 lines
Quoted 92 lines

So I think it is OK to call set_vq_ready without check.

Quoted 80 lines

And for safety, I suggest tagging this as 7.1.

Quoted 77 lines

---
v4 --> v3
Nothing changed, becasue of issue with mimecast, when the From: tag is different from the sender, the some mail client will take the patch as an attachment, RESEND v3 does not work, So resend the patch as v4

v3 --> v2:
Call vhost_dev_reset() at the end of vhost_net_stop().

Since the vDPA device need re-add the status bit VIRTIO_CONFIG_S_ACKNOWLEDGE and VIRTIO_CONFIG_S_DRIVER, simply, add them inside vhost_vdpa_reset_device, and the only way calling vhost_vdpa_reset_device is in vhost_net_stop(), so it keeps the same behavior as before.

v2 --> v1:
Implement a new function vhost_dev_reset,
reset the backend kernel device at last.

---
   hw/net/vhost_net.c        | 24 +++++++++++++++++++++---
   hw/virtio/vhost-vdpa.c    | 15 +++++++++------
   hw/virtio/vhost.c         | 15 ++++++++++++++-
   include/hw/virtio/vhost.h |  1 +
   4 files changed, 45 insertions(+), 10 deletions(-)
hw/net/vhost_net.c +17 -3
index 30379d2..422c9bf 100644
@@ -325,7 +325,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
       int total_notifiers = data_queue_pairs * 2 + cvq;
       VirtIONet *n = VIRTIO_NET(dev);
       int nvhosts = data_queue_pairs + cvq;
-    struct vhost_net *net;
+    struct vhost_net *net = NULL;
       int r, e, i, index_end = data_queue_pairs * 2;
       NetClientState *peer;
 
@@ -391,8 +391,17 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
   err_start:
       while (--i >= 0) {
           peer = qemu_get_peer(ncs , i);
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
       }
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
+    }
+
       e = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
       if (e < 0) {
           fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
@@ -410,6 +419,7 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
       VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
       VirtIONet *n = VIRTIO_NET(dev);
       NetClientState *peer;
+    struct vhost_net *net = NULL;
       int total_notifiers = data_queue_pairs * 2 + cvq;
       int nvhosts = data_queue_pairs + cvq;
       int i, r;
@@ -420,7 +430,15 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
           } else {
               peer = qemu_get_peer(ncs, n->max_queue_pairs);
           }
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
+    }
Quoted 8 lines

hw/net/vhost_net.c void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs, · line 442

Quoted 5 lines
hw/net/vhost_net.c +4 -0
+
+    /* We only reset backend vdpa device */
+    if (net && net->dev.vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA) {
+        vhost_dev_reset(&net->dev);
       }

So we've already reset the device in vhost_vdpa_dev_start(), any reason we need to do it again here?

Quoted 1 line

reset device in vhost_vdpa_dev_start if there is some error with start.

The rest should have been done in vhost_net_stop_one()?

Quoted 20 lines
Quoted 19 lines
Quoted 10 lines
hw/net/vhost_net.c +0 -0
       r = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
hw/virtio/vhost-vdpa.c +0 -0
index c5ed7a3..3ef0199 100644

hw/virtio/vhost-vdpa.c static int vhost_vdpa_reset_device(struct vhost_dev *dev) · line 714

Quoted 7 lines
hw/virtio/vhost-vdpa.c +4 -0
@@ -708,6 +708,11 @@ static int vhost_vdpa_reset_device(struct vhost_dev *dev)
 
       ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &status);
       trace_vhost_vdpa_reset_device(dev, status);
+
+    /* Add back this status, so that the device could work next time*/
+    vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
+                               VIRTIO_CONFIG_S_DRIVER);

This seems to contradict the semantic of reset

Quoted 8 lines
Quoted 5 lines

Yes, but it's hard to put it in other place, seems only vhost-vdpa need it, and for VM shutdown, qemu_del_nic() will do cleanup this like close vhost fds, which will call reset in kernel space without set those features.

So at last I put it here with no other inpact.

Can we move this to the suitable caller of this function?

This is vhost_vdpa backend specific requirement, if we move to the caller, we need a backend check after each vhost_dev_reset() been called.

Otherwise we need a new vhost API vhost_add_status(), it a bit
complex because we should consider live migration and error recovery as Si-Wei mentioned.

Could we just leave it here and move it to right place next time.

Thanks,
Michael

Quoted 111 lines

Thanks

Quoted 107 lines

Thanks,
Michael

Quoted 101 lines
Quoted 48 lines
hw/virtio/vhost-vdpa.c +5 -6
+
       return ret;
   }
 
@@ -719,14 +724,14 @@ static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx)
       return idx;
   }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, unsigned int ready)
   {
       int i;
       trace_vhost_vdpa_set_vring_ready(dev);
       for (i = 0; i < dev->nvqs; ++i) {
           struct vhost_vring_state state = {
               .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
           };
           vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
       }
@@ -1088,8 +1093,9 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
           if (unlikely(!ok)) {
               return -1;
           }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
       } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
           ok = vhost_vdpa_svqs_stop(dev);
           if (unlikely(!ok)) {
               return -1;
@@ -1105,9 +1111,6 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
           memory_listener_register(&v->listener, &address_space_memory);
           return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
       } else {
-        vhost_vdpa_reset_device(dev);
-        vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
-                                   VIRTIO_CONFIG_S_DRIVER);
           memory_listener_unregister(&v->listener);
 
           return 0;
hw/virtio/vhost.c +0 -0
index b643f42..7e0cdb6 100644

hw/virtio/vhost.c fail_features: · line 1823

Quoted 4 lines
hw/virtio/vhost.c +0 -1
@@ -1820,7 +1820,6 @@ fail_features:
   void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
   {
       int i;
-

Unnecessary changes.

hw/virtio/vhost.c int vhost_net_set_backend(struct vhost_dev *hdev, · line 1858

Quoted 10 lines
hw/virtio/vhost.c +3 -0
       /* should only be called after backend is connected */
       assert(hdev->vhost_ops);
 
@@ -1854,3 +1853,17 @@ int vhost_net_set_backend(struct vhost_dev *hdev,
 
       return -ENOSYS;
   }
+
+int vhost_dev_reset(struct vhost_dev *hdev)
+{

Let's use a separate patch for this.

Thanks

Quoted 28 lines
hw/virtio/vhost.c +11 -0
+    int ret = 0;
+
+    /* should only be called after backend is connected */
+    assert(hdev->vhost_ops);
+
+    if (hdev->vhost_ops->vhost_reset_device) {
+        ret = hdev->vhost_ops->vhost_reset_device(hdev);
+    }
+
+    return ret;
+}
include/hw/virtio/vhost.h +1 -1
index 58a73e7..b8b7c20 100644
@@ -114,6 +114,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
   void vhost_dev_cleanup(struct vhost_dev *hdev);
   int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
   void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
+int vhost_dev_reset(struct vhost_dev *hdev);
   int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
   void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
 
--

1.8.3.1

#39 lore raw

cover Refactor vhost device reset

In reply to Michael Qiu

Now adays, vhost framework does a misnamed for vhost_reset_device(), actually we need seperate vhost_reset_device() and vhost_reset_owner(), this patchset refactor it, and make different backend call the right function.

Base on those work, fix an issue of vdpa device reset for several times.

Test with kernel vhost, vhost-vdpa, DPDK vhost-user(vdpa), with shutdown ,reboot, and load/unload virtio_net driver in guest.

Michael Qiu (3):
vhost: Refactor vhost_reset_device() in VhostOps
vhost: add vhost_dev_reset()
vdpa: reset the backend device in the end of vhost_net_stop()

 hw/net/vhost_net.c                | 22 +++++++++++++++++++---
 hw/scsi/vhost-user-scsi.c         |  6 +++++-
 hw/virtio/vhost-backend.c         |  4 ++--
 hw/virtio/vhost-user.c            | 22 ++++++++++++++++++----
 hw/virtio/vhost-vdpa.c            | 15 +++++++++------
 hw/virtio/vhost.c                 | 14 ++++++++++++++
 include/hw/virtio/vhost-backend.h |  2 ++
 include/hw/virtio/vhost.h         |  1 +
 8 files changed, 70 insertions(+), 16 deletions(-)
Signature · 1 line
1.8.3.1
#40 lore raw

PATCH 1/3 vhost: Refactor vhost_reset_device() in VhostOps

Currently in vhost framwork, vhost_reset_device() is misnamed. Actually, it should be vhost_reset_owner().

In vhost user, it make compatible with reset device ops, but vhost kernel does not compatible with it, for vhost vdpa, it only implement reset device action.

So we need seperate the function into vhost_reset_owner() and vhost_reset_device(). So that different backend could use the correct function.

---
 hw/scsi/vhost-user-scsi.c         |  6 +++++-
 hw/virtio/vhost-backend.c         |  4 ++--
 hw/virtio/vhost-user.c            | 22 ++++++++++++++++++----
 include/hw/virtio/vhost-backend.h |  2 ++
 4 files changed, 27 insertions(+), 7 deletions(-)
hw/scsi/vhost-user-scsi.c +5 -1
index 1b2f7ee..f179626 100644
@@ -80,8 +80,12 @@ static void vhost_user_scsi_reset(VirtIODevice *vdev)
         return;
     }
 
-    if (dev->vhost_ops->vhost_reset_device) {
+    if (virtio_has_feature(dev->protocol_features,
+                           VHOST_USER_PROTOCOL_F_RESET_DEVICE) &&
+                           dev->vhost_ops->vhost_reset_device) {
         dev->vhost_ops->vhost_reset_device(dev);
+    } else if (dev->vhost_ops->vhost_reset_owner) {
+        dev->vhost_ops->vhost_reset_owner(dev);
     }
 }
 
hw/virtio/vhost-backend.c +2 -2
index e409a86..abbaa8b 100644
@@ -191,7 +191,7 @@ static int vhost_kernel_set_owner(struct vhost_dev *dev)
     return vhost_kernel_call(dev, VHOST_SET_OWNER, NULL);
 }
 
-static int vhost_kernel_reset_device(struct vhost_dev *dev)
+static int vhost_kernel_reset_owner(struct vhost_dev *dev)
 {
     return vhost_kernel_call(dev, VHOST_RESET_OWNER, NULL);
 }
@@ -317,7 +317,7 @@ const VhostOps kernel_ops = {
         .vhost_get_features = vhost_kernel_get_features,
         .vhost_set_backend_cap = vhost_kernel_set_backend_cap,
         .vhost_set_owner = vhost_kernel_set_owner,
-        .vhost_reset_device = vhost_kernel_reset_device,
+        .vhost_reset_owner = vhost_kernel_reset_owner,
         .vhost_get_vq_index = vhost_kernel_get_vq_index,
 #ifdef CONFIG_VHOST_VSOCK
         .vhost_vsock_set_guest_cid = vhost_kernel_vsock_set_guest_cid,
hw/virtio/vhost-user.c +18 -4
index 6abbc9d..4412008 100644
@@ -1475,16 +1475,29 @@ static int vhost_user_get_max_memslots(struct vhost_dev *dev,
     return 0;
 }
 
+static int vhost_user_reset_owner(struct vhost_dev *dev)
+{
+    VhostUserMsg msg = {
+        .hdr.request = VHOST_USER_RESET_OWNER,
+        .hdr.flags = VHOST_USER_VERSION,
+    };
+
+    return vhost_user_write(dev, &msg, NULL, 0);
+}
+
 static int vhost_user_reset_device(struct vhost_dev *dev)
 {
     VhostUserMsg msg = {
+        .hdr.request = VHOST_USER_RESET_DEVICE,
         .hdr.flags = VHOST_USER_VERSION,
     };
 
-    msg.hdr.request = virtio_has_feature(dev->protocol_features,
-                                         VHOST_USER_PROTOCOL_F_RESET_DEVICE)
-        ? VHOST_USER_RESET_DEVICE
-        : VHOST_USER_RESET_OWNER;
+    /* Caller must ensure the backend has VHOST_USER_PROTOCOL_F_RESET_DEVICE
+     * support */
+    if (!virtio_has_feature(dev->protocol_features,
+                       VHOST_USER_PROTOCOL_F_RESET_DEVICE)) {
+        return -EPERM;
+    }
 
     return vhost_user_write(dev, &msg, NULL, 0);
 }
@@ -2548,6 +2561,7 @@ const VhostOps user_ops = {
         .vhost_set_features = vhost_user_set_features,
         .vhost_get_features = vhost_user_get_features,
         .vhost_set_owner = vhost_user_set_owner,
+        .vhost_reset_owner = vhost_user_reset_owner,
         .vhost_reset_device = vhost_user_reset_device,
         .vhost_get_vq_index = vhost_user_get_vq_index,
         .vhost_set_vring_enable = vhost_user_set_vring_enable,
include/hw/virtio/vhost-backend.h +2 -0
index 81bf310..affeeb0 100644
@@ -77,6 +77,7 @@ typedef int (*vhost_get_features_op)(struct vhost_dev *dev,
                                      uint64_t *features);
 typedef int (*vhost_set_backend_cap_op)(struct vhost_dev *dev);
 typedef int (*vhost_set_owner_op)(struct vhost_dev *dev);
+typedef int (*vhost_reset_owner_op)(struct vhost_dev *dev);
 typedef int (*vhost_reset_device_op)(struct vhost_dev *dev);
 typedef int (*vhost_get_vq_index_op)(struct vhost_dev *dev, int idx);
 typedef int (*vhost_set_vring_enable_op)(struct vhost_dev *dev,
@@ -150,6 +151,7 @@ typedef struct VhostOps {
     vhost_get_features_op vhost_get_features;
     vhost_set_backend_cap_op vhost_set_backend_cap;
     vhost_set_owner_op vhost_set_owner;
+    vhost_reset_owner_op vhost_reset_owner;
     vhost_reset_device_op vhost_reset_device;
     vhost_get_vq_index_op vhost_get_vq_index;
     vhost_set_vring_enable_op vhost_set_vring_enable;
Signature · 1 line
1.8.3.1
#41 lore raw

Re: [PATCH 1/3] vhost: Refactor vhost_reset_device() in VhostOps

Quoted 24 lines · Michael Qiu

Currently in vhost framwork, vhost_reset_device() is misnamed. Actually, it should be vhost_reset_owner().

In vhost user, it make compatible with reset device ops, but vhost kernel does not compatible with it, for vhost vdpa, it only implement reset device action.

So we need seperate the function into vhost_reset_owner() and vhost_reset_device(). So that different backend could use the correct function.

---
  hw/scsi/vhost-user-scsi.c         |  6 +++++-
  hw/virtio/vhost-backend.c         |  4 ++--
  hw/virtio/vhost-user.c            | 22 ++++++++++++++++++----
  include/hw/virtio/vhost-backend.h |  2 ++
  4 files changed, 27 insertions(+), 7 deletions(-)
hw/scsi/vhost-user-scsi.c +0 -0
index 1b2f7ee..f179626 100644

hw/scsi/vhost-user-scsi.c static void vhost_user_scsi_reset(VirtIODevice *vdev) · line 84

Quoted 6 lines
hw/scsi/vhost-user-scsi.c +2 -1
@@ -80,8 +80,12 @@ static void vhost_user_scsi_reset(VirtIODevice *vdev)
          return;
      }
 
-    if (dev->vhost_ops->vhost_reset_device) {
+    if (virtio_has_feature(dev->protocol_features,
+                           VHOST_USER_PROTOCOL_F_RESET_DEVICE) &&

This line change is not needed. VHOST_USER_PROTOCOL_F_RESET_DEVICE is guaranteed to be available if getting here.

hw/scsi/vhost-user-scsi.c static void vhost_user_scsi_reset(VirtIODevice *vdev) · line 88

Quoted 4 lines
hw/scsi/vhost-user-scsi.c +3 -0
+                           dev->vhost_ops->vhost_reset_device) {
          dev->vhost_ops->vhost_reset_device(dev);
+    } else if (dev->vhost_ops->vhost_reset_owner) {
+        dev->vhost_ops->vhost_reset_owner(dev);

Nope, drop these two lines. The caller of vhost_user_scsi_reset() doesn't expect vhost_reset_owner to be called in case vhost_reset_device is not implemented.

Quoted 50 lines
hw/scsi/vhost-user-scsi.c +0 -0
      }
  }
 
hw/virtio/vhost-backend.c +2 -2
index e409a86..abbaa8b 100644
@@ -191,7 +191,7 @@ static int vhost_kernel_set_owner(struct vhost_dev *dev)
      return vhost_kernel_call(dev, VHOST_SET_OWNER, NULL);
  }
 
-static int vhost_kernel_reset_device(struct vhost_dev *dev)
+static int vhost_kernel_reset_owner(struct vhost_dev *dev)
  {
      return vhost_kernel_call(dev, VHOST_RESET_OWNER, NULL);
  }
@@ -317,7 +317,7 @@ const VhostOps kernel_ops = {
          .vhost_get_features = vhost_kernel_get_features,
          .vhost_set_backend_cap = vhost_kernel_set_backend_cap,
          .vhost_set_owner = vhost_kernel_set_owner,
-        .vhost_reset_device = vhost_kernel_reset_device,
+        .vhost_reset_owner = vhost_kernel_reset_owner,
          .vhost_get_vq_index = vhost_kernel_get_vq_index,
  #ifdef CONFIG_VHOST_VSOCK
          .vhost_vsock_set_guest_cid = vhost_kernel_vsock_set_guest_cid,
hw/virtio/vhost-user.c +11 -0
index 6abbc9d..4412008 100644
@@ -1475,16 +1475,29 @@ static int vhost_user_get_max_memslots(struct vhost_dev *dev,
      return 0;
  }
 
+static int vhost_user_reset_owner(struct vhost_dev *dev)
+{
+    VhostUserMsg msg = {
+        .hdr.request = VHOST_USER_RESET_OWNER,
+        .hdr.flags = VHOST_USER_VERSION,
+    };
+
+    return vhost_user_write(dev, &msg, NULL, 0);
+}
+
  static int vhost_user_reset_device(struct vhost_dev *dev)
  {
      VhostUserMsg msg = {
+        .hdr.request = VHOST_USER_RESET_DEVICE,
          .hdr.flags = VHOST_USER_VERSION,
      };
 

hw/virtio/vhost-user.c static int vhost_user_get_max_memslots(struct vhost_dev *dev, · line 1500

Quoted 10 lines
hw/virtio/vhost-user.c +6 -4
-    msg.hdr.request = virtio_has_feature(dev->protocol_features,
-                                         VHOST_USER_PROTOCOL_F_RESET_DEVICE)
-        ? VHOST_USER_RESET_DEVICE
-        : VHOST_USER_RESET_OWNER;
+    /* Caller must ensure the backend has VHOST_USER_PROTOCOL_F_RESET_DEVICE
+     * support */
+    if (!virtio_has_feature(dev->protocol_features,
+                       VHOST_USER_PROTOCOL_F_RESET_DEVICE)) {
+        return -EPERM;
+    }

I think we can safely remove this check, since the caller already guarantees VHOST_USER_PROTOCOL_F_RESET_DEVICE is around as what your comment mentions.

The previous branch condition is to reuse the vhost_reset_device op for two different ends, but there's no actual user for VHOST_USER_RESET_OWNER historically.

Thanks,
-Siwei

Quoted 31 lines
hw/virtio/vhost-user.c +1 -0
      return vhost_user_write(dev, &msg, NULL, 0);
  }
@@ -2548,6 +2561,7 @@ const VhostOps user_ops = {
          .vhost_set_features = vhost_user_set_features,
          .vhost_get_features = vhost_user_get_features,
          .vhost_set_owner = vhost_user_set_owner,
+        .vhost_reset_owner = vhost_user_reset_owner,
          .vhost_reset_device = vhost_user_reset_device,
          .vhost_get_vq_index = vhost_user_get_vq_index,
          .vhost_set_vring_enable = vhost_user_set_vring_enable,
include/hw/virtio/vhost-backend.h +2 -0
index 81bf310..affeeb0 100644
@@ -77,6 +77,7 @@ typedef int (*vhost_get_features_op)(struct vhost_dev *dev,
                                       uint64_t *features);
  typedef int (*vhost_set_backend_cap_op)(struct vhost_dev *dev);
  typedef int (*vhost_set_owner_op)(struct vhost_dev *dev);
+typedef int (*vhost_reset_owner_op)(struct vhost_dev *dev);
  typedef int (*vhost_reset_device_op)(struct vhost_dev *dev);
  typedef int (*vhost_get_vq_index_op)(struct vhost_dev *dev, int idx);
  typedef int (*vhost_set_vring_enable_op)(struct vhost_dev *dev,
@@ -150,6 +151,7 @@ typedef struct VhostOps {
      vhost_get_features_op vhost_get_features;
      vhost_set_backend_cap_op vhost_set_backend_cap;
      vhost_set_owner_op vhost_set_owner;
+    vhost_reset_owner_op vhost_reset_owner;
      vhost_reset_device_op vhost_reset_device;
      vhost_get_vq_index_op vhost_get_vq_index;
      vhost_set_vring_enable_op vhost_set_vring_enable;
#42 lore raw

Re: [PATCH 1/3] vhost: Refactor vhost_reset_device() in VhostOps

Quoted 33 lines · Si-Wei Liu
Quoted 24 lines · Michael Qiu

Currently in vhost framwork, vhost_reset_device() is misnamed. Actually, it should be vhost_reset_owner().

In vhost user, it make compatible with reset device ops, but vhost kernel does not compatible with it, for vhost vdpa, it only implement reset device action.

So we need seperate the function into vhost_reset_owner() and vhost_reset_device(). So that different backend could use the correct function.

---
  hw/scsi/vhost-user-scsi.c         |  6 +++++- hw/virtio/vhost-backend.c         |  4 ++-- hw/virtio/vhost-user.c            | 22 ++++++++++++++++++---- include/hw/virtio/vhost-backend.h |  2 ++

  4 files changed, 27 insertions(+), 7 deletions(-)
hw/scsi/vhost-user-scsi.c +0 -0
index 1b2f7ee..f179626 100644

hw/scsi/vhost-user-scsi.c static void vhost_user_scsi_reset(VirtIODevice *vdev) · line 83

Quoted 5 lines
hw/scsi/vhost-user-scsi.c +2 -1
@@ -80,8 +80,12 @@ static void vhost_user_scsi_reset(VirtIODevice *vdev)
           return;
       }
-    if (dev->vhost_ops->vhost_reset_device) {
+    if (virtio_has_feature(dev->protocol_features,
+                           VHOST_USER_PROTOCOL_F_RESET_DEVICE) &&

This line change is not needed. VHOST_USER_PROTOCOL_F_RESET_DEVICE is guaranteed to be available if getting here.

Quoted 6 lines

hw/scsi/vhost-user-scsi.c static void vhost_user_scsi_reset(VirtIODevice *vdev) · line 88

Quoted 4 lines
hw/scsi/vhost-user-scsi.c +3 -0
+                           dev->vhost_ops->vhost_reset_device) {
           dev->vhost_ops->vhost_reset_device(dev);
+    } else if (dev->vhost_ops->vhost_reset_owner) {
+        dev->vhost_ops->vhost_reset_owner(dev);

Nope, drop these two lines. The caller of vhost_user_scsi_reset() doesn't expect vhost_reset_owner to be called in case vhost_reset_device is not implemented.

You are right, I will drop these two lines and remove
VHOST_USER_PROTOCOL_F_RESET_DEVICE check.

Quoted 53 lines
Quoted 53 lines
hw/scsi/vhost-user-scsi.c +0 -0
       }
   }
hw/virtio/vhost-backend.c +2 -2
index e409a86..abbaa8b 100644
@@ -191,7 +191,7 @@ static int vhost_kernel_set_owner(struct vhost_dev 
 *dev)
       return vhost_kernel_call(dev, VHOST_SET_OWNER, NULL);
   }
-static int vhost_kernel_reset_device(struct vhost_dev *dev)
+static int vhost_kernel_reset_owner(struct vhost_dev *dev)
   {
       return vhost_kernel_call(dev, VHOST_RESET_OWNER, NULL);
   }
@@ -317,7 +317,7 @@ const VhostOps kernel_ops = {
           .vhost_get_features = vhost_kernel_get_features,
           .vhost_set_backend_cap = vhost_kernel_set_backend_cap,
           .vhost_set_owner = vhost_kernel_set_owner,
-        .vhost_reset_device = vhost_kernel_reset_device,
+        .vhost_reset_owner = vhost_kernel_reset_owner,
           .vhost_get_vq_index = vhost_kernel_get_vq_index,
   #ifdef CONFIG_VHOST_VSOCK
           .vhost_vsock_set_guest_cid = vhost_kernel_vsock_set_guest_cid,
hw/virtio/vhost-user.c +11 -4
index 6abbc9d..4412008 100644
@@ -1475,16 +1475,29 @@ static int vhost_user_get_max_memslots(struct 
 vhost_dev *dev,
       return 0;
   }
+static int vhost_user_reset_owner(struct vhost_dev *dev)
+{
+    VhostUserMsg msg = {
+        .hdr.request = VHOST_USER_RESET_OWNER,
+        .hdr.flags = VHOST_USER_VERSION,
+    };
+
+    return vhost_user_write(dev, &msg, NULL, 0);
+}
+
   static int vhost_user_reset_device(struct vhost_dev *dev)
   {
       VhostUserMsg msg = {
+        .hdr.request = VHOST_USER_RESET_DEVICE,
           .hdr.flags = VHOST_USER_VERSION,
       };
-    msg.hdr.request = virtio_has_feature(dev->protocol_features,
-                                         
 VHOST_USER_PROTOCOL_F_RESET_DEVICE)
-        ? VHOST_USER_RESET_DEVICE
-        : VHOST_USER_RESET_OWNER;
Quoted 9 lines
Quoted 7 lines
hw/virtio/vhost-user.c +1 -0
+    /* Caller must ensure the backend has 

VHOST_USER_PROTOCOL_F_RESET_DEVICE
+     * support */
+    if (!virtio_has_feature(dev->protocol_features, +                       VHOST_USER_PROTOCOL_F_RESET_DEVICE)) { +        return -EPERM;
+    }

I think we can safely remove this check, since the caller already guarantees VHOST_USER_PROTOCOL_F_RESET_DEVICE is around as what your comment mentions.

I think it probely worth to check, because for vhost_net_stop() it does not check this flag, otherwise we should check if the backend is vhost user with this flag enabled.

Quoted 38 lines

The previous branch condition is to reuse the vhost_reset_device op for two different ends, but there's no actual user for VHOST_USER_RESET_OWNER historically.

Thanks,
-Siwei

Quoted 31 lines

      return vhost_user_write(dev, &msg, NULL, 0); }

hw/virtio/vhost-user.c +1 -0
@@ -2548,6 +2561,7 @@ const VhostOps user_ops = {
           .vhost_set_features = vhost_user_set_features,
           .vhost_get_features = vhost_user_get_features,
           .vhost_set_owner = vhost_user_set_owner,
+        .vhost_reset_owner = vhost_user_reset_owner,
           .vhost_reset_device = vhost_user_reset_device,
           .vhost_get_vq_index = vhost_user_get_vq_index,
           .vhost_set_vring_enable = vhost_user_set_vring_enable,
include/hw/virtio/vhost-backend.h +1 -0
index 81bf310..affeeb0 100644
@@ -77,6 +77,7 @@ typedef int (*vhost_get_features_op)(struct 
 vhost_dev *dev,
                                        uint64_t *features);
   typedef int (*vhost_set_backend_cap_op)(struct vhost_dev *dev);
   typedef int (*vhost_set_owner_op)(struct vhost_dev *dev);
+typedef int (*vhost_reset_owner_op)(struct vhost_dev *dev);
   typedef int (*vhost_reset_device_op)(struct vhost_dev *dev);
   typedef int (*vhost_get_vq_index_op)(struct vhost_dev *dev, int idx);

  typedef int (*vhost_set_vring_enable_op)(struct vhost_dev *dev,

include/hw/virtio/vhost-backend.h +1 -0
@@ -150,6 +151,7 @@ typedef struct VhostOps {
       vhost_get_features_op vhost_get_features;
       vhost_set_backend_cap_op vhost_set_backend_cap;
       vhost_set_owner_op vhost_set_owner;
+    vhost_reset_owner_op vhost_reset_owner;
       vhost_reset_device_op vhost_reset_device;
       vhost_get_vq_index_op vhost_get_vq_index;
       vhost_set_vring_enable_op vhost_set_vring_enable;
#43 lore raw

Re: [PATCH 1/3] vhost: Refactor vhost_reset_device() in VhostOps

In reply to Michael Qiu
Quoted 10 lines · Michael Qiu

Currently in vhost framwork, vhost_reset_device() is misnamed. Actually, it should be vhost_reset_owner().

In vhost user, it make compatible with reset device ops, but vhost kernel does not compatible with it, for vhost vdpa, it only implement reset device action.

So we need seperate the function into vhost_reset_owner() and vhost_reset_device(). So that different backend could use the correct function.

I see no reason when RESET_OWNER needs to be done for kernel backend.

And if I understand the code correctly, vhost-user "abuse" RESET_OWNER for reset. So the current code looks fine?

Quoted 14 lines
---
  hw/scsi/vhost-user-scsi.c         |  6 +++++-
  hw/virtio/vhost-backend.c         |  4 ++--
  hw/virtio/vhost-user.c            | 22 ++++++++++++++++++----
  include/hw/virtio/vhost-backend.h |  2 ++
  4 files changed, 27 insertions(+), 7 deletions(-)
hw/scsi/vhost-user-scsi.c +0 -0
index 1b2f7ee..f179626 100644

hw/scsi/vhost-user-scsi.c static void vhost_user_scsi_reset(VirtIODevice *vdev) · line 88

Quoted 10 lines
hw/scsi/vhost-user-scsi.c +5 -1
@@ -80,8 +80,12 @@ static void vhost_user_scsi_reset(VirtIODevice *vdev)
          return;
      }
 
-    if (dev->vhost_ops->vhost_reset_device) {
+    if (virtio_has_feature(dev->protocol_features,
+                           VHOST_USER_PROTOCOL_F_RESET_DEVICE) &&
+                           dev->vhost_ops->vhost_reset_device) {
          dev->vhost_ops->vhost_reset_device(dev);
+    } else if (dev->vhost_ops->vhost_reset_owner) {
+        dev->vhost_ops->vhost_reset_owner(dev);

Actually, I fail to understand why we need an indirection via vhost_ops. It's guaranteed to be vhost_user_ops.

Quoted 17 lines
hw/scsi/vhost-user-scsi.c +0 -0
      }
  }
 
hw/virtio/vhost-backend.c +1 -1
index e409a86..abbaa8b 100644
@@ -191,7 +191,7 @@ static int vhost_kernel_set_owner(struct vhost_dev *dev)
      return vhost_kernel_call(dev, VHOST_SET_OWNER, NULL);
  }
 
-static int vhost_kernel_reset_device(struct vhost_dev *dev)
+static int vhost_kernel_reset_owner(struct vhost_dev *dev)
  {
      return vhost_kernel_call(dev, VHOST_RESET_OWNER, NULL);
  }

hw/virtio/vhost-backend.c const VhostOps kernel_ops = { · line 320

Quoted 5 lines
hw/virtio/vhost-backend.c +1 -1
@@ -317,7 +317,7 @@ const VhostOps kernel_ops = {
          .vhost_get_features = vhost_kernel_get_features,
          .vhost_set_backend_cap = vhost_kernel_set_backend_cap,
          .vhost_set_owner = vhost_kernel_set_owner,
-        .vhost_reset_device = vhost_kernel_reset_device,
+        .vhost_reset_owner = vhost_kernel_reset_owner,

I think we can delete the current vhost_reset_device() since it not used in any code path.

Thanks

Quoted 69 lines
hw/virtio/vhost-backend.c +0 -0
          .vhost_get_vq_index = vhost_kernel_get_vq_index,
  #ifdef CONFIG_VHOST_VSOCK
          .vhost_vsock_set_guest_cid = vhost_kernel_vsock_set_guest_cid,
hw/virtio/vhost-user.c +18 -4
index 6abbc9d..4412008 100644
@@ -1475,16 +1475,29 @@ static int vhost_user_get_max_memslots(struct vhost_dev *dev,
      return 0;
  }
 
+static int vhost_user_reset_owner(struct vhost_dev *dev)
+{
+    VhostUserMsg msg = {
+        .hdr.request = VHOST_USER_RESET_OWNER,
+        .hdr.flags = VHOST_USER_VERSION,
+    };
+
+    return vhost_user_write(dev, &msg, NULL, 0);
+}
+
  static int vhost_user_reset_device(struct vhost_dev *dev)
  {
      VhostUserMsg msg = {
+        .hdr.request = VHOST_USER_RESET_DEVICE,
          .hdr.flags = VHOST_USER_VERSION,
      };
 
-    msg.hdr.request = virtio_has_feature(dev->protocol_features,
-                                         VHOST_USER_PROTOCOL_F_RESET_DEVICE)
-        ? VHOST_USER_RESET_DEVICE
-        : VHOST_USER_RESET_OWNER;
+    /* Caller must ensure the backend has VHOST_USER_PROTOCOL_F_RESET_DEVICE
+     * support */
+    if (!virtio_has_feature(dev->protocol_features,
+                       VHOST_USER_PROTOCOL_F_RESET_DEVICE)) {
+        return -EPERM;
+    }
 
      return vhost_user_write(dev, &msg, NULL, 0);
  }
@@ -2548,6 +2561,7 @@ const VhostOps user_ops = {
          .vhost_set_features = vhost_user_set_features,
          .vhost_get_features = vhost_user_get_features,
          .vhost_set_owner = vhost_user_set_owner,
+        .vhost_reset_owner = vhost_user_reset_owner,
          .vhost_reset_device = vhost_user_reset_device,
          .vhost_get_vq_index = vhost_user_get_vq_index,
          .vhost_set_vring_enable = vhost_user_set_vring_enable,
include/hw/virtio/vhost-backend.h +2 -0
index 81bf310..affeeb0 100644
@@ -77,6 +77,7 @@ typedef int (*vhost_get_features_op)(struct vhost_dev *dev,
                                       uint64_t *features);
  typedef int (*vhost_set_backend_cap_op)(struct vhost_dev *dev);
  typedef int (*vhost_set_owner_op)(struct vhost_dev *dev);
+typedef int (*vhost_reset_owner_op)(struct vhost_dev *dev);
  typedef int (*vhost_reset_device_op)(struct vhost_dev *dev);
  typedef int (*vhost_get_vq_index_op)(struct vhost_dev *dev, int idx);
  typedef int (*vhost_set_vring_enable_op)(struct vhost_dev *dev,
@@ -150,6 +151,7 @@ typedef struct VhostOps {
      vhost_get_features_op vhost_get_features;
      vhost_set_backend_cap_op vhost_set_backend_cap;
      vhost_set_owner_op vhost_set_owner;
+    vhost_reset_owner_op vhost_reset_owner;
      vhost_reset_device_op vhost_reset_device;
      vhost_get_vq_index_op vhost_get_vq_index;
      vhost_set_vring_enable_op vhost_set_vring_enable;
#44 lore raw

Re: [PATCH 1/3] vhost: Refactor vhost_reset_device() in VhostOps

Quoted 9 lines · Jason Wang
Quoted 7 lines · Michael Qiu

Currently in vhost framwork, vhost_reset_device() is misnamed. Actually, it should be vhost_reset_owner().

In vhost user, it make compatible with reset device ops, but vhost kernel does not compatible with it, for vhost vdpa, it only implement reset device action.

Quoted 7 lines
Quoted 3 lines

So we need seperate the function into vhost_reset_owner() and vhost_reset_device(). So that different backend could use the correct function.

I see no reason when RESET_OWNER needs to be done for kernel backend.

In kernel vhost, RESET_OWNER indeed do vhost device level reset: vhost_net_reset_owner()

static long vhost_net_reset_owner(struct vhost_net *n) {
[...]

        err = vhost_dev_check_owner(&n->dev);
        if (err)
                goto done;
        umem = vhost_dev_reset_owner_prepare();
        if (!umem) {
                err = -ENOMEM;
                goto done;
        }
        vhost_net_stop(n, &tx_sock, &rx_sock);
        vhost_net_flush(n);
        vhost_dev_stop(&n->dev);
        vhost_dev_reset_owner(&n->dev, umem);
        vhost_net_vq_reset(n);

[...]

}

In the history of QEMU, There is a commit:
commit d1f8b30ec8dde0318fd1b98d24a64926feae9625

Author: Yuanhan Liu <[email protected]>
Date:   Wed Sep 23 12:19:57 2015 +0800

vhost: rename VHOST_RESET_OWNER to VHOST_RESET_DEVICE

Quote from Michael:

We really should rename VHOST_RESET_OWNER to VHOST_RESET_DEVICE.

but finally, it has been reverted by the author:
commit 60915dc4691768c4dc62458bb3e16c843fab091d

Author: Yuanhan Liu <[email protected]>
Date:   Wed Nov 11 21:24:37 2015 +0800

vhost: rename RESET_DEVICE backto RESET_OWNER

This patch basically reverts commit d1f8b30e.

It turned out that it breaks stuff, so revert it:

http://lists.nongnu.org/archive/html/qemu-devel/2015-10/msg00949.html

Seems kernel take RESET_OWNER for reset,but QEMU never call to this function to do a reset.

Quoted 47 lines

And if I understand the code correctly, vhost-user "abuse" RESET_OWNER for reset. So the current code looks fine?

Quoted 14 lines

---
  hw/scsi/vhost-user-scsi.c         |  6 +++++- hw/virtio/vhost-backend.c         |  4 ++-- hw/virtio/vhost-user.c            | 22 ++++++++++++++++++---- include/hw/virtio/vhost-backend.h |  2 ++

  4 files changed, 27 insertions(+), 7 deletions(-)
hw/scsi/vhost-user-scsi.c +0 -0
index 1b2f7ee..f179626 100644

hw/scsi/vhost-user-scsi.c static void vhost_user_scsi_reset(VirtIODevice *vdev) · line 87

Quoted 9 lines
hw/scsi/vhost-user-scsi.c +5 -1
@@ -80,8 +80,12 @@ static void vhost_user_scsi_reset(VirtIODevice *vdev)
           return;
       }
-    if (dev->vhost_ops->vhost_reset_device) {
+    if (virtio_has_feature(dev->protocol_features,
+                           VHOST_USER_PROTOCOL_F_RESET_DEVICE) &&
+                           dev->vhost_ops->vhost_reset_device) {
           dev->vhost_ops->vhost_reset_device(dev);
+    } else if (dev->vhost_ops->vhost_reset_owner) {
+        dev->vhost_ops->vhost_reset_owner(dev);

Actually, I fail to understand why we need an indirection via vhost_ops. It's guaranteed to be vhost_user_ops.

Quoted 16 lines
hw/scsi/vhost-user-scsi.c +0 -0
       }
   }
hw/virtio/vhost-backend.c +1 -1
index e409a86..abbaa8b 100644
@@ -191,7 +191,7 @@ static int vhost_kernel_set_owner(struct vhost_dev 
 *dev)
       return vhost_kernel_call(dev, VHOST_SET_OWNER, NULL);
   }
-static int vhost_kernel_reset_device(struct vhost_dev *dev)
+static int vhost_kernel_reset_owner(struct vhost_dev *dev)
   {
       return vhost_kernel_call(dev, VHOST_RESET_OWNER, NULL);
   }
@@ -317,7 +317,7 @@ const VhostOps kernel_ops = {
Quoted 9 lines

hw/virtio/vhost-backend.c const VhostOps kernel_ops = { · line 320

Quoted 5 lines
hw/virtio/vhost-backend.c +1 -1
           .vhost_get_features = vhost_kernel_get_features,
           .vhost_set_backend_cap = vhost_kernel_set_backend_cap,
           .vhost_set_owner = vhost_kernel_set_owner,
-        .vhost_reset_device = vhost_kernel_reset_device,
+        .vhost_reset_owner = vhost_kernel_reset_owner,

I think we can delete the current vhost_reset_device() since it not used in any code path.

I planned to use it for vDPA reset, and vhost-user-scsi also use device reset.

Thanks,
Michael

Quoted 75 lines

Thanks

Quoted 70 lines
hw/virtio/vhost-backend.c +0 -0
           .vhost_get_vq_index = vhost_kernel_get_vq_index,
   #ifdef CONFIG_VHOST_VSOCK
           .vhost_vsock_set_guest_cid = vhost_kernel_vsock_set_guest_cid,
hw/virtio/vhost-user.c +12 -4
index 6abbc9d..4412008 100644
@@ -1475,16 +1475,29 @@ static int vhost_user_get_max_memslots(struct 
 vhost_dev *dev,
       return 0;
   }
+static int vhost_user_reset_owner(struct vhost_dev *dev)
+{
+    VhostUserMsg msg = {
+        .hdr.request = VHOST_USER_RESET_OWNER,
+        .hdr.flags = VHOST_USER_VERSION,
+    };
+
+    return vhost_user_write(dev, &msg, NULL, 0);
+}
+
   static int vhost_user_reset_device(struct vhost_dev *dev)
   {
       VhostUserMsg msg = {
+        .hdr.request = VHOST_USER_RESET_DEVICE,
           .hdr.flags = VHOST_USER_VERSION,
       };
-    msg.hdr.request = virtio_has_feature(dev->protocol_features,
-                                         
 VHOST_USER_PROTOCOL_F_RESET_DEVICE)
-        ? VHOST_USER_RESET_DEVICE
-        : VHOST_USER_RESET_OWNER;
+    /* Caller must ensure the backend has 

VHOST_USER_PROTOCOL_F_RESET_DEVICE
+     * support */
+    if (!virtio_has_feature(dev->protocol_features, +                       VHOST_USER_PROTOCOL_F_RESET_DEVICE)) { +        return -EPERM;
+    }
      return vhost_user_write(dev, &msg, NULL, 0);
  }

hw/virtio/vhost-user.c +1 -0
@@ -2548,6 +2561,7 @@ const VhostOps user_ops = {
           .vhost_set_features = vhost_user_set_features,
           .vhost_get_features = vhost_user_get_features,
           .vhost_set_owner = vhost_user_set_owner,
+        .vhost_reset_owner = vhost_user_reset_owner,
           .vhost_reset_device = vhost_user_reset_device,
           .vhost_get_vq_index = vhost_user_get_vq_index,
           .vhost_set_vring_enable = vhost_user_set_vring_enable,
include/hw/virtio/vhost-backend.h +1 -0
index 81bf310..affeeb0 100644
@@ -77,6 +77,7 @@ typedef int (*vhost_get_features_op)(struct 
 vhost_dev *dev,
                                        uint64_t *features);
   typedef int (*vhost_set_backend_cap_op)(struct vhost_dev *dev);
   typedef int (*vhost_set_owner_op)(struct vhost_dev *dev);
+typedef int (*vhost_reset_owner_op)(struct vhost_dev *dev);
   typedef int (*vhost_reset_device_op)(struct vhost_dev *dev);
   typedef int (*vhost_get_vq_index_op)(struct vhost_dev *dev, int idx);

  typedef int (*vhost_set_vring_enable_op)(struct vhost_dev *dev,

include/hw/virtio/vhost-backend.h +1 -0
@@ -150,6 +151,7 @@ typedef struct VhostOps {
       vhost_get_features_op vhost_get_features;
       vhost_set_backend_cap_op vhost_set_backend_cap;
       vhost_set_owner_op vhost_set_owner;
+    vhost_reset_owner_op vhost_reset_owner;
       vhost_reset_device_op vhost_reset_device;
       vhost_get_vq_index_op vhost_get_vq_index;
       vhost_set_vring_enable_op vhost_set_vring_enable;

A message that is not in the archive (sent off-list or not yet received): [email protected]

#46 lore raw

Re: [PATCH 1/3] vhost: Refactor vhost_reset_device() in VhostOps

Quoted 60 lines · Michael Qiu
Quoted 9 lines · Jason Wang
Quoted 7 lines · Michael Qiu

Currently in vhost framwork, vhost_reset_device() is misnamed. Actually, it should be vhost_reset_owner().

In vhost user, it make compatible with reset device ops, but vhost kernel does not compatible with it, for vhost vdpa, it only implement reset device action.

Quoted 7 lines
Quoted 3 lines

So we need seperate the function into vhost_reset_owner() and vhost_reset_device(). So that different backend could use the correct function.

I see no reason when RESET_OWNER needs to be done for kernel backend.

In kernel vhost, RESET_OWNER  indeed do vhost device level reset: vhost_net_reset_owner()

static long vhost_net_reset_owner(struct vhost_net *n) {
[...]
        err = vhost_dev_check_owner(&n->dev); if (err)
                goto done;
        umem = vhost_dev_reset_owner_prepare(); if (!umem) {
                err = -ENOMEM;
                goto done;
        }
        vhost_net_stop(n, &tx_sock, &rx_sock); vhost_net_flush(n); vhost_dev_stop(&n->dev); vhost_dev_reset_owner(&n->dev, umem); vhost_net_vq_reset(n);
[...]

}

In the history of QEMU, There is a commit:
commit d1f8b30ec8dde0318fd1b98d24a64926feae9625
Author: Yuanhan Liu <[email protected]>
Date:   Wed Sep 23 12:19:57 2015 +0800

    vhost: rename VHOST_RESET_OWNER to VHOST_RESET_DEVICE

    Quote from Michael:

        We really should rename VHOST_RESET_OWNER to VHOST_RESET_DEVICE.

but finally, it has been reverted by the author:
commit 60915dc4691768c4dc62458bb3e16c843fab091d
Author: Yuanhan Liu <[email protected]>
Date:   Wed Nov 11 21:24:37 2015 +0800

    vhost: rename RESET_DEVICE backto RESET_OWNER

Quoted 7 lines

    This patch basically reverts commit d1f8b30e.

    It turned out that it breaks stuff, so revert it:

http://lists.nongnu.org/archive/html/qemu-devel/2015-10/msg00949.html

Seems kernel take RESET_OWNER for reset,but QEMU never call to this function to do a reset.

The question is, we manage to survive by not using RESET_OWNER for past 10 years. Any reason that we want to use that now?

Note that the RESET_OWNER is only useful the process want to drop the its mm refcnt from vhost, it doesn't reset the device (e.g it does not even call vhost_vq_reset()).

(Especially, it was deprecated in by the vhost-user protocol since its semantics is ambiguous)

Quoted 59 lines
Quoted 49 lines

And if I understand the code correctly, vhost-user "abuse" RESET_OWNER for reset. So the current code looks fine?

Quoted 14 lines

---
  hw/scsi/vhost-user-scsi.c         |  6 +++++- hw/virtio/vhost-backend.c         |  4 ++-- hw/virtio/vhost-user.c            | 22 ++++++++++++++++++---- include/hw/virtio/vhost-backend.h |  2 ++

  4 files changed, 27 insertions(+), 7 deletions(-)
hw/scsi/vhost-user-scsi.c +0 -0
index 1b2f7ee..f179626 100644

hw/scsi/vhost-user-scsi.c static void vhost_user_scsi_reset(VirtIODevice · line 88

Quoted 10 lines
hw/scsi/vhost-user-scsi.c +5 -1
@@ -80,8 +80,12 @@ static void vhost_user_scsi_reset(VirtIODevice 
 *vdev)
           return;
       }
-    if (dev->vhost_ops->vhost_reset_device) {
+    if (virtio_has_feature(dev->protocol_features,
+ VHOST_USER_PROTOCOL_F_RESET_DEVICE) &&
+ dev->vhost_ops->vhost_reset_device) {
           dev->vhost_ops->vhost_reset_device(dev);
+    } else if (dev->vhost_ops->vhost_reset_owner) {
+        dev->vhost_ops->vhost_reset_owner(dev);

Actually, I fail to understand why we need an indirection via vhost_ops. It's guaranteed to be vhost_user_ops.

Quoted 16 lines
hw/scsi/vhost-user-scsi.c +0 -0
       }
   }
hw/virtio/vhost-backend.c +1 -1
index e409a86..abbaa8b 100644
@@ -191,7 +191,7 @@ static int vhost_kernel_set_owner(struct 
 vhost_dev *dev)
       return vhost_kernel_call(dev, VHOST_SET_OWNER, NULL);
   }
-static int vhost_kernel_reset_device(struct vhost_dev *dev)
+static int vhost_kernel_reset_owner(struct vhost_dev *dev)
   {
       return vhost_kernel_call(dev, VHOST_RESET_OWNER, NULL);
   }
@@ -317,7 +317,7 @@ const VhostOps kernel_ops = {
Quoted 9 lines

hw/virtio/vhost-backend.c const VhostOps kernel_ops = { · line 320

Quoted 5 lines
hw/virtio/vhost-backend.c +1 -1
           .vhost_get_features = vhost_kernel_get_features,
           .vhost_set_backend_cap = vhost_kernel_set_backend_cap,
           .vhost_set_owner = vhost_kernel_set_owner,
-        .vhost_reset_device = vhost_kernel_reset_device,
+        .vhost_reset_owner = vhost_kernel_reset_owner,

I think we can delete the current vhost_reset_device() since it not used in any code path.

Quoted 1 line

I planned to use it for vDPA reset,

For vhost-vDPA it can call vhost_vdpa_reset_device() directly.

As I mentioned before, the only user of vhost_reset_device config ops is vhost-user-scsi but it should directly call the vhost_user_reset_device().

Thanks

Quoted 80 lines

and vhost-user-scsi also use device reset.

Thanks,
Michael

Quoted 74 lines

Thanks

Quoted 69 lines
hw/virtio/vhost-backend.c +0 -0
           .vhost_get_vq_index = vhost_kernel_get_vq_index,
   #ifdef CONFIG_VHOST_VSOCK
           .vhost_vsock_set_guest_cid = vhost_kernel_vsock_set_guest_cid,
hw/virtio/vhost-user.c +12 -4
index 6abbc9d..4412008 100644
@@ -1475,16 +1475,29 @@ static int 
 vhost_user_get_max_memslots(struct vhost_dev *dev,
       return 0;
   }
+static int vhost_user_reset_owner(struct vhost_dev *dev)
+{
+    VhostUserMsg msg = {
+        .hdr.request = VHOST_USER_RESET_OWNER,
+        .hdr.flags = VHOST_USER_VERSION,
+    };
+
+    return vhost_user_write(dev, &msg, NULL, 0);
+}
+
   static int vhost_user_reset_device(struct vhost_dev *dev)
   {
       VhostUserMsg msg = {
+        .hdr.request = VHOST_USER_RESET_DEVICE,
           .hdr.flags = VHOST_USER_VERSION,
       };
-    msg.hdr.request = virtio_has_feature(dev->protocol_features,
- VHOST_USER_PROTOCOL_F_RESET_DEVICE)
-        ? VHOST_USER_RESET_DEVICE
-        : VHOST_USER_RESET_OWNER;
+    /* Caller must ensure the backend has 

VHOST_USER_PROTOCOL_F_RESET_DEVICE
+     * support */
+    if (!virtio_has_feature(dev->protocol_features, +                       VHOST_USER_PROTOCOL_F_RESET_DEVICE)) { +        return -EPERM;
+    }
      return vhost_user_write(dev, &msg, NULL, 0);
  }

hw/virtio/vhost-user.c +1 -0
@@ -2548,6 +2561,7 @@ const VhostOps user_ops = {
           .vhost_set_features = vhost_user_set_features,
           .vhost_get_features = vhost_user_get_features,
           .vhost_set_owner = vhost_user_set_owner,
+        .vhost_reset_owner = vhost_user_reset_owner,
           .vhost_reset_device = vhost_user_reset_device,
           .vhost_get_vq_index = vhost_user_get_vq_index,
           .vhost_set_vring_enable = vhost_user_set_vring_enable,
include/hw/virtio/vhost-backend.h +1 -0
index 81bf310..affeeb0 100644
@@ -77,6 +77,7 @@ typedef int (*vhost_get_features_op)(struct 
 vhost_dev *dev,
                                        uint64_t *features);
   typedef int (*vhost_set_backend_cap_op)(struct vhost_dev *dev);
   typedef int (*vhost_set_owner_op)(struct vhost_dev *dev);
+typedef int (*vhost_reset_owner_op)(struct vhost_dev *dev);
   typedef int (*vhost_reset_device_op)(struct vhost_dev *dev);
   typedef int (*vhost_get_vq_index_op)(struct vhost_dev *dev, int idx);

  typedef int (*vhost_set_vring_enable_op)(struct vhost_dev *dev,

include/hw/virtio/vhost-backend.h +1 -0
@@ -150,6 +151,7 @@ typedef struct VhostOps {
       vhost_get_features_op vhost_get_features;
       vhost_set_backend_cap_op vhost_set_backend_cap;
       vhost_set_owner_op vhost_set_owner;
+    vhost_reset_owner_op vhost_reset_owner;
       vhost_reset_device_op vhost_reset_device;
       vhost_get_vq_index_op vhost_get_vq_index;
       vhost_set_vring_enable_op vhost_set_vring_enable;
#47 lore raw

Re: [PATCH 1/3] vhost: Refactor vhost_reset_device() in VhostOps

Quoted 68 lines · Jason Wang
Quoted 66 lines · Michael Qiu
Quoted 9 lines · Jason Wang
Quoted 7 lines · Michael Qiu

Currently in vhost framwork, vhost_reset_device() is misnamed. Actually, it should be vhost_reset_owner().

In vhost user, it make compatible with reset device ops, but vhost kernel does not compatible with it, for vhost vdpa, it only implement reset device action.

Quoted 7 lines
Quoted 3 lines

So we need seperate the function into vhost_reset_owner() and vhost_reset_device(). So that different backend could use the correct function.

I see no reason when RESET_OWNER needs to be done for kernel backend.

In kernel vhost, RESET_OWNER  indeed do vhost device level reset: vhost_net_reset_owner()

static long vhost_net_reset_owner(struct vhost_net *n) {
[...]
        err = vhost_dev_check_owner(&n->dev); if (err)
                goto done;
        umem = vhost_dev_reset_owner_prepare(); if (!umem) {
                err = -ENOMEM;
                goto done;
        }
        vhost_net_stop(n, &tx_sock, &rx_sock); vhost_net_flush(n); vhost_dev_stop(&n->dev); vhost_dev_reset_owner(&n->dev, umem); vhost_net_vq_reset(n);
[...]

}

In the history of QEMU, There is a commit:
commit d1f8b30ec8dde0318fd1b98d24a64926feae9625
Author: Yuanhan Liu <[email protected]>
Date:   Wed Sep 23 12:19:57 2015 +0800

    vhost: rename VHOST_RESET_OWNER to VHOST_RESET_DEVICE

    Quote from Michael:

        We really should rename VHOST_RESET_OWNER to VHOST_RESET_DEVICE.

but finally, it has been reverted by the author:
commit 60915dc4691768c4dc62458bb3e16c843fab091d
Author: Yuanhan Liu <[email protected]>
Date:   Wed Nov 11 21:24:37 2015 +0800

    vhost: rename RESET_DEVICE backto RESET_OWNER

    This patch basically reverts commit d1f8b30e.

    It turned out that it breaks stuff, so revert it:

http://lists.nongnu.org/archive/html/qemu-devel/2015-10/msg00949.html

Quoted 10 lines
Quoted 1 line

Seems kernel take RESET_OWNER for reset,but QEMU never call to this function to do a reset.

The question is, we manage to survive by not using RESET_OWNER for past 10 years. Any reason that we want to use that now?

Note that the RESET_OWNER is only useful the process want to drop the its mm refcnt from vhost, it doesn't reset the device (e.g it does not even call vhost_vq_reset()).

(Especially, it was deprecated in by the vhost-user protocol since its semantics is ambiguous)

So, you prefer to directly remove RESET_OWNER support now?

Quoted 59 lines
Quoted 59 lines
Quoted 58 lines

And if I understand the code correctly, vhost-user "abuse" RESET_OWNER for reset. So the current code looks fine?

Quoted 14 lines

---
  hw/scsi/vhost-user-scsi.c         |  6 +++++- hw/virtio/vhost-backend.c         |  4 ++-- hw/virtio/vhost-user.c            | 22 ++++++++++++++++++---- include/hw/virtio/vhost-backend.h |  2 ++

  4 files changed, 27 insertions(+), 7 deletions(-)
hw/scsi/vhost-user-scsi.c +0 -0
index 1b2f7ee..f179626 100644

hw/scsi/vhost-user-scsi.c static void vhost_user_scsi_reset(VirtIODevice · line 88

Quoted 10 lines
hw/scsi/vhost-user-scsi.c +5 -1
@@ -80,8 +80,12 @@ static void vhost_user_scsi_reset(VirtIODevice 
 *vdev)
           return;
       }
-    if (dev->vhost_ops->vhost_reset_device) {
+    if (virtio_has_feature(dev->protocol_features,
+ VHOST_USER_PROTOCOL_F_RESET_DEVICE) &&
+ dev->vhost_ops->vhost_reset_device) {
           dev->vhost_ops->vhost_reset_device(dev);
+    } else if (dev->vhost_ops->vhost_reset_owner) {
+        dev->vhost_ops->vhost_reset_owner(dev);

Actually, I fail to understand why we need an indirection via vhost_ops. It's guaranteed to be vhost_user_ops.

Quoted 16 lines
hw/scsi/vhost-user-scsi.c +0 -0
       }
   }
hw/virtio/vhost-backend.c +1 -1
index e409a86..abbaa8b 100644
@@ -191,7 +191,7 @@ static int vhost_kernel_set_owner(struct 
 vhost_dev *dev)
       return vhost_kernel_call(dev, VHOST_SET_OWNER, NULL);
   }
-static int vhost_kernel_reset_device(struct vhost_dev *dev)
+static int vhost_kernel_reset_owner(struct vhost_dev *dev)
   {
       return vhost_kernel_call(dev, VHOST_RESET_OWNER, NULL);
   }

hw/virtio/vhost-backend.c const VhostOps kernel_ops = { · line 320

Quoted 5 lines
hw/virtio/vhost-backend.c +1 -1
@@ -317,7 +317,7 @@ const VhostOps kernel_ops = {
           .vhost_get_features = vhost_kernel_get_features,
           .vhost_set_backend_cap = vhost_kernel_set_backend_cap,
           .vhost_set_owner = vhost_kernel_set_owner,
-        .vhost_reset_device = vhost_kernel_reset_device,
+        .vhost_reset_owner = vhost_kernel_reset_owner,

I think we can delete the current vhost_reset_device() since it not used in any code path.

Quoted 7 lines
Quoted 1 line

I planned to use it for vDPA reset,

For vhost-vDPA it can call vhost_vdpa_reset_device() directly.

As I mentioned before, the only user of vhost_reset_device config ops is vhost-user-scsi but it should directly call the vhost_user_reset_device().

Yes, but in the next patch I reuse it to reset backend device in vhost_net.

Quoted 86 lines

Thanks

Quoted 80 lines

and vhost-user-scsi also use device reset.

Thanks,
Michael

Quoted 74 lines

Thanks

Quoted 69 lines
hw/virtio/vhost-backend.c +0 -0
           .vhost_get_vq_index = vhost_kernel_get_vq_index,
   #ifdef CONFIG_VHOST_VSOCK
           .vhost_vsock_set_guest_cid = vhost_kernel_vsock_set_guest_cid,
hw/virtio/vhost-user.c +12 -4
index 6abbc9d..4412008 100644
@@ -1475,16 +1475,29 @@ static int 
 vhost_user_get_max_memslots(struct vhost_dev *dev,
       return 0;
   }
+static int vhost_user_reset_owner(struct vhost_dev *dev)
+{
+    VhostUserMsg msg = {
+        .hdr.request = VHOST_USER_RESET_OWNER,
+        .hdr.flags = VHOST_USER_VERSION,
+    };
+
+    return vhost_user_write(dev, &msg, NULL, 0);
+}
+
   static int vhost_user_reset_device(struct vhost_dev *dev)
   {
       VhostUserMsg msg = {
+        .hdr.request = VHOST_USER_RESET_DEVICE,
           .hdr.flags = VHOST_USER_VERSION,
       };
-    msg.hdr.request = virtio_has_feature(dev->protocol_features,
- VHOST_USER_PROTOCOL_F_RESET_DEVICE)
-        ? VHOST_USER_RESET_DEVICE
-        : VHOST_USER_RESET_OWNER;
+    /* Caller must ensure the backend has 

VHOST_USER_PROTOCOL_F_RESET_DEVICE
+     * support */
+    if (!virtio_has_feature(dev->protocol_features, +                       VHOST_USER_PROTOCOL_F_RESET_DEVICE)) { +        return -EPERM;
+    }
      return vhost_user_write(dev, &msg, NULL, 0);
  }

hw/virtio/vhost-user.c +1 -0
@@ -2548,6 +2561,7 @@ const VhostOps user_ops = {
           .vhost_set_features = vhost_user_set_features,
           .vhost_get_features = vhost_user_get_features,
           .vhost_set_owner = vhost_user_set_owner,
+        .vhost_reset_owner = vhost_user_reset_owner,
           .vhost_reset_device = vhost_user_reset_device,
           .vhost_get_vq_index = vhost_user_get_vq_index,
           .vhost_set_vring_enable = vhost_user_set_vring_enable,
include/hw/virtio/vhost-backend.h +1 -0
index 81bf310..affeeb0 100644
@@ -77,6 +77,7 @@ typedef int (*vhost_get_features_op)(struct 
 vhost_dev *dev,
                                        uint64_t *features);
   typedef int (*vhost_set_backend_cap_op)(struct vhost_dev *dev);
   typedef int (*vhost_set_owner_op)(struct vhost_dev *dev);
+typedef int (*vhost_reset_owner_op)(struct vhost_dev *dev);
   typedef int (*vhost_reset_device_op)(struct vhost_dev *dev);
   typedef int (*vhost_get_vq_index_op)(struct vhost_dev *dev, int idx);

  typedef int (*vhost_set_vring_enable_op)(struct vhost_dev *dev,

include/hw/virtio/vhost-backend.h +1 -0
@@ -150,6 +151,7 @@ typedef struct VhostOps {
       vhost_get_features_op vhost_get_features;
       vhost_set_backend_cap_op vhost_set_backend_cap;
       vhost_set_owner_op vhost_set_owner;
+    vhost_reset_owner_op vhost_reset_owner;
       vhost_reset_device_op vhost_reset_device;
       vhost_get_vq_index_op vhost_get_vq_index;
       vhost_set_vring_enable_op vhost_set_vring_enable;
#48 lore raw

Re: [PATCH 1/3] vhost: Refactor vhost_reset_device() in VhostOps

Quoted 146 lines · Michael Qiu
Quoted 68 lines · Jason Wang
Quoted 66 lines · Michael Qiu
Quoted 9 lines · Jason Wang
Quoted 7 lines · Michael Qiu

Currently in vhost framwork, vhost_reset_device() is misnamed. Actually, it should be vhost_reset_owner().

In vhost user, it make compatible with reset device ops, but vhost kernel does not compatible with it, for vhost vdpa, it only implement reset device action.

Quoted 7 lines
Quoted 3 lines

So we need seperate the function into vhost_reset_owner() and vhost_reset_device(). So that different backend could use the correct function.

I see no reason when RESET_OWNER needs to be done for kernel backend.

In kernel vhost, RESET_OWNER  indeed do vhost device level reset: vhost_net_reset_owner()

static long vhost_net_reset_owner(struct vhost_net *n) {
[...]
        err = vhost_dev_check_owner(&n->dev); if (err)
                goto done;
        umem = vhost_dev_reset_owner_prepare(); if (!umem) {
                err = -ENOMEM;
                goto done;
        }
        vhost_net_stop(n, &tx_sock, &rx_sock); vhost_net_flush(n); vhost_dev_stop(&n->dev); vhost_dev_reset_owner(&n->dev, umem); vhost_net_vq_reset(n);
[...]

}

In the history of QEMU, There is a commit:
commit d1f8b30ec8dde0318fd1b98d24a64926feae9625
Author: Yuanhan Liu <[email protected]>
Date:   Wed Sep 23 12:19:57 2015 +0800

    vhost: rename VHOST_RESET_OWNER to VHOST_RESET_DEVICE

    Quote from Michael:

        We really should rename VHOST_RESET_OWNER to VHOST_RESET_DEVICE.

but finally, it has been reverted by the author:
commit 60915dc4691768c4dc62458bb3e16c843fab091d
Author: Yuanhan Liu <[email protected]>
Date:   Wed Nov 11 21:24:37 2015 +0800

    vhost: rename RESET_DEVICE backto RESET_OWNER

    This patch basically reverts commit d1f8b30e.

    It turned out that it breaks stuff, so revert it:

https://urldefense.com/v3/__http://lists.nongnu.org/archive/html/qemu-devel/2015-10/msg00949.html__;!!ACWV5N9M2RV99hQ!bgCEUDnSCLVO8LxXlwcdiHrtwqH5ip_sVcKscrtJve5eSzJfNN9JZuf-8HQIQ1Re$

Quoted 10 lines
Quoted 1 line

Seems kernel take RESET_OWNER for reset,but QEMU never call to this function to do a reset.

The question is, we manage to survive by not using RESET_OWNER for past 10 years. Any reason that we want to use that now?

Note that the RESET_OWNER is only useful the process want to drop the its mm refcnt from vhost, it doesn't reset the device (e.g it does not even call vhost_vq_reset()).

(Especially, it was deprecated in by the vhost-user protocol since its semantics is ambiguous)

So, you prefer to directly remove RESET_OWNER support now?

Quoted 62 lines
Quoted 60 lines
Quoted 49 lines

And if I understand the code correctly, vhost-user "abuse" RESET_OWNER for reset. So the current code looks fine?

Quoted 14 lines

---
  hw/scsi/vhost-user-scsi.c         |  6 +++++- hw/virtio/vhost-backend.c         |  4 ++-- hw/virtio/vhost-user.c            | 22 ++++++++++++++++++---- include/hw/virtio/vhost-backend.h |  2 ++

  4 files changed, 27 insertions(+), 7 deletions(-)
hw/scsi/vhost-user-scsi.c +0 -0
index 1b2f7ee..f179626 100644

hw/scsi/vhost-user-scsi.c static void vhost_user_scsi_reset(VirtIODevice · line 88

Quoted 10 lines
hw/scsi/vhost-user-scsi.c +5 -1
@@ -80,8 +80,12 @@ static void vhost_user_scsi_reset(VirtIODevice 
 *vdev)
           return;
       }
-    if (dev->vhost_ops->vhost_reset_device) {
+    if (virtio_has_feature(dev->protocol_features,
+ VHOST_USER_PROTOCOL_F_RESET_DEVICE) &&
+ dev->vhost_ops->vhost_reset_device) {
           dev->vhost_ops->vhost_reset_device(dev);
+    } else if (dev->vhost_ops->vhost_reset_owner) {
+        dev->vhost_ops->vhost_reset_owner(dev);

Actually, I fail to understand why we need an indirection via vhost_ops. It's guaranteed to be vhost_user_ops.

Quoted 16 lines
hw/scsi/vhost-user-scsi.c +0 -0
       }
   }
hw/virtio/vhost-backend.c +1 -1
index e409a86..abbaa8b 100644
@@ -191,7 +191,7 @@ static int vhost_kernel_set_owner(struct 
 vhost_dev *dev)
       return vhost_kernel_call(dev, VHOST_SET_OWNER, NULL);
   }
-static int vhost_kernel_reset_device(struct vhost_dev *dev)
+static int vhost_kernel_reset_owner(struct vhost_dev *dev)
   {
       return vhost_kernel_call(dev, VHOST_RESET_OWNER, NULL);
   }
@@ -317,7 +317,7 @@ const VhostOps kernel_ops = {
Quoted 9 lines

hw/virtio/vhost-backend.c const VhostOps kernel_ops = { · line 320

Quoted 5 lines
hw/virtio/vhost-backend.c +1 -1
           .vhost_get_features = vhost_kernel_get_features,
           .vhost_set_backend_cap = vhost_kernel_set_backend_cap,
           .vhost_set_owner = vhost_kernel_set_owner,
-        .vhost_reset_device = vhost_kernel_reset_device,
+        .vhost_reset_owner = vhost_kernel_reset_owner,

I think we can delete the current vhost_reset_device() since it not used in any code path.

I planned to use it for vDPA reset,

Quoted 7 lines
Quoted 4 lines

For vhost-vDPA it can call vhost_vdpa_reset_device() directly.

As I mentioned before, the only user of vhost_reset_device config ops is vhost-user-scsi but it should directly call the vhost_user_reset_device().

Yes, but in the next patch I reuse it to reset backend device in vhost_net.

I recall vhost-user has a different way to reset the net backend, so probably we can leave out implementing the .vhost_reset_device() op for vhost-user as Jason suggested. In that case vhost-user-scsi will call into vhost_user_reset_device() directly without using the .vhost_reset_device() op.

-Siwei

Quoted 89 lines
Quoted 88 lines

Thanks

Quoted 80 lines

and vhost-user-scsi also use device reset.

Thanks,
Michael

Quoted 74 lines

Thanks

Quoted 69 lines
hw/virtio/vhost-backend.c +0 -0
           .vhost_get_vq_index = vhost_kernel_get_vq_index,
   #ifdef CONFIG_VHOST_VSOCK
           .vhost_vsock_set_guest_cid = vhost_kernel_vsock_set_guest_cid,
hw/virtio/vhost-user.c +12 -4
index 6abbc9d..4412008 100644
@@ -1475,16 +1475,29 @@ static int 
 vhost_user_get_max_memslots(struct vhost_dev *dev,
       return 0;
   }
+static int vhost_user_reset_owner(struct vhost_dev *dev)
+{
+    VhostUserMsg msg = {
+        .hdr.request = VHOST_USER_RESET_OWNER,
+        .hdr.flags = VHOST_USER_VERSION,
+    };
+
+    return vhost_user_write(dev, &msg, NULL, 0);
+}
+
   static int vhost_user_reset_device(struct vhost_dev *dev)
   {
       VhostUserMsg msg = {
+        .hdr.request = VHOST_USER_RESET_DEVICE,
           .hdr.flags = VHOST_USER_VERSION,
       };
-    msg.hdr.request = virtio_has_feature(dev->protocol_features,
- VHOST_USER_PROTOCOL_F_RESET_DEVICE)
-        ? VHOST_USER_RESET_DEVICE
-        : VHOST_USER_RESET_OWNER;
+    /* Caller must ensure the backend has 

VHOST_USER_PROTOCOL_F_RESET_DEVICE
+     * support */
+    if (!virtio_has_feature(dev->protocol_features,
+ VHOST_USER_PROTOCOL_F_RESET_DEVICE)) {
+        return -EPERM;
+    }
      return vhost_user_write(dev, &msg, NULL, 0); }

hw/virtio/vhost-user.c +1 -0
@@ -2548,6 +2561,7 @@ const VhostOps user_ops = {
           .vhost_set_features = vhost_user_set_features,
           .vhost_get_features = vhost_user_get_features,
           .vhost_set_owner = vhost_user_set_owner,
+        .vhost_reset_owner = vhost_user_reset_owner,
           .vhost_reset_device = vhost_user_reset_device,
           .vhost_get_vq_index = vhost_user_get_vq_index,
           .vhost_set_vring_enable = vhost_user_set_vring_enable,
include/hw/virtio/vhost-backend.h +1 -0
index 81bf310..affeeb0 100644
@@ -77,6 +77,7 @@ typedef int (*vhost_get_features_op)(struct 
 vhost_dev *dev,
                                        uint64_t *features);
   typedef int (*vhost_set_backend_cap_op)(struct vhost_dev *dev);
   typedef int (*vhost_set_owner_op)(struct vhost_dev *dev);
+typedef int (*vhost_reset_owner_op)(struct vhost_dev *dev);
   typedef int (*vhost_reset_device_op)(struct vhost_dev *dev);
   typedef int (*vhost_get_vq_index_op)(struct vhost_dev *dev, int idx);

  typedef int (*vhost_set_vring_enable_op)(struct vhost_dev *dev,

include/hw/virtio/vhost-backend.h +1 -0
@@ -150,6 +151,7 @@ typedef struct VhostOps {
       vhost_get_features_op vhost_get_features;
       vhost_set_backend_cap_op vhost_set_backend_cap;
       vhost_set_owner_op vhost_set_owner;
+    vhost_reset_owner_op vhost_reset_owner;
       vhost_reset_device_op vhost_reset_device;
       vhost_get_vq_index_op vhost_get_vq_index;
       vhost_set_vring_enable_op vhost_set_vring_enable;
#49 lore raw

Re: [PATCH 1/3] vhost: Refactor vhost_reset_device() in VhostOps

Quoted 168 lines · Si-Wei Liu
Quoted 164 lines · Michael Qiu
Quoted 77 lines · Jason Wang
Quoted 62 lines · Michael Qiu
Quoted 9 lines · Jason Wang
Quoted 7 lines · Michael Qiu

Currently in vhost framwork, vhost_reset_device() is misnamed. Actually, it should be vhost_reset_owner().

In vhost user, it make compatible with reset device ops, but vhost kernel does not compatible with it, for vhost vdpa, it only implement reset device action.

Quoted 7 lines
Quoted 3 lines

So we need seperate the function into vhost_reset_owner() and vhost_reset_device(). So that different backend could use the correct function.

I see no reason when RESET_OWNER needs to be done for kernel backend.

In kernel vhost, RESET_OWNER indeed do vhost device level reset:
vhost_net_reset_owner()

static long vhost_net_reset_owner(struct vhost_net *n) {
[...]

        err = vhost_dev_check_owner(&n->dev);
        if (err)
                goto done;
        umem = vhost_dev_reset_owner_prepare();
        if (!umem) {
                err = -ENOMEM;
                goto done;
        }
        vhost_net_stop(n, &tx_sock, &rx_sock);
        vhost_net_flush(n);
        vhost_dev_stop(&n->dev);
        vhost_dev_reset_owner(&n->dev, umem);
        vhost_net_vq_reset(n);

[...]

}

In the history of QEMU, There is a commit:
commit d1f8b30ec8dde0318fd1b98d24a64926feae9625

Author: Yuanhan Liu <[email protected]>
Date:   Wed Sep 23 12:19:57 2015 +0800

vhost: rename VHOST_RESET_OWNER to VHOST_RESET_DEVICE

Quote from Michael:

We really should rename VHOST_RESET_OWNER to
VHOST_RESET_DEVICE.

but finally, it has been reverted by the author:
commit 60915dc4691768c4dc62458bb3e16c843fab091d

Author: Yuanhan Liu <[email protected]>
Date:   Wed Nov 11 21:24:37 2015 +0800

vhost: rename RESET_DEVICE backto RESET_OWNER

Quoted 8 lines

This patch basically reverts commit d1f8b30e.

It turned out that it breaks stuff, so revert it:

https://urldefense.com/v3/__http://lists.nongnu.org/archive/html/qemu-devel/2015-10/msg00949.html__;!!ACWV5N9M2RV99hQ!bgCEUDnSCLVO8LxXlwcdiHrtwqH5ip_sVcKscrtJve5eSzJfNN9JZuf-8HQIQ1Re$

Seems kernel take RESET_OWNER for reset,but QEMU never call to this function to do a reset.

The question is, we manage to survive by not using RESET_OWNER for past 10 years. Any reason that we want to use that now?

Quoted 8 lines

Note that the RESET_OWNER is only useful the process want to drop the its mm refcnt from vhost, it doesn't reset the device (e.g it does not even call vhost_vq_reset()).

(Especially, it was deprecated in by the vhost-user protocol since its semantics is ambiguous)

So, you prefer to directly remove RESET_OWNER support now?

Quoted 71 lines
Quoted 62 lines
Quoted 52 lines

And if I understand the code correctly, vhost-user "abuse" RESET_OWNER for reset. So the current code looks fine?

Quoted 14 lines
---
  hw/scsi/vhost-user-scsi.c         |  6 +++++-
  hw/virtio/vhost-backend.c         |  4 ++--
  hw/virtio/vhost-user.c            | 22 ++++++++++++++++++----
  include/hw/virtio/vhost-backend.h |  2 ++
  4 files changed, 27 insertions(+), 7 deletions(-)
hw/scsi/vhost-user-scsi.c +0 -0
index 1b2f7ee..f179626 100644

hw/scsi/vhost-user-scsi.c static void vhost_user_scsi_reset(VirtIODevice · line 88

Quoted 10 lines
hw/scsi/vhost-user-scsi.c +5 -1
@@ -80,8 +80,12 @@ static void vhost_user_scsi_reset(VirtIODevice
 *vdev)
          return;
      }
-    if (dev->vhost_ops->vhost_reset_device) {
+    if (virtio_has_feature(dev->protocol_features,
+ VHOST_USER_PROTOCOL_F_RESET_DEVICE) &&
+ dev->vhost_ops->vhost_reset_device) {
          dev->vhost_ops->vhost_reset_device(dev);
+    } else if (dev->vhost_ops->vhost_reset_owner) {
+        dev->vhost_ops->vhost_reset_owner(dev);

Actually, I fail to understand why we need an indirection via vhost_ops. It's guaranteed to be vhost_user_ops.

Quoted 17 lines
hw/scsi/vhost-user-scsi.c +0 -0
      }
  }
hw/virtio/vhost-backend.c +1 -1
index e409a86..abbaa8b 100644
@@ -191,7 +191,7 @@ static int vhost_kernel_set_owner(struct
 vhost_dev *dev)
      return vhost_kernel_call(dev, VHOST_SET_OWNER, NULL);
  }
-static int vhost_kernel_reset_device(struct vhost_dev *dev)
+static int vhost_kernel_reset_owner(struct vhost_dev *dev)
  {
      return vhost_kernel_call(dev, VHOST_RESET_OWNER, NULL);
  }
@@ -317,7 +317,7 @@ const VhostOps kernel_ops = {
          .vhost_get_features = vhost_kernel_get_features,
Quoted 9 lines

hw/virtio/vhost-backend.c const VhostOps kernel_ops = { · line 320

Quoted 4 lines
hw/virtio/vhost-backend.c +1 -1
          .vhost_set_backend_cap = vhost_kernel_set_backend_cap,
          .vhost_set_owner = vhost_kernel_set_owner,
-        .vhost_reset_device = vhost_kernel_reset_device,
+        .vhost_reset_owner = vhost_kernel_reset_owner,

I think we can delete the current vhost_reset_device() since it not used in any code path.

Quoted 1 line

I planned to use it for vDPA reset,

For vhost-vDPA it can call vhost_vdpa_reset_device() directly.

As I mentioned before, the only user of vhost_reset_device config ops is vhost-user-scsi but it should directly call the vhost_user_reset_device().

Quoted 3 lines
Quoted 2 lines

Yes, but in the next patch I reuse it to reset backend device in vhost_net.

I recall vhost-user has a different way to reset the net backend,

Yes, it has VHOST_USER_RESET_DEVICE.

Thanks

Quoted 101 lines

so
probably we can leave out implementing the .vhost_reset_device() op for vhost-user as Jason suggested. In that case vhost-user-scsi will call into vhost_user_reset_device() directly without using the .vhost_reset_device() op.

-Siwei

Quoted 92 lines
Quoted 91 lines

Thanks

Quoted 83 lines

and vhost-user-scsi also use device reset.

Thanks,
Michael

Quoted 77 lines

Thanks

Quoted 72 lines
hw/virtio/vhost-backend.c +0 -0
          .vhost_get_vq_index = vhost_kernel_get_vq_index,
  #ifdef CONFIG_VHOST_VSOCK
          .vhost_vsock_set_guest_cid =

vhost_kernel_vsock_set_guest_cid,

hw/virtio/vhost-user.c +12 -4
index 6abbc9d..4412008 100644
@@ -1475,16 +1475,29 @@ static int
 vhost_user_get_max_memslots(struct vhost_dev *dev,
      return 0;
  }
+static int vhost_user_reset_owner(struct vhost_dev *dev)
+{
+    VhostUserMsg msg = {
+        .hdr.request = VHOST_USER_RESET_OWNER,
+        .hdr.flags = VHOST_USER_VERSION,
+    };
+
+    return vhost_user_write(dev, &msg, NULL, 0);
+}
+
  static int vhost_user_reset_device(struct vhost_dev *dev)
  {
      VhostUserMsg msg = {
+        .hdr.request = VHOST_USER_RESET_DEVICE,
          .hdr.flags = VHOST_USER_VERSION,
      };
-    msg.hdr.request = virtio_has_feature(dev->protocol_features,
- VHOST_USER_PROTOCOL_F_RESET_DEVICE)
-        ? VHOST_USER_RESET_DEVICE
-        : VHOST_USER_RESET_OWNER;
+    /* Caller must ensure the backend has

VHOST_USER_PROTOCOL_F_RESET_DEVICE

hw/virtio/vhost-user.c +6 -0
+     * support */
+    if (!virtio_has_feature(dev->protocol_features,
+ VHOST_USER_PROTOCOL_F_RESET_DEVICE)) {
+        return -EPERM;
+    }
      return vhost_user_write(dev, &msg, NULL, 0);
  }
@@ -2548,6 +2561,7 @@ const VhostOps user_ops = {
          .vhost_set_features = vhost_user_set_features,
          .vhost_get_features = vhost_user_get_features,
          .vhost_set_owner = vhost_user_set_owner,
+        .vhost_reset_owner = vhost_user_reset_owner,
          .vhost_reset_device = vhost_user_reset_device,
          .vhost_get_vq_index = vhost_user_get_vq_index,
          .vhost_set_vring_enable = vhost_user_set_vring_enable,
include/hw/virtio/vhost-backend.h +1 -0
index 81bf310..affeeb0 100644
@@ -77,6 +77,7 @@ typedef int (*vhost_get_features_op)(struct
 vhost_dev *dev,
                                       uint64_t *features);
  typedef int (*vhost_set_backend_cap_op)(struct vhost_dev *dev);
  typedef int (*vhost_set_owner_op)(struct vhost_dev *dev);
+typedef int (*vhost_reset_owner_op)(struct vhost_dev *dev);
  typedef int (*vhost_reset_device_op)(struct vhost_dev *dev);
  typedef int (*vhost_get_vq_index_op)(struct vhost_dev *dev, int

idx);
typedef int (*vhost_set_vring_enable_op)(struct vhost_dev *dev,

include/hw/virtio/vhost-backend.h +1 -0
@@ -150,6 +151,7 @@ typedef struct VhostOps {
      vhost_get_features_op vhost_get_features;
      vhost_set_backend_cap_op vhost_set_backend_cap;
      vhost_set_owner_op vhost_set_owner;
+    vhost_reset_owner_op vhost_reset_owner;
      vhost_reset_device_op vhost_reset_device;
      vhost_get_vq_index_op vhost_get_vq_index;
      vhost_set_vring_enable_op vhost_set_vring_enable;
#50 lore raw

PATCH 2/3 vhost: add vhost_dev_reset()

In reply to Michael Qiu

Not all vhost-user backends support ops->vhost_reset_device(). Instead of adding backend check and call backend ops directly, it's better to implement a function in vhost framework, so that it could hide vhost_ops details.

---
 hw/virtio/vhost.c         | 14 ++++++++++++++
 include/hw/virtio/vhost.h |  1 +
 2 files changed, 15 insertions(+)
hw/virtio/vhost.c +14 -0
index b643f42..26667ae 100644
@@ -1854,3 +1854,17 @@ int vhost_net_set_backend(struct vhost_dev *hdev,
 
     return -ENOSYS;
 }
+
+int vhost_dev_reset(struct vhost_dev *hdev)
+{
+    int ret = 0;
+
+    /* should only be called after backend is connected */
+    assert(hdev->vhost_ops);
+
+    if (hdev->vhost_ops->vhost_reset_device) {
+        ret = hdev->vhost_ops->vhost_reset_device(hdev);
+    }
+
+    return ret;
+}
include/hw/virtio/vhost.h +1 -0
index 58a73e7..b8b7c20 100644
@@ -114,6 +114,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
 void vhost_dev_cleanup(struct vhost_dev *hdev);
 int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
+int vhost_dev_reset(struct vhost_dev *hdev);
 int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
 void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
 
Signature · 1 line
1.8.3.1
#51 lore raw

Re: [PATCH 2/3] vhost: add vhost_dev_reset()

Quoted 16 lines · Michael Qiu

Not all vhost-user backends support ops->vhost_reset_device(). Instead of adding backend check and call backend ops directly, it's better to implement a function in vhost framework, so that it could hide vhost_ops details.

---
  hw/virtio/vhost.c         | 14 ++++++++++++++
  include/hw/virtio/vhost.h |  1 +
  2 files changed, 15 insertions(+)
hw/virtio/vhost.c +0 -0
index b643f42..26667ae 100644

hw/virtio/vhost.c int vhost_net_set_backend(struct vhost_dev *hdev, · line 1858

Quoted 5 lines
hw/virtio/vhost.c +2 -0
@@ -1854,3 +1854,17 @@ int vhost_net_set_backend(struct vhost_dev *hdev,
 
      return -ENOSYS;
  }
+
+int vhost_dev_reset(struct vhost_dev *hdev)

Maybe vhost_user_scsi_reset() can call this function instead?

-Siwei

Quoted 24 lines
hw/virtio/vhost.c +12 -0
+{
+    int ret = 0;
+
+    /* should only be called after backend is connected */
+    assert(hdev->vhost_ops);
+
+    if (hdev->vhost_ops->vhost_reset_device) {
+        ret = hdev->vhost_ops->vhost_reset_device(hdev);
+    }
+
+    return ret;
+}
include/hw/virtio/vhost.h +1 -0
index 58a73e7..b8b7c20 100644
@@ -114,6 +114,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
  void vhost_dev_cleanup(struct vhost_dev *hdev);
  int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
  void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
+int vhost_dev_reset(struct vhost_dev *hdev);
  int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
  void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
 
#52 lore raw

PATCH v5 3/3 vdpa: reset the backend device in the end of vhost_net_stop()

In reply to Michael Qiu

Currently, when VM poweroff, it will trigger vdpa device(such as mlx bluefield2 VF) reset many times(with 1 datapath queue pair and one control queue, triggered 3 times), this leads to below issue:

vhost VQ 2 ring restore failed: -22: Invalid argument (22)

This because in vhost_net_stop(), it will stop all vhost device bind to this virtio device, and in vhost_dev_stop(), qemu tries to stop the device , then stop the queue: vhost_virtqueue_stop().

In vhost_dev_stop(), it resets the device, which clear some flags in low level driver, and in next loop(stop other vhost backends), qemu try to stop the queue corresponding to the vhost backend,
the driver finds that the VQ is invalied, this is the root cause.

To solve the issue, vdpa should set vring unready, and remove reset ops in device stop: vhost_dev_start(hdev, false).

and implement a new function vhost_dev_reset, only reset backend device after all vhost(per-queue) stopped.

---
v5 --> v4:
move vhost_dev_reset() call after set_guest_notifiers

remove implementation of vhost_dev_reset()

remove backend check for VHOST_BACKEND_TYPE_VDPA

v4 --> v3:
Nothing changed, becasue of issue with mimecast, when the From: tag is different from the sender, the some mail client will take the patch as an attachment, RESEND v3 does not work, So resend the patch as v4

v3 --> v2:
Call vhost_dev_reset() at the end of vhost_net_stop().

Since the vDPA device need re-add the status bit VIRTIO_CONFIG_S_ACKNOWLEDGE and VIRTIO_CONFIG_S_DRIVER, simply, add them inside vhost_vdpa_reset_device, and the only way calling vhost_vdpa_reset_device is in vhost_net_stop(), so it keeps the same behavior as before.

v2 --> v1:
Implement a new function vhost_dev_reset,
reset the backend kernel device at last.

---
 hw/net/vhost_net.c     | 22 +++++++++++++++++++---
 hw/virtio/vhost-vdpa.c | 15 +++++++++------
 2 files changed, 28 insertions(+), 9 deletions(-)
hw/net/vhost_net.c +19 -3
index 30379d2..30c76ca 100644
@@ -325,7 +325,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
     int total_notifiers = data_queue_pairs * 2 + cvq;
     VirtIONet *n = VIRTIO_NET(dev);
     int nvhosts = data_queue_pairs + cvq;
-    struct vhost_net *net;
+    struct vhost_net *net = NULL;
     int r, e, i, index_end = data_queue_pairs * 2;
     NetClientState *peer;
 
@@ -391,13 +391,21 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
 err_start:
     while (--i >= 0) {
         peer = qemu_get_peer(ncs , i);
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
     }
+
     e = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
     if (e < 0) {
         fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
         fflush(stderr);
     }
+
+    if (net) {
+        vhost_dev_reset(&net->dev);
+    }
 err:
     return r;
 }
@@ -410,6 +418,7 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
     VirtIONet *n = VIRTIO_NET(dev);
     NetClientState *peer;
+    struct vhost_net *net = NULL;
     int total_notifiers = data_queue_pairs * 2 + cvq;
     int nvhosts = data_queue_pairs + cvq;
     int i, r;
@@ -420,7 +429,10 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
         } else {
             peer = qemu_get_peer(ncs, n->max_queue_pairs);
         }
-        vhost_net_stop_one(get_vhost_net(peer), dev);
+
+        net = get_vhost_net(peer);
+
+        vhost_net_stop_one(net, dev);
     }
 
     r = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
@@ -429,6 +441,10 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
         fflush(stderr);
     }
     assert(r >= 0);
+
+    if (net) {
+        vhost_dev_reset(&net->dev);
+    }
 }
 
 void vhost_net_cleanup(struct vhost_net *net)
hw/virtio/vhost-vdpa.c +9 -6
index c5ed7a3..3ef0199 100644
@@ -708,6 +708,11 @@ static int vhost_vdpa_reset_device(struct vhost_dev *dev)
 
     ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &status);
     trace_vhost_vdpa_reset_device(dev, status);
+
+    /* Add back this status, so that the device could work next time*/
+    vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
+                               VIRTIO_CONFIG_S_DRIVER);
+
     return ret;
 }
 
@@ -719,14 +724,14 @@ static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx)
     return idx;
 }
 
-static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
+static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev, unsigned int ready)
 {
     int i;
     trace_vhost_vdpa_set_vring_ready(dev);
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_vring_state state = {
             .index = dev->vq_index + i,
-            .num = 1,
+            .num = ready,
         };
         vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
     }
@@ -1088,8 +1093,9 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
         if (unlikely(!ok)) {
             return -1;
         }
-        vhost_vdpa_set_vring_ready(dev);
+        vhost_vdpa_set_vring_ready(dev, 1);
     } else {
+        vhost_vdpa_set_vring_ready(dev, 0);
         ok = vhost_vdpa_svqs_stop(dev);
         if (unlikely(!ok)) {
             return -1;
@@ -1105,9 +1111,6 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
         memory_listener_register(&v->listener, &address_space_memory);
         return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
     } else {
-        vhost_vdpa_reset_device(dev);
-        vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
-                                   VIRTIO_CONFIG_S_DRIVER);
         memory_listener_unregister(&v->listener);
 
         return 0;
Signature · 1 line
1.8.3.1