From nobody Mon Feb 9 11:06:20 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1505326925549989.1388484064604; Wed, 13 Sep 2017 11:22:05 -0700 (PDT) Received: from localhost ([::1]:43994 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dsCIW-0000he-KS for importer@patchew.org; Wed, 13 Sep 2017 14:22:04 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36470) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dsCGE-0007jD-BK for qemu-devel@nongnu.org; Wed, 13 Sep 2017 14:19:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dsCGD-0004ia-3M for qemu-devel@nongnu.org; Wed, 13 Sep 2017 14:19:42 -0400 Received: from mx1.redhat.com ([209.132.183.28]:47724) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dsCGA-0004gQ-CI; Wed, 13 Sep 2017 14:19:38 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7332CC04B924; Wed, 13 Sep 2017 18:19:37 +0000 (UTC) Received: from localhost (ovpn-204-23.brq.redhat.com [10.40.204.23]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DB3BC60BE4; Wed, 13 Sep 2017 18:19:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 7332CC04B924 Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=mreitz@redhat.com From: Max Reitz To: qemu-block@nongnu.org Date: Wed, 13 Sep 2017 20:18:54 +0200 Message-Id: <20170913181910.29688-3-mreitz@redhat.com> In-Reply-To: <20170913181910.29688-1-mreitz@redhat.com> References: <20170913181910.29688-1-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Wed, 13 Sep 2017 18:19:37 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 02/18] block: BDS deletion during bdrv_drain_recurse X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Fam Zheng , qemu-devel@nongnu.org, Max Reitz , Stefan Hajnoczi , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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. 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) =20 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 =3D QLIST_HEAD_INITIALIZER(bs_list); + bool in_main_loop =3D + qemu_get_current_aio_context() =3D=3D qemu_get_aio_context(); =20 waited =3D BDRV_POLL_WHILE(bs, atomic_read(&bs->in_flight) > 0); =20 /* Ensure any pending metadata writes are submitted to bs->file. */ bdrv_drain_invoke(bs); =20 - QLIST_FOREACH_SAFE(child, &bs->children, next, tmp) { - BlockDriverState *bs =3D child->bs; - bool in_main_loop =3D - qemu_get_current_aio_context() =3D=3D 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 |=3D bdrv_drain_recurse(bs); - if (in_main_loop) { - bdrv_unref(bs); + /* Draining children may result in other children being removed and ma= ybe + * even deleted, so copy the children list first */ + QLIST_FOREACH(child, &bs->children, next) { + struct BDSToDrain *bs2d =3D g_new0(struct BDSToDrain, 1); + + bs2d->bs =3D 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 =3D 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 |=3D bdrv_drain_recurse(bs2d->bs); + if (in_main_loop) { + bdrv_unref(bs2d->bs); + } } + + g_free(bs2d); } =20 return waited; @@ -252,17 +276,25 @@ static void coroutine_fn bdrv_co_yield_to_drain(Block= DriverState *bs) =20 void bdrv_drained_begin(BlockDriverState *bs) { + BdrvDeletedStatus del_stat =3D { .deleted =3D false }; + if (qemu_in_coroutine()) { bdrv_co_yield_to_drain(bs); return; } =20 + QLIST_INSERT_HEAD(&bs->deleted_status, &del_stat, next); + if (atomic_fetch_inc(&bs->quiesce_counter) =3D=3D 0) { aio_disable_external(bdrv_get_aio_context(bs)); bdrv_parent_drained_begin(bs); } =20 - bdrv_drain_recurse(bs); + if (!del_stat.deleted) { + QLIST_REMOVE(&del_stat, next); + + bdrv_drain_recurse(bs); + } } =20 void bdrv_drained_end(BlockDriverState *bs) --=20 2.13.5