git: cce522853aae - main - virtio_random: stop on detach, don't poll

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Wed, 04 Nov 2026 21:34:33 UTC
The branch main has been updated by kevans:

URL: https://cgit.FreeBSD.org/src/commit/?id=cce522853aae3da45b131c2522dd2510edf083e7

commit cce522853aae3da45b131c2522dd2510edf083e7
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2026-11-04 21:31:31 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2026-11-04 21:33:33 +0000

    virtio_random: stop on detach, don't poll
    
    If the device is hung, then polling will block the detach and hang us.
    Stop it and drain the queue instead.  This fixes a hang on detach in
    some circumstances without the upcoming virtio_pci_modern fix.
    
    Reviewed by:    bnovkov, kfv
    Sponsored by:   Stormshield
    Sponsored by:   Klara, Inc.
    Differential Revision:  https://reviews.freebsd.org/D61051
---
 sys/dev/virtio/random/virtio_random.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/sys/dev/virtio/random/virtio_random.c b/sys/dev/virtio/random/virtio_random.c
index a253db5535dc..774221bd2bfd 100644
--- a/sys/dev/virtio/random/virtio_random.c
+++ b/sys/dev/virtio/random/virtio_random.c
@@ -201,7 +201,7 @@ static int
 vtrnd_detach(device_t dev)
 {
 	struct vtrnd_softc *sc;
-	uint32_t rdlen;
+	int last;
 
 	sc = device_get_softc(dev);
 	KASSERT(
@@ -215,8 +215,14 @@ vtrnd_detach(device_t dev)
 	}
 	random_source_deregister(&random_vtrnd);
 
-	/* clear the queue */
-	virtqueue_poll(sc->vtrnd_vq, &rdlen);
+	/*
+	 * Just reset the device; if it's hanging, for some reason, then polling
+	 * may put us in a bad position.
+	 */
+	virtio_stop(dev);
+	last = 0;
+	while (virtqueue_drain(sc->vtrnd_vq, &last) != NULL)
+		;
 
 	atomic_store_explicit(&g_vtrnd_softc, NULL, memory_order_release);
 	sglist_free(sc->vtrnd_sg);