From nobody Fri Oct 24 22:14:58 2025 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; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1519841414876228.9234414718128; Wed, 28 Feb 2018 10:10:14 -0800 (PST) Received: from localhost ([::1]:45961 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1er6BB-0007y3-Ok for importer@patchew.org; Wed, 28 Feb 2018 13:10:13 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41489) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1er66W-00044E-TX for qemu-devel@nongnu.org; Wed, 28 Feb 2018 13:05:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1er66V-00042e-Oe for qemu-devel@nongnu.org; Wed, 28 Feb 2018 13:05:24 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:42064 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1er66P-0003yq-BE; Wed, 28 Feb 2018 13:05:17 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 73839402290A; Wed, 28 Feb 2018 18:05:15 +0000 (UTC) Received: from localhost (unknown [10.40.205.159]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9112B10B0F53; Wed, 28 Feb 2018 18:05:10 +0000 (UTC) From: Max Reitz To: qemu-block@nongnu.org Date: Wed, 28 Feb 2018 19:04:52 +0100 Message-Id: <20180228180507.3964-2-mreitz@redhat.com> In-Reply-To: <20180228180507.3964-1-mreitz@redhat.com> References: <20180228180507.3964-1-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Wed, 28 Feb 2018 18:05:15 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Wed, 28 Feb 2018 18:05:15 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'mreitz@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH v3 01/16] 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" Draining a BDS child may lead to other children of the same parent being detached and/or deleted. We should prepare for the former case (by copying the children list before iterating through it) and prevent the latter (by bdrv_ref()'ing all nodes if we are in the main loop). Signed-off-by: Max Reitz Reviewed-by: Jeff Cody --- block/io.c | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/block/io.c b/block/io.c index 4d3d1f640a..ead12c4136 100644 --- a/block/io.c +++ b/block/io.c @@ -189,31 +189,51 @@ static void bdrv_drain_invoke(BlockDriverState *bs, b= ool begin, bool recursive) =20 static bool bdrv_drain_recurse(BlockDriverState *bs) { - BdrvChild *child, *tmp; + BdrvChild *child; bool waited; + struct BDSToDrain { + BlockDriverState *bs; + 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 /* Wait for drained requests to finish */ waited =3D BDRV_POLL_WHILE(bs, atomic_read(&bs->in_flight) > 0); =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); + /* Draining children may result in other children being removed from t= his + * parent and maybe 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; 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. + * 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(bs); + bdrv_ref(bs2d->bs); } - waited |=3D bdrv_drain_recurse(bs); + + QLIST_INSERT_HEAD(&bs_list, bs2d, next); + } + + while (!QLIST_EMPTY(&bs_list)) { + struct BDSToDrain *bs2d =3D QLIST_FIRST(&bs_list); + QLIST_REMOVE(bs2d, next); + + assert(bs2d->bs->refcnt > 0); + + waited |=3D bdrv_drain_recurse(bs2d->bs); if (in_main_loop) { - bdrv_unref(bs); + bdrv_unref(bs2d->bs); } + + g_free(bs2d); } =20 return waited; --=20 2.14.3