diff mbox series

[RFC,net] virtio-net: add timeout for virtnet_send_command()

Message ID 20230718190920.53544-1-shannon.nelson@amd.com (mailing list archive)
State RFC
Delegated to: Netdev Maintainers
Headers show
Series [RFC,net] virtio-net: add timeout for virtnet_send_command() | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1342 this patch: 1342
netdev/cc_maintainers fail 2 blamed authors not CCed: rusty@rustcorp.com.au alex.williamson@hp.com; 5 maintainers not CCed: rusty@rustcorp.com.au alex.williamson@hp.com pabeni@redhat.com xuanzhuo@linux.alibaba.com edumazet@google.com
netdev/build_clang success Errors and warnings before: 1365 this patch: 1365
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 1365 this patch: 1365
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 26 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Nelson, Shannon July 18, 2023, 7:09 p.m. UTC
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/20230524081842.3060-1-jasowang@redhat.com/

Fixes: 2a41f71d3bd9 ("virtio_net: Add a virtqueue for outbound control commands")
Signed-off-by: Shannon Nelson <shannon.nelson@amd.com>
---
 drivers/net/virtio_net.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index 0db14f6b87d3..c3bf1c9f3244 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -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)