Skip to contentSkip to conversation

virtio-net: don't busy poll for cvq command

PATCH Superseded Jason Wang 4 participants 53 messages Dec 26, 2022 – Jan 31, 2023 1 message not in the archive

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Jason Wang

author
#0 lore raw

cover virtio-net: don't busy poll for cvq command

Hi all:

The code used to busy poll for cvq command which turns out to have several side effects:

1) infinite poll for buggy devices
2) bad interaction with scheduler

So this series tries to use sleep + timeout instead of busy polling.

Please review.

Thanks

Changes since RFC:

- switch to use BAD_RING in virtio_break_device()
- check virtqueue_is_broken() after being woken up
- use more_used() instead of virtqueue_get_buf() to allow caller to get buffers afterwards
- break the virtio-net device when timeout
- get buffer manually since the virtio core check more_used() instead

Jason Wang (4):
virtio-net: convert rx mode setting to use workqueue
virtio_ring: switch to use BAD_RING()
virtio_ring: introduce a per virtqueue waitqueue
virtio-net: sleep instead of busy waiting for cvq command

 drivers/net/virtio_net.c     | 90 +++++++++++++++++++++++++++++++-----
 drivers/virtio/virtio_ring.c | 37 +++++++++++++--
 include/linux/virtio.h       |  3 ++
 3 files changed, 115 insertions(+), 15 deletions(-)
Signature · 6 lines
2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
author
#1 lore raw

PATCH 1/4 virtio-net: convert rx mode setting to use workqueue

This patch convert rx mode setting to be done in a workqueue, this is a must for allow to sleep when waiting for the cvq command to response since current code is executed under addr spin lock.

---
 drivers/net/virtio_net.c | 66 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 63 insertions(+), 3 deletions(-)
drivers/net/virtio_net.c +63 -3
index 86e52454b5b5..efd9dd55828b 100644
@@ -260,6 +260,15 @@ struct virtnet_info {
 	/* Work struct for config space updates */
 	struct work_struct config_work;
 
+	/* Work struct for config rx mode */
+	struct work_struct rx_mode_work;
+
+	/* Is rx_mode_work enabled? */
+	bool rx_mode_work_enabled;
+
+	/* The lock to synchronize the access to refill_enabled */
+	spinlock_t rx_mode_lock;
+
 	/* Does the affinity hint is set for virtqueues? */
 	bool affinity_hint_set;
 
@@ -383,6 +392,22 @@ static void disable_delayed_refill(struct virtnet_info *vi)
 	spin_unlock_bh(&vi->refill_lock);
 }
 
+static void enable_rx_mode_work(struct virtnet_info *vi)
+{
+	spin_lock_bh(&vi->rx_mode_lock);
+	vi->rx_mode_work_enabled = true;
+	spin_unlock_bh(&vi->rx_mode_lock);
+}
+
+static void disable_rx_mode_work(struct virtnet_info *vi)
+{
+	spin_lock_bh(&vi->rx_mode_lock);
+	vi->rx_mode_work_enabled = false;
+	spin_unlock_bh(&vi->rx_mode_lock);
+
+	flush_work(&vi->rx_mode_work);
+}
+
 static void virtqueue_napi_schedule(struct napi_struct *napi,
 				    struct virtqueue *vq)
 {
@@ -1974,6 +1999,8 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
 	/* Caller should know better */
 	BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
 
+	ASSERT_RTNL();
+
 	vi->ctrl->status = ~0;
 	vi->ctrl->hdr.class = class;
 	vi->ctrl->hdr.cmd = cmd;
@@ -2160,9 +2187,11 @@ static int virtnet_close(struct net_device *dev)
 	return 0;
 }
 
-static void virtnet_set_rx_mode(struct net_device *dev)
+static void virtnet_rx_mode_work(struct work_struct *work)
 {
-	struct virtnet_info *vi = netdev_priv(dev);
+	struct virtnet_info *vi =
+		container_of(work, struct virtnet_info, rx_mode_work);
+	struct net_device *dev = vi->dev;
 	struct scatterlist sg[2];
 	struct virtio_net_ctrl_mac *mac_data;
 	struct netdev_hw_addr *ha;
@@ -2175,8 +2204,12 @@ static void virtnet_set_rx_mode(struct net_device *dev)
 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
 		return;
 
+	rtnl_lock();
+
+	netif_addr_lock_bh(dev);
 	vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0);
 	vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
+	netif_addr_unlock_bh(dev);
 
 	sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc));
 
@@ -2192,14 +2225,19 @@ static void virtnet_set_rx_mode(struct net_device *dev)
 		dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
 			 vi->ctrl->allmulti ? "en" : "dis");
 
