git: 41aa43fd5ad1 - stable/14 - virtio_random: avoid using vtrnd_detach in the failure path

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

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

commit 41aa43fd5ad1d8b0c8b7e17a6e419279e8e5ddaa
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:11 +0000

    virtio_random: avoid using vtrnd_detach in the failure path
    
    On the off chance that we discover more than one virtio-random device,
    the second one will deregister the global instance or trip an invariant
    when it fails to attach and calls vtrnd_detach().
    
    We don't have that many resources to free, so just inline those.  This
    is purely a hygiene thing, we haven't seen an instance of this in the
    wild (to my knowledge).
    
    Reviewed by:    bnovkov, kfv
    Sponsored by:   Stormshield
    Sponsored by:   Klara, Inc.
    
    (cherry picked from commit 26ba71f73e8914ecd9e5b1e7bb864fee41158d04)
---
 sys/dev/virtio/random/virtio_random.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/sys/dev/virtio/random/virtio_random.c b/sys/dev/virtio/random/virtio_random.c
index 4a661ad0c19d..86c8b52444ec 100644
--- a/sys/dev/virtio/random/virtio_random.c
+++ b/sys/dev/virtio/random/virtio_random.c
@@ -185,8 +185,15 @@ vtrnd_attach(device_t dev)
 	vtrnd_enqueue(sc);
 
 fail:
-	if (error)
-		vtrnd_detach(dev);
+	if (error != 0) {
+		/* Parent will free the virtqueue. */
+		if (g_vtrnd_softc == sc) {
+			atomic_store_explicit(&g_vtrnd_softc, NULL,
+			    memory_order_release);
+		}
+		sglist_free(sc->vtrnd_sg);
+		zfree(sc->vtrnd_value, M_DEVBUF);
+	}
 
 	return (error);
 }