git: 45819a7c26d3 - stable/15 - virtio_random: avoid using vtrnd_detach in the failure path

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

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

commit 45819a7c26d376730404caafba9f13b1a70185b5
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:12:06 +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 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);
 }