+	netif_addr_lock_bh(dev);
+
 	uc_count = netdev_uc_count(dev);
 	mc_count = netdev_mc_count(dev);
 	/* MAC filter - use one buffer for both lists */
 	buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
 		      (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
 	mac_data = buf;
-	if (!buf)
+	if (!buf) {
+		netif_addr_unlock_bh(dev);
+		rtnl_unlock();
 		return;
+	}
 
 	sg_init_table(sg, 2);
 
@@ -2220,6 +2258,8 @@ static void virtnet_set_rx_mode(struct net_device *dev)
 	netdev_for_each_mc_addr(ha, dev)
 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
 
+	netif_addr_unlock_bh(dev);
+
 	sg_set_buf(&sg[1], mac_data,
 		   sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
 
@@ -2227,9 +2267,21 @@ static void virtnet_set_rx_mode(struct net_device *dev)
 				  VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
 		dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
 
+	rtnl_unlock();
+
 	kfree(buf);
 }
 
+static void virtnet_set_rx_mode(struct net_device *dev)
+{
+	struct virtnet_info *vi = netdev_priv(dev);
+
+	spin_lock(&vi->rx_mode_lock);
+	if (vi->rx_mode_work_enabled)
+		schedule_work(&vi->rx_mode_work);
+	spin_unlock(&vi->rx_mode_lock);
+}
+
 static int virtnet_vlan_rx_add_vid(struct net_device *dev,
 				   __be16 proto, u16 vid)
 {
@@ -3000,6 +3052,7 @@ static void virtnet_freeze_down(struct virtio_device *vdev)
 
 	/* Make sure no work handler is accessing the device */
 	flush_work(&vi->config_work);
+	disable_rx_mode_work(vi);
 
 	netif_tx_lock_bh(vi->dev);
 	netif_device_detach(vi->dev);
@@ -3022,6 +3075,8 @@ static int virtnet_restore_up(struct virtio_device *vdev)
 	virtio_device_ready(vdev);
 
 	enable_delayed_refill(vi);
+	enable_rx_mode_work(vi);
+	virtnet_set_rx_mode(vi->dev);
 
 	if (netif_running(vi->dev)) {
 		err = virtnet_open(vi->dev);
@@ -3799,7 +3854,9 @@ static int virtnet_probe(struct virtio_device *vdev)
 	vdev->priv = vi;
 
 	INIT_WORK(&vi->config_work, virtnet_config_changed_work);
+	INIT_WORK(&vi->rx_mode_work, virtnet_rx_mode_work);
 	spin_lock_init(&vi->refill_lock);
+	spin_lock_init(&vi->rx_mode_lock);
 
 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
 		vi->mergeable_rx_bufs = true;
@@ -3905,6 +3962,8 @@ static int virtnet_probe(struct virtio_device *vdev)
 	if (vi->has_rss || vi->has_rss_hash_report)
 		virtnet_init_default_rss(vi);
 
+	enable_rx_mode_work(vi);
+
 	/* serialize netdev register + virtio_device_ready() with ndo_open() */
 	rtnl_lock();
 
@@ -3984,6 +4043,7 @@ static void virtnet_remove(struct virtio_device *vdev)
 
 	/* Make sure no work handler is accessing the device. */
 	flush_work(&vi->config_work);
+	disable_rx_mode_work(vi);
 
 	unregister_netdev(vi->dev);
 
Signature · 6 lines
2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#2 lore raw

Re: [PATCH 1/4] virtio-net: convert rx mode setting to use workqueue

Quoted 12 lines · Jason Wang
drivers/net/virtio_net.c +5 -0
@@ -2227,9 +2267,21 @@ static void virtnet_set_rx_mode(struct net_device *dev)
 				  VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
 		dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
 
+	rtnl_unlock();
+
 	kfree(buf);
 }
 
+static void virtnet_set_rx_mode(struct net_device *dev)
+{
+	struct virtnet_info *vi = netdev_priv(dev);

drivers/net/virtio_net.c static void virtnet_set_rx_mode(struct net_device *dev) · line 2287

Quoted 10 lines
drivers/net/virtio_net.c +7 -0
+
+	spin_lock(&vi->rx_mode_lock);
+	if (vi->rx_mode_work_enabled)
+		schedule_work(&vi->rx_mode_work);
+	spin_unlock(&vi->rx_mode_lock);
+}
+
 static int virtnet_vlan_rx_add_vid(struct net_device *dev,
 				   __be16 proto, u16 vid)
 {

Hmm so user tells us to e.g enable promisc. We report completion but card is still dropping packets. I think this
has a chance to break some setups.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
author
#3 lore raw

Re: [PATCH 1/4] virtio-net: convert rx mode setting to use workqueue

Quoted 10 lines · Michael S. Tsirkin
Quoted 9 lines · Jason Wang
drivers/net/virtio_net.c +2 -0
@@ -2227,9 +2267,21 @@ static void virtnet_set_rx_mode(struct net_device *dev)
  				  VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
  		dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
 
+	rtnl_unlock();
+
  	kfree(buf);
  }
 
Quoted 16 lines

drivers/net/virtio_net.c static void virtnet_set_rx_mode(struct net_device *dev) · line 2287

Quoted 13 lines
drivers/net/virtio_net.c +10 -0
+static void virtnet_set_rx_mode(struct net_device *dev)
+{
+	struct virtnet_info *vi = netdev_priv(dev);
+
+	spin_lock(&vi->rx_mode_lock);
+	if (vi->rx_mode_work_enabled)
+		schedule_work(&vi->rx_mode_work);
+	spin_unlock(&vi->rx_mode_lock);
+}
+
  static int virtnet_vlan_rx_add_vid(struct net_device *dev,
  				   __be16 proto, u16 vid)
  {

Hmm so user tells us to e.g enable promisc. We report completion but card is still dropping packets. I think this
has a chance to break some setups.

I think all those filters are best efforts, am I wrong?

Thanks

Quoted 1 line

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

maintainer
#4 lore raw

Re: [PATCH 1/4] virtio-net: convert rx mode setting to use workqueue

Quoted 5 lines · On Tue, 27 Dec 2022 17:06:10 +0800 Jason Wang wrote:
Quoted 3 lines

Hmm so user tells us to e.g enable promisc. We report completion but card is still dropping packets. I think this
has a chance to break some setups.

I think all those filters are best efforts, am I wrong?

Are the flags protected by the addr lock which needs BH, tho?

Taking netif_addr_lock_bh() to look at dev->flags seems a bit surprising to me.

author
#5 lore raw

Re: [PATCH 1/4] virtio-net: convert rx mode setting to use workqueue

Quoted 6 lines · Jakub Kicinski
Quoted 4 lines · On Tue, 27 Dec 2022 17:06:10 +0800 Jason Wang wrote:
Quoted 3 lines

Hmm so user tells us to e.g enable promisc. We report completion but card is still dropping packets. I think this
has a chance to break some setups.

Quoted 7 lines
Quoted 1 line

I think all those filters are best efforts, am I wrong?

Are the flags protected by the addr lock which needs BH, tho?

Taking netif_addr_lock_bh() to look at dev->flags seems a bit surprising to me.

Yes, RTNL should be sufficient here. Will fix it.

Thanks

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#6 lore raw

PATCH 2/4 virtio_ring: switch to use BAD_RING()

In reply to Jason Wang

Switch to reuse BAD_RING() to allow common logic to be implemented in BAD_RING().

---
Changes since V1:
- switch to use BAD_RING in virtio_break_device()

---
 drivers/virtio/virtio_ring.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/virtio/virtio_ring.c +4 -4
index 2e7689bb933b..5cfb2fa8abee 100644
@@ -58,7 +58,8 @@
 	do {							\
 		dev_err(&_vq->vq.vdev->dev,			\
 			"%s:"fmt, (_vq)->vq.name, ##args);	\
-		(_vq)->broken = true;				\
+		/* Pairs with READ_ONCE() in virtqueue_is_broken(). */ \
+		WRITE_ONCE((_vq)->broken, true);		       \
 	} while (0)
 #define START_USE(vq)
 #define END_USE(vq)
@@ -2237,7 +2238,7 @@ bool virtqueue_notify(struct virtqueue *_vq)
 
 	/* Prod other side to tell it about changes. */
 	if (!vq->notify(_vq)) {
-		vq->broken = true;
+		BAD_RING(vq, "vq %d is broken\n", vq->vq.index);
 		return false;
 	}
 	return true;
@@ -2786,8 +2787,7 @@ void virtio_break_device(struct virtio_device *dev)
 	list_for_each_entry(_vq, &dev->vqs, list) {
 		struct vring_virtqueue *vq = to_vvq(_vq);
 
-		/* Pairs with READ_ONCE() in virtqueue_is_broken(). */
-		WRITE_ONCE(vq->broken, true);
+		BAD_RING(vq, "Device break vq %d", _vq->index);
 	}
 	spin_unlock(&dev->vqs_list_lock);
 }
Signature · 6 lines
2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#7 lore raw

Re: [PATCH 2/4] virtio_ring: switch to use BAD_RING()

Quoted 16 lines · Jason Wang

Switch to reuse BAD_RING() to allow common logic to be implemented in BAD_RING().

---
Changes since V1:
- switch to use BAD_RING in virtio_break_device()

---
 drivers/virtio/virtio_ring.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/virtio/virtio_ring.c +0 -0
index 2e7689bb933b..5cfb2fa8abee 100644

drivers/virtio/virtio_ring.c line 61

Quoted 5 lines
drivers/virtio/virtio_ring.c +1 -1
@@ -58,7 +58,8 @@
 	do {							\
 		dev_err(&_vq->vq.vdev->dev,			\
 			"%s:"fmt, (_vq)->vq.name, ##args);	\
-		(_vq)->broken = true;				\
+		/* Pairs with READ_ONCE() in virtqueue_is_broken(). */ \

I don't think WRITE_ONCE/READ_ONCE pair as such. Can you point me at documentation of such pairing?

Quoted 25 lines
drivers/virtio/virtio_ring.c +3 -3
+		WRITE_ONCE((_vq)->broken, true);		       \
 	} while (0)
 #define START_USE(vq)
 #define END_USE(vq)
@@ -2237,7 +2238,7 @@ bool virtqueue_notify(struct virtqueue *_vq)
 
 	/* Prod other side to tell it about changes. */
 	if (!vq->notify(_vq)) {
-		vq->broken = true;
+		BAD_RING(vq, "vq %d is broken\n", vq->vq.index);
 		return false;
 	}
 	return true;
@@ -2786,8 +2787,7 @@ void virtio_break_device(struct virtio_device *dev)
 	list_for_each_entry(_vq, &dev->vqs, list) {
 		struct vring_virtqueue *vq = to_vvq(_vq);
 
-		/* Pairs with READ_ONCE() in virtqueue_is_broken(). */
-		WRITE_ONCE(vq->broken, true);
+		BAD_RING(vq, "Device break vq %d", _vq->index);
 	}
 	spin_unlock(&dev->vqs_list_lock);
 }
Signature · 1 line
2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#8 lore raw

Re: [PATCH 2/4] virtio_ring: switch to use BAD_RING()

Quoted 13 lines · Michael S. Tsirkin
Quoted 11 lines · Jason Wang

Switch to reuse BAD_RING() to allow common logic to be implemented in BAD_RING().

---
Changes since V1:
- switch to use BAD_RING in virtio_break_device()

---
 drivers/virtio/virtio_ring.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Quoted 13 lines

drivers/virtio/virtio_ring.c line 61

Quoted 10 lines
drivers/virtio/virtio_ring.c +1 -1
index 2e7689bb933b..5cfb2fa8abee 100644
@@ -58,7 +58,8 @@
      do {                                                    \
              dev_err(&_vq->vq.vdev->dev,                     \
                      "%s:"fmt, (_vq)->vq.name, ##args);      \
-             (_vq)->broken = true;                           \
+             /* Pairs with READ_ONCE() in virtqueue_is_broken(). */ \

I don't think WRITE_ONCE/READ_ONCE pair as such. Can you point me at documentation of such pairing?

Introduced by:

commit 60f0779862e4ab943810187752c462e85f5fa371

Author: Parav Pandit <[email protected]>
Date:   Wed Jul 21 17:26:45 2021 +0300
    virtio: Improve vq->broken access to avoid any compiler optimization

I think it might still apply here since virtqueue_is_broken() is still put into a loop inside wait_event().

Thanks

Quoted 18 lines
Quoted 18 lines
drivers/virtio/virtio_ring.c +2 -1
+             WRITE_ONCE((_vq)->broken, true);                       \
      } while (0)
 #define START_USE(vq)
 #define END_USE(vq)
@@ -2237,7 +2238,7 @@ bool virtqueue_notify(struct virtqueue *_vq)
 
      /* Prod other side to tell it about changes. */
      if (!vq->notify(_vq)) {
-             vq->broken = true;
+             BAD_RING(vq, "vq %d is broken\n", vq->vq.index);
              return false;
      }
      return true;
@@ -2786,8 +2787,7 @@ void virtio_break_device(struct virtio_device *dev)
      list_for_each_entry(_vq, &dev->vqs, list) {
              struct vring_virtqueue *vq = to_vvq(_vq);
 
Quoted 9 lines
Quoted 8 lines
drivers/virtio/virtio_ring.c +1 -3
-             /* Pairs with READ_ONCE() in virtqueue_is_broken(). */
-             WRITE_ONCE(vq->broken, true);
+             BAD_RING(vq, "Device break vq %d", _vq->index);
      }
      spin_unlock(&dev->vqs_list_lock);
 }
--

2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#9 lore raw

Re: [PATCH 2/4] virtio_ring: switch to use BAD_RING()

Quoted 34 lines · Jason Wang
Quoted 13 lines · Michael S. Tsirkin
Quoted 11 lines · Jason Wang

Switch to reuse BAD_RING() to allow common logic to be implemented in BAD_RING().

---
Changes since V1:
- switch to use BAD_RING in virtio_break_device()

---
 drivers/virtio/virtio_ring.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Quoted 13 lines

drivers/virtio/virtio_ring.c line 61

Quoted 10 lines
drivers/virtio/virtio_ring.c +1 -1
index 2e7689bb933b..5cfb2fa8abee 100644
@@ -58,7 +58,8 @@
      do {                                                    \
              dev_err(&_vq->vq.vdev->dev,                     \
                      "%s:"fmt, (_vq)->vq.name, ##args);      \
-             (_vq)->broken = true;                           \
+             /* Pairs with READ_ONCE() in virtqueue_is_broken(). */ \

I don't think WRITE_ONCE/READ_ONCE pair as such. Can you point me at documentation of such pairing?

Introduced by:

commit 60f0779862e4ab943810187752c462e85f5fa371

Author: Parav Pandit <[email protected]>
Date:   Wed Jul 21 17:26:45 2021 +0300
Quoted 6 lines
    virtio: Improve vq->broken access to avoid any compiler optimization

I think it might still apply here since virtqueue_is_broken() is still put into a loop inside wait_event().

Thanks

Oh I see. Maybe it's a response to some discussion we had at the time, at this point I can no longer say what it meant.
But you are doing right not changing it here of course.

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
drivers/virtio/virtio_ring.c +2 -1
+             WRITE_ONCE((_vq)->broken, true);                       \
      } while (0)
 #define START_USE(vq)
 #define END_USE(vq)
@@ -2237,7 +2238,7 @@ bool virtqueue_notify(struct virtqueue *_vq)
 
      /* Prod other side to tell it about changes. */
      if (!vq->notify(_vq)) {
-             vq->broken = true;
+             BAD_RING(vq, "vq %d is broken\n", vq->vq.index);
              return false;
      }
      return true;
@@ -2786,8 +2787,7 @@ void virtio_break_device(struct virtio_device *dev)
      list_for_each_entry(_vq, &dev->vqs, list) {
              struct vring_virtqueue *vq = to_vvq(_vq);
 
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
drivers/virtio/virtio_ring.c +1 -3
-             /* Pairs with READ_ONCE() in virtqueue_is_broken(). */
-             WRITE_ONCE(vq->broken, true);
+             BAD_RING(vq, "Device break vq %d", _vq->index);
      }
      spin_unlock(&dev->vqs_list_lock);
 }
--

2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#10 lore raw

PATCH 3/4 virtio_ring: introduce a per virtqueue waitqueue

In reply to Jason Wang

This patch introduces a per virtqueue waitqueue to allow driver to sleep and wait for more used. Two new helpers are introduced to allow driver to sleep and wake up.

---
Changes since V1:
- check virtqueue_is_broken() as well
- use more_used() instead of virtqueue_get_buf() to allow caller to get buffers afterwards

---
 drivers/virtio/virtio_ring.c | 29 +++++++++++++++++++++++++++++
 include/linux/virtio.h       |  3 +++
 2 files changed, 32 insertions(+)
drivers/virtio/virtio_ring.c +29 -0
index 5cfb2fa8abee..9c83eb945493 100644
@@ -13,6 +13,7 @@
 #include <linux/dma-mapping.h>
 #include <linux/kmsan.h>
 #include <linux/spinlock.h>
+#include <linux/wait.h>
 #include <xen/xen.h>
 
 #ifdef DEBUG
@@ -60,6 +61,7 @@
 			"%s:"fmt, (_vq)->vq.name, ##args);	\
 		/* Pairs with READ_ONCE() in virtqueue_is_broken(). */ \
 		WRITE_ONCE((_vq)->broken, true);		       \
+		wake_up_interruptible(&(_vq)->wq);		       \
 	} while (0)
 #define START_USE(vq)
 #define END_USE(vq)
@@ -203,6 +205,9 @@ struct vring_virtqueue {
 	/* DMA, allocation, and size information */
 	bool we_own_ring;
 
+	/* Wait for buffer to be used */
+	wait_queue_head_t wq;
+
 #ifdef DEBUG
 	/* They're supposed to lock for us. */
 	unsigned int in_use;
@@ -2024,6 +2029,8 @@ static struct virtqueue *vring_create_virtqueue_packed(
 	if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
 		vq->weak_barriers = false;
 
+	init_waitqueue_head(&vq->wq);
+
 	err = vring_alloc_state_extra_packed(&vring_packed);
 	if (err)
 		goto err_state_extra;
@@ -2517,6 +2524,8 @@ static struct virtqueue *__vring_new_virtqueue(unsigned int index,
 	if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
 		vq->weak_barriers = false;
 
+	init_waitqueue_head(&vq->wq);
+
 	err = vring_alloc_state_extra_split(vring_split);
 	if (err) {
 		kfree(vq);
@@ -2654,6 +2663,8 @@ static void vring_free(struct virtqueue *_vq)
 {
 	struct vring_virtqueue *vq = to_vvq(_vq);
 
+	wake_up_interruptible(&vq->wq);
+
 	if (vq->we_own_ring) {
 		if (vq->packed_ring) {
 			vring_free_queue(vq->vq.vdev,
@@ -2863,4 +2874,22 @@ const struct vring *virtqueue_get_vring(struct virtqueue *vq)
 }
 EXPORT_SYMBOL_GPL(virtqueue_get_vring);
 
+int virtqueue_wait_for_used(struct virtqueue *_vq)
+{
+	struct vring_virtqueue *vq = to_vvq(_vq);
+
+	/* TODO: Tweak the timeout. */
+	return wait_event_interruptible_timeout(vq->wq,
+	       virtqueue_is_broken(_vq) || more_used(vq), HZ);
+}
+EXPORT_SYMBOL_GPL(virtqueue_wait_for_used);
+
+void virtqueue_wake_up(struct virtqueue *_vq)
+{
+	struct vring_virtqueue *vq = to_vvq(_vq);
+
+	wake_up_interruptible(&vq->wq);
+}
+EXPORT_SYMBOL_GPL(virtqueue_wake_up);
+
 MODULE_LICENSE("GPL");
include/linux/virtio.h +3 -0
index dcab9c7e8784..2eb62c774895 100644
@@ -72,6 +72,9 @@ void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len);
 void *virtqueue_get_buf_ctx(struct virtqueue *vq, unsigned int *len,
 			    void **ctx);
 
+int virtqueue_wait_for_used(struct virtqueue *vq);
+void virtqueue_wake_up(struct virtqueue *vq);
+
 void virtqueue_disable_cb(struct virtqueue *vq);
 
 bool virtqueue_enable_cb(struct virtqueue *vq);
Signature · 6 lines
2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#11 lore raw

Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue

Quoted 73 lines · Jason Wang

This patch introduces a per virtqueue waitqueue to allow driver to sleep and wait for more used. Two new helpers are introduced to allow driver to sleep and wake up.

---
Changes since V1:
- check virtqueue_is_broken() as well
- use more_used() instead of virtqueue_get_buf() to allow caller to get buffers afterwards

---
 drivers/virtio/virtio_ring.c | 29 +++++++++++++++++++++++++++++
 include/linux/virtio.h       |  3 +++
 2 files changed, 32 insertions(+)
drivers/virtio/virtio_ring.c +11 -0
index 5cfb2fa8abee..9c83eb945493 100644
@@ -13,6 +13,7 @@
 #include <linux/dma-mapping.h>
 #include <linux/kmsan.h>
 #include <linux/spinlock.h>
+#include <linux/wait.h>
 #include <xen/xen.h>
 
 #ifdef DEBUG
@@ -60,6 +61,7 @@
 			"%s:"fmt, (_vq)->vq.name, ##args);	\
 		/* Pairs with READ_ONCE() in virtqueue_is_broken(). */ \
 		WRITE_ONCE((_vq)->broken, true);		       \
+		wake_up_interruptible(&(_vq)->wq);		       \
 	} while (0)
 #define START_USE(vq)
 #define END_USE(vq)
@@ -203,6 +205,9 @@ struct vring_virtqueue {
 	/* DMA, allocation, and size information */
 	bool we_own_ring;
 
+	/* Wait for buffer to be used */
+	wait_queue_head_t wq;
+
 #ifdef DEBUG
 	/* They're supposed to lock for us. */
 	unsigned int in_use;
@@ -2024,6 +2029,8 @@ static struct virtqueue *vring_create_virtqueue_packed(
 	if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
 		vq->weak_barriers = false;
 
+	init_waitqueue_head(&vq->wq);
+
 	err = vring_alloc_state_extra_packed(&vring_packed);
 	if (err)
 		goto err_state_extra;
@@ -2517,6 +2524,8 @@ static struct virtqueue *__vring_new_virtqueue(unsigned int index,
 	if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
 		vq->weak_barriers = false;
 
+	init_waitqueue_head(&vq->wq);
+
 	err = vring_alloc_state_extra_split(vring_split);
 	if (err) {
 		kfree(vq);
@@ -2654,6 +2663,8 @@ static void vring_free(struct virtqueue *_vq)
 {
 	struct vring_virtqueue *vq = to_vvq(_vq);
 
+	wake_up_interruptible(&vq->wq);
+
 	if (vq->we_own_ring) {
 		if (vq->packed_ring) {
 			vring_free_queue(vq->vq.vdev,

drivers/virtio/virtio_ring.c const struct vring *virtqueue_get_vring(struct virtqueue *vq) · line 2883

Quoted 10 lines
drivers/virtio/virtio_ring.c +7 -0
@@ -2863,4 +2874,22 @@ const struct vring *virtqueue_get_vring(struct virtqueue *vq)
 }
 EXPORT_SYMBOL_GPL(virtqueue_get_vring);
 
+int virtqueue_wait_for_used(struct virtqueue *_vq)
+{
+	struct vring_virtqueue *vq = to_vvq(_vq);
+
+	/* TODO: Tweak the timeout. */
+	return wait_event_interruptible_timeout(vq->wq,
+	       virtqueue_is_broken(_vq) || more_used(vq), HZ);

There's no good timeout. Let's not even go there, if device goes bad it should set the need reset bit.

Quoted 28 lines
drivers/virtio/virtio_ring.c +11 -0
+}
+EXPORT_SYMBOL_GPL(virtqueue_wait_for_used);
+
+void virtqueue_wake_up(struct virtqueue *_vq)
+{
+	struct vring_virtqueue *vq = to_vvq(_vq);
+
+	wake_up_interruptible(&vq->wq);
+}
+EXPORT_SYMBOL_GPL(virtqueue_wake_up);
+
 MODULE_LICENSE("GPL");
include/linux/virtio.h +3 -0
index dcab9c7e8784..2eb62c774895 100644
@@ -72,6 +72,9 @@ void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len);
 void *virtqueue_get_buf_ctx(struct virtqueue *vq, unsigned int *len,
 			    void **ctx);
 
+int virtqueue_wait_for_used(struct virtqueue *vq);
+void virtqueue_wake_up(struct virtqueue *vq);
+
 void virtqueue_disable_cb(struct virtqueue *vq);
 
 bool virtqueue_enable_cb(struct virtqueue *vq);
Signature · 1 line
2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#12 lore raw

Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue

Quoted 78 lines · Michael S. Tsirkin
Quoted 76 lines · Jason Wang

This patch introduces a per virtqueue waitqueue to allow driver to sleep and wait for more used. Two new helpers are introduced to allow driver to sleep and wake up.

---
Changes since V1:
- check virtqueue_is_broken() as well
- use more_used() instead of virtqueue_get_buf() to allow caller to get buffers afterwards

---
 drivers/virtio/virtio_ring.c | 29 +++++++++++++++++++++++++++++
 include/linux/virtio.h       |  3 +++
 2 files changed, 32 insertions(+)
drivers/virtio/virtio_ring.c +11 -0
index 5cfb2fa8abee..9c83eb945493 100644
@@ -13,6 +13,7 @@
 #include <linux/dma-mapping.h>
 #include <linux/kmsan.h>
 #include <linux/spinlock.h>
+#include <linux/wait.h>
 #include <xen/xen.h>
 
 #ifdef DEBUG
@@ -60,6 +61,7 @@
                      "%s:"fmt, (_vq)->vq.name, ##args);      \
              /* Pairs with READ_ONCE() in virtqueue_is_broken(). */ \
              WRITE_ONCE((_vq)->broken, true);                       \
+             wake_up_interruptible(&(_vq)->wq);                     \
      } while (0)
 #define START_USE(vq)
 #define END_USE(vq)
@@ -203,6 +205,9 @@ struct vring_virtqueue {
      /* DMA, allocation, and size information */
      bool we_own_ring;
 
+     /* Wait for buffer to be used */
+     wait_queue_head_t wq;
+
 #ifdef DEBUG
      /* They're supposed to lock for us. */
      unsigned int in_use;
@@ -2024,6 +2029,8 @@ static struct virtqueue *vring_create_virtqueue_packed(
      if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
              vq->weak_barriers = false;
 
+     init_waitqueue_head(&vq->wq);
+
      err = vring_alloc_state_extra_packed(&vring_packed);
      if (err)
              goto err_state_extra;
@@ -2517,6 +2524,8 @@ static struct virtqueue *__vring_new_virtqueue(unsigned int index,
      if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
              vq->weak_barriers = false;
 
+     init_waitqueue_head(&vq->wq);
+
      err = vring_alloc_state_extra_split(vring_split);
      if (err) {
              kfree(vq);
@@ -2654,6 +2663,8 @@ static void vring_free(struct virtqueue *_vq)
 {
      struct vring_virtqueue *vq = to_vvq(_vq);
 
+     wake_up_interruptible(&vq->wq);
+
      if (vq->we_own_ring) {
              if (vq->packed_ring) {
                      vring_free_queue(vq->vq.vdev,
@@ -2863,4 +2874,22 @@ const struct vring *virtqueue_get_vring(struct virtqueue *vq)
 }
 EXPORT_SYMBOL_GPL(virtqueue_get_vring);
 
Quoted 10 lines

drivers/virtio/virtio_ring.c const struct vring *virtqueue_get_vring(struct virtqueue *vq) · line 2883

Quoted 7 lines
drivers/virtio/virtio_ring.c +7 -0
+int virtqueue_wait_for_used(struct virtqueue *_vq)
+{
+     struct vring_virtqueue *vq = to_vvq(_vq);
+
+     /* TODO: Tweak the timeout. */
+     return wait_event_interruptible_timeout(vq->wq,
+            virtqueue_is_broken(_vq) || more_used(vq), HZ);

There's no good timeout. Let's not even go there, if device goes bad it should set the need reset bit.

The problem is that we can't depend on the device. If it takes too long for the device to respond to cvq, there's a high possibility that the device is buggy or even malicious. We can have a higher timeout here and it should be still better than waiting forever (the cvq commands need to be serialized so it needs to hold a lock anyway (RTNL) ).

Thanks

Quoted 22 lines
Quoted 22 lines
drivers/virtio/virtio_ring.c +11 -0
+}
+EXPORT_SYMBOL_GPL(virtqueue_wait_for_used);
+
+void virtqueue_wake_up(struct virtqueue *_vq)
+{
+     struct vring_virtqueue *vq = to_vvq(_vq);
+
+     wake_up_interruptible(&vq->wq);
+}
+EXPORT_SYMBOL_GPL(virtqueue_wake_up);
+
 MODULE_LICENSE("GPL");
include/linux/virtio.h +0 -0
index dcab9c7e8784..2eb62c774895 100644
@@ -72,6 +72,9 @@ void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len);
 void *virtqueue_get_buf_ctx(struct virtqueue *vq, unsigned int *len,
                          void **ctx);
 
Quoted 9 lines
Quoted 8 lines
include/linux/virtio.h +3 -1
+int virtqueue_wait_for_used(struct virtqueue *vq);
+void virtqueue_wake_up(struct virtqueue *vq);
+
 void virtqueue_disable_cb(struct virtqueue *vq);
 
 bool virtqueue_enable_cb(struct virtqueue *vq);
--

2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#13 lore raw

Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue

Quoted 90 lines · Jason Wang
Quoted 78 lines · Michael S. Tsirkin
Quoted 76 lines · Jason Wang

This patch introduces a per virtqueue waitqueue to allow driver to sleep and wait for more used. Two new helpers are introduced to allow driver to sleep and wake up.

---
Changes since V1:
- check virtqueue_is_broken() as well
- use more_used() instead of virtqueue_get_buf() to allow caller to get buffers afterwards

---
 drivers/virtio/virtio_ring.c | 29 +++++++++++++++++++++++++++++
 include/linux/virtio.h       |  3 +++
 2 files changed, 32 insertions(+)
drivers/virtio/virtio_ring.c +11 -0
index 5cfb2fa8abee..9c83eb945493 100644
@@ -13,6 +13,7 @@
 #include <linux/dma-mapping.h>
 #include <linux/kmsan.h>
 #include <linux/spinlock.h>
+#include <linux/wait.h>
 #include <xen/xen.h>
 
 #ifdef DEBUG
@@ -60,6 +61,7 @@
                      "%s:"fmt, (_vq)->vq.name, ##args);      \
              /* Pairs with READ_ONCE() in virtqueue_is_broken(). */ \
              WRITE_ONCE((_vq)->broken, true);                       \
+             wake_up_interruptible(&(_vq)->wq);                     \
      } while (0)
 #define START_USE(vq)
 #define END_USE(vq)
@@ -203,6 +205,9 @@ struct vring_virtqueue {
      /* DMA, allocation, and size information */
      bool we_own_ring;
 
+     /* Wait for buffer to be used */
+     wait_queue_head_t wq;
+
 #ifdef DEBUG
      /* They're supposed to lock for us. */
      unsigned int in_use;
@@ -2024,6 +2029,8 @@ static struct virtqueue *vring_create_virtqueue_packed(
      if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
              vq->weak_barriers = false;
 
+     init_waitqueue_head(&vq->wq);
+
      err = vring_alloc_state_extra_packed(&vring_packed);
      if (err)
              goto err_state_extra;
@@ -2517,6 +2524,8 @@ static struct virtqueue *__vring_new_virtqueue(unsigned int index,
      if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
              vq->weak_barriers = false;
 
+     init_waitqueue_head(&vq->wq);
+
      err = vring_alloc_state_extra_split(vring_split);
      if (err) {
              kfree(vq);
@@ -2654,6 +2663,8 @@ static void vring_free(struct virtqueue *_vq)
 {
      struct vring_virtqueue *vq = to_vvq(_vq);
 
+     wake_up_interruptible(&vq->wq);
+
      if (vq->we_own_ring) {
              if (vq->packed_ring) {
                      vring_free_queue(vq->vq.vdev,
@@ -2863,4 +2874,22 @@ const struct vring *virtqueue_get_vring(struct virtqueue *vq)
 }
 EXPORT_SYMBOL_GPL(virtqueue_get_vring);
 
Quoted 10 lines

drivers/virtio/virtio_ring.c const struct vring *virtqueue_get_vring(struct virtqueue *vq) · line 2883

Quoted 7 lines
drivers/virtio/virtio_ring.c +7 -0
+int virtqueue_wait_for_used(struct virtqueue *_vq)
+{
+     struct vring_virtqueue *vq = to_vvq(_vq);
+
+     /* TODO: Tweak the timeout. */
+     return wait_event_interruptible_timeout(vq->wq,
+            virtqueue_is_broken(_vq) || more_used(vq), HZ);

There's no good timeout. Let's not even go there, if device goes bad it should set the need reset bit.

Quoted 8 lines

The problem is that we can't depend on the device. If it takes too long for the device to respond to cvq, there's a high possibility that the device is buggy or even malicious. We can have a higher timeout here and it should be still better than waiting forever (the cvq commands need to be serialized so it needs to hold a lock anyway (RTNL) ).

Thanks

With a TODO item like this I'd expect this to be an RFC. Here's why:

Making driver more robust from device failures is a laudable goal but it's really hard to be 100% foolproof here. E.g. device can just block pci reads and it would be very hard to recover. So I'm going to only merge patches like this if they at least theoretically have very little chance
of breaking existing users.

And note that in most setups, CVQ is only used at startup and then left mostly alone.

Finally, note that lots of guests need virtio to do anything useful at all. So just failing commands is not enough to recover - you need to try harder maybe by attempting to reset device. Could be a question of policy - might need to make this guest configurable.

Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
drivers/virtio/virtio_ring.c +11 -0
+}
+EXPORT_SYMBOL_GPL(virtqueue_wait_for_used);
+
+void virtqueue_wake_up(struct virtqueue *_vq)
+{
+     struct vring_virtqueue *vq = to_vvq(_vq);
+
+     wake_up_interruptible(&vq->wq);
+}
+EXPORT_SYMBOL_GPL(virtqueue_wake_up);
+
 MODULE_LICENSE("GPL");
include/linux/virtio.h +3 -0
index dcab9c7e8784..2eb62c774895 100644
@@ -72,6 +72,9 @@ void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len);
 void *virtqueue_get_buf_ctx(struct virtqueue *vq, unsigned int *len,
                          void **ctx);
 
+int virtqueue_wait_for_used(struct virtqueue *vq);
+void virtqueue_wake_up(struct virtqueue *vq);
+
 void virtqueue_disable_cb(struct virtqueue *vq);
 
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
include/linux/virtio.h +0 -1
 bool virtqueue_enable_cb(struct virtqueue *vq);
--

2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#14 lore raw

Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue

Quoted 95 lines · Michael S. Tsirkin
Quoted 94 lines · Jason Wang
Quoted 77 lines · Michael S. Tsirkin
Quoted 76 lines · Jason Wang

This patch introduces a per virtqueue waitqueue to allow driver to sleep and wait for more used. Two new helpers are introduced to allow driver to sleep and wake up.

---
Changes since V1:
- check virtqueue_is_broken() as well
- use more_used() instead of virtqueue_get_buf() to allow caller to get buffers afterwards

---
  drivers/virtio/virtio_ring.c | 29 +++++++++++++++++++++++++++++
  include/linux/virtio.h       |  3 +++
  2 files changed, 32 insertions(+)
drivers/virtio/virtio_ring.c +11 -0
index 5cfb2fa8abee..9c83eb945493 100644
@@ -13,6 +13,7 @@
  #include <linux/dma-mapping.h>
  #include <linux/kmsan.h>
  #include <linux/spinlock.h>
+#include <linux/wait.h>
  #include <xen/xen.h>
 
  #ifdef DEBUG
@@ -60,6 +61,7 @@
                       "%s:"fmt, (_vq)->vq.name, ##args);      \
               /* Pairs with READ_ONCE() in virtqueue_is_broken(). */ \
               WRITE_ONCE((_vq)->broken, true);                       \
+             wake_up_interruptible(&(_vq)->wq);                     \
       } while (0)
  #define START_USE(vq)
  #define END_USE(vq)
@@ -203,6 +205,9 @@ struct vring_virtqueue {
       /* DMA, allocation, and size information */
       bool we_own_ring;
 
+     /* Wait for buffer to be used */
+     wait_queue_head_t wq;
+
  #ifdef DEBUG
       /* They're supposed to lock for us. */
       unsigned int in_use;
@@ -2024,6 +2029,8 @@ static struct virtqueue *vring_create_virtqueue_packed(
       if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
               vq->weak_barriers = false;
 
+     init_waitqueue_head(&vq->wq);
+
       err = vring_alloc_state_extra_packed(&vring_packed);
       if (err)
               goto err_state_extra;
@@ -2517,6 +2524,8 @@ static struct virtqueue *__vring_new_virtqueue(unsigned int index,
       if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
               vq->weak_barriers = false;
 
+     init_waitqueue_head(&vq->wq);
+
       err = vring_alloc_state_extra_split(vring_split);
       if (err) {
               kfree(vq);
@@ -2654,6 +2663,8 @@ static void vring_free(struct virtqueue *_vq)
  {
       struct vring_virtqueue *vq = to_vvq(_vq);
 
+     wake_up_interruptible(&vq->wq);
+
       if (vq->we_own_ring) {
               if (vq->packed_ring) {
                       vring_free_queue(vq->vq.vdev,
@@ -2863,4 +2874,22 @@ const struct vring *virtqueue_get_vring(struct virtqueue *vq)
  }
  EXPORT_SYMBOL_GPL(virtqueue_get_vring);
 
Quoted 9 lines

drivers/virtio/virtio_ring.c const struct vring *virtqueue_get_vring(struct virtqueue *vq) · line 2883

Quoted 7 lines
drivers/virtio/virtio_ring.c +7 -0
+int virtqueue_wait_for_used(struct virtqueue *_vq)
+{
+     struct vring_virtqueue *vq = to_vvq(_vq);
+
+     /* TODO: Tweak the timeout. */
+     return wait_event_interruptible_timeout(vq->wq,
+            virtqueue_is_broken(_vq) || more_used(vq), HZ);

There's no good timeout. Let's not even go there, if device goes bad it should set the need reset bit.

The problem is that we can't depend on the device. If it takes too long for the device to respond to cvq, there's a high possibility that the device is buggy or even malicious. We can have a higher timeout here and it should be still better than waiting forever (the cvq commands need to be serialized so it needs to hold a lock anyway (RTNL) ).

Quoted 7 lines
Quoted 1 line

Thanks

With a TODO item like this I'd expect this to be an RFC. Here's why:

Making driver more robust from device failures is a laudable goal but it's really hard to be 100% foolproof here. E.g. device can just block pci reads and it would be very hard to recover.

Yes.

Quoted 3 lines

So I'm going to only merge patches
like this if they at least theoretically have very little chance of breaking existing users.

AFAIK, this is not theoretical, consider:

1) DPU may implement virtio-net CVQ with codes running in CPU
2) VDUSE may want to support CVQ in the future

Quoted 6 lines

And note that in most setups, CVQ is only used at startup and then left mostly alone.

Finally, note that lots of guests need virtio to do anything useful at all. So just failing commands is not enough to recover - you need to try harder maybe by attempting to reset device.

This requires upper layer support which seems not existed in the networking subsystem.

Quoted 2 lines

Could be a question of
policy - might need to make this guest configurable.

Yes.

Thanks

Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
drivers/virtio/virtio_ring.c +11 -0
+}
+EXPORT_SYMBOL_GPL(virtqueue_wait_for_used);
+
+void virtqueue_wake_up(struct virtqueue *_vq)
+{
+     struct vring_virtqueue *vq = to_vvq(_vq);
+
+     wake_up_interruptible(&vq->wq);
+}
+EXPORT_SYMBOL_GPL(virtqueue_wake_up);
+
  MODULE_LICENSE("GPL");
include/linux/virtio.h +0 -0
index dcab9c7e8784..2eb62c774895 100644
@@ -72,6 +72,9 @@ void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len);
  void *virtqueue_get_buf_ctx(struct virtqueue *vq, unsigned int *len,
                           void **ctx);
 
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
include/linux/virtio.h +3 -1
+int virtqueue_wait_for_used(struct virtqueue *vq);
+void virtqueue_wake_up(struct virtqueue *vq);
+
  void virtqueue_disable_cb(struct virtqueue *vq);
 
  bool virtqueue_enable_cb(struct virtqueue *vq);
--

2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#15 lore raw

Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue

In reply to Jason Wang
Quoted 73 lines · Jason Wang

This patch introduces a per virtqueue waitqueue to allow driver to sleep and wait for more used. Two new helpers are introduced to allow driver to sleep and wake up.

---
Changes since V1:
- check virtqueue_is_broken() as well
- use more_used() instead of virtqueue_get_buf() to allow caller to get buffers afterwards

---
 drivers/virtio/virtio_ring.c | 29 +++++++++++++++++++++++++++++
 include/linux/virtio.h       |  3 +++
 2 files changed, 32 insertions(+)
drivers/virtio/virtio_ring.c +11 -0
index 5cfb2fa8abee..9c83eb945493 100644
@@ -13,6 +13,7 @@
 #include <linux/dma-mapping.h>
 #include <linux/kmsan.h>
 #include <linux/spinlock.h>
+#include <linux/wait.h>
 #include <xen/xen.h>
 
 #ifdef DEBUG
@@ -60,6 +61,7 @@
 			"%s:"fmt, (_vq)->vq.name, ##args);	\
 		/* Pairs with READ_ONCE() in virtqueue_is_broken(). */ \
 		WRITE_ONCE((_vq)->broken, true);		       \
+		wake_up_interruptible(&(_vq)->wq);		       \
 	} while (0)
 #define START_USE(vq)
 #define END_USE(vq)
@@ -203,6 +205,9 @@ struct vring_virtqueue {
 	/* DMA, allocation, and size information */
 	bool we_own_ring;
 
+	/* Wait for buffer to be used */
+	wait_queue_head_t wq;
+
 #ifdef DEBUG
 	/* They're supposed to lock for us. */
 	unsigned int in_use;
@@ -2024,6 +2029,8 @@ static struct virtqueue *vring_create_virtqueue_packed(
 	if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
 		vq->weak_barriers = false;
 
+	init_waitqueue_head(&vq->wq);
+
 	err = vring_alloc_state_extra_packed(&vring_packed);
 	if (err)
 		goto err_state_extra;
@@ -2517,6 +2524,8 @@ static struct virtqueue *__vring_new_virtqueue(unsigned int index,
 	if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
 		vq->weak_barriers = false;
 
+	init_waitqueue_head(&vq->wq);
+
 	err = vring_alloc_state_extra_split(vring_split);
 	if (err) {
 		kfree(vq);
@@ -2654,6 +2663,8 @@ static void vring_free(struct virtqueue *_vq)
 {
 	struct vring_virtqueue *vq = to_vvq(_vq);
 
+	wake_up_interruptible(&vq->wq);
+
 	if (vq->we_own_ring) {
 		if (vq->packed_ring) {
 			vring_free_queue(vq->vq.vdev,

drivers/virtio/virtio_ring.c const struct vring *virtqueue_get_vring(struct virtqueue *vq) · line 2883

Quoted 10 lines
drivers/virtio/virtio_ring.c +7 -0
@@ -2863,4 +2874,22 @@ const struct vring *virtqueue_get_vring(struct virtqueue *vq)
 }
 EXPORT_SYMBOL_GPL(virtqueue_get_vring);
 
+int virtqueue_wait_for_used(struct virtqueue *_vq)
+{
+	struct vring_virtqueue *vq = to_vvq(_vq);
+
+	/* TODO: Tweak the timeout. */
+	return wait_event_interruptible_timeout(vq->wq,
+	       virtqueue_is_broken(_vq) || more_used(vq), HZ);

BTW undocumented that you also make it interruptible. So if we get an interrupt then this will fail. But device is still going and will later use the buffers.

Same for timeout really.

Quoted 28 lines
drivers/virtio/virtio_ring.c +11 -0
+}
+EXPORT_SYMBOL_GPL(virtqueue_wait_for_used);
+
+void virtqueue_wake_up(struct virtqueue *_vq)
+{
+	struct vring_virtqueue *vq = to_vvq(_vq);
+
+	wake_up_interruptible(&vq->wq);
+}
+EXPORT_SYMBOL_GPL(virtqueue_wake_up);
+
 MODULE_LICENSE("GPL");
include/linux/virtio.h +3 -0
index dcab9c7e8784..2eb62c774895 100644
@@ -72,6 +72,9 @@ void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len);
 void *virtqueue_get_buf_ctx(struct virtqueue *vq, unsigned int *len,
 			    void **ctx);
 
+int virtqueue_wait_for_used(struct virtqueue *vq);
+void virtqueue_wake_up(struct virtqueue *vq);
+
 void virtqueue_disable_cb(struct virtqueue *vq);
 
 bool virtqueue_enable_cb(struct virtqueue *vq);
Signature · 1 line
2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#16 lore raw

Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue

Quoted 78 lines · Michael S. Tsirkin
Quoted 76 lines · Jason Wang

This patch introduces a per virtqueue waitqueue to allow driver to sleep and wait for more used. Two new helpers are introduced to allow driver to sleep and wake up.

---
Changes since V1:
- check virtqueue_is_broken() as well
- use more_used() instead of virtqueue_get_buf() to allow caller to get buffers afterwards

---
 drivers/virtio/virtio_ring.c | 29 +++++++++++++++++++++++++++++
 include/linux/virtio.h       |  3 +++
 2 files changed, 32 insertions(+)
drivers/virtio/virtio_ring.c +11 -0
index 5cfb2fa8abee..9c83eb945493 100644
@@ -13,6 +13,7 @@
 #include <linux/dma-mapping.h>
 #include <linux/kmsan.h>
 #include <linux/spinlock.h>
+#include <linux/wait.h>
 #include <xen/xen.h>
 
 #ifdef DEBUG
@@ -60,6 +61,7 @@
                      "%s:"fmt, (_vq)->vq.name, ##args);      \
              /* Pairs with READ_ONCE() in virtqueue_is_broken(). */ \
              WRITE_ONCE((_vq)->broken, true);                       \
+             wake_up_interruptible(&(_vq)->wq);                     \
      } while (0)
 #define START_USE(vq)
 #define END_USE(vq)
@@ -203,6 +205,9 @@ struct vring_virtqueue {
      /* DMA, allocation, and size information */
      bool we_own_ring;
 
+     /* Wait for buffer to be used */
+     wait_queue_head_t wq;
+
 #ifdef DEBUG
      /* They're supposed to lock for us. */
      unsigned int in_use;
@@ -2024,6 +2029,8 @@ static struct virtqueue *vring_create_virtqueue_packed(
      if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
              vq->weak_barriers = false;
 
+     init_waitqueue_head(&vq->wq);
+
      err = vring_alloc_state_extra_packed(&vring_packed);
      if (err)
              goto err_state_extra;
@@ -2517,6 +2524,8 @@ static struct virtqueue *__vring_new_virtqueue(unsigned int index,
      if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
              vq->weak_barriers = false;
 
+     init_waitqueue_head(&vq->wq);
+
      err = vring_alloc_state_extra_split(vring_split);
      if (err) {
              kfree(vq);
@@ -2654,6 +2663,8 @@ static void vring_free(struct virtqueue *_vq)
 {
      struct vring_virtqueue *vq = to_vvq(_vq);
 
+     wake_up_interruptible(&vq->wq);
+
      if (vq->we_own_ring) {
              if (vq->packed_ring) {
                      vring_free_queue(vq->vq.vdev,
@@ -2863,4 +2874,22 @@ const struct vring *virtqueue_get_vring(struct virtqueue *vq)
 }
 EXPORT_SYMBOL_GPL(virtqueue_get_vring);
 
Quoted 10 lines

drivers/virtio/virtio_ring.c const struct vring *virtqueue_get_vring(struct virtqueue *vq) · line 2883

Quoted 7 lines
drivers/virtio/virtio_ring.c +7 -0
+int virtqueue_wait_for_used(struct virtqueue *_vq)
+{
+     struct vring_virtqueue *vq = to_vvq(_vq);
+
+     /* TODO: Tweak the timeout. */
+     return wait_event_interruptible_timeout(vq->wq,
+            virtqueue_is_broken(_vq) || more_used(vq), HZ);

BTW undocumented that you also make it interruptible. So if we get an interrupt then this will fail.

Yes, this is sub-optimal.

Quoted 3 lines

But device is still going and will later use the buffers.

Same for timeout really.

Avoiding infinite wait/poll is one of the goals, another is to sleep. If we think the timeout is hard, we can start from the wait.

Thanks

Quoted 23 lines
Quoted 23 lines
drivers/virtio/virtio_ring.c +11 -0
+}
+EXPORT_SYMBOL_GPL(virtqueue_wait_for_used);
+
+void virtqueue_wake_up(struct virtqueue *_vq)
+{
+     struct vring_virtqueue *vq = to_vvq(_vq);
+
+     wake_up_interruptible(&vq->wq);
+}
+EXPORT_SYMBOL_GPL(virtqueue_wake_up);
+
 MODULE_LICENSE("GPL");
include/linux/virtio.h +0 -0
index dcab9c7e8784..2eb62c774895 100644
@@ -72,6 +72,9 @@ void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len);
 void *virtqueue_get_buf_ctx(struct virtqueue *vq, unsigned int *len,
                          void **ctx);
 
Quoted 9 lines
Quoted 8 lines
include/linux/virtio.h +3 -1
+int virtqueue_wait_for_used(struct virtqueue *vq);
+void virtqueue_wake_up(struct virtqueue *vq);
+
 void virtqueue_disable_cb(struct virtqueue *vq);
 
 bool virtqueue_enable_cb(struct virtqueue *vq);
--

2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#17 lore raw

Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue

Quoted 8 lines · Jason Wang
Quoted 3 lines

But device is still going and will later use the buffers.

Same for timeout really.

Avoiding infinite wait/poll is one of the goals, another is to sleep. If we think the timeout is hard, we can start from the wait.

Thanks

If the goal is to avoid disrupting traffic while CVQ is in use, that sounds more reasonable. E.g. someone is turning on promisc, a spike in CPU usage might be unwelcome.

things we should be careful to address then:
1- debugging. Currently it's easy to see a warning if CPU is stuck
in a loop for a while, and we also get a backtrace. E.g. with this - how do we know who has the RTNL?
We need to integrate with kernel/watchdog.c for good results and to make sure policy is consistent.
2- overhead. In a very common scenario when device is in hypervisor,
programming timers etc has a very high overhead, at bootup lots of CVQ commands are run and slowing boot down is not nice. let's poll for a bit before waiting?
3- suprise removal. need to wake up thread in some way. what about
other cases of device breakage - is there a chance this introduces new bugs around that? at least enumerate them please.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
author
#18 lore raw

Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue

Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang
Quoted 3 lines

But device is still going and will later use the buffers.

Same for timeout really.

Avoiding infinite wait/poll is one of the goals, another is to sleep. If we think the timeout is hard, we can start from the wait.

Thanks

If the goal is to avoid disrupting traffic while CVQ is in use, that sounds more reasonable. E.g. someone is turning on promisc, a spike in CPU usage might be unwelcome.

Yes, this would be more obvious is UP is used.

Quoted 7 lines

things we should be careful to address then:
1- debugging. Currently it's easy to see a warning if CPU is stuck
in a loop for a while, and we also get a backtrace. E.g. with this - how do we know who has the RTNL? We need to integrate with kernel/watchdog.c for good results and to make sure policy is consistent.

That's fine, will consider this.

Quoted 4 lines

2- overhead. In a very common scenario when device is in hypervisor,
programming timers etc has a very high overhead, at bootup lots of CVQ commands are run and slowing boot down is not nice. let's poll for a bit before waiting?

Then we go back to the question of choosing a good timeout for poll. And poll seems problematic in the case of UP, scheduler might not have the chance to run.

Quoted 3 lines

3- suprise removal. need to wake up thread in some way. what about
other cases of device breakage - is there a chance this introduces new bugs around that? at least enumerate them please.

The current code did:

1) check for vq->broken
2) wakeup during BAD_RING()

So we won't end up with a never woke up process which should be fine.

Thanks

Quoted 2 lines

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#19 lore raw

Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue

Quoted 36 lines · Jason Wang
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang
Quoted 3 lines

But device is still going and will later use the buffers.

Same for timeout really.

Avoiding infinite wait/poll is one of the goals, another is to sleep. If we think the timeout is hard, we can start from the wait.

Thanks

If the goal is to avoid disrupting traffic while CVQ is in use, that sounds more reasonable. E.g. someone is turning on promisc, a spike in CPU usage might be unwelcome.

Yes, this would be more obvious is UP is used.

Quoted 7 lines

things we should be careful to address then:
1- debugging. Currently it's easy to see a warning if CPU is stuck
in a loop for a while, and we also get a backtrace. E.g. with this - how do we know who has the RTNL? We need to integrate with kernel/watchdog.c for good results and to make sure policy is consistent.

That's fine, will consider this.

Quoted 4 lines

2- overhead. In a very common scenario when device is in hypervisor,
programming timers etc has a very high overhead, at bootup lots of CVQ commands are run and slowing boot down is not nice. let's poll for a bit before waiting?

Quoted 3 lines

Then we go back to the question of choosing a good timeout for poll. And poll seems problematic in the case of UP, scheduler might not have the chance to run.

Poll just a bit :) Seriously I don't know, but at least check once after kick.

Quoted 6 lines
Quoted 4 lines

3- suprise removal. need to wake up thread in some way. what about
other cases of device breakage - is there a chance this introduces new bugs around that? at least enumerate them please.

Quoted 8 lines

The current code did:

1) check for vq->broken
2) wakeup during BAD_RING()

So we won't end up with a never woke up process which should be fine.

Thanks

BTW BAD_RING on removal will trigger dev_err. Not sure that is a good idea - can cause crashes if kernel panics on error.

Quoted 3 lines
Quoted 3 lines

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#20 lore raw

Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue

Quoted 32 lines · Michael S. Tsirkin
Quoted 31 lines · Jason Wang
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang
Quoted 3 lines

But device is still going and will later use the buffers.

Same for timeout really.

Avoiding infinite wait/poll is one of the goals, another is to sleep. If we think the timeout is hard, we can start from the wait.

Thanks

If the goal is to avoid disrupting traffic while CVQ is in use, that sounds more reasonable. E.g. someone is turning on promisc, a spike in CPU usage might be unwelcome.

Yes, this would be more obvious is UP is used.

Quoted 6 lines

things we should be careful to address then:
1- debugging. Currently it's easy to see a warning if CPU is stuck
in a loop for a while, and we also get a backtrace. E.g. with this - how do we know who has the RTNL? We need to integrate with kernel/watchdog.c for good results and to make sure policy is consistent.

That's fine, will consider this.

Quoted 4 lines

2- overhead. In a very common scenario when device is in hypervisor,
programming timers etc has a very high overhead, at bootup lots of CVQ commands are run and slowing boot down is not nice. let's poll for a bit before waiting?

Quoted 5 lines
Quoted 3 lines

Then we go back to the question of choosing a good timeout for poll. And poll seems problematic in the case of UP, scheduler might not have the chance to run.

Poll just a bit :) Seriously I don't know, but at least check once after kick.

I think it is what the current code did where the condition will be check before trying to sleep in the wait_event().

Quoted 10 lines
Quoted 10 lines
Quoted 4 lines

3- suprise removal. need to wake up thread in some way. what about
other cases of device breakage - is there a chance this introduces new bugs around that? at least enumerate them please.

The current code did:

1) check for vq->broken
2) wakeup during BAD_RING()

Quoted 6 lines
Quoted 3 lines

So we won't end up with a never woke up process which should be fine.

Thanks

BTW BAD_RING on removal will trigger dev_err. Not sure that is a good idea - can cause crashes if kernel panics on error.

Yes, it's better to use __virtqueue_break() instead.

But consider we will start from a wait first, I will limit the changes in virtio-net without bothering virtio core.

Thanks

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#21 lore raw

Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue

Quoted 20 lines · Jason Wang
Quoted 17 lines · Michael S. Tsirkin
Quoted 16 lines · Jason Wang
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang
Quoted 3 lines

But device is still going and will later use the buffers.

Same for timeout really.

Avoiding infinite wait/poll is one of the goals, another is to sleep. If we think the timeout is hard, we can start from the wait.

Thanks

If the goal is to avoid disrupting traffic while CVQ is in use, that sounds more reasonable. E.g. someone is turning on promisc, a spike in CPU usage might be unwelcome.

Yes, this would be more obvious is UP is used.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

things we should be careful to address then:
1- debugging. Currently it's easy to see a warning if CPU is stuck
in a loop for a while, and we also get a backtrace. E.g. with this - how do we know who has the RTNL? We need to integrate with kernel/watchdog.c for good results and to make sure policy is consistent.

That's fine, will consider this.

So after some investigation, it seems the watchdog.c doesn't help. The only export helper is touch_softlockup_watchdog() which tries to avoid triggering the lockups warning for the known slow path.

And before the patch, we end up with a real infinite loop which could be caught by RCU stall detector which is not the case of the sleep. What we can do is probably do a periodic netdev_err().

Thanks

Quoted 36 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

2- overhead. In a very common scenario when device is in hypervisor,
programming timers etc has a very high overhead, at bootup lots of CVQ commands are run and slowing boot down is not nice. let's poll for a bit before waiting?

Quoted 5 lines
Quoted 3 lines

Then we go back to the question of choosing a good timeout for poll. And poll seems problematic in the case of UP, scheduler might not have the chance to run.

Poll just a bit :) Seriously I don't know, but at least check once after kick.

I think it is what the current code did where the condition will be check before trying to sleep in the wait_event().

Quoted 10 lines
Quoted 10 lines
Quoted 4 lines

3- suprise removal. need to wake up thread in some way. what about
other cases of device breakage - is there a chance this introduces new bugs around that? at least enumerate them please.

The current code did:

1) check for vq->broken
2) wakeup during BAD_RING()

Quoted 6 lines
Quoted 3 lines

So we won't end up with a never woke up process which should be fine.

Thanks

BTW BAD_RING on removal will trigger dev_err. Not sure that is a good idea - can cause crashes if kernel panics on error.

Quoted 10 lines

Yes, it's better to use __virtqueue_break() instead.

But consider we will start from a wait first, I will limit the changes in virtio-net without bothering virtio core.

Thanks

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#22 lore raw

Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue

Quoted 28 lines · Jason Wang
Quoted 27 lines · Jason Wang
Quoted 24 lines · Michael S. Tsirkin
Quoted 23 lines · Jason Wang
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang
Quoted 3 lines

But device is still going and will later use the buffers.

Same for timeout really.

Avoiding infinite wait/poll is one of the goals, another is to sleep. If we think the timeout is hard, we can start from the wait.

Thanks

If the goal is to avoid disrupting traffic while CVQ is in use, that sounds more reasonable. E.g. someone is turning on promisc, a spike in CPU usage might be unwelcome.

Yes, this would be more obvious is UP is used.

Quoted 6 lines

things we should be careful to address then:
1- debugging. Currently it's easy to see a warning if CPU is stuck
in a loop for a while, and we also get a backtrace. E.g. with this - how do we know who has the RTNL? We need to integrate with kernel/watchdog.c for good results and to make sure policy is consistent.

Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

That's fine, will consider this.

So after some investigation, it seems the watchdog.c doesn't help. The only export helper is touch_softlockup_watchdog() which tries to avoid triggering the lockups warning for the known slow path.

I never said you can just use existing exporting APIs. You'll have to write new ones :)

Quoted 5 lines

And before the patch, we end up with a real infinite loop which could be caught by RCU stall detector which is not the case of the sleep. What we can do is probably do a periodic netdev_err().

Thanks

Only with a bad device.

Quoted 38 lines
Quoted 38 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

2- overhead. In a very common scenario when device is in hypervisor,
programming timers etc has a very high overhead, at bootup lots of CVQ commands are run and slowing boot down is not nice. let's poll for a bit before waiting?

Quoted 5 lines
Quoted 3 lines

Then we go back to the question of choosing a good timeout for poll. And poll seems problematic in the case of UP, scheduler might not have the chance to run.

Poll just a bit :) Seriously I don't know, but at least check once after kick.

I think it is what the current code did where the condition will be check before trying to sleep in the wait_event().

Quoted 10 lines
Quoted 10 lines
Quoted 4 lines

3- suprise removal. need to wake up thread in some way. what about
other cases of device breakage - is there a chance this introduces new bugs around that? at least enumerate them please.

The current code did:

1) check for vq->broken
2) wakeup during BAD_RING()

Quoted 6 lines
Quoted 3 lines

So we won't end up with a never woke up process which should be fine.

Thanks

BTW BAD_RING on removal will trigger dev_err. Not sure that is a good idea - can cause crashes if kernel panics on error.

Yes, it's better to use __virtqueue_break() instead.

Quoted 8 lines
Quoted 8 lines

But consider we will start from a wait first, I will limit the changes in virtio-net without bothering virtio core.

Thanks

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#23 lore raw

Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue

Quoted 30 lines · Michael S. Tsirkin
Quoted 28 lines · Jason Wang
Quoted 27 lines · Jason Wang
Quoted 24 lines · Michael S. Tsirkin
Quoted 23 lines · Jason Wang
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang
Quoted 3 lines

But device is still going and will later use the buffers.

Same for timeout really.

Avoiding infinite wait/poll is one of the goals, another is to sleep. If we think the timeout is hard, we can start from the wait.

Thanks

If the goal is to avoid disrupting traffic while CVQ is in use, that sounds more reasonable. E.g. someone is turning on promisc, a spike in CPU usage might be unwelcome.

Yes, this would be more obvious is UP is used.

Quoted 6 lines

things we should be careful to address then:
1- debugging. Currently it's easy to see a warning if CPU is stuck
in a loop for a while, and we also get a backtrace. E.g. with this - how do we know who has the RTNL? We need to integrate with kernel/watchdog.c for good results and to make sure policy is consistent.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

That's fine, will consider this.

So after some investigation, it seems the watchdog.c doesn't help. The only export helper is touch_softlockup_watchdog() which tries to avoid triggering the lockups warning for the known slow path.

I never said you can just use existing exporting APIs. You'll have to write new ones :)

Ok, I thought you wanted to trigger similar warnings as a watchdog.

Btw, I wonder what kind of logic you want here. If we switch to using sleep, there won't be soft lockup anymore. A simple wait + timeout + warning seems sufficient?

Thanks

Quoted 56 lines
Quoted 6 lines

And before the patch, we end up with a real infinite loop which could be caught by RCU stall detector which is not the case of the sleep. What we can do is probably do a periodic netdev_err().

Thanks

Only with a bad device.

Quoted 46 lines
Quoted 46 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

2- overhead. In a very common scenario when device is in hypervisor,
programming timers etc has a very high overhead, at bootup lots of CVQ commands are run and slowing boot down is not nice. let's poll for a bit before waiting?

Quoted 5 lines
Quoted 3 lines

Then we go back to the question of choosing a good timeout for poll. And poll seems problematic in the case of UP, scheduler might not have the chance to run.

Poll just a bit :) Seriously I don't know, but at least check once after kick.

I think it is what the current code did where the condition will be check before trying to sleep in the wait_event().

Quoted 10 lines
Quoted 10 lines
Quoted 4 lines

3- suprise removal. need to wake up thread in some way. what about
other cases of device breakage - is there a chance this introduces new bugs around that? at least enumerate them please.

The current code did:

1) check for vq->broken
2) wakeup during BAD_RING()

Quoted 6 lines
Quoted 3 lines

So we won't end up with a never woke up process which should be fine.

Thanks

BTW BAD_RING on removal will trigger dev_err. Not sure that is a good idea - can cause crashes if kernel panics on error.

Yes, it's better to use __virtqueue_break() instead.

But consider we will start from a wait first, I will limit the changes in virtio-net without bothering virtio core.

Thanks

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#24 lore raw

Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue

Quoted 40 lines · Jason Wang
Quoted 30 lines · Michael S. Tsirkin
Quoted 28 lines · Jason Wang
Quoted 27 lines · Jason Wang
Quoted 24 lines · Michael S. Tsirkin
Quoted 23 lines · Jason Wang
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang
Quoted 3 lines

But device is still going and will later use the buffers.

Same for timeout really.

Avoiding infinite wait/poll is one of the goals, another is to sleep. If we think the timeout is hard, we can start from the wait.

Thanks

If the goal is to avoid disrupting traffic while CVQ is in use, that sounds more reasonable. E.g. someone is turning on promisc, a spike in CPU usage might be unwelcome.

Yes, this would be more obvious is UP is used.

Quoted 6 lines

things we should be careful to address then:
1- debugging. Currently it's easy to see a warning if CPU is stuck
in a loop for a while, and we also get a backtrace. E.g. with this - how do we know who has the RTNL? We need to integrate with kernel/watchdog.c for good results and to make sure policy is consistent.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

That's fine, will consider this.

So after some investigation, it seems the watchdog.c doesn't help. The only export helper is touch_softlockup_watchdog() which tries to avoid triggering the lockups warning for the known slow path.

I never said you can just use existing exporting APIs. You'll have to write new ones :)

Quoted 7 lines

Ok, I thought you wanted to trigger similar warnings as a watchdog.

Btw, I wonder what kind of logic you want here. If we switch to using sleep, there won't be soft lockup anymore. A simple wait + timeout + warning seems sufficient?

Thanks

I'd like to avoid need to teach users new APIs. So watchdog setup to apply to this driver. The warning can be different.

Quoted 50 lines
Quoted 50 lines
Quoted 6 lines

And before the patch, we end up with a real infinite loop which could be caught by RCU stall detector which is not the case of the sleep. What we can do is probably do a periodic netdev_err().

Thanks

Only with a bad device.

Quoted 41 lines
Quoted 41 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

2- overhead. In a very common scenario when device is in hypervisor,
programming timers etc has a very high overhead, at bootup lots of CVQ commands are run and slowing boot down is not nice. let's poll for a bit before waiting?

Quoted 5 lines
Quoted 3 lines

Then we go back to the question of choosing a good timeout for poll. And poll seems problematic in the case of UP, scheduler might not have the chance to run.

Poll just a bit :) Seriously I don't know, but at least check once after kick.

I think it is what the current code did where the condition will be check before trying to sleep in the wait_event().

Quoted 10 lines
Quoted 10 lines
Quoted 4 lines

3- suprise removal. need to wake up thread in some way. what about
other cases of device breakage - is there a chance this introduces new bugs around that? at least enumerate them please.

The current code did:

1) check for vq->broken
2) wakeup during BAD_RING()

Quoted 6 lines
Quoted 3 lines

So we won't end up with a never woke up process which should be fine.

Thanks

BTW BAD_RING on removal will trigger dev_err. Not sure that is a good idea - can cause crashes if kernel panics on error.

Yes, it's better to use __virtqueue_break() instead.

But consider we will start from a wait first, I will limit the changes in virtio-net without bothering virtio core.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

Thanks

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#25 lore raw

Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue

Quoted 44 lines · Michael S. Tsirkin
Quoted 42 lines · Jason Wang
Quoted 30 lines · Michael S. Tsirkin
Quoted 28 lines · Jason Wang
Quoted 27 lines · Jason Wang
Quoted 24 lines · Michael S. Tsirkin
Quoted 23 lines · Jason Wang
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang
Quoted 3 lines

But device is still going and will later use the buffers.

Same for timeout really.

Avoiding infinite wait/poll is one of the goals, another is to sleep. If we think the timeout is hard, we can start from the wait.

Thanks

If the goal is to avoid disrupting traffic while CVQ is in use, that sounds more reasonable. E.g. someone is turning on promisc, a spike in CPU usage might be unwelcome.

Yes, this would be more obvious is UP is used.

Quoted 6 lines

things we should be careful to address then:
1- debugging. Currently it's easy to see a warning if CPU is stuck
in a loop for a while, and we also get a backtrace. E.g. with this - how do we know who has the RTNL? We need to integrate with kernel/watchdog.c for good results and to make sure policy is consistent.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

That's fine, will consider this.

So after some investigation, it seems the watchdog.c doesn't help. The only export helper is touch_softlockup_watchdog() which tries to avoid triggering the lockups warning for the known slow path.

I never said you can just use existing exporting APIs. You'll have to write new ones :)

Ok, I thought you wanted to trigger similar warnings as a watchdog.

Quoted 8 lines
Quoted 5 lines

Btw, I wonder what kind of logic you want here. If we switch to using sleep, there won't be soft lockup anymore. A simple wait + timeout + warning seems sufficient?

Thanks

I'd like to avoid need to teach users new APIs. So watchdog setup to apply to this driver. The warning can be different.

Right, so it looks to me the only possible setup is the watchdog_thres. I plan to trigger the warning every watchdog_thres * 2 second (as softlockup did).

And I think it would still make sense to fail, we can start with a very long timeout like 1 minutes and break the device. Does this make sense?

Thanks

Quoted 52 lines
Quoted 52 lines
Quoted 52 lines
Quoted 8 lines

And before the patch, we end up with a real infinite loop which could be caught by RCU stall detector which is not the case of the sleep. What we can do is probably do a periodic netdev_err().

Thanks

Only with a bad device.

Quoted 41 lines
Quoted 41 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

2- overhead. In a very common scenario when device is in hypervisor,
programming timers etc has a very high overhead, at bootup lots of CVQ commands are run and slowing boot down is not nice. let's poll for a bit before waiting?

Quoted 5 lines
Quoted 3 lines

Then we go back to the question of choosing a good timeout for poll. And poll seems problematic in the case of UP, scheduler might not have the chance to run.

Poll just a bit :) Seriously I don't know, but at least check once after kick.

I think it is what the current code did where the condition will be check before trying to sleep in the wait_event().

Quoted 10 lines
Quoted 10 lines
Quoted 4 lines

3- suprise removal. need to wake up thread in some way. what about
other cases of device breakage - is there a chance this introduces new bugs around that? at least enumerate them please.

The current code did:

1) check for vq->broken
2) wakeup during BAD_RING()

Quoted 6 lines
Quoted 3 lines

So we won't end up with a never woke up process which should be fine.

Thanks

BTW BAD_RING on removal will trigger dev_err. Not sure that is a good idea - can cause crashes if kernel panics on error.

Yes, it's better to use __virtqueue_break() instead.

But consider we will start from a wait first, I will limit the changes in virtio-net without bothering virtio core.

Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

Thanks

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#26 lore raw

Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue

Quoted 58 lines · Jason Wang
Quoted 44 lines · Michael S. Tsirkin
Quoted 42 lines · Jason Wang
Quoted 30 lines · Michael S. Tsirkin
Quoted 28 lines · Jason Wang
Quoted 27 lines · Jason Wang
Quoted 24 lines · Michael S. Tsirkin
Quoted 23 lines · Jason Wang
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang
Quoted 3 lines

But device is still going and will later use the buffers.

Same for timeout really.

Avoiding infinite wait/poll is one of the goals, another is to sleep. If we think the timeout is hard, we can start from the wait.

Thanks

If the goal is to avoid disrupting traffic while CVQ is in use, that sounds more reasonable. E.g. someone is turning on promisc, a spike in CPU usage might be unwelcome.

Yes, this would be more obvious is UP is used.

Quoted 6 lines

things we should be careful to address then:
1- debugging. Currently it's easy to see a warning if CPU is stuck
in a loop for a while, and we also get a backtrace. E.g. with this - how do we know who has the RTNL? We need to integrate with kernel/watchdog.c for good results and to make sure policy is consistent.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

That's fine, will consider this.

So after some investigation, it seems the watchdog.c doesn't help. The only export helper is touch_softlockup_watchdog() which tries to avoid triggering the lockups warning for the known slow path.

I never said you can just use existing exporting APIs. You'll have to write new ones :)

Ok, I thought you wanted to trigger similar warnings as a watchdog.

Quoted 8 lines
Quoted 5 lines

Btw, I wonder what kind of logic you want here. If we switch to using sleep, there won't be soft lockup anymore. A simple wait + timeout + warning seems sufficient?

Thanks

I'd like to avoid need to teach users new APIs. So watchdog setup to apply to this driver. The warning can be different.

Right, so it looks to me the only possible setup is the watchdog_thres. I plan to trigger the warning every watchdog_thres * 2 second (as softlockup did).

Quoted 5 lines

And I think it would still make sense to fail, we can start with a very long timeout like 1 minutes and break the device. Does this make sense?

Thanks

I'd say we need to make this manageable then. Can't we do it normally e.g. react to an interrupt to return to userspace?

Quoted 52 lines
Quoted 52 lines
Quoted 52 lines
Quoted 52 lines
Quoted 8 lines

And before the patch, we end up with a real infinite loop which could be caught by RCU stall detector which is not the case of the sleep. What we can do is probably do a periodic netdev_err().

Thanks

Only with a bad device.

Quoted 41 lines
Quoted 41 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

2- overhead. In a very common scenario when device is in hypervisor,
programming timers etc has a very high overhead, at bootup lots of CVQ commands are run and slowing boot down is not nice. let's poll for a bit before waiting?

Quoted 5 lines
Quoted 3 lines

Then we go back to the question of choosing a good timeout for poll. And poll seems problematic in the case of UP, scheduler might not have the chance to run.

Poll just a bit :) Seriously I don't know, but at least check once after kick.

I think it is what the current code did where the condition will be check before trying to sleep in the wait_event().

Quoted 10 lines
Quoted 10 lines
Quoted 4 lines

3- suprise removal. need to wake up thread in some way. what about
other cases of device breakage - is there a chance this introduces new bugs around that? at least enumerate them please.

The current code did:

1) check for vq->broken
2) wakeup during BAD_RING()

Quoted 6 lines
Quoted 3 lines

So we won't end up with a never woke up process which should be fine.

Thanks

BTW BAD_RING on removal will trigger dev_err. Not sure that is a good idea - can cause crashes if kernel panics on error.

Yes, it's better to use __virtqueue_break() instead.

But consider we will start from a wait first, I will limit the changes in virtio-net without bothering virtio core.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

Thanks

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#27 lore raw

Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue

Quoted 60 lines · Michael S. Tsirkin
Quoted 58 lines · Jason Wang
Quoted 44 lines · Michael S. Tsirkin
Quoted 42 lines · Jason Wang
Quoted 30 lines · Michael S. Tsirkin
Quoted 28 lines · Jason Wang
Quoted 27 lines · Jason Wang
Quoted 24 lines · Michael S. Tsirkin
Quoted 23 lines · Jason Wang
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang
Quoted 3 lines

But device is still going and will later use the buffers.

Same for timeout really.

Avoiding infinite wait/poll is one of the goals, another is to sleep. If we think the timeout is hard, we can start from the wait.

Thanks

If the goal is to avoid disrupting traffic while CVQ is in use, that sounds more reasonable. E.g. someone is turning on promisc, a spike in CPU usage might be unwelcome.

Yes, this would be more obvious is UP is used.

Quoted 6 lines

things we should be careful to address then:
1- debugging. Currently it's easy to see a warning if CPU is stuck
in a loop for a while, and we also get a backtrace. E.g. with this - how do we know who has the RTNL? We need to integrate with kernel/watchdog.c for good results and to make sure policy is consistent.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

That's fine, will consider this.

So after some investigation, it seems the watchdog.c doesn't help. The only export helper is touch_softlockup_watchdog() which tries to avoid triggering the lockups warning for the known slow path.

I never said you can just use existing exporting APIs. You'll have to write new ones :)

Ok, I thought you wanted to trigger similar warnings as a watchdog.

Quoted 8 lines
Quoted 5 lines

Btw, I wonder what kind of logic you want here. If we switch to using sleep, there won't be soft lockup anymore. A simple wait + timeout + warning seems sufficient?

Thanks

I'd like to avoid need to teach users new APIs. So watchdog setup to apply to this driver. The warning can be different.

Right, so it looks to me the only possible setup is the watchdog_thres. I plan to trigger the warning every watchdog_thres * 2 second (as softlockup did).

Quoted 7 lines
Quoted 5 lines

And I think it would still make sense to fail, we can start with a very long timeout like 1 minutes and break the device. Does this make sense?

Thanks

I'd say we need to make this manageable then.

Did you mean something like sysfs or module parameters?

Quoted 2 lines

Can't we do it normally
e.g. react to an interrupt to return to userspace?

I didn't get the meaning of this. Sorry.

Thanks

Quoted 55 lines
Quoted 55 lines
Quoted 55 lines
Quoted 55 lines
Quoted 55 lines
Quoted 11 lines

And before the patch, we end up with a real infinite loop which could be caught by RCU stall detector which is not the case of the sleep. What we can do is probably do a periodic netdev_err().

Thanks

Only with a bad device.

Quoted 41 lines
Quoted 41 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

2- overhead. In a very common scenario when device is in hypervisor,
programming timers etc has a very high overhead, at bootup lots of CVQ commands are run and slowing boot down is not nice. let's poll for a bit before waiting?

Quoted 5 lines
Quoted 3 lines

Then we go back to the question of choosing a good timeout for poll. And poll seems problematic in the case of UP, scheduler might not have the chance to run.

Poll just a bit :) Seriously I don't know, but at least check once after kick.

I think it is what the current code did where the condition will be check before trying to sleep in the wait_event().

Quoted 10 lines
Quoted 10 lines
Quoted 4 lines

3- suprise removal. need to wake up thread in some way. what about
other cases of device breakage - is there a chance this introduces new bugs around that? at least enumerate them please.

The current code did:

1) check for vq->broken
2) wakeup during BAD_RING()

Quoted 6 lines
Quoted 3 lines

So we won't end up with a never woke up process which should be fine.

Thanks

BTW BAD_RING on removal will trigger dev_err. Not sure that is a good idea - can cause crashes if kernel panics on error.

Yes, it's better to use __virtqueue_break() instead.

But consider we will start from a wait first, I will limit the changes in virtio-net without bothering virtio core.

Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

Thanks

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#28 lore raw

Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue

Quoted 65 lines · Jason Wang
Quoted 64 lines · Michael S. Tsirkin
Quoted 62 lines · Jason Wang
Quoted 44 lines · Michael S. Tsirkin
Quoted 42 lines · Jason Wang
Quoted 30 lines · Michael S. Tsirkin
Quoted 28 lines · Jason Wang
Quoted 27 lines · Jason Wang
Quoted 24 lines · Michael S. Tsirkin
Quoted 23 lines · Jason Wang
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang
Quoted 3 lines

But device is still going and will later use the buffers.

Same for timeout really.

Avoiding infinite wait/poll is one of the goals, another is to sleep. If we think the timeout is hard, we can start from the wait.

Thanks

If the goal is to avoid disrupting traffic while CVQ is in use, that sounds more reasonable. E.g. someone is turning on promisc, a spike in CPU usage might be unwelcome.

Yes, this would be more obvious is UP is used.

Quoted 6 lines

things we should be careful to address then:
1- debugging. Currently it's easy to see a warning if CPU is stuck
in a loop for a while, and we also get a backtrace. E.g. with this - how do we know who has the RTNL? We need to integrate with kernel/watchdog.c for good results and to make sure policy is consistent.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

That's fine, will consider this.

So after some investigation, it seems the watchdog.c doesn't help. The only export helper is touch_softlockup_watchdog() which tries to avoid triggering the lockups warning for the known slow path.

I never said you can just use existing exporting APIs. You'll have to write new ones :)

Ok, I thought you wanted to trigger similar warnings as a watchdog.

Quoted 8 lines
Quoted 5 lines

Btw, I wonder what kind of logic you want here. If we switch to using sleep, there won't be soft lockup anymore. A simple wait + timeout + warning seems sufficient?

Thanks

I'd like to avoid need to teach users new APIs. So watchdog setup to apply to this driver. The warning can be different.

Right, so it looks to me the only possible setup is the watchdog_thres. I plan to trigger the warning every watchdog_thres * 2 second (as softlockup did).

And I think it would still make sense to fail, we can start with a very long timeout like 1 minutes and break the device. Does this make sense?

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Thanks

I'd say we need to make this manageable then.

Did you mean something like sysfs or module parameters?

No I'd say pass it with an ioctl.

Quoted 6 lines
Quoted 2 lines

Can't we do it normally
e.g. react to an interrupt to return to userspace?

I didn't get the meaning of this. Sorry.

Thanks

Standard way to handle things that can timeout and where userspace did not supply the time is to block until an interrupt then return EINTR. Userspace controls the timeout by using e.g. alarm(2).

Quoted 55 lines
Quoted 55 lines
Quoted 55 lines
Quoted 55 lines
Quoted 55 lines
Quoted 55 lines
Quoted 11 lines

And before the patch, we end up with a real infinite loop which could be caught by RCU stall detector which is not the case of the sleep. What we can do is probably do a periodic netdev_err().

Thanks

Only with a bad device.

Quoted 41 lines
Quoted 41 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

2- overhead. In a very common scenario when device is in hypervisor,
programming timers etc has a very high overhead, at bootup lots of CVQ commands are run and slowing boot down is not nice. let's poll for a bit before waiting?

Quoted 5 lines
Quoted 3 lines

Then we go back to the question of choosing a good timeout for poll. And poll seems problematic in the case of UP, scheduler might not have the chance to run.

Poll just a bit :) Seriously I don't know, but at least check once after kick.

I think it is what the current code did where the condition will be check before trying to sleep in the wait_event().

Quoted 10 lines
Quoted 10 lines
Quoted 4 lines

3- suprise removal. need to wake up thread in some way. what about
other cases of device breakage - is there a chance this introduces new bugs around that? at least enumerate them please.

The current code did:

1) check for vq->broken
2) wakeup during BAD_RING()

Quoted 6 lines
Quoted 3 lines

So we won't end up with a never woke up process which should be fine.

Thanks

BTW BAD_RING on removal will trigger dev_err. Not sure that is a good idea - can cause crashes if kernel panics on error.

Yes, it's better to use __virtqueue_break() instead.

But consider we will start from a wait first, I will limit the changes in virtio-net without bothering virtio core.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

Thanks

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#29 lore raw

Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue

Quoted 78 lines · Michael S. Tsirkin
Quoted 65 lines · Jason Wang
Quoted 64 lines · Michael S. Tsirkin
Quoted 62 lines · Jason Wang
Quoted 44 lines · Michael S. Tsirkin
Quoted 42 lines · Jason Wang
Quoted 30 lines · Michael S. Tsirkin
Quoted 28 lines · Jason Wang
Quoted 27 lines · Jason Wang
Quoted 24 lines · Michael S. Tsirkin
Quoted 23 lines · Jason Wang
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang

But device is still going and will later use the buffers.

Same for timeout really.
Avoiding infinite wait/poll is one of the goals, another is to sleep.
If we think the timeout is hard, we can start from the wait.

Thanks

If the goal is to avoid disrupting traffic while CVQ is in use, that sounds more reasonable. E.g. someone is turning on promisc, a spike in CPU usage might be unwelcome.

Yes, this would be more obvious is UP is used.

Quoted 6 lines

things we should be careful to address then:
1- debugging. Currently it's easy to see a warning if CPU is stuck
in a loop for a while, and we also get a backtrace. E.g. with this - how do we know who has the RTNL? We need to integrate with kernel/watchdog.c for good results and to make sure policy is consistent.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

That's fine, will consider this.

So after some investigation, it seems the watchdog.c doesn't help. The only export helper is touch_softlockup_watchdog() which tries to avoid triggering the lockups warning for the known slow path.

I never said you can just use existing exporting APIs. You'll have to write new ones :)

Ok, I thought you wanted to trigger similar warnings as a watchdog.

Quoted 8 lines
Quoted 5 lines

Btw, I wonder what kind of logic you want here. If we switch to using sleep, there won't be soft lockup anymore. A simple wait + timeout + warning seems sufficient?

Thanks

I'd like to avoid need to teach users new APIs. So watchdog setup to apply to this driver. The warning can be different.

Right, so it looks to me the only possible setup is the watchdog_thres. I plan to trigger the warning every watchdog_thres * 2 second (as softlockup did).

And I think it would still make sense to fail, we can start with a very long timeout like 1 minutes and break the device. Does this make sense?

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Thanks

I'd say we need to make this manageable then.

Did you mean something like sysfs or module parameters?

No I'd say pass it with an ioctl.

Quoted 3 lines
Quoted 2 lines

Can't we do it normally
e.g. react to an interrupt to return to userspace?

Quoted 7 lines
Quoted 3 lines

I didn't get the meaning of this. Sorry.

Thanks

Standard way to handle things that can timeout and where userspace did not supply the time is to block until an interrupt then return EINTR.

Well this seems to be a huge change, ioctl(2) doesn't say it can return EINTR now.

Actually, a driver timeout is used by other drivers when using controlq/adminq (e.g i40e). Starting from a sane value (e.g 1 minutes to avoid false negatives) seems to be a good first step.

Quoted 2 lines

Userspace controls the timeout by
using e.g. alarm(2).

Not used in iproute2 after a git grep.

Thanks

Quoted 57 lines
Quoted 57 lines
Quoted 57 lines
Quoted 57 lines
Quoted 57 lines
Quoted 57 lines
Quoted 57 lines
Quoted 8 lines
Quoted 5 lines

And before the patch, we end up with a real infinite loop which could be caught by RCU stall detector which is not the case of the sleep. What we can do is probably do a periodic netdev_err().

Thanks

Only with a bad device.

Quoted 41 lines
Quoted 41 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

2- overhead. In a very common scenario when device is in hypervisor,
programming timers etc has a very high overhead, at bootup lots of CVQ commands are run and slowing boot down is not nice. let's poll for a bit before waiting?

Quoted 5 lines
Quoted 3 lines

Then we go back to the question of choosing a good timeout for poll. And poll seems problematic in the case of UP, scheduler might not have the chance to run.

Poll just a bit :) Seriously I don't know, but at least check once after kick.

I think it is what the current code did where the condition will be check before trying to sleep in the wait_event().

Quoted 10 lines
Quoted 10 lines
Quoted 4 lines

3- suprise removal. need to wake up thread in some way. what about
other cases of device breakage - is there a chance this introduces new bugs around that? at least enumerate them please.

The current code did:

1) check for vq->broken
2) wakeup during BAD_RING()

Quoted 6 lines
Quoted 3 lines

So we won't end up with a never woke up process which should be fine.

Thanks

BTW BAD_RING on removal will trigger dev_err. Not sure that is a good idea - can cause crashes if kernel panics on error.

Yes, it's better to use __virtqueue_break() instead.

But consider we will start from a wait first, I will limit the changes in virtio-net without bothering virtio core.

Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

Thanks

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#30 lore raw

Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue

Quoted 81 lines · Jason Wang
Quoted 80 lines · Michael S. Tsirkin
Quoted 65 lines · Jason Wang
Quoted 64 lines · Michael S. Tsirkin
Quoted 62 lines · Jason Wang
Quoted 44 lines · Michael S. Tsirkin
Quoted 42 lines · Jason Wang
Quoted 30 lines · Michael S. Tsirkin
Quoted 28 lines · Jason Wang
Quoted 27 lines · Jason Wang
Quoted 24 lines · Michael S. Tsirkin
Quoted 23 lines · Jason Wang
Quoted 11 lines · Michael S. Tsirkin

On Tue, Dec 27, 2022 at 12:30:35PM +0800, Jason Wang wrote:
But device is still going and will later use the buffers.

Same for timeout really.
Avoiding infinite wait/poll is one of the goals, another is to sleep.
If we think the timeout is hard, we can start from the wait.

Thanks
If the goal is to avoid disrupting traffic while CVQ is in use,
that sounds more reasonable. E.g. someone is turning on promisc,
a spike in CPU usage might be unwelcome.

Yes, this would be more obvious is UP is used.

Quoted 6 lines

things we should be careful to address then:
1- debugging. Currently it's easy to see a warning if CPU is stuck
in a loop for a while, and we also get a backtrace.
E.g. with this - how do we know who has the RTNL?
We need to integrate with kernel/watchdog.c for good results
and to make sure policy is consistent.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

That's fine, will consider this.

So after some investigation, it seems the watchdog.c doesn't help. The only export helper is touch_softlockup_watchdog() which tries to avoid triggering the lockups warning for the known slow path.

I never said you can just use existing exporting APIs. You'll have to write new ones :)

Ok, I thought you wanted to trigger similar warnings as a watchdog.

Quoted 8 lines
Quoted 5 lines

Btw, I wonder what kind of logic you want here. If we switch to using sleep, there won't be soft lockup anymore. A simple wait + timeout + warning seems sufficient?

Thanks

I'd like to avoid need to teach users new APIs. So watchdog setup to apply to this driver. The warning can be different.

Right, so it looks to me the only possible setup is the watchdog_thres. I plan to trigger the warning every watchdog_thres * 2 second (as softlockup did).

And I think it would still make sense to fail, we can start with a very long timeout like 1 minutes and break the device. Does this make sense?

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Thanks

I'd say we need to make this manageable then.

Did you mean something like sysfs or module parameters?

No I'd say pass it with an ioctl.

Quoted 5 lines
Quoted 2 lines

Can't we do it normally
e.g. react to an interrupt to return to userspace?

I didn't get the meaning of this. Sorry.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Thanks

Standard way to handle things that can timeout and where userspace did not supply the time is to block until an interrupt then return EINTR.

Well this seems to be a huge change, ioctl(2) doesn't say it can return EINTR now.

the one on fedora 37 does not but it says:
No single standard. Arguments, returns, and semantics of ioctl() vary according to the device driver in question (the call is used as a catch-all for operations that don't cleanly fit the UNIX stream I/O model).

so it depends on the device e.g. for a streams device it does:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/ioctl.html
has EINTR.

Quoted 3 lines

Actually, a driver timeout is used by other drivers when using controlq/adminq (e.g i40e). Starting from a sane value (e.g 1 minutes to avoid false negatives) seems to be a good first step.

Well because it's specific hardware so timeout matches what it can promise. virtio spec does not give guarantees. One issue is with software implementations. At the moment I can set a breakpoint in qemu or vhost user backend and nothing bad happens in just continues.

Quoted 6 lines
Quoted 2 lines

Userspace controls the timeout by
using e.g. alarm(2).

Not used in iproute2 after a git grep.

Thanks

No need for iproute2 to do it user can just do it from shell. Or user can just press CTRL-C.

Quoted 57 lines
Quoted 57 lines
Quoted 57 lines
Quoted 57 lines
Quoted 57 lines
Quoted 57 lines
Quoted 57 lines
Quoted 57 lines
Quoted 8 lines
Quoted 5 lines

And before the patch, we end up with a real infinite loop which could be caught by RCU stall detector which is not the case of the sleep. What we can do is probably do a periodic netdev_err().

Thanks

Only with a bad device.

Quoted 41 lines
Quoted 41 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

2- overhead. In a very common scenario when device is in hypervisor,
programming timers etc has a very high overhead, at bootup
lots of CVQ commands are run and slowing boot down is not nice.
let's poll for a bit before waiting?

Quoted 5 lines
Quoted 3 lines

Then we go back to the question of choosing a good timeout for poll. And poll seems problematic in the case of UP, scheduler might not have the chance to run.

Poll just a bit :) Seriously I don't know, but at least check once after kick.

I think it is what the current code did where the condition will be check before trying to sleep in the wait_event().

Quoted 10 lines
Quoted 10 lines
Quoted 4 lines

3- suprise removal. need to wake up thread in some way. what about
other cases of device breakage - is there a chance this
introduces new bugs around that? at least enumerate them please.

The current code did:

1) check for vq->broken
2) wakeup during BAD_RING()

