Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev Added flags to store status of attaching a virtio de...



details:   https://anonhg.NetBSD.org/src/rev/6cd6773c8f9c
branches:  trunk
changeset: 374089:6cd6773c8f9c
user:      yamaguchi <yamaguchi%NetBSD.org@localhost>
date:      Fri Mar 31 07:34:26 2023 +0000

description:
Added flags to store status of attaching a virtio device

This prevents a panic on reboot after a virtio device had called
virtio_child_attach_failed().

diffstat:

 sys/dev/pci/virtio.c         |  31 ++++++++++++++++++++-----------
 sys/dev/pci/virtio_pci.c     |  14 ++++++--------
 sys/dev/pci/virtiovar.h      |  11 ++++++-----
 sys/dev/virtio/virtio_mmio.c |  15 +++++++--------
 4 files changed, 39 insertions(+), 32 deletions(-)

diffs (212 lines):

diff -r b02d7ba3d2c4 -r 6cd6773c8f9c sys/dev/pci/virtio.c
--- a/sys/dev/pci/virtio.c      Fri Mar 31 07:31:48 2023 +0000
+++ b/sys/dev/pci/virtio.c      Fri Mar 31 07:34:26 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: virtio.c,v 1.73 2023/03/31 07:31:48 yamaguchi Exp $    */
+/*     $NetBSD: virtio.c,v 1.74 2023/03/31 07:34:26 yamaguchi Exp $    */
 
 /*
  * Copyright (c) 2020 The NetBSD Foundation, Inc.
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: virtio.c,v 1.73 2023/03/31 07:31:48 yamaguchi Exp $");
+__KERNEL_RCSID(0, "$NetBSD: virtio.c,v 1.74 2023/03/31 07:34:26 yamaguchi Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -1327,6 +1327,9 @@ virtio_child_attach_start(struct virtio_
 {
        char buf[1024];
 
+       KASSERT(sc->sc_child == NULL);
+       KASSERT(!ISSET(sc->sc_child_flags, VIRTIO_CHILD_DETACHED));
+
        sc->sc_child = child;
        sc->sc_ipl = ipl;
 
@@ -1359,7 +1362,6 @@ virtio_child_attach_finish(struct virtio
        }
 #endif
 
-       sc->sc_finished_called = true;
 
        sc->sc_vqs = vqs;
        sc->sc_nvqs = nvqs;
@@ -1402,6 +1404,7 @@ virtio_child_attach_finish(struct virtio
                }
        }
 
+       SET(sc->sc_child_flags, VIRTIO_CHILD_ATTACH_FINISHED);
        virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER_OK);
        return 0;
 
@@ -1420,7 +1423,11 @@ fail:
 void
 virtio_child_detach(struct virtio_softc *sc)
 {
-       sc->sc_child = NULL;
+
+       /* already detached */
+       if (ISSET(sc->sc_child_flags, VIRTIO_CHILD_DETACHED))
+               return;
+
        sc->sc_vqs = NULL;
 
        virtio_device_reset(sc);
@@ -1431,6 +1438,8 @@ virtio_child_detach(struct virtio_softc 
                softint_disestablish(sc->sc_soft_ih);
                sc->sc_soft_ih = NULL;
        }
+
+       SET(sc->sc_child_flags, VIRTIO_CHILD_DETACHED);
 }
 
 void
@@ -1440,7 +1449,7 @@ virtio_child_attach_failed(struct virtio
 
        virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_FAILED);
 
-       sc->sc_child = VIRTIO_CHILD_FAILED;
+       SET(sc->sc_child_flags, VIRTIO_CHILD_ATTACH_FAILED);
 }
 
 bus_dma_tag_t
