git: 1a10bff4ce47 - stable/15 - virtio_random: defer first enqueue until after DRIVER_OK

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

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

commit 1a10bff4ce4772bb1611c286df8728cace5dfb65
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2026-11-05 21:53:30 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2026-11-09 03:12:24 +0000

    virtio_random: defer first enqueue until after DRIVER_OK
    
    According to the virtio spec:
    
    > The driver MUST NOT send any buffer available notifications to the
    > device before setting DRIVER_OK.
    
    DRIVER_OK isn't set until after attach completes, so defer the first
    enqueue.  We haven't found any problems as a result of this to the best
    of my knowledge: q35 and other devices that we *have* been used with
    will apparently check when we go DRIVER_OK.
    
    Tested with q35 virtio-random, which still works.
    
    Reported by:    llm-postcr
    Reviewed by:    kfv, markj
    Fixes:  f1c5a2e3a62505 ("virtio_random: Pipeline fetching the data")
    Sponsored by:   Stormshield
    Sponsored by:   Klara, Inc.
    
    (cherry picked from commit 12f90bbd3029fedea93e9bb0b1f02c3afafe237b)
---
 sys/dev/virtio/random/virtio_random.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/sys/dev/virtio/random/virtio_random.c b/sys/dev/virtio/random/virtio_random.c
index 774221bd2bfd..75addaaf322f 100644
--- a/sys/dev/virtio/random/virtio_random.c
+++ b/sys/dev/virtio/random/virtio_random.c
@@ -47,6 +47,8 @@
 #include <dev/virtio/virtio.h>
 #include <dev/virtio/virtqueue.h>
 
+#include "virtio_if.h"
+
 struct vtrnd_softc {
 	device_t		 vtrnd_dev;
 	uint64_t		 vtrnd_features;
@@ -69,6 +71,7 @@ static int	vtrnd_setup_features(struct vtrnd_softc *);
 static int	vtrnd_alloc_virtqueue(struct vtrnd_softc *);
 static int	vtrnd_harvest(struct vtrnd_softc *, void *, size_t *);
 static void	vtrnd_enqueue(struct vtrnd_softc *sc);
+static int	vtrnd_attach_completed(device_t);
 static unsigned	vtrnd_read(void *, unsigned);
 
 #define VTRND_FEATURES	0
@@ -93,6 +96,9 @@ static device_method_t vtrnd_methods[] = {
 	DEVMETHOD(device_detach,	vtrnd_detach),
 	DEVMETHOD(device_shutdown,	vtrnd_shutdown),
 
+	/* Virtio methods. */
+	DEVMETHOD(virtio_attach_completed,	vtrnd_attach_completed),
+
 	DEVMETHOD_END
 };
 
@@ -181,8 +187,6 @@ vtrnd_attach(device_t dev)
 	sc->inactive = false;
 	random_source_register(&random_vtrnd);
 
-	vtrnd_enqueue(sc);
-
 fail:
 	if (error != 0) {
 		/* Parent will free the virtqueue. */
@@ -340,3 +344,13 @@ vtrnd_read(void *buf, unsigned usz)
 
 	return (sz);
 }
+
+static int
+vtrnd_attach_completed(device_t dev)
+{
+	struct vtrnd_softc *sc;
+
+	sc = device_get_softc(dev);
+	vtrnd_enqueue(sc);
+	return (0);
+}