Quoted 6 lines
Quoted 3 lines

So we won't end up with a never woke up process which should be fine.

Thanks

BTW BAD_RING on removal will trigger dev_err. Not sure that is a good idea - can cause crashes if kernel panics on error.

Yes, it's better to use __virtqueue_break() instead.

But consider we will start from a wait first, I will limit the changes in virtio-net without bothering virtio core.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

Thanks

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#31 lore raw

Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue

Quoted 92 lines · Michael S. Tsirkin
Quoted 81 lines · Jason Wang
Quoted 80 lines · Michael S. Tsirkin
Quoted 65 lines · Jason Wang
Quoted 64 lines · Michael S. Tsirkin
Quoted 62 lines · Jason Wang
Quoted 44 lines · Michael S. Tsirkin
Quoted 42 lines · Jason Wang
Quoted 30 lines · Michael S. Tsirkin
Quoted 28 lines · Jason Wang
Quoted 27 lines · Jason Wang
Quoted 24 lines · Michael S. Tsirkin
Quoted 23 lines · Jason Wang

在 2022/12/27 15:33, Michael S. Tsirkin 写道:
On Tue, Dec 27, 2022 at 12:30:35PM +0800, Jason Wang wrote:
But device is still going and will later use the buffers.

Same for timeout really.
Avoiding infinite wait/poll is one of the goals, another is to sleep.
If we think the timeout is hard, we can start from the wait.

