Index: sys/vm/swap_pager.c =================================================================== --- sys/vm/swap_pager.c +++ sys/vm/swap_pager.c @@ -2548,7 +2548,7 @@ bp->b_error = bp2->bio_error; bufdone(bp); mtx_lock(&sw_dev_mtx); - if ((--cp->index) == 0 && cp->private) { + if ((--cp->index) == 0) { if (g_post_event(swapgeom_close_ev, cp, M_NOWAIT, NULL) == 0) { sp = bp2->bio_caller1; sp->sw_id = NULL; @@ -2573,6 +2573,8 @@ bufdone(bp); return; } + KASSERT(cp->index, + ("Reference count in swapgeom consumer impossibly zero.")); cp->index++; mtx_unlock(&sw_dev_mtx); if (bp->b_iocmd == BIO_WRITE) @@ -2580,6 +2582,9 @@ else bio = g_alloc_bio(); if (bio == NULL) { + mtx_lock(&sw_dev_mtx); + --cp->index; + mtx_unlock(&sw_dev_mtx); bp->b_error = ENOMEM; bp->b_ioflags |= BIO_ERROR; bufdone(bp); @@ -2619,7 +2624,7 @@ break; } } - cp->private = (void *)(uintptr_t)1; + --cp->index; /* Drop reference we were created with */ destroy = ((sp != NULL) && (cp->index == 0)); if (destroy) sp->sw_id = NULL; @@ -2680,8 +2685,8 @@ if (gp == NULL) gp = g_new_geomf(&g_swap_class, "swap"); cp = g_new_consumer(gp); - cp->index = 0; /* Number of active I/Os. */ - cp->private = NULL; /* Orphanization flag */ + cp->index = 1; /* Number of active I/Os, plus one for being active. */ + cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; g_attach(cp, pp); /* * XXX: Everytime you think you can improve the margin for