Author: mav
Date: Sun Nov 27 05:59:17 2016
New Revision: 309206
URL: https://svnweb.freebsd.org/changeset/base/309206

Log:
  MFC r308608:
  Use providergone method to cover race between destroy and g_access().

Modified:
  stable/10/sys/geom/mirror/g_mirror.c
  stable/10/sys/geom/mirror/g_mirror.h
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/sys/geom/mirror/g_mirror.c
==============================================================================
--- stable/10/sys/geom/mirror/g_mirror.c        Sun Nov 27 05:58:47 2016        
(r309205)
+++ stable/10/sys/geom/mirror/g_mirror.c        Sun Nov 27 05:59:17 2016        
(r309206)
@@ -84,12 +84,12 @@ SYSCTL_UINT(_kern_geom_mirror, OID_AUTO,
 static eventhandler_tag g_mirror_post_sync = NULL;
 static int g_mirror_shutdown = 0;
 
-static int g_mirror_destroy_geom(struct gctl_req *req, struct g_class *mp,
-    struct g_geom *gp);
+static g_ctl_destroy_geom_t g_mirror_destroy_geom;
 static g_taste_t g_mirror_taste;
+static g_init_t g_mirror_init;
+static g_fini_t g_mirror_fini;
+static g_provgone_t g_mirror_providergone;
 static g_resize_t g_mirror_resize;
-static void g_mirror_init(struct g_class *mp);
-static void g_mirror_fini(struct g_class *mp);
 
 struct g_class g_mirror_class = {
        .name = G_MIRROR_CLASS_NAME,
@@ -99,6 +99,7 @@ struct g_class g_mirror_class = {
        .destroy_geom = g_mirror_destroy_geom,
        .init = g_mirror_init,
        .fini = g_mirror_fini,
+       .providergone = g_mirror_providergone,
        .resize = g_mirror_resize
 };
 
@@ -513,6 +514,26 @@ g_mirror_destroy_disk(struct g_mirror_di
 }
 
 static void
+g_mirror_free_device(struct g_mirror_softc *sc)
+{
+
+       mtx_destroy(&sc->sc_queue_mtx);
+       mtx_destroy(&sc->sc_events_mtx);
+       mtx_destroy(&sc->sc_done_mtx);
+       sx_destroy(&sc->sc_lock);
+       free(sc, M_MIRROR);
+}
+
+static void
+g_mirror_providergone(struct g_provider *pp)
+{
+       struct g_mirror_softc *sc = pp->private;
+
+       if ((--sc->sc_refcnt) == 0)
+               g_mirror_free_device(sc);
+}
+
+static void
 g_mirror_destroy_device(struct g_mirror_softc *sc)
 {
        struct g_mirror_disk *disk;
@@ -554,12 +575,10 @@ g_mirror_destroy_device(struct g_mirror_
        g_wither_geom(sc->sc_sync.ds_geom, ENXIO);
        G_MIRROR_DEBUG(0, "Device %s destroyed.", gp->name);
        g_wither_geom(gp, ENXIO);
-       g_topology_unlock();
-       mtx_destroy(&sc->sc_queue_mtx);
-       mtx_destroy(&sc->sc_events_mtx);
-       mtx_destroy(&sc->sc_done_mtx);
        sx_xunlock(&sc->sc_lock);
-       sx_destroy(&sc->sc_lock);
+       if ((--sc->sc_refcnt) == 0)
+               g_mirror_free_device(sc);
+       g_topology_unlock();
 }
 
 static void
@@ -872,7 +891,7 @@ g_mirror_flush_done(struct bio *bp)
        struct bio *pbp;
 
        pbp = bp->bio_parent;
-       sc = pbp->bio_to->geom->softc;
+       sc = pbp->bio_to->private;
        mtx_lock(&sc->sc_done_mtx);
        if (pbp->bio_error == 0)
                pbp->bio_error = bp->bio_error;
@@ -909,7 +928,7 @@ g_mirror_regular_request(struct bio *bp)
        g_topology_assert_not();
 
        pbp = bp->bio_parent;
-       sc = pbp->bio_to->geom->softc;
+       sc = pbp->bio_to->private;
        bp->bio_from->index--;
        if (bp->bio_cmd == BIO_WRITE)
                sc->sc_writes--;
@@ -1033,7 +1052,7 @@ g_mirror_candelete(struct bio *bp)
        struct g_mirror_disk *disk;
        int *val;
 
-       sc = bp->bio_to->geom->softc;
+       sc = bp->bio_to->private;
        LIST_FOREACH(disk, &sc->sc_disks, d_next) {
                if (disk->d_flags & G_MIRROR_DISK_FLAG_CANDELETE)
                        break;
@@ -1058,7 +1077,7 @@ g_mirror_kernel_dump(struct bio *bp)
         * we will not be able to read the dump after the reboot if it will be
         * connected and synchronized later. Can we do something better?
         */
-       sc = bp->bio_to->geom->softc;
+       sc = bp->bio_to->private;
        disk = LIST_FIRST(&sc->sc_disks);
 
        gkd = (struct g_kerneldump *)bp->bio_data;
@@ -1118,7 +1137,7 @@ g_mirror_start(struct bio *bp)
 {
        struct g_mirror_softc *sc;
 
-       sc = bp->bio_to->geom->softc;
+       sc = bp->bio_to->private;
        /*
         * If sc == NULL or there are no valid disks, provider's error
         * should be set and g_mirror_start() should not be called at all.
@@ -1625,7 +1644,7 @@ g_mirror_register_request(struct bio *bp
 {
        struct g_mirror_softc *sc;
 
-       sc = bp->bio_to->geom->softc;
+       sc = bp->bio_to->private;
        switch (bp->bio_cmd) {
        case BIO_READ:
                switch (sc->sc_balance) {
@@ -1791,7 +1810,6 @@ g_mirror_try_destroy(struct g_mirror_sof
        } else {
                g_topology_unlock();
                g_mirror_destroy_device(sc);
-               free(sc, M_MIRROR);
        }
        return (1);
 }
@@ -2103,6 +2121,8 @@ g_mirror_launch_provider(struct g_mirror
                        }
                }
        }
+       pp->private = sc;
+       sc->sc_refcnt++;
        sc->sc_provider = pp;
        g_error_provider(pp, 0);
        g_topology_unlock();
@@ -2877,9 +2897,7 @@ g_mirror_access(struct g_provider *pp, i
        G_MIRROR_DEBUG(2, "Access request for %s: r%dw%de%d.", pp->name, acr,
            acw, ace);
 
-       sc = pp->geom->softc;
-       if (sc == NULL && acr <= 0 && acw <= 0 && ace <= 0)
-               return (0);
+       sc = pp->private;
        KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
 
        g_topology_unlock();
@@ -2938,6 +2956,7 @@ g_mirror_create(struct g_class *mp, cons
        sc->sc_idle = 1;
        sc->sc_last_write = time_uptime;
        sc->sc_writes = 0;
+       sc->sc_refcnt = 1;
        sx_init(&sc->sc_lock, "gmirror:lock");
        bioq_init(&sc->sc_queue);
        mtx_init(&sc->sc_queue_mtx, "gmirror:queue", NULL, MTX_DEF);
@@ -2968,12 +2987,8 @@ g_mirror_create(struct g_class *mp, cons
                G_MIRROR_DEBUG(1, "Cannot create kernel thread for %s.",
                    sc->sc_name);
                g_destroy_geom(sc->sc_sync.ds_geom);
-               mtx_destroy(&sc->sc_done_mtx);
-               mtx_destroy(&sc->sc_events_mtx);
-               mtx_destroy(&sc->sc_queue_mtx);
-               sx_destroy(&sc->sc_lock);
                g_destroy_geom(sc->sc_geom);
-               free(sc, M_MIRROR);
+               g_mirror_free_device(sc);
                return (NULL);
        }
 
@@ -2996,8 +3011,6 @@ g_mirror_destroy(struct g_mirror_softc *
        struct g_mirror_disk *disk;
 
        g_topology_assert_not();
-       if (sc == NULL)
-               return (ENXIO);
        sx_assert(&sc->sc_lock, SX_XLOCKED);
 
        if (sc->sc_provider_open != 0) {
@@ -3047,7 +3060,6 @@ g_mirror_destroy(struct g_mirror_softc *
        G_MIRROR_DEBUG(4, "%s: Woken up %p.", __func__, &sc->sc_worker);
        sx_xlock(&sc->sc_lock);
        g_mirror_destroy_device(sc);
-       free(sc, M_MIRROR);
        return (0);
 }
 

Modified: stable/10/sys/geom/mirror/g_mirror.h
==============================================================================
--- stable/10/sys/geom/mirror/g_mirror.h        Sun Nov 27 05:58:47 2016        
(r309205)
+++ stable/10/sys/geom/mirror/g_mirror.h        Sun Nov 27 05:59:17 2016        
(r309206)
@@ -207,6 +207,7 @@ struct g_mirror_softc {
        int             sc_idle;        /* DIRTY flags removed. */
        time_t          sc_last_write;
        u_int           sc_writes;
+       u_int           sc_refcnt;      /* Number of softc references */
 
        TAILQ_HEAD(, g_mirror_event) sc_events;
        struct mtx      sc_events_mtx;
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to