Thanks
If the goal is to avoid disrupting traffic while CVQ is in use,
that sounds more reasonable. E.g. someone is turning on promisc,
a spike in CPU usage might be unwelcome.

Yes, this would be more obvious is UP is used.

things we should be careful to address then:
1- debugging. Currently it's easy to see a warning if CPU is stuck
in a loop for a while, and we also get a backtrace.
E.g. with this - how do we know who has the RTNL?
We need to integrate with kernel/watchdog.c for good results
and to make sure policy is consistent.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

That's fine, will consider this.

So after some investigation, it seems the watchdog.c doesn't help. The only export helper is touch_softlockup_watchdog() which tries to avoid triggering the lockups warning for the known slow path.

I never said you can just use existing exporting APIs. You'll have to write new ones :)

Ok, I thought you wanted to trigger similar warnings as a watchdog.

Quoted 8 lines
Quoted 5 lines

Btw, I wonder what kind of logic you want here. If we switch to using sleep, there won't be soft lockup anymore. A simple wait + timeout + warning seems sufficient?

Thanks

I'd like to avoid need to teach users new APIs. So watchdog setup to apply to this driver. The warning can be different.

Right, so it looks to me the only possible setup is the watchdog_thres. I plan to trigger the warning every watchdog_thres * 2 second (as softlockup did).

