git: 26ba71f73e89 - main - virtio_random: avoid using vtrnd_detach in the failure path

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

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

commit 26ba71f73e8914ecd9e5b1e7bb864fee41158d04
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: 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.
    Differential Revision:  https://reviews.freebsd.org/D61050
---
 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 3f30c8b68f4c..a253db5535dc 100644
--- a/sys/dev/virtio/random/virtio_random.c
+++ b/sys/dev/virtio/random/virtio_random.c
@@ -184,8 +184,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);
 }