From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49475) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dtmzF-0000Bi-MA for qemu-devel@nongnu.org; Sun, 17 Sep 2017 23:44:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dtmzD-00026B-Uh for qemu-devel@nongnu.org; Sun, 17 Sep 2017 23:44:45 -0400 Date: Mon, 18 Sep 2017 11:44:31 +0800 From: Fam Zheng Message-ID: <20170918034431.GA15551@lemon.lan> References: <20170913181910.29688-1-mreitz@redhat.com> <20170913181910.29688-3-mreitz@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20170913181910.29688-3-mreitz@redhat.com> Subject: Re: [Qemu-devel] [PATCH 02/18] block: BDS deletion during bdrv_drain_recurse List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Max Reitz Cc: qemu-block@nongnu.org, qemu-devel@nongnu.org, Kevin Wolf , Stefan Hajnoczi , John Snow On Wed, 09/13 20:18, Max Reitz wrote: > Drainined a BDS child may lead to both the original BDS and/or its other > children being deleted (e.g. if the original BDS represents a block > job). We should prepare for this in both bdrv_drain_recurse() and > bdrv_drained_begin() by monitoring whether the BDS we are about to drain > still exists at all. Can the deletion happen when IOThread calls bdrv_drain_recurse/bdrv_drained_begin? If not, is it enough to do ... if (in_main_loop) { bdrv_ref(bs); } ... if (in_main_loop) { bdrv_unref(bs); } to protect the main loop case? So the BdrvDeletedStatus state is not needed. Fam > > Signed-off-by: Max Reitz > --- > block/io.c | 72 +++++++++++++++++++++++++++++++++++++++++++++----------------- > 1 file changed, 52 insertions(+), 20 deletions(-) > > diff --git a/block/io.c b/block/io.c > index 4378ae4c7d..8ec1a564ad 100644 > --- a/block/io.c > +++ b/block/io.c > @@ -182,33 +182,57 @@ static void bdrv_drain_invoke(BlockDriverState *bs) > > static bool bdrv_drain_recurse(BlockDriverState *bs) > { > - BdrvChild *child, *tmp; > + BdrvChild *child; > bool waited; > + struct BDSToDrain { > + BlockDriverState *bs; > + BdrvDeletedStatus del_stat; > + QLIST_ENTRY(BDSToDrain) next; > + }; > + QLIST_HEAD(, BDSToDrain) bs_list = QLIST_HEAD_INITIALIZER(bs_list); > + bool in_main_loop = > + qemu_get_current_aio_context() == qemu_get_aio_context(); > > waited = BDRV_POLL_WHILE(bs, atomic_read(&bs->in_flight) > 0); > > /* Ensure any pending metadata writes are submitted to bs->file. */ > bdrv_drain_invoke(bs); > > - QLIST_FOREACH_SAFE(child, &bs->children, next, tmp) { > - BlockDriverState *bs = child->bs; > - bool in_main_loop = > - qemu_get_current_aio_context() == qemu_get_aio_context(); > - assert(bs->refcnt > 0); > - if (in_main_loop) { > - /* In case the recursive bdrv_drain_recurse processes a > - * block_job_defer_to_main_loop BH and modifies the graph, > - * let's hold a reference to bs until we are done. > - * > - * IOThread doesn't have such a BH, and it is not safe to call > - * bdrv_unref without BQL, so skip doing it there. > - */ > - bdrv_ref(bs); > - } > - waited |= bdrv_drain_recurse(bs); > - if (in_main_loop) { > - bdrv_unref(bs); > + /* Draining children may result in other children being removed and maybe > + * even deleted, so copy the children list first */ > + QLIST_FOREACH(child, &bs->children, next) { > + struct BDSToDrain *bs2d = g_new0(struct BDSToDrain, 1); > + > + bs2d->bs = child->bs; > + QLIST_INSERT_HEAD(&bs->deleted_status, &bs2d->del_stat, next); > + > + QLIST_INSERT_HEAD(&bs_list, bs2d, next); > + } > + > + while (!QLIST_EMPTY(&bs_list)) { > + struct BDSToDrain *bs2d = QLIST_FIRST(&bs_list); > + QLIST_REMOVE(bs2d, next); > + > + if (!bs2d->del_stat.deleted) { > + QLIST_REMOVE(&bs2d->del_stat, next); > + > + if (in_main_loop) { > + /* In case the recursive bdrv_drain_recurse processes a > + * block_job_defer_to_main_loop BH and modifies the graph, > + * let's hold a reference to the BDS until we are done. > + * > + * IOThread doesn't have such a BH, and it is not safe to call > + * bdrv_unref without BQL, so skip doing it there. > + */ > + bdrv_ref(bs2d->bs); > + } > + waited |= bdrv_drain_recurse(bs2d->bs); > + if (in_main_loop) { > + bdrv_unref(bs2d->bs); > + } > } > + > + g_free(bs2d); > } > > return waited; > @@ -252,17 +276,25 @@ static void coroutine_fn bdrv_co_yield_to_drain(BlockDriverState *bs) > > void bdrv_drained_begin(BlockDriverState *bs) > { > + BdrvDeletedStatus del_stat = { .deleted = false }; > + > if (qemu_in_coroutine()) { > bdrv_co_yield_to_drain(bs); > return; > } > > + QLIST_INSERT_HEAD(&bs->deleted_status, &del_stat, next); > + > if (atomic_fetch_inc(&bs->quiesce_counter) == 0) { > aio_disable_external(bdrv_get_aio_context(bs)); > bdrv_parent_drained_begin(bs); > } > > - bdrv_drain_recurse(bs); > + if (!del_stat.deleted) { > + QLIST_REMOVE(&del_stat, next); > + > + bdrv_drain_recurse(bs); > + } > } > > void bdrv_drained_end(BlockDriverState *bs) > -- > 2.13.5 >