And I think it would still make sense to fail, we can start with a very long timeout like 1 minutes and break the device. Does this make sense?

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Thanks

I'd say we need to make this manageable then.

Did you mean something like sysfs or module parameters?

No I'd say pass it with an ioctl.

Quoted 5 lines
Quoted 2 lines

Can't we do it normally
e.g. react to an interrupt to return to userspace?

I didn't get the meaning of this. Sorry.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Thanks

Standard way to handle things that can timeout and where userspace did not supply the time is to block until an interrupt then return EINTR.

Well this seems to be a huge change, ioctl(2) doesn't say it can return EINTR now.

Quoted 7 lines

the one on fedora 37 does not but it says:
No single standard. Arguments, returns, and semantics of ioctl() vary according to the device driver in question (the call is used as a catch-all for operations that don't cleanly fit the UNIX stream I/O model).

so it depends on the device e.g. for a streams device it does:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/ioctl.html
has EINTR.

Ok, I saw signal(7) also mention about EINTR for ioctl(2):

"""
If a blocked call to one of the following interfaces is
interrupted by a signal handler, then the call is automatically restarted after the signal handler re‐
turns if the SA_RESTART flag was used; otherwise the call fails
with the error EINTR:

* read(2), readv(2), write(2), writev(2), and ioctl(2) calls on
"slow" devices. A "slow" device is one where the I/O call may block for an indefinite time, for
example, a terminal, pipe, or socket. If an I/O call on a
slow device has already transferred some data by the time it is interrupted by a signal handler,
then the call will return a success status (normally, the
number of bytes transferred). Note that a (local) disk is not a slow device according to this defi‐
nition; I/O operations on disk devices are not interrupted by signals.
"""

Quoted 11 lines
Quoted 6 lines

Actually, a driver timeout is used by other drivers when using controlq/adminq (e.g i40e). Starting from a sane value (e.g 1 minutes to avoid false negatives) seems to be a good first step.

Well because it's specific hardware so timeout matches what it can promise. virtio spec does not give guarantees. One issue is with software implementations. At the moment I can set a breakpoint in qemu or vhost user backend and nothing bad happens in just continues.

Yes but it should be no difference from using a kgdb to debug i40e drivers.

Quoted 10 lines
Quoted 8 lines
Quoted 4 lines

Userspace controls the timeout by
using e.g. alarm(2).

Not used in iproute2 after a git grep.

Thanks

No need for iproute2 to do it user can just do it from shell. Or user can just press CTRL-C.

Yes, but iproute2 needs to deal with EINTR, that is the challenge part, if we simply return an error, the next cvq command might get confused.

Thanks

Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 9 lines
Quoted 5 lines

And before the patch, we end up with a real infinite loop which could be caught by RCU stall detector which is not the case of the sleep. What we can do is probably do a periodic netdev_err().

Thanks

Only with a bad device.

Quoted 41 lines
Quoted 41 lines
Quoted 7 lines
Quoted 7 lines

2- overhead. In a very common scenario when device is in hypervisor,
programming timers etc has a very high overhead, at bootup
lots of CVQ commands are run and slowing boot down is not nice.
let's poll for a bit before waiting?

Quoted 5 lines
Quoted 3 lines

Then we go back to the question of choosing a good timeout for poll. And
poll seems problematic in the case of UP, scheduler might not have the
chance to run.

Poll just a bit :) Seriously I don't know, but at least check once after kick.

I think it is what the current code did where the condition will be check before trying to sleep in the wait_event().

Quoted 10 lines
Quoted 10 lines

3- suprise removal. need to wake up thread in some way. what about
other cases of device breakage - is there a chance this
introduces new bugs around that? at least enumerate them please.

The current code did:

1) check for vq->broken
2) wakeup during BAD_RING()

Quoted 6 lines
Quoted 3 lines

So we won't end up with a never woke up process which should be fine.

Thanks

BTW BAD_RING on removal will trigger dev_err. Not sure that is a good idea - can cause crashes if kernel panics on error.

Yes, it's better to use __virtqueue_break() instead.

But consider we will start from a wait first, I will limit the changes in virtio-net without bothering virtio core.

Quoted 10 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

Thanks

Quoted 2 lines
Quoted 2 lines

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#32 lore raw

Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue

Quoted 110 lines · Jason Wang
Quoted 92 lines · Michael S. Tsirkin
Quoted 81 lines · Jason Wang
Quoted 80 lines · Michael S. Tsirkin
Quoted 65 lines · Jason Wang
Quoted 64 lines · Michael S. Tsirkin
Quoted 62 lines · Jason Wang
Quoted 44 lines · Michael S. Tsirkin
Quoted 42 lines · Jason Wang
Quoted 30 lines · Michael S. Tsirkin
Quoted 28 lines · Jason Wang
Quoted 27 lines · Jason Wang
Quoted 24 lines · Michael S. Tsirkin

On Tue, Dec 27, 2022 at 05:12:58PM +0800, Jason Wang wrote:
在 2022/12/27 15:33, Michael S. Tsirkin 写道:
On Tue, Dec 27, 2022 at 12:30:35PM +0800, Jason Wang wrote:
But device is still going and will later use the buffers.

Same for timeout really.
Avoiding infinite wait/poll is one of the goals, another is to sleep.
If we think the timeout is hard, we can start from the wait.

Thanks
If the goal is to avoid disrupting traffic while CVQ is in use,
that sounds more reasonable. E.g. someone is turning on promisc,
a spike in CPU usage might be unwelcome.

Yes, this would be more obvious is UP is used.

things we should be careful to address then:
1- debugging. Currently it's easy to see a warning if CPU is stuck
in a loop for a while, and we also get a backtrace.
E.g. with this - how do we know who has the RTNL?
We need to integrate with kernel/watchdog.c for good results
and to make sure policy is consistent.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

That's fine, will consider this.

So after some investigation, it seems the watchdog.c doesn't help. The only export helper is touch_softlockup_watchdog() which tries to avoid triggering the lockups warning for the known slow path.

I never said you can just use existing exporting APIs. You'll have to write new ones :)

Ok, I thought you wanted to trigger similar warnings as a watchdog.

Quoted 8 lines
Quoted 5 lines

Btw, I wonder what kind of logic you want here. If we switch to using sleep, there won't be soft lockup anymore. A simple wait + timeout + warning seems sufficient?

Thanks

I'd like to avoid need to teach users new APIs. So watchdog setup to apply to this driver. The warning can be different.

Right, so it looks to me the only possible setup is the watchdog_thres. I plan to trigger the warning every watchdog_thres * 2 second (as softlockup did).

And I think it would still make sense to fail, we can start with a very long timeout like 1 minutes and break the device. Does this make sense?

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Thanks

I'd say we need to make this manageable then.

Did you mean something like sysfs or module parameters?

No I'd say pass it with an ioctl.

Quoted 5 lines
Quoted 2 lines

Can't we do it normally
e.g. react to an interrupt to return to userspace?

I didn't get the meaning of this. Sorry.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Thanks

Standard way to handle things that can timeout and where userspace did not supply the time is to block until an interrupt then return EINTR.

Well this seems to be a huge change, ioctl(2) doesn't say it can return EINTR now.

Quoted 7 lines

the one on fedora 37 does not but it says:
No single standard. Arguments, returns, and semantics of ioctl() vary according to the device driver in question (the call is used as a catch-all for operations that don't cleanly fit the UNIX stream I/O model).

so it depends on the device e.g. for a streams device it does:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/ioctl.html
has EINTR.

Ok, I saw signal(7) also mention about EINTR for ioctl(2):

"""
If a blocked call to one of the following interfaces is
interrupted by a signal handler, then the call is automatically restarted after the signal handler re‐
turns if the SA_RESTART flag was used; otherwise the call fails
with the error EINTR:

Quoted 11 lines

* read(2), readv(2), write(2), writev(2), and ioctl(2) calls on
"slow" devices. A "slow" device is one where the I/O call may block for an indefinite time, for
example, a terminal, pipe, or socket. If an I/O call on a
slow device has already transferred some data by the time it is interrupted by a signal handler,
then the call will return a success status (normally, the
number of bytes transferred). Note that a (local) disk is not a slow device according to this defi‐
nition; I/O operations on disk devices are not interrupted by signals.
"""

And note that if you interrupt then you don't know whether ioctl changed device state or not generally.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

Actually, a driver timeout is used by other drivers when using controlq/adminq (e.g i40e). Starting from a sane value (e.g 1 minutes to avoid false negatives) seems to be a good first step.

Quoted 6 lines
Quoted 4 lines

Well because it's specific hardware so timeout matches what it can promise. virtio spec does not give guarantees. One issue is with software implementations. At the moment I can set a breakpoint in qemu or vhost user backend and nothing bad happens in just continues.

Yes but it should be no difference from using a kgdb to debug i40e drivers.

Except one of the reasons people prefer programming in userspace is because debugging is so much less painful. Someone using kgdb knows what driver is doing and can work around that.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 4 lines

Userspace controls the timeout by
using e.g. alarm(2).

Not used in iproute2 after a git grep.

Thanks

Quoted 7 lines
Quoted 1 line

No need for iproute2 to do it user can just do it from shell. Or user can just press CTRL-C.

Yes, but iproute2 needs to deal with EINTR, that is the challenge part, if we simply return an error, the next cvq command might get confused.

Thanks

You mean this:
start command
interrupt
start next command

?

next command is confused?
I think if you try a new command until previous
one finished it's ok to just return EBUSY.

Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 9 lines
Quoted 5 lines

And before the patch, we end up with a real infinite loop which could be caught by RCU stall detector which is not the case of the sleep. What we can do is probably do a periodic netdev_err().

Thanks

Only with a bad device.

Quoted 41 lines
Quoted 41 lines
Quoted 12 lines

2- overhead. In a very common scenario when device is in hypervisor,
programming timers etc has a very high overhead, at bootup
lots of CVQ commands are run and slowing boot down is not nice.
let's poll for a bit before waiting?

Then we go back to the question of choosing a good timeout for poll. And
poll seems problematic in the case of UP, scheduler might not have the
chance to run.
Poll just a bit :) Seriously I don't know, but at least check once
after kick.

I think it is what the current code did where the condition will be check before trying to sleep in the wait_event().

Quoted 16 lines

3- suprise removal. need to wake up thread in some way. what about
other cases of device breakage - is there a chance this
introduces new bugs around that? at least enumerate them please.

The current code did:

1) check for vq->broken
2) wakeup during BAD_RING()

So we won't end up with a never woke up process which should be fine.

Thanks

BTW BAD_RING on removal will trigger dev_err. Not sure that is a good
idea - can cause crashes if kernel panics on error.

Yes, it's better to use __virtqueue_break() instead.

But consider we will start from a wait first, I will limit the changes in virtio-net without bothering virtio core.

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

Thanks

Quoted 2 lines

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#33 lore raw

Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue

Quoted 125 lines · Michael S. Tsirkin
Quoted 110 lines · Jason Wang
Quoted 92 lines · Michael S. Tsirkin
Quoted 81 lines · Jason Wang
Quoted 80 lines · Michael S. Tsirkin
Quoted 65 lines · Jason Wang
Quoted 64 lines · Michael S. Tsirkin
Quoted 62 lines · Jason Wang
Quoted 44 lines · Michael S. Tsirkin
Quoted 42 lines · Jason Wang
Quoted 30 lines · Michael S. Tsirkin
Quoted 28 lines · Jason Wang
Quoted 27 lines · Jason Wang

在 2022/12/27 17:38, Michael S. Tsirkin 写道:
On Tue, Dec 27, 2022 at 05:12:58PM +0800, Jason Wang wrote:
在 2022/12/27 15:33, Michael S. Tsirkin 写道:
On Tue, Dec 27, 2022 at 12:30:35PM +0800, Jason Wang wrote:
But device is still going and will later use the buffers.

Same for timeout really.
Avoiding infinite wait/poll is one of the goals, another is to sleep.
If we think the timeout is hard, we can start from the wait.

Thanks
If the goal is to avoid disrupting traffic while CVQ is in use,
that sounds more reasonable. E.g. someone is turning on promisc,
a spike in CPU usage might be unwelcome.

Yes, this would be more obvious is UP is used.

things we should be careful to address then:
1- debugging. Currently it's easy to see a warning if CPU is stuck
in a loop for a while, and we also get a backtrace.
E.g. with this - how do we know who has the RTNL?
We need to integrate with kernel/watchdog.c for good results
and to make sure policy is consistent.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

That's fine, will consider this.

So after some investigation, it seems the watchdog.c doesn't help. The only export helper is touch_softlockup_watchdog() which tries to avoid triggering the lockups warning for the known slow path.

I never said you can just use existing exporting APIs. You'll have to write new ones :)

Ok, I thought you wanted to trigger similar warnings as a watchdog.

Quoted 8 lines
Quoted 5 lines

Btw, I wonder what kind of logic you want here. If we switch to using sleep, there won't be soft lockup anymore. A simple wait + timeout + warning seems sufficient?

Thanks

I'd like to avoid need to teach users new APIs. So watchdog setup to apply to this driver. The warning can be different.

Right, so it looks to me the only possible setup is the watchdog_thres. I plan to trigger the warning every watchdog_thres * 2 second (as softlockup did).

And I think it would still make sense to fail, we can start with a very long timeout like 1 minutes and break the device. Does this make sense?

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Thanks

I'd say we need to make this manageable then.

Did you mean something like sysfs or module parameters?

No I'd say pass it with an ioctl.

Quoted 5 lines
Quoted 2 lines

Can't we do it normally
e.g. react to an interrupt to return to userspace?

I didn't get the meaning of this. Sorry.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Thanks

Standard way to handle things that can timeout and where userspace did not supply the time is to block until an interrupt then return EINTR.

Well this seems to be a huge change, ioctl(2) doesn't say it can return EINTR now.

Quoted 7 lines

the one on fedora 37 does not but it says:
No single standard. Arguments, returns, and semantics of ioctl() vary according to the device driver in question (the call is used as a catch-all for operations that don't cleanly fit the UNIX stream I/O model).

so it depends on the device e.g. for a streams device it does:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/ioctl.html
has EINTR.

Ok, I saw signal(7) also mention about EINTR for ioctl(2):

"""
If a blocked call to one of the following interfaces is
interrupted by a signal handler, then the call is automatically restarted after the signal handler re‐
turns if the SA_RESTART flag was used; otherwise the call fails
with the error EINTR:

Quoted 11 lines

* read(2), readv(2), write(2), writev(2), and ioctl(2) calls on
"slow" devices. A "slow" device is one where the I/O call may block for an indefinite time, for
example, a terminal, pipe, or socket. If an I/O call on a
slow device has already transferred some data by the time it is interrupted by a signal handler,
then the call will return a success status (normally, the
number of bytes transferred). Note that a (local) disk is not a slow device according to this defi‐
nition; I/O operations on disk devices are not interrupted by signals.
"""

Quoted 2 lines

And note that if you interrupt then you don't know whether ioctl changed device state or not generally.

Yes.

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
Quoted 6 lines

Actually, a driver timeout is used by other drivers when using controlq/adminq (e.g i40e). Starting from a sane value (e.g 1 minutes to avoid false negatives) seems to be a good first step.

Well because it's specific hardware so timeout matches what it can promise. virtio spec does not give guarantees. One issue is with software implementations. At the moment I can set a breakpoint in qemu or vhost user backend and nothing bad happens in just continues.

Quoted 5 lines
Quoted 1 line

Yes but it should be no difference from using a kgdb to debug i40e drivers.

Except one of the reasons people prefer programming in userspace is because debugging is so much less painful. Someone using kgdb knows what driver is doing and can work around that.

Ok.

Quoted 23 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 5 lines

Userspace controls the timeout by
using e.g. alarm(2).

Not used in iproute2 after a git grep.

Thanks

Quoted 7 lines
Quoted 1 line

No need for iproute2 to do it user can just do it from shell. Or user can just press CTRL-C.

Yes, but iproute2 needs to deal with EINTR, that is the challenge part, if we simply return an error, the next cvq command might get confused.

Thanks

You mean this:
start command
interrupt
start next command

Quoted 5 lines

?

next command is confused?
I think if you try a new command until previous
one finished it's ok to just return EBUSY.

That would be fine.

And we go back to somehow the idea here:

https://lore.kernel.org/all/CACGkMEvQwhOhgGW6F22+3vmR4AW90qYXF+ZO6BQZguUF2xt2SA@mail.gmail.com/T/#m2da63932eae775d7d05d93d44c2f1d115ffbcefe

Will try to do that in the next version.

Thanks

Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
Quoted 10 lines
Quoted 5 lines

And before the patch, we end up with a real infinite loop which could be caught by RCU stall detector which is not the case of the sleep. What we can do is probably do a periodic netdev_err().

Thanks

Only with a bad device.

Quoted 41 lines
Quoted 41 lines

2- overhead. In a very common scenario when device is in hypervisor,
programming timers etc has a very high overhead, at bootup
lots of CVQ commands are run and slowing boot down is not nice.
let's poll for a bit before waiting?

Then we go back to the question of choosing a good timeout for poll. And
poll seems problematic in the case of UP, scheduler might not have the
chance to run.
Poll just a bit :) Seriously I don't know, but at least check once
after kick.

I think it is what the current code did where the condition will be
check before trying to sleep in the wait_event().

3- suprise removal. need to wake up thread in some way. what about
other cases of device breakage - is there a chance this
introduces new bugs around that? at least enumerate them please.

The current code did:

1) check for vq->broken
2) wakeup during BAD_RING()

So we won't end up with a never woke up process which should be fine.

Thanks

BTW BAD_RING on removal will trigger dev_err. Not sure that is a good
idea - can cause crashes if kernel panics on error.

Yes, it's better to use __virtqueue_break() instead.

But consider we will start from a wait first, I will limit the changes
in virtio-net without bothering virtio core.

Quoted 11 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

Thanks

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#34 lore raw

Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue

Quoted 182 lines · Jason Wang
Quoted 125 lines · Michael S. Tsirkin
Quoted 110 lines · Jason Wang
Quoted 92 lines · Michael S. Tsirkin
Quoted 81 lines · Jason Wang
Quoted 80 lines · Michael S. Tsirkin
Quoted 65 lines · Jason Wang
Quoted 64 lines · Michael S. Tsirkin
Quoted 62 lines · Jason Wang
Quoted 44 lines · Michael S. Tsirkin
Quoted 42 lines · Jason Wang
Quoted 30 lines · Michael S. Tsirkin
Quoted 28 lines · Jason Wang

On Wed, Dec 28, 2022 at 2:34 PM Jason Wang <[email protected]> wrote:

在 2022/12/27 17:38, Michael S. Tsirkin 写道:
On Tue, Dec 27, 2022 at 05:12:58PM +0800, Jason Wang wrote:
在 2022/12/27 15:33, Michael S. Tsirkin 写道:
On Tue, Dec 27, 2022 at 12:30:35PM +0800, Jason Wang wrote:
But device is still going and will later use the buffers.

Same for timeout really.
Avoiding infinite wait/poll is one of the goals, another is to sleep.
If we think the timeout is hard, we can start from the wait.

Thanks
If the goal is to avoid disrupting traffic while CVQ is in use,
that sounds more reasonable. E.g. someone is turning on promisc,
a spike in CPU usage might be unwelcome.

Yes, this would be more obvious is UP is used.

things we should be careful to address then:
1- debugging. Currently it's easy to see a warning if CPU is stuck
in a loop for a while, and we also get a backtrace.
E.g. with this - how do we know who has the RTNL?
We need to integrate with kernel/watchdog.c for good results
and to make sure policy is consistent.

Quoted 8 lines
Quoted 5 lines

That's fine, will consider this.

So after some investigation, it seems the watchdog.c doesn't help. The
only export helper is touch_softlockup_watchdog() which tries to avoid
triggering the lockups warning for the known slow path.

I never said you can just use existing exporting APIs. You'll have to write new ones :)

Ok, I thought you wanted to trigger similar warnings as a watchdog.

Quoted 8 lines
Quoted 5 lines

Btw, I wonder what kind of logic you want here. If we switch to using sleep, there won't be soft lockup anymore. A simple wait + timeout + warning seems sufficient?

Thanks

I'd like to avoid need to teach users new APIs. So watchdog setup to apply to this driver. The warning can be different.

Right, so it looks to me the only possible setup is the watchdog_thres. I plan to trigger the warning every watchdog_thres * 2 second (as softlockup did).

And I think it would still make sense to fail, we can start with a very long timeout like 1 minutes and break the device. Does this make sense?

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Thanks

I'd say we need to make this manageable then.

Did you mean something like sysfs or module parameters?

No I'd say pass it with an ioctl.

Quoted 5 lines
Quoted 2 lines

Can't we do it normally
e.g. react to an interrupt to return to userspace?

I didn't get the meaning of this. Sorry.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Thanks

Standard way to handle things that can timeout and where userspace did not supply the time is to block until an interrupt then return EINTR.

Well this seems to be a huge change, ioctl(2) doesn't say it can return EINTR now.

Quoted 7 lines

the one on fedora 37 does not but it says:
No single standard. Arguments, returns, and semantics of ioctl() vary according to the device driver in question (the call is used as a catch-all for operations that don't cleanly fit the UNIX stream I/O model).

so it depends on the device e.g. for a streams device it does:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/ioctl.html
has EINTR.

Ok, I saw signal(7) also mention about EINTR for ioctl(2):

"""
If a blocked call to one of the following interfaces is
interrupted by a signal handler, then the call is automatically restarted after the signal handler re‐
turns if the SA_RESTART flag was used; otherwise the call fails
with the error EINTR:

Quoted 11 lines

* read(2), readv(2), write(2), writev(2), and ioctl(2) calls on
"slow" devices. A "slow" device is one where the I/O call may block for an indefinite time, for
example, a terminal, pipe, or socket. If an I/O call on a
slow device has already transferred some data by the time it is interrupted by a signal handler,
then the call will return a success status (normally, the
number of bytes transferred). Note that a (local) disk is not a slow device according to this defi‐
nition; I/O operations on disk devices are not interrupted by signals.
"""

Quoted 2 lines

And note that if you interrupt then you don't know whether ioctl changed device state or not generally.

Yes.

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
Quoted 6 lines

Actually, a driver timeout is used by other drivers when using controlq/adminq (e.g i40e). Starting from a sane value (e.g 1 minutes to avoid false negatives) seems to be a good first step.

Well because it's specific hardware so timeout matches what it can promise. virtio spec does not give guarantees. One issue is with software implementations. At the moment I can set a breakpoint in qemu or vhost user backend and nothing bad happens in just continues.

Quoted 5 lines
Quoted 1 line

Yes but it should be no difference from using a kgdb to debug i40e drivers.

Except one of the reasons people prefer programming in userspace is because debugging is so much less painful. Someone using kgdb knows what driver is doing and can work around that.

Ok.

Quoted 23 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 5 lines

Userspace controls the timeout by
using e.g. alarm(2).

Not used in iproute2 after a git grep.

Thanks

Quoted 7 lines
Quoted 1 line

No need for iproute2 to do it user can just do it from shell. Or user can just press CTRL-C.

Yes, but iproute2 needs to deal with EINTR, that is the challenge part, if we simply return an error, the next cvq command might get confused.

Thanks

You mean this:
start command
interrupt
start next command

Quoted 5 lines

?

next command is confused?
I think if you try a new command until previous
one finished it's ok to just return EBUSY.

That would be fine.

Quoted 7 lines

And we go back to somehow the idea here:

https://lore.kernel.org/all/CACGkMEvQwhOhgGW6F22+3vmR4AW90qYXF+ZO6BQZguUF2xt2SA@mail.gmail.com/T/#m2da63932eae775d7d05d93d44c2f1d115ffbcefe

Will try to do that in the next version.

Thanks

Where you wrote:
We can put the process into interruptible sleep, then it should be fine?

(FYI, some transport specific methods may sleep e.g ccw).

indeed.

Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
Quoted 15 lines

And before the patch, we end up with a real infinite loop which could
be caught by RCU stall detector which is not the case of the sleep.
What we can do is probably do a periodic netdev_err().

Thanks

Only with a bad device.

Quoted 41 lines

2- overhead. In a very common scenario when device is in hypervisor,
programming timers etc has a very high overhead, at bootup
lots of CVQ commands are run and slowing boot down is not nice.
let's poll for a bit before waiting?

Then we go back to the question of choosing a good timeout for poll. And
poll seems problematic in the case of UP, scheduler might not have the
chance to run.
Poll just a bit :) Seriously I don't know, but at least check once
after kick.