@@ -1476,19 +1485,19 @@ virtio_attach_failed(struct virtio_softc
        if (sc->sc_childdevid == 0)
                return 1;
 
+       if (ISSET(sc->sc_child_flags, VIRTIO_CHILD_ATTACH_FAILED)) {
+               aprint_error_dev(self, "virtio configuration failed\n");
+               return 1;
+       }
+
        if (sc->sc_child == NULL) {
                aprint_error_dev(self,
                    "no matching child driver; not configured\n");
                return 1;
        }
 
-       if (sc->sc_child == VIRTIO_CHILD_FAILED) {
-               aprint_error_dev(self, "virtio configuration failed\n");
-               return 1;
-       }
-
        /* sanity check */
-       if (!sc->sc_finished_called) {
+       if (!ISSET(sc->sc_child_flags, VIRTIO_CHILD_ATTACH_FINISHED)) {
                aprint_error_dev(self, "virtio internal error, child driver "
                    "signaled OK but didn't initialize interrupts\n");
                return 1;
diff -r b02d7ba3d2c4 -r 6cd6773c8f9c sys/dev/pci/virtio_pci.c
--- a/sys/dev/pci/virtio_pci.c  Fri Mar 31 07:31:48 2023 +0000
+++ b/sys/dev/pci/virtio_pci.c  Fri Mar 31 07:34:26 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: virtio_pci.c,v 1.39 2023/03/23 03:27:48 yamaguchi Exp $ */
+/* $NetBSD: virtio_pci.c,v 1.40 2023/03/31 07:34:26 yamaguchi Exp $ */
 
 /*
  * Copyright (c) 2020 The NetBSD Foundation, Inc.
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: virtio_pci.c,v 1.39 2023/03/23 03:27:48 yamaguchi Exp $");
+__KERNEL_RCSID(0, "$NetBSD: virtio_pci.c,v 1.40 2023/03/31 07:34:26 yamaguchi Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -329,14 +329,12 @@ virtio_pci_detach(device_t self, int fla
        struct virtio_softc * const sc = &psc->sc_sc;
        int r;
 
-       if (sc->sc_child != NULL) {
-               r = config_detach(sc->sc_child, flags);
-               if (r)
-                       return r;
-       }
+       r = config_detach_children(self, flags);
+       if (r != 0)
+               return r;
 
        /* Check that child detached properly */
-       KASSERT(sc->sc_child == NULL);
+       KASSERT(ISSET(sc->sc_child_flags, VIRTIO_CHILD_DETACHED));
        KASSERT(sc->sc_vqs == NULL);
        KASSERT(psc->sc_ihs_num == 0);
 
diff -r b02d7ba3d2c4 -r 6cd6773c8f9c sys/dev/pci/virtiovar.h
--- a/sys/dev/pci/virtiovar.h   Fri Mar 31 07:31:48 2023 +0000
+++ b/sys/dev/pci/virtiovar.h   Fri Mar 31 07:34:26 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: virtiovar.h,v 1.27 2023/03/31 07:31:48 yamaguchi Exp $ */
+/*     $NetBSD: virtiovar.h,v 1.28 2023/03/31 07:34:26 yamaguchi Exp $ */
 
 /*
  * Copyright (c) 2010 Minoura Makoto.
@@ -158,13 +158,17 @@ struct virtio_softc {
        uint64_t                sc_active_features;
        bool                    sc_indirect;
        bool                    sc_version_1;
-       bool                    sc_finished_called;
 
        int                     sc_nvqs; /* set by child */
        struct virtqueue        *sc_vqs; /* set by child */
 
        int                     sc_childdevid;
        device_t                sc_child;               /* set by child */
+       uint32_t                sc_child_flags;
+#define VIRTIO_CHILD_ATTACH_FINISHED   __BIT(0)
+#define VIRTIO_CHILD_ATTACH_FAILED     __BIT(1)
+#define VIRTIO_CHILD_DETACHED          __BIT(2)
+
        virtio_callback         sc_config_change;       /* set by child */
        virtio_callback         sc_intrhand;
 };
@@ -179,9 +183,6 @@ struct virtio_softc;
 #define VIRTIO_F_INTR_MSIX     (1 << 2)
 #define VIRTIO_F_INTR_PERVQ    (1 << 3)
 
-
-#define        VIRTIO_CHILD_FAILED             ((void *)1)
-
 /* public interface */
 void virtio_negotiate_features(struct virtio_softc*, uint64_t);
 
diff -r b02d7ba3d2c4 -r 6cd6773c8f9c sys/dev/virtio/virtio_mmio.c
--- a/sys/dev/virtio/virtio_mmio.c      Fri Mar 31 07:31:48 2023 +0000
+++ b/sys/dev/virtio/virtio_mmio.c      Fri Mar 31 07:34:26 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: virtio_mmio.c,v 1.7 2021/10/22 02:57:23 yamaguchi Exp $        */
+/*     $NetBSD: virtio_mmio.c,v 1.8 2023/03/31 07:34:26 yamaguchi Exp $        */
 /*     $OpenBSD: virtio_mmio.c,v 1.2 2017/02/24 17:12:31 patrick Exp $ */
 
 /*
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: virtio_mmio.c,v 1.7 2021/10/22 02:57:23 yamaguchi Exp $");
+__KERNEL_RCSID(0, "$NetBSD: virtio_mmio.c,v 1.8 2023/03/31 07:34:26 yamaguchi Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -213,12 +213,11 @@ virtio_mmio_common_detach(struct virtio_
        struct virtio_softc *vsc = &sc->sc_sc;
        int r;
 
-       if (vsc->sc_child != NULL && vsc->sc_child != VIRTIO_CHILD_FAILED) {
-               r = config_detach(vsc->sc_child, flags);
-               if (r)
-                       return r;
-       }
-       KASSERT(vsc->sc_child == NULL || vsc->sc_child == VIRTIO_CHILD_FAILED);
+       r = config_detach_children(self, flags);
+       if (r != 0)
+               return r;
+
+       KASSERT(ISSET(sc->sc_child_flags, VIRTIO_CHILD_DETACHED));
        KASSERT(vsc->sc_vqs == NULL);
        KASSERT(sc->sc_ih == NULL);
 



Home | Main Index | Thread Index | Old Index