Skip to contentSkip to conversation

virtio-net: add timeout for virtnet_send_command()

RFC PATCH New Shannon Nelson 1 participant 1 message Jul 18, 2023

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 virtio-net: add timeout for virtnet_send_command()

When trying to talk to a device that has gone out to lunch, the virtnet_send_command() will sit and spin forever, causing a soft lockup and eventually crashing the kernel. Add a limit to the spin and return false if we hit the timeout. The 2 second time limit seems a bit arbitrary, but a reasonable place to start.

This is a little more brute force than Jason's suggestions in [1], but at least prevents the soft lockups and eventual kernel crash that we were seeing in testing.

[1]: https://lore.kernel.org/netdev/[email protected]/

Fixes 2a41f71d3bd9 ("virtio_net: Add a virtqueue for outbound control commands")
---
 drivers/net/virtio_net.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/net/virtio_net.c +9 -2
index 0db14f6b87d3..c3bf1c9f3244 100644
@@ -2264,6 +2264,8 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
 {
 	struct scatterlist *sgs[4], hdr, stat;
 	unsigned out_num = 0, tmp;
+	unsigned long deadline;
+	bool timeout;
 	int ret;
 
 	/* Caller should know better */
@@ -2297,11 +2299,16 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
 	/* Spin for a response, the kick causes an ioport write, trapping
 	 * into the hypervisor, so the request should be handled immediately.
 	 */
+	deadline = jiffies + 2 * HZ;
+	timeout = false;
 	while (!virtqueue_get_buf(vi->cvq, &tmp) &&
-	       !virtqueue_is_broken(vi->cvq))
+	       !virtqueue_is_broken(vi->cvq) &&
+	       !timeout) {
 		cpu_relax();
+		timeout = time_after(jiffies, deadline);
+	}
 
-	return vi->ctrl->status == VIRTIO_NET_OK;
+	return vi->ctrl->status == VIRTIO_NET_OK && !timeout;
 }
 
 static int virtnet_set_mac_address(struct net_device *dev, void *p)
Signature · 1 line
2.17.1