I think it is what the current code did where the condition will be
check before trying to sleep in the wait_event().

3- suprise removal. need to wake up thread in some way. what about
other cases of device breakage - is there a chance this
introduces new bugs around that? at least enumerate them please.

The current code did:

1) check for vq->broken
2) wakeup during BAD_RING()

So we won't end up with a never woke up process which should be fine.

Thanks

BTW BAD_RING on removal will trigger dev_err. Not sure that is a good
idea - can cause crashes if kernel panics on error.

Yes, it's better to use __virtqueue_break() instead.

But consider we will start from a wait first, I will limit the changes
in virtio-net without bothering virtio core.

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

Thanks

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

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

author
#36 lore raw

Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue

Quoted 25 lines · Hillf Danton

On Mon, 26 Dec 2022 15:49:07 +0800 Jason Wang <[email protected]>

Quoted 12 lines
drivers/virtio/virtio_ring.c +2 -0
@@ -2654,6 +2663,8 @@ static void vring_free(struct virtqueue *_vq)
 {
      struct vring_virtqueue *vq = to_vvq(_vq);
 
+     wake_up_interruptible(&vq->wq);
+
      if (vq->we_own_ring) {
              if (vq->packed_ring) {
                      vring_free_queue(vq->vq.vdev,
@@ -2863,4 +2874,22 @@ const struct vring *virtqueue_get_vring(struct virtqueue *vq)
 }
 EXPORT_SYMBOL_GPL(virtqueue_get_vring);

drivers/virtio/virtio_ring.c const struct vring *virtqueue_get_vring(struct virtqueue *vq) · line 2885

Quoted 10 lines
drivers/virtio/virtio_ring.c +9 -0
 
+int virtqueue_wait_for_used(struct virtqueue *_vq)
+{
+     struct vring_virtqueue *vq = to_vvq(_vq);
+
+     /* TODO: Tweak the timeout. */
+     return wait_event_interruptible_timeout(vq->wq,
+            virtqueue_is_broken(_vq) || more_used(vq), HZ);
+}
+EXPORT_SYMBOL_GPL(virtqueue_wait_for_used);
Quoted 9 lines
        waker           waiter
        ---             ---
        vring_del_virtqueue
          vring_free(_vq);
            wakeup
          kfree(vq);
                        get on CPU a tick later
                        uaf ?

Exactly, this wakeup of vring_free is not needed. It's up to the driver to do the proper wake up to avoid race when subsystem un registration.

Thanks

author
#37 lore raw

PATCH 4/4 virtio-net: sleep instead of busy waiting for cvq command

In reply to Jason Wang

We used to busy waiting on the cvq command this tends to be problematic since:

1) CPU could wait for ever on a buggy/malicous device
2) There's no wait to terminate the process that triggers the cvq command

So this patch switch to use virtqueue_wait_for_used() to sleep with a timeout (1s) instead of busy polling for the cvq command forever. This gives the scheduler a breath and can let the process can respond to asignal. If the device doesn't respond in the timeout, break the device.

---
Changes since V1:
- break the device when timeout
- get buffer manually since the virtio core check more_used() instead

---
 drivers/net/virtio_net.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
drivers/net/virtio_net.c +16 -8
index efd9dd55828b..6a2ea64cfcb5 100644
@@ -405,6 +405,7 @@ static void disable_rx_mode_work(struct virtnet_info *vi)
 	vi->rx_mode_work_enabled = false;
 	spin_unlock_bh(&vi->rx_mode_lock);
 
+	virtqueue_wake_up(vi->cvq);
 	flush_work(&vi->rx_mode_work);
 }
 
@@ -1497,6 +1498,11 @@ static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
 	return !oom;
 }
 
+static void virtnet_cvq_done(struct virtqueue *cvq)
+{
+	virtqueue_wake_up(cvq);
+}
+
 static void skb_recv_done(struct virtqueue *rvq)
 {
 	struct virtnet_info *vi = rvq->vdev->priv;
@@ -1984,6 +1990,8 @@ static int virtnet_tx_resize(struct virtnet_info *vi,
 	return err;
 }
 
+static int virtnet_close(struct net_device *dev);
+
 /*
  * Send command via the control virtqueue and check status.  Commands
  * supported by the hypervisor, as indicated by feature bits, should
@@ -2026,14 +2034,14 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
 	if (unlikely(!virtqueue_kick(vi->cvq)))
 		return vi->ctrl->status == VIRTIO_NET_OK;
 
-	/* Spin for a response, the kick causes an ioport write, trapping
-	 * into the hypervisor, so the request should be handled immediately.
-	 */
-	while (!virtqueue_get_buf(vi->cvq, &tmp) &&
-	       !virtqueue_is_broken(vi->cvq))
-		cpu_relax();
+	if (virtqueue_wait_for_used(vi->cvq)) {
+		virtqueue_get_buf(vi->cvq, &tmp);
+		return vi->ctrl->status == VIRTIO_NET_OK;
+	}
 
-	return vi->ctrl->status == VIRTIO_NET_OK;
+	netdev_err(vi->dev, "CVQ command timeout, break the virtio device.");
+	virtio_break_device(vi->vdev);
+	return VIRTIO_NET_ERR;
 }
 
 static int virtnet_set_mac_address(struct net_device *dev, void *p)
@@ -3526,7 +3534,7 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
 
 	/* Parameters for control virtqueue, if any */
 	if (vi->has_cvq) {
-		callbacks[total_vqs - 1] = NULL;
+		callbacks[total_vqs - 1] = virtnet_cvq_done;
 		names[total_vqs - 1] = "control";
 	}
 
Signature · 6 lines
2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
reviewer
#38 lore raw

Re: [PATCH 4/4] virtio-net: sleep instead of busy waiting for cvq command

Quoted 9 lines · Jason Wang

We used to busy waiting on the cvq command this tends to be problematic since:

1) CPU could wait for ever on a buggy/malicous device
2) There's no wait to terminate the process that triggers the cvq command

So this patch switch to use virtqueue_wait_for_used() to sleep with a timeout (1s) instead of busy polling for the cvq command forever. This

I don't think that a fixed 1S is a good choice. Some of the DPUs are very lazy for cvq handle. In particular, we will also directly break the device.

I think it is necessary to add a Virtio-Net parameter to allow users to define this timeout by themselves. Although I don't think this is a good way.

Thanks.

Quoted 84 lines

gives the scheduler a breath and can let the process can respond to asignal. If the device doesn't respond in the timeout, break the device.

---
Changes since V1:
- break the device when timeout
- get buffer manually since the virtio core check more_used() instead

---
 drivers/net/virtio_net.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
drivers/net/virtio_net.c +16 -9
index efd9dd55828b..6a2ea64cfcb5 100644
@@ -405,6 +405,7 @@ static void disable_rx_mode_work(struct virtnet_info *vi)
 	vi->rx_mode_work_enabled = false;
 	spin_unlock_bh(&vi->rx_mode_lock);
 
+	virtqueue_wake_up(vi->cvq);
 	flush_work(&vi->rx_mode_work);
 }
 
@@ -1497,6 +1498,11 @@ static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
 	return !oom;
 }
 
+static void virtnet_cvq_done(struct virtqueue *cvq)
+{
+	virtqueue_wake_up(cvq);
+}
+
 static void skb_recv_done(struct virtqueue *rvq)
 {
 	struct virtnet_info *vi = rvq->vdev->priv;
@@ -1984,6 +1990,8 @@ static int virtnet_tx_resize(struct virtnet_info *vi,
 	return err;
 }
 
+static int virtnet_close(struct net_device *dev);
+
 /*
  * Send command via the control virtqueue and check status.  Commands
  * supported by the hypervisor, as indicated by feature bits, should
@@ -2026,14 +2034,14 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
 	if (unlikely(!virtqueue_kick(vi->cvq)))
 		return vi->ctrl->status == VIRTIO_NET_OK;
 
-	/* Spin for a response, the kick causes an ioport write, trapping
-	 * into the hypervisor, so the request should be handled immediately.
-	 */
-	while (!virtqueue_get_buf(vi->cvq, &tmp) &&
-	       !virtqueue_is_broken(vi->cvq))
-		cpu_relax();
+	if (virtqueue_wait_for_used(vi->cvq)) {
+		virtqueue_get_buf(vi->cvq, &tmp);
+		return vi->ctrl->status == VIRTIO_NET_OK;
+	}
 
-	return vi->ctrl->status == VIRTIO_NET_OK;
+	netdev_err(vi->dev, "CVQ command timeout, break the virtio device.");
+	virtio_break_device(vi->vdev);
+	return VIRTIO_NET_ERR;
 }
 
 static int virtnet_set_mac_address(struct net_device *dev, void *p)
@@ -3526,7 +3534,7 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
 
 	/* Parameters for control virtqueue, if any */
 	if (vi->has_cvq) {
-		callbacks[total_vqs - 1] = NULL;
+		callbacks[total_vqs - 1] = virtnet_cvq_done;
 		names[total_vqs - 1] = "control";
 	}
 
--

2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#39 lore raw

Re: [PATCH 4/4] virtio-net: sleep instead of busy waiting for cvq command

Quoted 5 lines · Xuan Zhuo
Quoted 3 lines · Jason Wang

We used to busy waiting on the cvq command this tends to be problematic since:

Quoted 8 lines
Quoted 6 lines

1) CPU could wait for ever on a buggy/malicous device
2) There's no wait to terminate the process that triggers the cvq command

So this patch switch to use virtqueue_wait_for_used() to sleep with a timeout (1s) instead of busy polling for the cvq command forever. This

I don't think that a fixed 1S is a good choice.

Well, it could be tweaked to be a little bit longer.

One way, as discussed, is to let the device advertise a timeout then the driver can validate if it's valid and use that timeout. But it needs extension to the spec.

Quoted 2 lines

Some of the DPUs are very
lazy for cvq handle.

Such design needs to be revisited, cvq (control path) should have a better priority or QOS than datapath.

Quoted 1 line

In particular, we will also directly break the device.

It's kind of hardening for malicious devices.

Quoted 3 lines

I think it is necessary to add a Virtio-Net parameter to allow users to define this timeout by themselves. Although I don't think this is a good way.

Very hard and unfriendly to the end users.

Thanks

Quoted 81 lines

Thanks.

Quoted 77 lines

gives the scheduler a breath and can let the process can respond to asignal. If the device doesn't respond in the timeout, break the device.

---
Changes since V1:
- break the device when timeout
- get buffer manually since the virtio core check more_used() instead

---
 drivers/net/virtio_net.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
drivers/net/virtio_net.c +16 -8
index efd9dd55828b..6a2ea64cfcb5 100644
@@ -405,6 +405,7 @@ static void disable_rx_mode_work(struct virtnet_info *vi)
      vi->rx_mode_work_enabled = false;
      spin_unlock_bh(&vi->rx_mode_lock);
 
+     virtqueue_wake_up(vi->cvq);
      flush_work(&vi->rx_mode_work);
 }
 
@@ -1497,6 +1498,11 @@ static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
      return !oom;
 }
 
+static void virtnet_cvq_done(struct virtqueue *cvq)
+{
+     virtqueue_wake_up(cvq);
+}
+
 static void skb_recv_done(struct virtqueue *rvq)
 {
      struct virtnet_info *vi = rvq->vdev->priv;
@@ -1984,6 +1990,8 @@ static int virtnet_tx_resize(struct virtnet_info *vi,
      return err;
 }
 
+static int virtnet_close(struct net_device *dev);
+
 /*
  * Send command via the control virtqueue and check status.  Commands
  * supported by the hypervisor, as indicated by feature bits, should
@@ -2026,14 +2034,14 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
      if (unlikely(!virtqueue_kick(vi->cvq)))
              return vi->ctrl->status == VIRTIO_NET_OK;
 
-     /* Spin for a response, the kick causes an ioport write, trapping
-      * into the hypervisor, so the request should be handled immediately.
-      */
-     while (!virtqueue_get_buf(vi->cvq, &tmp) &&
-            !virtqueue_is_broken(vi->cvq))
-             cpu_relax();
+     if (virtqueue_wait_for_used(vi->cvq)) {
+             virtqueue_get_buf(vi->cvq, &tmp);
+             return vi->ctrl->status == VIRTIO_NET_OK;
+     }
 
-     return vi->ctrl->status == VIRTIO_NET_OK;
+     netdev_err(vi->dev, "CVQ command timeout, break the virtio device.");
+     virtio_break_device(vi->vdev);
+     return VIRTIO_NET_ERR;
 }
 
 static int virtnet_set_mac_address(struct net_device *dev, void *p)
@@ -3526,7 +3534,7 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
 
      /* Parameters for control virtqueue, if any */
      if (vi->has_cvq) {
-             callbacks[total_vqs - 1] = NULL;
+             callbacks[total_vqs - 1] = virtnet_cvq_done;
              names[total_vqs - 1] = "control";
      }
 
Quoted 8 lines
Quoted 7 lines

--
2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#40 lore raw

Re: [PATCH 4/4] virtio-net: sleep instead of busy waiting for cvq command

Quoted 13 lines · Jason Wang
Quoted 12 lines · Xuan Zhuo
Quoted 9 lines · Jason Wang

We used to busy waiting on the cvq command this tends to be problematic since:

1) CPU could wait for ever on a buggy/malicous device
2) There's no wait to terminate the process that triggers the cvq command

So this patch switch to use virtqueue_wait_for_used() to sleep with a timeout (1s) instead of busy polling for the cvq command forever. This

Quoted 7 lines
Quoted 1 line

I don't think that a fixed 1S is a good choice.

Well, it could be tweaked to be a little bit longer.

One way, as discussed, is to let the device advertise a timeout then the driver can validate if it's valid and use that timeout. But it needs extension to the spec.

Controlling timeout from device is a good idea, e.g. hardware devices would benefit from a shorter timeout, hypervisor devices from a longer timeout or no timeout.

Quoted 6 lines
Quoted 3 lines

Some of the DPUs are very
lazy for cvq handle.

Such design needs to be revisited, cvq (control path) should have a better priority or QOS than datapath.

Spec says nothing about this, so driver can't assume this either.

Quoted 3 lines
Quoted 1 line

In particular, we will also directly break the device.

It's kind of hardening for malicious devices.

ATM no amount of hardening can prevent a malicious hypervisor from blocking the guest. Recovering when a hardware device is broken would be nice but I think if we do bother then we should try harder to recover, such as by driving device reset.

Also, does your patch break surprise removal? There's no callback in this case ATM.

Quoted 97 lines
Quoted 3 lines

I think it is necessary to add a Virtio-Net parameter to allow users to define this timeout by themselves. Although I don't think this is a good way.

Very hard and unfriendly to the end users.

Thanks

Quoted 89 lines

Thanks.

Quoted 84 lines

gives the scheduler a breath and can let the process can respond to asignal. If the device doesn't respond in the timeout, break the device.

---
Changes since V1:
- break the device when timeout
- get buffer manually since the virtio core check more_used() instead

---
 drivers/net/virtio_net.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
drivers/net/virtio_net.c +16 -9
index efd9dd55828b..6a2ea64cfcb5 100644
@@ -405,6 +405,7 @@ static void disable_rx_mode_work(struct virtnet_info *vi)
      vi->rx_mode_work_enabled = false;
      spin_unlock_bh(&vi->rx_mode_lock);
 
+     virtqueue_wake_up(vi->cvq);
      flush_work(&vi->rx_mode_work);
 }
 
@@ -1497,6 +1498,11 @@ static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
      return !oom;
 }
 
+static void virtnet_cvq_done(struct virtqueue *cvq)
+{
+     virtqueue_wake_up(cvq);
+}
+
 static void skb_recv_done(struct virtqueue *rvq)
 {
      struct virtnet_info *vi = rvq->vdev->priv;
@@ -1984,6 +1990,8 @@ static int virtnet_tx_resize(struct virtnet_info *vi,
      return err;
 }
 
+static int virtnet_close(struct net_device *dev);
+
 /*
  * Send command via the control virtqueue and check status.  Commands
  * supported by the hypervisor, as indicated by feature bits, should
@@ -2026,14 +2034,14 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
      if (unlikely(!virtqueue_kick(vi->cvq)))
              return vi->ctrl->status == VIRTIO_NET_OK;
 
-     /* Spin for a response, the kick causes an ioport write, trapping
-      * into the hypervisor, so the request should be handled immediately.
-      */
-     while (!virtqueue_get_buf(vi->cvq, &tmp) &&
-            !virtqueue_is_broken(vi->cvq))
-             cpu_relax();
+     if (virtqueue_wait_for_used(vi->cvq)) {
+             virtqueue_get_buf(vi->cvq, &tmp);
+             return vi->ctrl->status == VIRTIO_NET_OK;
+     }
 
-     return vi->ctrl->status == VIRTIO_NET_OK;
+     netdev_err(vi->dev, "CVQ command timeout, break the virtio device.");
+     virtio_break_device(vi->vdev);
+     return VIRTIO_NET_ERR;
 }
 
 static int virtnet_set_mac_address(struct net_device *dev, void *p)
@@ -3526,7 +3534,7 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
 
      /* Parameters for control virtqueue, if any */
      if (vi->has_cvq) {
-             callbacks[total_vqs - 1] = NULL;
+             callbacks[total_vqs - 1] = virtnet_cvq_done;
              names[total_vqs - 1] = "control";
      }
 
--

2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#41 lore raw

Re: [PATCH 4/4] virtio-net: sleep instead of busy waiting for cvq command

Quoted 13 lines · Michael S. Tsirkin
Quoted 12 lines · Jason Wang
Quoted 11 lines · Xuan Zhuo
Quoted 9 lines · Jason Wang

We used to busy waiting on the cvq command this tends to be problematic since:

1) CPU could wait for ever on a buggy/malicous device
2) There's no wait to terminate the process that triggers the cvq command

So this patch switch to use virtqueue_wait_for_used() to sleep with a timeout (1s) instead of busy polling for the cvq command forever. This

I don't think that a fixed 1S is a good choice.

Quoted 8 lines
Quoted 5 lines

Well, it could be tweaked to be a little bit longer.

One way, as discussed, is to let the device advertise a timeout then the driver can validate if it's valid and use that timeout. But it needs extension to the spec.

Controlling timeout from device is a good idea, e.g. hardware devices would benefit from a shorter timeout, hypervisor devices from a longer timeout or no timeout.

Yes.

Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

Some of the DPUs are very
lazy for cvq handle.

Such design needs to be revisited, cvq (control path) should have a better priority or QOS than datapath.

Spec says nothing about this, so driver can't assume this either.

Well, my understanding is that it's more than what spec can define or it's a kind of best practice.

The current code is one example, that is, driver may choose to busy poll which cause spike.

Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

In particular, we will also directly break the device.

It's kind of hardening for malicious devices.

ATM no amount of hardening can prevent a malicious hypervisor from blocking the guest. Recovering when a hardware device is broken would be nice but I think if we do bother then we should try harder to recover, such as by driving device reset.

Probably, but as discussed in another thread, it needs co-operation in the upper layer (networking core).

Quoted 4 lines

Also, does your patch break surprise removal? There's no callback in this case ATM.

I think not (see reply in another thread).

Thanks

Quoted 87 lines
Quoted 87 lines
Quoted 3 lines

I think it is necessary to add a Virtio-Net parameter to allow users to define this timeout by themselves. Although I don't think this is a good way.

Very hard and unfriendly to the end users.

Thanks

Quoted 80 lines

Thanks.

Quoted 77 lines

gives the scheduler a breath and can let the process can respond to asignal. If the device doesn't respond in the timeout, break the device.

---
Changes since V1:
- break the device when timeout
- get buffer manually since the virtio core check more_used() instead

---
  drivers/net/virtio_net.c | 24 ++++++++++++++++--------
  1 file changed, 16 insertions(+), 8 deletions(-)
drivers/net/virtio_net.c +16 -8
index efd9dd55828b..6a2ea64cfcb5 100644
@@ -405,6 +405,7 @@ static void disable_rx_mode_work(struct virtnet_info *vi)
       vi->rx_mode_work_enabled = false;
       spin_unlock_bh(&vi->rx_mode_lock);
 
+     virtqueue_wake_up(vi->cvq);
       flush_work(&vi->rx_mode_work);
  }
 
@@ -1497,6 +1498,11 @@ static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
       return !oom;
  }
 
+static void virtnet_cvq_done(struct virtqueue *cvq)
+{
+     virtqueue_wake_up(cvq);
+}
+
  static void skb_recv_done(struct virtqueue *rvq)
  {
       struct virtnet_info *vi = rvq->vdev->priv;
@@ -1984,6 +1990,8 @@ static int virtnet_tx_resize(struct virtnet_info *vi,
       return err;
  }
 
+static int virtnet_close(struct net_device *dev);
+
  /*
   * Send command via the control virtqueue and check status.  Commands
   * supported by the hypervisor, as indicated by feature bits, should
@@ -2026,14 +2034,14 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
       if (unlikely(!virtqueue_kick(vi->cvq)))
               return vi->ctrl->status == VIRTIO_NET_OK;
 
-     /* Spin for a response, the kick causes an ioport write, trapping
-      * into the hypervisor, so the request should be handled immediately.
-      */
-     while (!virtqueue_get_buf(vi->cvq, &tmp) &&
-            !virtqueue_is_broken(vi->cvq))
-             cpu_relax();
+     if (virtqueue_wait_for_used(vi->cvq)) {
+             virtqueue_get_buf(vi->cvq, &tmp);
+             return vi->ctrl->status == VIRTIO_NET_OK;
+     }
 
-     return vi->ctrl->status == VIRTIO_NET_OK;
+     netdev_err(vi->dev, "CVQ command timeout, break the virtio device.");
+     virtio_break_device(vi->vdev);
+     return VIRTIO_NET_ERR;
  }
 
  static int virtnet_set_mac_address(struct net_device *dev, void *p)
@@ -3526,7 +3534,7 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
 
       /* Parameters for control virtqueue, if any */
       if (vi->has_cvq) {
-             callbacks[total_vqs - 1] = NULL;
+             callbacks[total_vqs - 1] = virtnet_cvq_done;
               names[total_vqs - 1] = "control";
       }
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

--
2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#42 lore raw

Re: [PATCH 4/4] virtio-net: sleep instead of busy waiting for cvq command

Quoted 10 lines · Jason Wang
Quoted 6 lines
Quoted 2 lines
Quoted 1 line

In particular, we will also directly break the device.

It's kind of hardening for malicious devices.

ATM no amount of hardening can prevent a malicious hypervisor from blocking the guest. Recovering when a hardware device is broken would be nice but I think if we do bother then we should try harder to recover, such as by driving device reset.

Probably, but as discussed in another thread, it needs co-operation in the upper layer (networking core).

To track all state? Yea, maybe. For sure it's doable just in virtio, but if you can find 1-2 other drivers that do this internally then factoring this out to net core will likely be accepted.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
author
#43 lore raw

Re: [PATCH 4/4] virtio-net: sleep instead of busy waiting for cvq command

Quoted 8 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang
Quoted 6 lines
Quoted 2 lines
Quoted 1 line

In particular, we will also directly break the device.

It's kind of hardening for malicious devices.

ATM no amount of hardening can prevent a malicious hypervisor from blocking the guest. Recovering when a hardware device is broken would be nice but I think if we do bother then we should try harder to recover, such as by driving device reset.

Quoted 5 lines
Quoted 2 lines

Probably, but as discussed in another thread, it needs co-operation in the upper layer (networking core).

To track all state? Yea, maybe. For sure it's doable just in virtio, but if you can find 1-2 other drivers that do this internally then factoring this out to net core will likely be accepted.

One thing that might be useful is to reuse tx_timeout() but current virtio-net doesn't do more than a simple warning (other drivers may try to reset).

So I would leave it for future investigation.

Thanks

Quoted 1 line

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

reviewer
#44 lore raw

Re: [PATCH 4/4] virtio-net: sleep instead of busy waiting for cvq command

In reply to Michael S. Tsirkin
Quoted 18 lines · Michael S. Tsirkin
Quoted 17 lines · Jason Wang
Quoted 5 lines · Xuan Zhuo
Quoted 3 lines · Jason Wang

We used to busy waiting on the cvq command this tends to be problematic since:

Quoted 8 lines
Quoted 6 lines

1) CPU could wait for ever on a buggy/malicous device
2) There's no wait to terminate the process that triggers the cvq command

