Author: mav
Date: Fri Jan 10 00:43:40 2020
New Revision: 356580
URL: https://svnweb.freebsd.org/changeset/base/356580

Log:
  MFC r356151: Fix GEOM_GATE orphanization.
  
  Previous code closed and destroyed direct read consumer even with I/O still
  in progress.  This patch adds locking and request counting to postpone the
  close till the last of running requests completes.

Modified:
  stable/12/sys/geom/gate/g_gate.c
  stable/12/sys/geom/gate/g_gate.h
Directory Properties:
  stable/12/   (props changed)

Modified: stable/12/sys/geom/gate/g_gate.c
==============================================================================
--- stable/12/sys/geom/gate/g_gate.c    Fri Jan 10 00:43:08 2020        
(r356579)
+++ stable/12/sys/geom/gate/g_gate.c    Fri Jan 10 00:43:40 2020        
(r356580)
@@ -88,6 +88,19 @@ static struct g_gate_softc **g_gate_units;
 static u_int g_gate_nunits;
 static struct mtx g_gate_units_lock;
 
+static void
+g_gate_detach(void *arg, int flags __unused)
+{
+       struct g_consumer *cp = arg;
+
+       g_topology_assert();
+       G_GATE_DEBUG(1, "Destroying read consumer on provider %s orphan.",
+           cp->provider->name);
+       (void)g_access(cp, -1, 0, 0);
+       g_detach(cp);
+       g_destroy_consumer(cp);
+}
+
 static int
 g_gate_destroy(struct g_gate_softc *sc, boolean_t force)
 {
@@ -139,6 +152,7 @@ g_gate_destroy(struct g_gate_softc *sc, boolean_t forc
        g_gate_nunits--;
        mtx_unlock(&g_gate_units_lock);
        mtx_destroy(&sc->sc_queue_mtx);
+       mtx_destroy(&sc->sc_read_mtx);
        g_topology_lock();
        if ((cp = sc->sc_readcons) != NULL) {
                sc->sc_readcons = NULL;
@@ -207,8 +221,11 @@ g_gate_queue_io(struct bio *bp)
 static void
 g_gate_done(struct bio *cbp)
 {
+       struct g_gate_softc *sc;
        struct bio *pbp;
+       struct g_consumer *cp;
 
+       cp = cbp->bio_from;
        pbp = cbp->bio_parent;
        if (cbp->bio_error == 0) {
                pbp->bio_completed = cbp->bio_completed;
@@ -221,12 +238,20 @@ g_gate_done(struct bio *cbp)
                pbp->bio_children--;
                g_gate_queue_io(pbp);
        }
+
+       sc = cp->geom->softc;
+       mtx_lock(&sc->sc_read_mtx);
+       if (--cp->index == 0 && sc->sc_readcons != cp)
+               g_post_event(g_gate_detach, cp, M_NOWAIT, NULL);
+       mtx_unlock(&sc->sc_read_mtx);
 }
 
 static void
 g_gate_start(struct bio *pbp)
 {
        struct g_gate_softc *sc;
+       struct g_consumer *cp;
+       struct bio *cbp;
 
        sc = pbp->bio_to->geom->softc;
        if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
@@ -236,21 +261,26 @@ g_gate_start(struct bio *pbp)
        G_GATE_LOGREQ(2, pbp, "Request received.");
        switch (pbp->bio_cmd) {
        case BIO_READ:
-               if (sc->sc_readcons != NULL) {
-                       struct bio *cbp;
-
-                       cbp = g_clone_bio(pbp);
-                       if (cbp == NULL) {
-                               g_io_deliver(pbp, ENOMEM);
-                               return;
-                       }
-                       cbp->bio_done = g_gate_done;
-                       cbp->bio_offset = pbp->bio_offset + sc->sc_readoffset;
-                       cbp->bio_to = sc->sc_readcons->provider;
-                       g_io_request(cbp, sc->sc_readcons);
+               if (sc->sc_readcons == NULL)
+                       break;
+               cbp = g_clone_bio(pbp);
+               if (cbp == NULL) {
+                       g_io_deliver(pbp, ENOMEM);
                        return;
                }
-               break;
+               mtx_lock(&sc->sc_read_mtx);
+               if ((cp = sc->sc_readcons) == NULL) {
+                       mtx_unlock(&sc->sc_read_mtx);
+                       g_destroy_bio(cbp);
+                       pbp->bio_children--;
+                       break;
+               }
+               cp->index++;
+               cbp->bio_offset = pbp->bio_offset + sc->sc_readoffset;
+               mtx_unlock(&sc->sc_read_mtx);
+               cbp->bio_done = g_gate_done;
+               g_io_request(cbp, cp);
+               return;
        case BIO_DELETE:
        case BIO_WRITE:
        case BIO_FLUSH:
@@ -376,20 +406,18 @@ g_gate_orphan(struct g_consumer *cp)
 {
        struct g_gate_softc *sc;
        struct g_geom *gp;
+       int done;
 
        g_topology_assert();
        gp = cp->geom;
        sc = gp->softc;
-       if (sc == NULL)
-               return;
-       KASSERT(cp == sc->sc_readcons, ("cp=%p sc_readcons=%p", cp,
-           sc->sc_readcons));
-       sc->sc_readcons = NULL;
-       G_GATE_DEBUG(1, "Destroying read consumer on provider %s orphan.",
-           cp->provider->name);
-       (void)g_access(cp, -1, 0, 0);
-       g_detach(cp);
-       g_destroy_consumer(cp);
+       mtx_lock(&sc->sc_read_mtx);
+       if (sc->sc_readcons == cp)
+               sc->sc_readcons = NULL;
+       done = (cp->index == 0);
+       mtx_unlock(&sc->sc_read_mtx);
+       if (done)
+               g_gate_detach(cp, 0);
 }
 
 static void
@@ -482,6 +510,7 @@ g_gate_create(struct g_gate_ctl_create *ggio)
        bioq_init(&sc->sc_inqueue);
        bioq_init(&sc->sc_outqueue);
        mtx_init(&sc->sc_queue_mtx, "gg:queue", NULL, MTX_DEF);
+       mtx_init(&sc->sc_read_mtx, "gg:read", NULL, MTX_DEF);
        sc->sc_queue_count = 0;
        sc->sc_queue_size = ggio->gctl_maxcount;
        if (sc->sc_queue_size > G_GATE_MAX_QUEUE_SIZE)
@@ -595,6 +624,7 @@ fail2:
 fail1:
        mtx_unlock(&g_gate_units_lock);
        mtx_destroy(&sc->sc_queue_mtx);
+       mtx_destroy(&sc->sc_read_mtx);
        free(sc, M_GATE);
        return (error);
 }
@@ -604,7 +634,7 @@ g_gate_modify(struct g_gate_softc *sc, struct g_gate_c
 {
        struct g_provider *pp;
        struct g_consumer *cp;
-       int error;
+       int done, error;
 
        if ((ggio->gctl_modify & GG_MODIFY_MEDIASIZE) != 0) {
                if (ggio->gctl_mediasize <= 0) {
@@ -627,13 +657,15 @@ g_gate_modify(struct g_gate_softc *sc, struct g_gate_c
 
        if ((ggio->gctl_modify & GG_MODIFY_READPROV) != 0) {
                g_topology_lock();
-               if (sc->sc_readcons != NULL) {
-                       cp = sc->sc_readcons;
+               mtx_lock(&sc->sc_read_mtx);
+               if ((cp = sc->sc_readcons) != NULL) {
                        sc->sc_readcons = NULL;
-                       (void)g_access(cp, -1, 0, 0);
-                       g_detach(cp);
-                       g_destroy_consumer(cp);
-               }
+                       done = (cp->index == 0);
+                       mtx_unlock(&sc->sc_read_mtx);
+                       if (done)
+                               g_gate_detach(cp, 0);
+               } else
+                       mtx_unlock(&sc->sc_read_mtx);
                if (ggio->gctl_readprov[0] != '\0') {
                        pp = g_provider_by_name(ggio->gctl_readprov);
                        if (pp == NULL) {

Modified: stable/12/sys/geom/gate/g_gate.h
==============================================================================
--- stable/12/sys/geom/gate/g_gate.h    Fri Jan 10 00:43:08 2020        
(r356579)
+++ stable/12/sys/geom/gate/g_gate.h    Fri Jan 10 00:43:40 2020        
(r356580)
@@ -91,16 +91,15 @@ struct g_gate_softc {
        uint32_t                 sc_queue_count;        /* P: sc_queue_mtx */
        uint32_t                 sc_queue_size;         /* P: (read-only) */
        u_int                    sc_timeout;            /* P: (read-only) */
-       struct g_consumer       *sc_readcons;           /* P: XXX */
-       off_t                    sc_readoffset;         /* P: XXX */
+       struct g_consumer       *sc_readcons;           /* P: sc_read_mtx */
+       off_t                    sc_readoffset;         /* P: sc_read_mtx */
        struct callout           sc_callout;            /* P: (modified only
                                                               from callout
                                                               thread) */
-       uintptr_t                sc_seq;                /* P: (modified only
-                                                              from g_down
-                                                              thread) */
+       uintptr_t                sc_seq;                /* P: sc_queue_mtx */
        LIST_ENTRY(g_gate_softc) sc_next;               /* P: g_gate_list_mtx */
        char                     sc_info[G_GATE_INFOSIZE]; /* P: (read-only) */
+       struct mtx               sc_read_mtx;
 };
 
 #define        G_GATE_DEBUG(lvl, ...)  do {                                    
\
_______________________________________________
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