git: 601d2a9a31d1 - stable/14 - virtio_random: stop on detach, don't poll

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Mon, 09 Nov 2026 03:14:23 UTC
The branch stable/14 has been updated by kevans:

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

commit 601d2a9a31d1a6253673551e403e7e4c84a6be67
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2026-11-04 21:31:31 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2026-11-09 03:14:12 +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.
    
    (cherry picked from commit cce522853aae3da45b131c2522dd2510edf083e7)
---
 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 86c8b52444ec..0cffcc3962e6 100644
--- a/sys/dev/virtio/random/virtio_random.c
+++ b/sys/dev/virtio/random/virtio_random.c
@@ -202,7 +202,7 @@ static int
 vtrnd_detach(device_t dev)
 {
 	struct vtrnd_softc *sc;
-	uint32_t rdlen;
+	int last;
 
 	sc = device_get_softc(dev);
 	KASSERT(
@@ -216,8 +216,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);