So this patch switch to use virtqueue_wait_for_used() to sleep with a timeout (1s) instead of busy polling for the cvq command forever. This

I don't think that a fixed 1S is a good choice.

Well, it could be tweaked to be a little bit longer.

Quoted 7 lines
Quoted 3 lines

One way, as discussed, is to let the device advertise a timeout then the driver can validate if it's valid and use that timeout. But it needs extension to the spec.

Controlling timeout from device is a good idea, e.g. hardware devices would benefit from a shorter timeout, hypervisor devices from a longer timeout or no timeout.

Yes. That is good.

Before introducing this feature, I personally like to use "wait", rather than define a timeout.

Thanks.

Quoted 121 lines
Quoted 7 lines
Quoted 4 lines

Some of the DPUs are very
lazy for cvq handle.

Such design needs to be revisited, cvq (control path) should have a better priority or QOS than datapath.

Spec says nothing about this, so driver can't assume this either.

Quoted 3 lines
Quoted 1 line

In particular, we will also directly break the device.

It's kind of hardening for malicious devices.

ATM no amount of hardening can prevent a malicious hypervisor from blocking the guest. Recovering when a hardware device is broken would be nice but I think if we do bother then we should try harder to recover, such as by driving device reset.

Also, does your patch break surprise removal? There's no callback in this case ATM.

Quoted 97 lines
Quoted 3 lines

I think it is necessary to add a Virtio-Net parameter to allow users to define this timeout by themselves. Although I don't think this is a good way.

Very hard and unfriendly to the end users.

Thanks

Quoted 89 lines

Thanks.

Quoted 84 lines

gives the scheduler a breath and can let the process can respond to asignal. If the device doesn't respond in the timeout, break the device.

---
Changes since V1:
- break the device when timeout
- get buffer manually since the virtio core check more_used() instead

---
 drivers/net/virtio_net.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
drivers/net/virtio_net.c +16 -9
index efd9dd55828b..6a2ea64cfcb5 100644
@@ -405,6 +405,7 @@ static void disable_rx_mode_work(struct virtnet_info *vi)
      vi->rx_mode_work_enabled = false;
      spin_unlock_bh(&vi->rx_mode_lock);
 
+     virtqueue_wake_up(vi->cvq);
      flush_work(&vi->rx_mode_work);
 }
 
@@ -1497,6 +1498,11 @@ static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
      return !oom;
 }
 
+static void virtnet_cvq_done(struct virtqueue *cvq)
+{
+     virtqueue_wake_up(cvq);
+}
+
 static void skb_recv_done(struct virtqueue *rvq)
 {
      struct virtnet_info *vi = rvq->vdev->priv;
@@ -1984,6 +1990,8 @@ static int virtnet_tx_resize(struct virtnet_info *vi,
      return err;
 }
 
+static int virtnet_close(struct net_device *dev);
+
 /*
  * Send command via the control virtqueue and check status.  Commands
  * supported by the hypervisor, as indicated by feature bits, should
@@ -2026,14 +2034,14 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
      if (unlikely(!virtqueue_kick(vi->cvq)))
              return vi->ctrl->status == VIRTIO_NET_OK;
 
-     /* Spin for a response, the kick causes an ioport write, trapping
-      * into the hypervisor, so the request should be handled immediately.
-      */
-     while (!virtqueue_get_buf(vi->cvq, &tmp) &&
-            !virtqueue_is_broken(vi->cvq))
-             cpu_relax();
+     if (virtqueue_wait_for_used(vi->cvq)) {
+             virtqueue_get_buf(vi->cvq, &tmp);
+             return vi->ctrl->status == VIRTIO_NET_OK;
+     }
 
-     return vi->ctrl->status == VIRTIO_NET_OK;
+     netdev_err(vi->dev, "CVQ command timeout, break the virtio device.");
+     virtio_break_device(vi->vdev);
+     return VIRTIO_NET_ERR;
 }
 
 static int virtnet_set_mac_address(struct net_device *dev, void *p)
@@ -3526,7 +3534,7 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
 
      /* Parameters for control virtqueue, if any */
      if (vi->has_cvq) {
-             callbacks[total_vqs - 1] = NULL;
+             callbacks[total_vqs - 1] = virtnet_cvq_done;
              names[total_vqs - 1] = "control";
      }
 
--

2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#45 lore raw

Re: [PATCH 4/4] virtio-net: sleep instead of busy waiting for cvq command

Quoted 24 lines · Xuan Zhuo
Quoted 22 lines · Michael S. Tsirkin
Quoted 20 lines · Jason Wang
Quoted 5 lines · Xuan Zhuo
Quoted 3 lines · Jason Wang

We used to busy waiting on the cvq command this tends to be problematic since:

Quoted 8 lines
Quoted 6 lines

1) CPU could wait for ever on a buggy/malicous device
2) There's no wait to terminate the process that triggers the cvq command

So this patch switch to use virtqueue_wait_for_used() to sleep with a timeout (1s) instead of busy polling for the cvq command forever. This

I don't think that a fixed 1S is a good choice.

Well, it could be tweaked to be a little bit longer.

One way, as discussed, is to let the device advertise a timeout then the driver can validate if it's valid and use that timeout. But it needs extension to the spec.

Quoted 8 lines
Quoted 3 lines

Controlling timeout from device is a good idea, e.g. hardware devices would benefit from a shorter timeout, hypervisor devices from a longer timeout or no timeout.

Yes. That is good.

Before introducing this feature, I personally like to use "wait", rather than define a timeout.

Note that the driver still needs to validate what device advertises to avoid infinite wait.

Thanks

Quoted 126 lines

Thanks.

Quoted 121 lines
Quoted 7 lines
Quoted 4 lines

Some of the DPUs are very
lazy for cvq handle.

Such design needs to be revisited, cvq (control path) should have a better priority or QOS than datapath.

Spec says nothing about this, so driver can't assume this either.

Quoted 3 lines
Quoted 1 line

In particular, we will also directly break the device.

It's kind of hardening for malicious devices.

ATM no amount of hardening can prevent a malicious hypervisor from blocking the guest. Recovering when a hardware device is broken would be nice but I think if we do bother then we should try harder to recover, such as by driving device reset.

Also, does your patch break surprise removal? There's no callback in this case ATM.

Quoted 97 lines
Quoted 3 lines

I think it is necessary to add a Virtio-Net parameter to allow users to define this timeout by themselves. Although I don't think this is a good way.

Very hard and unfriendly to the end users.

Thanks

Quoted 89 lines

Thanks.

Quoted 84 lines

gives the scheduler a breath and can let the process can respond to asignal. If the device doesn't respond in the timeout, break the device.

---
Changes since V1:
- break the device when timeout
- get buffer manually since the virtio core check more_used() instead

---
 drivers/net/virtio_net.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
drivers/net/virtio_net.c +16 -9
index efd9dd55828b..6a2ea64cfcb5 100644
@@ -405,6 +405,7 @@ static void disable_rx_mode_work(struct virtnet_info *vi)
      vi->rx_mode_work_enabled = false;
      spin_unlock_bh(&vi->rx_mode_lock);
 
+     virtqueue_wake_up(vi->cvq);
      flush_work(&vi->rx_mode_work);
 }
 
@@ -1497,6 +1498,11 @@ static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
      return !oom;
 }
 
+static void virtnet_cvq_done(struct virtqueue *cvq)
+{
+     virtqueue_wake_up(cvq);
+}
+
 static void skb_recv_done(struct virtqueue *rvq)
 {
      struct virtnet_info *vi = rvq->vdev->priv;
@@ -1984,6 +1990,8 @@ static int virtnet_tx_resize(struct virtnet_info *vi,
      return err;
 }
 
+static int virtnet_close(struct net_device *dev);
+
 /*
  * Send command via the control virtqueue and check status.  Commands
  * supported by the hypervisor, as indicated by feature bits, should
@@ -2026,14 +2034,14 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
      if (unlikely(!virtqueue_kick(vi->cvq)))
              return vi->ctrl->status == VIRTIO_NET_OK;
 
-     /* Spin for a response, the kick causes an ioport write, trapping
-      * into the hypervisor, so the request should be handled immediately.
-      */
-     while (!virtqueue_get_buf(vi->cvq, &tmp) &&
-            !virtqueue_is_broken(vi->cvq))
-             cpu_relax();
+     if (virtqueue_wait_for_used(vi->cvq)) {
+             virtqueue_get_buf(vi->cvq, &tmp);
+             return vi->ctrl->status == VIRTIO_NET_OK;
+     }
 
-     return vi->ctrl->status == VIRTIO_NET_OK;
+     netdev_err(vi->dev, "CVQ command timeout, break the virtio device.");
+     virtio_break_device(vi->vdev);
+     return VIRTIO_NET_ERR;
 }
 
 static int virtnet_set_mac_address(struct net_device *dev, void *p)
@@ -3526,7 +3534,7 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
 
      /* Parameters for control virtqueue, if any */
      if (vi->has_cvq) {
-             callbacks[total_vqs - 1] = NULL;
+             callbacks[total_vqs - 1] = virtnet_cvq_done;
              names[total_vqs - 1] = "control";
      }
 
--

2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

reviewer
#46 lore raw

Re: [PATCH 4/4] virtio-net: sleep instead of busy waiting for cvq command

Quoted 29 lines · Jason Wang
Quoted 28 lines · Xuan Zhuo
Quoted 25 lines · Michael S. Tsirkin
Quoted 13 lines · Jason Wang
Quoted 12 lines · Xuan Zhuo
Quoted 9 lines · Jason Wang

We used to busy waiting on the cvq command this tends to be problematic since:

1) CPU could wait for ever on a buggy/malicous device
2) There's no wait to terminate the process that triggers the cvq command

So this patch switch to use virtqueue_wait_for_used() to sleep with a timeout (1s) instead of busy polling for the cvq command forever. This

Quoted 7 lines
Quoted 1 line

I don't think that a fixed 1S is a good choice.

Well, it could be tweaked to be a little bit longer.

One way, as discussed, is to let the device advertise a timeout then the driver can validate if it's valid and use that timeout. But it needs extension to the spec.

Controlling timeout from device is a good idea, e.g. hardware devices would benefit from a shorter timeout, hypervisor devices from a longer timeout or no timeout.

Quoted 7 lines
Quoted 4 lines

Yes. That is good.

Before introducing this feature, I personally like to use "wait", rather than define a timeout.

Note that the driver still needs to validate what device advertises to avoid infinite wait.

Sorry, I didn't understand what you mean.

Thanks.

Quoted 130 lines

Thanks

Quoted 126 lines

Thanks.

Quoted 121 lines
Quoted 7 lines
Quoted 4 lines

Some of the DPUs are very
lazy for cvq handle.

Such design needs to be revisited, cvq (control path) should have a better priority or QOS than datapath.

Spec says nothing about this, so driver can't assume this either.

Quoted 3 lines
Quoted 1 line

In particular, we will also directly break the device.

It's kind of hardening for malicious devices.

ATM no amount of hardening can prevent a malicious hypervisor from blocking the guest. Recovering when a hardware device is broken would be nice but I think if we do bother then we should try harder to recover, such as by driving device reset.

Also, does your patch break surprise removal? There's no callback in this case ATM.

Quoted 97 lines
Quoted 3 lines

I think it is necessary to add a Virtio-Net parameter to allow users to define this timeout by themselves. Although I don't think this is a good way.

Very hard and unfriendly to the end users.

Thanks

Quoted 89 lines

Thanks.

Quoted 84 lines

gives the scheduler a breath and can let the process can respond to asignal. If the device doesn't respond in the timeout, break the device.

---
Changes since V1:
- break the device when timeout
- get buffer manually since the virtio core check more_used() instead

---
 drivers/net/virtio_net.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
drivers/net/virtio_net.c +16 -9
index efd9dd55828b..6a2ea64cfcb5 100644
@@ -405,6 +405,7 @@ static void disable_rx_mode_work(struct virtnet_info *vi)
      vi->rx_mode_work_enabled = false;
      spin_unlock_bh(&vi->rx_mode_lock);
 
+     virtqueue_wake_up(vi->cvq);
      flush_work(&vi->rx_mode_work);
 }
 
@@ -1497,6 +1498,11 @@ static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
      return !oom;
 }
 
+static void virtnet_cvq_done(struct virtqueue *cvq)
+{
+     virtqueue_wake_up(cvq);
+}
+
 static void skb_recv_done(struct virtqueue *rvq)
 {
      struct virtnet_info *vi = rvq->vdev->priv;
@@ -1984,6 +1990,8 @@ static int virtnet_tx_resize(struct virtnet_info *vi,
      return err;
 }
 
+static int virtnet_close(struct net_device *dev);
+
 /*
  * Send command via the control virtqueue and check status.  Commands
  * supported by the hypervisor, as indicated by feature bits, should
@@ -2026,14 +2034,14 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
      if (unlikely(!virtqueue_kick(vi->cvq)))
              return vi->ctrl->status == VIRTIO_NET_OK;
 
-     /* Spin for a response, the kick causes an ioport write, trapping
-      * into the hypervisor, so the request should be handled immediately.
-      */
-     while (!virtqueue_get_buf(vi->cvq, &tmp) &&
-            !virtqueue_is_broken(vi->cvq))
-             cpu_relax();
+     if (virtqueue_wait_for_used(vi->cvq)) {
+             virtqueue_get_buf(vi->cvq, &tmp);
+             return vi->ctrl->status == VIRTIO_NET_OK;
+     }
 
-     return vi->ctrl->status == VIRTIO_NET_OK;
+     netdev_err(vi->dev, "CVQ command timeout, break the virtio device.");
+     virtio_break_device(vi->vdev);
+     return VIRTIO_NET_ERR;
 }
 
 static int virtnet_set_mac_address(struct net_device *dev, void *p)
@@ -3526,7 +3534,7 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
 
      /* Parameters for control virtqueue, if any */
      if (vi->has_cvq) {
-             callbacks[total_vqs - 1] = NULL;
+             callbacks[total_vqs - 1] = virtnet_cvq_done;
              names[total_vqs - 1] = "control";
      }
 
--

2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#47 lore raw

Re: [PATCH 4/4] virtio-net: sleep instead of busy waiting for cvq command

Quoted 33 lines · Xuan Zhuo
Quoted 31 lines · Jason Wang
Quoted 30 lines · Xuan Zhuo
Quoted 18 lines · Michael S. Tsirkin
Quoted 17 lines · Jason Wang
Quoted 5 lines · Xuan Zhuo
Quoted 3 lines · Jason Wang

We used to busy waiting on the cvq command this tends to be problematic since:

Quoted 8 lines
Quoted 6 lines

1) CPU could wait for ever on a buggy/malicous device
2) There's no wait to terminate the process that triggers the cvq command

So this patch switch to use virtqueue_wait_for_used() to sleep with a timeout (1s) instead of busy polling for the cvq command forever. This

I don't think that a fixed 1S is a good choice.

Well, it could be tweaked to be a little bit longer.

Quoted 7 lines
Quoted 3 lines

One way, as discussed, is to let the device advertise a timeout then the driver can validate if it's valid and use that timeout. But it needs extension to the spec.

Controlling timeout from device is a good idea, e.g. hardware devices would benefit from a shorter timeout, hypervisor devices from a longer timeout or no timeout.

Yes. That is good.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Before introducing this feature, I personally like to use "wait", rather than define a timeout.

Note that the driver still needs to validate what device advertises to avoid infinite wait.

Sorry, I didn't understand what you mean.

I meant the interface needs to carefully designed to

1) avoid device to advertise a infinite (or very long) timeout
2) driver need to have its own max timeout regardless what device advertises

Thanks

Quoted 134 lines

Thanks.

Quoted 130 lines

Thanks

Quoted 126 lines

Thanks.

Quoted 121 lines
Quoted 7 lines
Quoted 4 lines

Some of the DPUs are very
lazy for cvq handle.

Such design needs to be revisited, cvq (control path) should have a better priority or QOS than datapath.

Spec says nothing about this, so driver can't assume this either.

Quoted 3 lines
Quoted 1 line

In particular, we will also directly break the device.

It's kind of hardening for malicious devices.

ATM no amount of hardening can prevent a malicious hypervisor from blocking the guest. Recovering when a hardware device is broken would be nice but I think if we do bother then we should try harder to recover, such as by driving device reset.

Also, does your patch break surprise removal? There's no callback in this case ATM.

Quoted 97 lines
Quoted 3 lines

I think it is necessary to add a Virtio-Net parameter to allow users to define this timeout by themselves. Although I don't think this is a good way.

Very hard and unfriendly to the end users.

Thanks

Quoted 89 lines

Thanks.

Quoted 84 lines

gives the scheduler a breath and can let the process can respond to asignal. If the device doesn't respond in the timeout, break the device.

---
Changes since V1:
- break the device when timeout
- get buffer manually since the virtio core check more_used() instead

---
 drivers/net/virtio_net.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
drivers/net/virtio_net.c +16 -9
index efd9dd55828b..6a2ea64cfcb5 100644
@@ -405,6 +405,7 @@ static void disable_rx_mode_work(struct virtnet_info *vi)
      vi->rx_mode_work_enabled = false;
      spin_unlock_bh(&vi->rx_mode_lock);
 
+     virtqueue_wake_up(vi->cvq);
      flush_work(&vi->rx_mode_work);
 }
 
@@ -1497,6 +1498,11 @@ static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
      return !oom;
 }
 
+static void virtnet_cvq_done(struct virtqueue *cvq)
+{
+     virtqueue_wake_up(cvq);
+}
+
 static void skb_recv_done(struct virtqueue *rvq)
 {
      struct virtnet_info *vi = rvq->vdev->priv;
@@ -1984,6 +1990,8 @@ static int virtnet_tx_resize(struct virtnet_info *vi,
      return err;
 }
 
+static int virtnet_close(struct net_device *dev);
+
 /*
  * Send command via the control virtqueue and check status.  Commands
  * supported by the hypervisor, as indicated by feature bits, should
@@ -2026,14 +2034,14 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
      if (unlikely(!virtqueue_kick(vi->cvq)))
              return vi->ctrl->status == VIRTIO_NET_OK;
 
-     /* Spin for a response, the kick causes an ioport write, trapping
-      * into the hypervisor, so the request should be handled immediately.
-      */
-     while (!virtqueue_get_buf(vi->cvq, &tmp) &&
-            !virtqueue_is_broken(vi->cvq))
-             cpu_relax();
+     if (virtqueue_wait_for_used(vi->cvq)) {
+             virtqueue_get_buf(vi->cvq, &tmp);
+             return vi->ctrl->status == VIRTIO_NET_OK;
+     }
 
-     return vi->ctrl->status == VIRTIO_NET_OK;
+     netdev_err(vi->dev, "CVQ command timeout, break the virtio device.");
+     virtio_break_device(vi->vdev);
+     return VIRTIO_NET_ERR;
 }
 
 static int virtnet_set_mac_address(struct net_device *dev, void *p)
@@ -3526,7 +3534,7 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
 
      /* Parameters for control virtqueue, if any */
      if (vi->has_cvq) {
-             callbacks[total_vqs - 1] = NULL;
+             callbacks[total_vqs - 1] = virtnet_cvq_done;
              names[total_vqs - 1] = "control";
      }
 
--

2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

reviewer
#48 lore raw

Re: [PATCH 4/4] virtio-net: sleep instead of busy waiting for cvq command

Quoted 40 lines · Jason Wang
Quoted 39 lines · Xuan Zhuo
Quoted 36 lines · Jason Wang
Quoted 24 lines · Xuan Zhuo
Quoted 22 lines · Michael S. Tsirkin
Quoted 20 lines · Jason Wang
Quoted 5 lines · Xuan Zhuo
Quoted 3 lines · Jason Wang

We used to busy waiting on the cvq command this tends to be problematic since:

Quoted 8 lines
Quoted 6 lines

1) CPU could wait for ever on a buggy/malicous device
2) There's no wait to terminate the process that triggers the cvq command

So this patch switch to use virtqueue_wait_for_used() to sleep with a timeout (1s) instead of busy polling for the cvq command forever. This

I don't think that a fixed 1S is a good choice.

Well, it could be tweaked to be a little bit longer.

One way, as discussed, is to let the device advertise a timeout then the driver can validate if it's valid and use that timeout. But it needs extension to the spec.

Quoted 8 lines
Quoted 3 lines

Controlling timeout from device is a good idea, e.g. hardware devices would benefit from a shorter timeout, hypervisor devices from a longer timeout or no timeout.

Yes. That is good.

Before introducing this feature, I personally like to use "wait", rather than define a timeout.

Note that the driver still needs to validate what device advertises to avoid infinite wait.

Quoted 6 lines
Quoted 1 line

Sorry, I didn't understand what you mean.

I meant the interface needs to carefully designed to

1) avoid device to advertise a infinite (or very long) timeout
2) driver need to have its own max timeout regardless what device advertises

I see.

As far as I know, different operations will take different time. For example, the queues are initialized one by one when performing VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET commands. If the number of queues is large, then this time will be very long.

So we should set different timeouts for different commands.

Thanks.

Quoted 138 lines

Thanks

Quoted 134 lines

Thanks.

Quoted 130 lines

Thanks

Quoted 126 lines

Thanks.

Quoted 121 lines
Quoted 7 lines
Quoted 4 lines

Some of the DPUs are very
lazy for cvq handle.

Such design needs to be revisited, cvq (control path) should have a better priority or QOS than datapath.

Spec says nothing about this, so driver can't assume this either.

Quoted 3 lines
Quoted 1 line

In particular, we will also directly break the device.

It's kind of hardening for malicious devices.

ATM no amount of hardening can prevent a malicious hypervisor from blocking the guest. Recovering when a hardware device is broken would be nice but I think if we do bother then we should try harder to recover, such as by driving device reset.

Also, does your patch break surprise removal? There's no callback in this case ATM.

Quoted 97 lines
Quoted 3 lines

I think it is necessary to add a Virtio-Net parameter to allow users to define this timeout by themselves. Although I don't think this is a good way.

Very hard and unfriendly to the end users.

Thanks

Quoted 89 lines

Thanks.

Quoted 84 lines

gives the scheduler a breath and can let the process can respond to asignal. If the device doesn't respond in the timeout, break the device.

---
Changes since V1:
- break the device when timeout
- get buffer manually since the virtio core check more_used() instead

---
 drivers/net/virtio_net.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
drivers/net/virtio_net.c +16 -9
index efd9dd55828b..6a2ea64cfcb5 100644
@@ -405,6 +405,7 @@ static void disable_rx_mode_work(struct virtnet_info *vi)
      vi->rx_mode_work_enabled = false;
      spin_unlock_bh(&vi->rx_mode_lock);
 
+     virtqueue_wake_up(vi->cvq);
      flush_work(&vi->rx_mode_work);
 }
 
@@ -1497,6 +1498,11 @@ static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
      return !oom;
 }
 
+static void virtnet_cvq_done(struct virtqueue *cvq)
+{
+     virtqueue_wake_up(cvq);
+}
+
 static void skb_recv_done(struct virtqueue *rvq)
 {
      struct virtnet_info *vi = rvq->vdev->priv;
@@ -1984,6 +1990,8 @@ static int virtnet_tx_resize(struct virtnet_info *vi,
      return err;
 }
 
+static int virtnet_close(struct net_device *dev);
+
 /*
  * Send command via the control virtqueue and check status.  Commands
  * supported by the hypervisor, as indicated by feature bits, should
@@ -2026,14 +2034,14 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
      if (unlikely(!virtqueue_kick(vi->cvq)))
              return vi->ctrl->status == VIRTIO_NET_OK;
 
-     /* Spin for a response, the kick causes an ioport write, trapping
-      * into the hypervisor, so the request should be handled immediately.
-      */
-     while (!virtqueue_get_buf(vi->cvq, &tmp) &&
-            !virtqueue_is_broken(vi->cvq))
-             cpu_relax();
+     if (virtqueue_wait_for_used(vi->cvq)) {
+             virtqueue_get_buf(vi->cvq, &tmp);
+             return vi->ctrl->status == VIRTIO_NET_OK;
+     }
 
-     return vi->ctrl->status == VIRTIO_NET_OK;
+     netdev_err(vi->dev, "CVQ command timeout, break the virtio device.");
+     virtio_break_device(vi->vdev);
+     return VIRTIO_NET_ERR;
 }
 
 static int virtnet_set_mac_address(struct net_device *dev, void *p)
@@ -3526,7 +3534,7 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
 
      /* Parameters for control virtqueue, if any */
      if (vi->has_cvq) {
-             callbacks[total_vqs - 1] = NULL;
+             callbacks[total_vqs - 1] = virtnet_cvq_done;
              names[total_vqs - 1] = "control";
      }
 
--

2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#49 lore raw

Re: [PATCH 4/4] virtio-net: sleep instead of busy waiting for cvq command

Quoted 50 lines · Xuan Zhuo
Quoted 40 lines · Jason Wang
Quoted 39 lines · Xuan Zhuo
Quoted 36 lines · Jason Wang
Quoted 24 lines · Xuan Zhuo
Quoted 22 lines · Michael S. Tsirkin
Quoted 20 lines · Jason Wang
Quoted 5 lines · Xuan Zhuo
Quoted 3 lines · Jason Wang

We used to busy waiting on the cvq command this tends to be problematic since:

Quoted 8 lines
Quoted 6 lines

1) CPU could wait for ever on a buggy/malicous device
2) There's no wait to terminate the process that triggers the cvq command

So this patch switch to use virtqueue_wait_for_used() to sleep with a timeout (1s) instead of busy polling for the cvq command forever. This

I don't think that a fixed 1S is a good choice.

Well, it could be tweaked to be a little bit longer.

One way, as discussed, is to let the device advertise a timeout then the driver can validate if it's valid and use that timeout. But it needs extension to the spec.

Quoted 8 lines
Quoted 3 lines

Controlling timeout from device is a good idea, e.g. hardware devices would benefit from a shorter timeout, hypervisor devices from a longer timeout or no timeout.

Yes. That is good.

Before introducing this feature, I personally like to use "wait", rather than define a timeout.

Note that the driver still needs to validate what device advertises to avoid infinite wait.

Quoted 6 lines
Quoted 1 line

Sorry, I didn't understand what you mean.

I meant the interface needs to carefully designed to

1) avoid device to advertise a infinite (or very long) timeout
2) driver need to have its own max timeout regardless what device advertises

Quoted 6 lines

I see.

As far as I know, different operations will take different time. For example, the queues are initialized one by one when performing VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET commands. If the number of queues is large, then this time will be very long.

I see. This is the case even for the software backends.

Quoted 2 lines

So we should set different timeouts for different commands.

Probably but it would result in a very complex interface, the device can just choose to advertise the maximum timeout of all the commands in this case. As discussed, I think we can start a very long timeout. Is 1 minutes sufficient in this case?

Thanks

Quoted 142 lines

Thanks.

Quoted 138 lines

Thanks

Quoted 134 lines

Thanks.

Quoted 130 lines

Thanks

Quoted 126 lines

Thanks.

Quoted 121 lines
Quoted 7 lines
Quoted 4 lines

Some of the DPUs are very
lazy for cvq handle.

Such design needs to be revisited, cvq (control path) should have a better priority or QOS than datapath.

Spec says nothing about this, so driver can't assume this either.

Quoted 3 lines
Quoted 1 line

In particular, we will also directly break the device.

It's kind of hardening for malicious devices.

ATM no amount of hardening can prevent a malicious hypervisor from blocking the guest. Recovering when a hardware device is broken would be nice but I think if we do bother then we should try harder to recover, such as by driving device reset.

Also, does your patch break surprise removal? There's no callback in this case ATM.

Quoted 97 lines
Quoted 3 lines

I think it is necessary to add a Virtio-Net parameter to allow users to define this timeout by themselves. Although I don't think this is a good way.

Very hard and unfriendly to the end users.

Thanks

Quoted 89 lines

Thanks.

Quoted 84 lines

gives the scheduler a breath and can let the process can respond to asignal. If the device doesn't respond in the timeout, break the device.

---
Changes since V1:
- break the device when timeout
- get buffer manually since the virtio core check more_used() instead

---
 drivers/net/virtio_net.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
drivers/net/virtio_net.c +16 -9
index efd9dd55828b..6a2ea64cfcb5 100644
@@ -405,6 +405,7 @@ static void disable_rx_mode_work(struct virtnet_info *vi)
      vi->rx_mode_work_enabled = false;
      spin_unlock_bh(&vi->rx_mode_lock);
 
+     virtqueue_wake_up(vi->cvq);
      flush_work(&vi->rx_mode_work);
 }
 
@@ -1497,6 +1498,11 @@ static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
      return !oom;
 }
 
+static void virtnet_cvq_done(struct virtqueue *cvq)
+{
+     virtqueue_wake_up(cvq);
+}
+
 static void skb_recv_done(struct virtqueue *rvq)
 {
      struct virtnet_info *vi = rvq->vdev->priv;
@@ -1984,6 +1990,8 @@ static int virtnet_tx_resize(struct virtnet_info *vi,
      return err;
 }
 
+static int virtnet_close(struct net_device *dev);
+
 /*
  * Send command via the control virtqueue and check status.  Commands
  * supported by the hypervisor, as indicated by feature bits, should
@@ -2026,14 +2034,14 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
      if (unlikely(!virtqueue_kick(vi->cvq)))
              return vi->ctrl->status == VIRTIO_NET_OK;
 
-     /* Spin for a response, the kick causes an ioport write, trapping
-      * into the hypervisor, so the request should be handled immediately.
-      */
-     while (!virtqueue_get_buf(vi->cvq, &tmp) &&
-            !virtqueue_is_broken(vi->cvq))
-             cpu_relax();
+     if (virtqueue_wait_for_used(vi->cvq)) {
+             virtqueue_get_buf(vi->cvq, &tmp);
+             return vi->ctrl->status == VIRTIO_NET_OK;
+     }
 
-     return vi->ctrl->status == VIRTIO_NET_OK;
+     netdev_err(vi->dev, "CVQ command timeout, break the virtio device.");
+     virtio_break_device(vi->vdev);
+     return VIRTIO_NET_ERR;
 }
 
 static int virtnet_set_mac_address(struct net_device *dev, void *p)
@@ -3526,7 +3534,7 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
 
      /* Parameters for control virtqueue, if any */
      if (vi->has_cvq) {
-             callbacks[total_vqs - 1] = NULL;
+             callbacks[total_vqs - 1] = virtnet_cvq_done;
              names[total_vqs - 1] = "control";
      }
 
--

2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

reviewer
#50 lore raw

Re: [PATCH 4/4] virtio-net: sleep instead of busy waiting for cvq command

Quoted 61 lines · Jason Wang
Quoted 50 lines · Xuan Zhuo
Quoted 40 lines · Jason Wang
Quoted 39 lines · Xuan Zhuo
Quoted 36 lines · Jason Wang
Quoted 24 lines · Xuan Zhuo
Quoted 22 lines · Michael S. Tsirkin
Quoted 20 lines · Jason Wang
Quoted 5 lines · Xuan Zhuo
Quoted 3 lines · Jason Wang

We used to busy waiting on the cvq command this tends to be problematic since:

Quoted 8 lines
Quoted 6 lines

1) CPU could wait for ever on a buggy/malicous device
2) There's no wait to terminate the process that triggers the cvq command

So this patch switch to use virtqueue_wait_for_used() to sleep with a timeout (1s) instead of busy polling for the cvq command forever. This

I don't think that a fixed 1S is a good choice.

Well, it could be tweaked to be a little bit longer.

One way, as discussed, is to let the device advertise a timeout then the driver can validate if it's valid and use that timeout. But it needs extension to the spec.

Quoted 8 lines
Quoted 3 lines

Controlling timeout from device is a good idea, e.g. hardware devices would benefit from a shorter timeout, hypervisor devices from a longer timeout or no timeout.

Yes. That is good.

Before introducing this feature, I personally like to use "wait", rather than define a timeout.

Note that the driver still needs to validate what device advertises to avoid infinite wait.

Quoted 6 lines
Quoted 1 line

Sorry, I didn't understand what you mean.

I meant the interface needs to carefully designed to

1) avoid device to advertise a infinite (or very long) timeout
2) driver need to have its own max timeout regardless what device advertises

Quoted 6 lines

I see.

As far as I know, different operations will take different time. For example, the queues are initialized one by one when performing VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET commands. If the number of queues is large, then this time will be very long.

I see. This is the case even for the software backends.

Quoted 1 line
Quoted 6 lines
Quoted 1 line

So we should set different timeouts for different commands.

Probably but it would result in a very complex interface, the device can just choose to advertise the maximum timeout of all the commands in this case. As discussed, I think we can start a very long timeout. Is 1 minutes sufficient in this case?

For now, 1 minutes are safe.

Thanks.

Quoted 146 lines

Thanks

Quoted 142 lines

Thanks.

Quoted 138 lines

Thanks

Quoted 134 lines

Thanks.

Quoted 130 lines

Thanks

Quoted 126 lines

Thanks.

Quoted 121 lines
Quoted 7 lines
Quoted 4 lines

Some of the DPUs are very
lazy for cvq handle.

Such design needs to be revisited, cvq (control path) should have a better priority or QOS than datapath.

Spec says nothing about this, so driver can't assume this either.

Quoted 3 lines
Quoted 1 line

In particular, we will also directly break the device.

It's kind of hardening for malicious devices.

ATM no amount of hardening can prevent a malicious hypervisor from blocking the guest. Recovering when a hardware device is broken would be nice but I think if we do bother then we should try harder to recover, such as by driving device reset.

Also, does your patch break surprise removal? There's no callback in this case ATM.

Quoted 97 lines
Quoted 3 lines

I think it is necessary to add a Virtio-Net parameter to allow users to define this timeout by themselves. Although I don't think this is a good way.

Very hard and unfriendly to the end users.

Thanks

Quoted 89 lines

Thanks.

Quoted 84 lines

gives the scheduler a breath and can let the process can respond to asignal. If the device doesn't respond in the timeout, break the device.

---
Changes since V1:
- break the device when timeout
- get buffer manually since the virtio core check more_used() instead

---
 drivers/net/virtio_net.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
drivers/net/virtio_net.c +16 -9
index efd9dd55828b..6a2ea64cfcb5 100644
@@ -405,6 +405,7 @@ static void disable_rx_mode_work(struct virtnet_info *vi)
      vi->rx_mode_work_enabled = false;
      spin_unlock_bh(&vi->rx_mode_lock);
 
+     virtqueue_wake_up(vi->cvq);
      flush_work(&vi->rx_mode_work);
 }
 
@@ -1497,6 +1498,11 @@ static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
      return !oom;
 }
 
+static void virtnet_cvq_done(struct virtqueue *cvq)
+{
+     virtqueue_wake_up(cvq);
+}
+
 static void skb_recv_done(struct virtqueue *rvq)
 {
      struct virtnet_info *vi = rvq->vdev->priv;
@@ -1984,6 +1990,8 @@ static int virtnet_tx_resize(struct virtnet_info *vi,
      return err;
 }
 
+static int virtnet_close(struct net_device *dev);
+
 /*
  * Send command via the control virtqueue and check status.  Commands
  * supported by the hypervisor, as indicated by feature bits, should
@@ -2026,14 +2034,14 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
      if (unlikely(!virtqueue_kick(vi->cvq)))
              return vi->ctrl->status == VIRTIO_NET_OK;
 
-     /* Spin for a response, the kick causes an ioport write, trapping
-      * into the hypervisor, so the request should be handled immediately.
-      */
-     while (!virtqueue_get_buf(vi->cvq, &tmp) &&
-            !virtqueue_is_broken(vi->cvq))
-             cpu_relax();
+     if (virtqueue_wait_for_used(vi->cvq)) {
+             virtqueue_get_buf(vi->cvq, &tmp);
+             return vi->ctrl->status == VIRTIO_NET_OK;
+     }
 
-     return vi->ctrl->status == VIRTIO_NET_OK;
+     netdev_err(vi->dev, "CVQ command timeout, break the virtio device.");
+     virtio_break_device(vi->vdev);
+     return VIRTIO_NET_ERR;
 }
 
 static int virtnet_set_mac_address(struct net_device *dev, void *p)
@@ -3526,7 +3534,7 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
 
      /* Parameters for control virtqueue, if any */
      if (vi->has_cvq) {
-             callbacks[total_vqs - 1] = NULL;
+             callbacks[total_vqs - 1] = virtnet_cvq_done;
              names[total_vqs - 1] = "control";
      }
 
--

2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

reviewer
#51 lore raw

Re: [PATCH 4/4] virtio-net: sleep instead of busy waiting for cvq command

In reply to Jason Wang
Quoted 24 lines · Jason Wang
Quoted 5 lines · Xuan Zhuo
Quoted 3 lines · Jason Wang

We used to busy waiting on the cvq command this tends to be problematic since:

Quoted 8 lines
Quoted 6 lines

1) CPU could wait for ever on a buggy/malicous device
2) There's no wait to terminate the process that triggers the cvq command

So this patch switch to use virtqueue_wait_for_used() to sleep with a timeout (1s) instead of busy polling for the cvq command forever. This

I don't think that a fixed 1S is a good choice.

Well, it could be tweaked to be a little bit longer.

One way, as discussed, is to let the device advertise a timeout then the driver can validate if it's valid and use that timeout. But it needs extension to the spec.

Quoted 2 lines

Some of the DPUs are very
lazy for cvq handle.

Quoted 6 lines

Such design needs to be revisited, cvq (control path) should have a better priority or QOS than datapath.

Quoted 1 line

In particular, we will also directly break the device.

It's kind of hardening for malicious devices.

Just based on timeout, it is judged that it is a malicious device. I think it is too arbitrary.

Thanks.

Quoted 99 lines
Quoted 4 lines

I think it is necessary to add a Virtio-Net parameter to allow users to define this timeout by themselves. Although I don't think this is a good way.

Very hard and unfriendly to the end users.

Thanks

Quoted 89 lines

Thanks.

Quoted 84 lines

gives the scheduler a breath and can let the process can respond to asignal. If the device doesn't respond in the timeout, break the device.

---
Changes since V1:
- break the device when timeout
- get buffer manually since the virtio core check more_used() instead

---
 drivers/net/virtio_net.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
drivers/net/virtio_net.c +16 -9
index efd9dd55828b..6a2ea64cfcb5 100644
@@ -405,6 +405,7 @@ static void disable_rx_mode_work(struct virtnet_info *vi)
      vi->rx_mode_work_enabled = false;
      spin_unlock_bh(&vi->rx_mode_lock);
 
+     virtqueue_wake_up(vi->cvq);
      flush_work(&vi->rx_mode_work);
 }
 
@@ -1497,6 +1498,11 @@ static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
      return !oom;
 }
 
+static void virtnet_cvq_done(struct virtqueue *cvq)
+{
+     virtqueue_wake_up(cvq);
+}
+
 static void skb_recv_done(struct virtqueue *rvq)
 {
      struct virtnet_info *vi = rvq->vdev->priv;
@@ -1984,6 +1990,8 @@ static int virtnet_tx_resize(struct virtnet_info *vi,
      return err;
 }
 
+static int virtnet_close(struct net_device *dev);
+
 /*
  * Send command via the control virtqueue and check status.  Commands
  * supported by the hypervisor, as indicated by feature bits, should
@@ -2026,14 +2034,14 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
      if (unlikely(!virtqueue_kick(vi->cvq)))
              return vi->ctrl->status == VIRTIO_NET_OK;
 
-     /* Spin for a response, the kick causes an ioport write, trapping
-      * into the hypervisor, so the request should be handled immediately.
-      */
-     while (!virtqueue_get_buf(vi->cvq, &tmp) &&
-            !virtqueue_is_broken(vi->cvq))
-             cpu_relax();
+     if (virtqueue_wait_for_used(vi->cvq)) {
+             virtqueue_get_buf(vi->cvq, &tmp);
+             return vi->ctrl->status == VIRTIO_NET_OK;
+     }
 
-     return vi->ctrl->status == VIRTIO_NET_OK;
+     netdev_err(vi->dev, "CVQ command timeout, break the virtio device.");
+     virtio_break_device(vi->vdev);
+     return VIRTIO_NET_ERR;
 }
 
 static int virtnet_set_mac_address(struct net_device *dev, void *p)
@@ -3526,7 +3534,7 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
 
      /* Parameters for control virtqueue, if any */
      if (vi->has_cvq) {
-             callbacks[total_vqs - 1] = NULL;
+             callbacks[total_vqs - 1] = virtnet_cvq_done;
              names[total_vqs - 1] = "control";
      }
 
--

2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#52 lore raw

Re: [PATCH 4/4] virtio-net: sleep instead of busy waiting for cvq command

Quoted 29 lines · Xuan Zhuo
Quoted 27 lines · Jason Wang
Quoted 5 lines · Xuan Zhuo
Quoted 3 lines · Jason Wang

We used to busy waiting on the cvq command this tends to be problematic since:

Quoted 8 lines
Quoted 6 lines

1) CPU could wait for ever on a buggy/malicous device
2) There's no wait to terminate the process that triggers the cvq command

So this patch switch to use virtqueue_wait_for_used() to sleep with a timeout (1s) instead of busy polling for the cvq command forever. This

I don't think that a fixed 1S is a good choice.

Well, it could be tweaked to be a little bit longer.

One way, as discussed, is to let the device advertise a timeout then the driver can validate if it's valid and use that timeout. But it needs extension to the spec.

Quoted 2 lines

Some of the DPUs are very
lazy for cvq handle.

Such design needs to be revisited, cvq (control path) should have a better priority or QOS than datapath.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

In particular, we will also directly break the device.

It's kind of hardening for malicious devices.

Just based on timeout, it is judged that it is a malicious device. I think it is too arbitrary.

Drivers have very little information to make the decision. So it's really a balance.

We can start with a very long timeout like 10 minutes. Otherwise a buggy/malicious device will block a lot of important things (reboot, modprobe) even if the scheduler is still functional.

Thanks

Quoted 104 lines

Thanks.

Quoted 99 lines
Quoted 4 lines

I think it is necessary to add a Virtio-Net parameter to allow users to define this timeout by themselves. Although I don't think this is a good way.

Very hard and unfriendly to the end users.

Thanks

Quoted 89 lines

Thanks.

Quoted 84 lines

gives the scheduler a breath and can let the process can respond to asignal. If the device doesn't respond in the timeout, break the device.

---
Changes since V1:
- break the device when timeout
- get buffer manually since the virtio core check more_used() instead

---
 drivers/net/virtio_net.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
drivers/net/virtio_net.c +16 -9
index efd9dd55828b..6a2ea64cfcb5 100644
@@ -405,6 +405,7 @@ static void disable_rx_mode_work(struct virtnet_info *vi)
      vi->rx_mode_work_enabled = false;
      spin_unlock_bh(&vi->rx_mode_lock);
 
+     virtqueue_wake_up(vi->cvq);
      flush_work(&vi->rx_mode_work);
 }
 
@@ -1497,6 +1498,11 @@ static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
      return !oom;
 }
 
+static void virtnet_cvq_done(struct virtqueue *cvq)
+{
+     virtqueue_wake_up(cvq);
+}
+
 static void skb_recv_done(struct virtqueue *rvq)
 {
      struct virtnet_info *vi = rvq->vdev->priv;
@@ -1984,6 +1990,8 @@ static int virtnet_tx_resize(struct virtnet_info *vi,
      return err;
 }
 
+static int virtnet_close(struct net_device *dev);
+
 /*
  * Send command via the control virtqueue and check status.  Commands
  * supported by the hypervisor, as indicated by feature bits, should
@@ -2026,14 +2034,14 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
      if (unlikely(!virtqueue_kick(vi->cvq)))
              return vi->ctrl->status == VIRTIO_NET_OK;
 
-     /* Spin for a response, the kick causes an ioport write, trapping
-      * into the hypervisor, so the request should be handled immediately.
-      */
-     while (!virtqueue_get_buf(vi->cvq, &tmp) &&
-            !virtqueue_is_broken(vi->cvq))
-             cpu_relax();
+     if (virtqueue_wait_for_used(vi->cvq)) {
+             virtqueue_get_buf(vi->cvq, &tmp);
+             return vi->ctrl->status == VIRTIO_NET_OK;
+     }
 
-     return vi->ctrl->status == VIRTIO_NET_OK;
+     netdev_err(vi->dev, "CVQ command timeout, break the virtio device.");
+     virtio_break_device(vi->vdev);
+     return VIRTIO_NET_ERR;
 }
 
 static int virtnet_set_mac_address(struct net_device *dev, void *p)
@@ -3526,7 +3534,7 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
 
      /* Parameters for control virtqueue, if any */
      if (vi->has_cvq) {
-             callbacks[total_vqs - 1] = NULL;
+             callbacks[total_vqs - 1] = virtnet_cvq_done;
              names[total_vqs - 1] = "control";
      }
 
--

2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

reviewer
#53 lore raw

Re: [PATCH 4/4] virtio-net: sleep instead of busy waiting for cvq command

Quoted 37 lines · Jason Wang
Quoted 29 lines · Xuan Zhuo
Quoted 27 lines · Jason Wang
Quoted 5 lines · Xuan Zhuo
Quoted 3 lines · Jason Wang

We used to busy waiting on the cvq command this tends to be problematic since:

Quoted 8 lines
Quoted 6 lines

1) CPU could wait for ever on a buggy/malicous device
2) There's no wait to terminate the process that triggers the cvq command

So this patch switch to use virtqueue_wait_for_used() to sleep with a timeout (1s) instead of busy polling for the cvq command forever. This

I don't think that a fixed 1S is a good choice.

Well, it could be tweaked to be a little bit longer.

One way, as discussed, is to let the device advertise a timeout then the driver can validate if it's valid and use that timeout. But it needs extension to the spec.

Quoted 2 lines

Some of the DPUs are very
lazy for cvq handle.

Such design needs to be revisited, cvq (control path) should have a better priority or QOS than datapath.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

In particular, we will also directly break the device.

It's kind of hardening for malicious devices.

Just based on timeout, it is judged that it is a malicious device. I think it is too arbitrary.

Quoted 6 lines

Drivers have very little information to make the decision. So it's really a balance.

We can start with a very long timeout like 10 minutes. Otherwise a buggy/malicious device will block a lot of important things (reboot, modprobe) even if the scheduler is still functional.

Relatively speaking, starting from a 1min+ timeout, I think it is safe.

Thanks.

Quoted 108 lines

Thanks

Quoted 104 lines

Thanks.

Quoted 99 lines
Quoted 4 lines

I think it is necessary to add a Virtio-Net parameter to allow users to define this timeout by themselves. Although I don't think this is a good way.

Very hard and unfriendly to the end users.

Thanks

Quoted 89 lines

Thanks.

Quoted 84 lines

gives the scheduler a breath and can let the process can respond to asignal. If the device doesn't respond in the timeout, break the device.

---
Changes since V1:
- break the device when timeout
- get buffer manually since the virtio core check more_used() instead

---
 drivers/net/virtio_net.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
drivers/net/virtio_net.c +16 -9
index efd9dd55828b..6a2ea64cfcb5 100644
@@ -405,6 +405,7 @@ static void disable_rx_mode_work(struct virtnet_info *vi)
      vi->rx_mode_work_enabled = false;
      spin_unlock_bh(&vi->rx_mode_lock);
 
+     virtqueue_wake_up(vi->cvq);
      flush_work(&vi->rx_mode_work);
 }
 
@@ -1497,6 +1498,11 @@ static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
      return !oom;
 }
 
+static void virtnet_cvq_done(struct virtqueue *cvq)
+{
+     virtqueue_wake_up(cvq);
+}
+
 static void skb_recv_done(struct virtqueue *rvq)
 {
      struct virtnet_info *vi = rvq->vdev->priv;
@@ -1984,6 +1990,8 @@ static int virtnet_tx_resize(struct virtnet_info *vi,
      return err;
 }
 
+static int virtnet_close(struct net_device *dev);
+
 /*
  * Send command via the control virtqueue and check status.  Commands
  * supported by the hypervisor, as indicated by feature bits, should
@@ -2026,14 +2034,14 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
      if (unlikely(!virtqueue_kick(vi->cvq)))
              return vi->ctrl->status == VIRTIO_NET_OK;
 
-     /* Spin for a response, the kick causes an ioport write, trapping
-      * into the hypervisor, so the request should be handled immediately.
-      */
-     while (!virtqueue_get_buf(vi->cvq, &tmp) &&
-            !virtqueue_is_broken(vi->cvq))
-             cpu_relax();
+     if (virtqueue_wait_for_used(vi->cvq)) {
+             virtqueue_get_buf(vi->cvq, &tmp);
+             return vi->ctrl->status == VIRTIO_NET_OK;
+     }
 
-     return vi->ctrl->status == VIRTIO_NET_OK;
+     netdev_err(vi->dev, "CVQ command timeout, break the virtio device.");
+     virtio_break_device(vi->vdev);
+     return VIRTIO_NET_ERR;
 }
 
 static int virtnet_set_mac_address(struct net_device *dev, void *p)
@@ -3526,7 +3534,7 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
 
      /* Parameters for control virtqueue, if any */
      if (vi->has_cvq) {
-             callbacks[total_vqs - 1] = NULL;
+             callbacks[total_vqs - 1] = virtnet_cvq_done;
              names[total_vqs - 1] = "control";
      }
 
--

2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization