From nobody Mon Nov 3 19:58:54 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1506000018393342.6646494543993; Thu, 21 Sep 2017 06:20:18 -0700 (PDT) Received: from localhost ([::1]:53667 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dv1Or-0006L1-La for importer@patchew.org; Thu, 21 Sep 2017 09:20:17 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56354) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dv1Mo-00057a-H2 for qemu-devel@nongnu.org; Thu, 21 Sep 2017 09:18:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dv1Mn-00049Q-6a for qemu-devel@nongnu.org; Thu, 21 Sep 2017 09:18:10 -0400 Received: from smtp1.ntua.gr ([2001:648:2000:de::183]:39074) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dv1Ma-00040G-AG; Thu, 21 Sep 2017 09:17:56 -0400 Received: from mail.ntua.gr (carp0.noc.ntua.gr [147.102.222.60]) (authenticated bits=0) by smtp1.ntua.gr (8.15.2/8.15.2) with ESMTPSA id v8LDHoWi083306 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 21 Sep 2017 16:17:50 +0300 (EEST) (envelope-from el13635@mail.ntua.gr) X-Authentication-Warning: smtp1.ntua.gr: Host carp0.noc.ntua.gr [147.102.222.60] claimed to be mail.ntua.gr From: Manos Pitsidianakis To: qemu-devel Date: Thu, 21 Sep 2017 16:17:05 +0300 Message-Id: <20170921131707.17364-2-el13635@mail.ntua.gr> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170921131707.17364-1-el13635@mail.ntua.gr> References: <20170921131707.17364-1-el13635@mail.ntua.gr> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:648:2000:de::183 Subject: [Qemu-devel] [PATCH v2 1/3] block: add bdrv_co_drain_end callback 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 , Stefan Hajnoczi , qemu-block , Max Reitz 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" BlockDriverState has a bdrv_do_drain() callback but no equivalent for the e= nd of the drain. The throttle driver (block/throttle.c) needs a way to mark the end of the drain in order to toggle io_limits_disabled correctly, thus bdrv_co_drain_end is needed. Signed-off-by: Manos Pitsidianakis Reviewed-by: Fam Zheng --- include/block/block_int.h | 6 ++++++ block/io.c | 48 +++++++++++++++++++++++++++++++++----------= ---- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/include/block/block_int.h b/include/block/block_int.h index ba4c383393..21950cfda3 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -356,8 +356,14 @@ struct BlockDriver { /** * Drain and stop any internal sources of requests in the driver, and * remain so until next I/O callback (e.g. bdrv_co_writev) is called. + * + * The callbacks are called at the beginning and ending of the drain + * respectively. They should be implemented if the driver needs to e.g. + * manage scheduled I/O requests, or toggle an internal state. After an + * bdrv_co_drain_end() invocation new requests will continue normally. */ void coroutine_fn (*bdrv_co_drain)(BlockDriverState *bs); + void coroutine_fn (*bdrv_co_drain_end)(BlockDriverState *bs); =20 void (*bdrv_add_child)(BlockDriverState *parent, BlockDriverState *chi= ld, Error **errp); diff --git a/block/io.c b/block/io.c index 4378ae4c7d..b0a10ad3ef 100644 --- a/block/io.c +++ b/block/io.c @@ -153,6 +153,7 @@ typedef struct { Coroutine *co; BlockDriverState *bs; bool done; + bool begin; } BdrvCoDrainData; =20 static void coroutine_fn bdrv_drain_invoke_entry(void *opaque) @@ -160,18 +161,23 @@ static void coroutine_fn bdrv_drain_invoke_entry(void= *opaque) BdrvCoDrainData *data =3D opaque; BlockDriverState *bs =3D data->bs; =20 - bs->drv->bdrv_co_drain(bs); + if (data->begin) { + bs->drv->bdrv_co_drain(bs); + } else { + bs->drv->bdrv_co_drain_end(bs); + } =20 /* Set data->done before reading bs->wakeup. */ atomic_mb_set(&data->done, true); bdrv_wakeup(bs); } =20 -static void bdrv_drain_invoke(BlockDriverState *bs) +static void bdrv_drain_invoke(BlockDriverState *bs, bool begin) { - BdrvCoDrainData data =3D { .bs =3D bs, .done =3D false }; + BdrvCoDrainData data =3D { .bs =3D bs, .done =3D false, .begin =3D beg= in}; =20 - if (!bs->drv || !bs->drv->bdrv_co_drain) { + if (!bs->drv || (begin && !bs->drv->bdrv_co_drain) || + (!begin && !bs->drv->bdrv_co_drain_end)) { return; } =20 @@ -180,15 +186,16 @@ static void bdrv_drain_invoke(BlockDriverState *bs) BDRV_POLL_WHILE(bs, !data.done); } =20 -static bool bdrv_drain_recurse(BlockDriverState *bs) +static bool bdrv_drain_recurse(BlockDriverState *bs, bool begin) { BdrvChild *child, *tmp; bool waited; =20 - waited =3D BDRV_POLL_WHILE(bs, atomic_read(&bs->in_flight) > 0); - /* Ensure any pending metadata writes are submitted to bs->file. */ - bdrv_drain_invoke(bs); + bdrv_drain_invoke(bs, begin); + + /* 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; @@ -205,7 +212,7 @@ static bool bdrv_drain_recurse(BlockDriverState *bs) */ bdrv_ref(bs); } - waited |=3D bdrv_drain_recurse(bs); + waited |=3D bdrv_drain_recurse(bs, begin); if (in_main_loop) { bdrv_unref(bs); } @@ -221,12 +228,18 @@ static void bdrv_co_drain_bh_cb(void *opaque) BlockDriverState *bs =3D data->bs; =20 bdrv_dec_in_flight(bs); - bdrv_drained_begin(bs); + if (data->begin) { + bdrv_drained_begin(bs); + } else { + bdrv_drained_end(bs); + } + data->done =3D true; aio_co_wake(co); } =20 -static void coroutine_fn bdrv_co_yield_to_drain(BlockDriverState *bs) +static void coroutine_fn bdrv_co_yield_to_drain(BlockDriverState *bs, + bool begin) { BdrvCoDrainData data; =20 @@ -239,6 +252,7 @@ static void coroutine_fn bdrv_co_yield_to_drain(BlockDr= iverState *bs) .co =3D qemu_coroutine_self(), .bs =3D bs, .done =3D false, + .begin =3D begin, }; bdrv_inc_in_flight(bs); aio_bh_schedule_oneshot(bdrv_get_aio_context(bs), @@ -253,7 +267,7 @@ static void coroutine_fn bdrv_co_yield_to_drain(BlockDr= iverState *bs) void bdrv_drained_begin(BlockDriverState *bs) { if (qemu_in_coroutine()) { - bdrv_co_yield_to_drain(bs); + bdrv_co_yield_to_drain(bs, true); return; } =20 @@ -262,17 +276,22 @@ void bdrv_drained_begin(BlockDriverState *bs) bdrv_parent_drained_begin(bs); } =20 - bdrv_drain_recurse(bs); + bdrv_drain_recurse(bs, true); } =20 void bdrv_drained_end(BlockDriverState *bs) { + if (qemu_in_coroutine()) { + bdrv_co_yield_to_drain(bs, false); + return; + } assert(bs->quiesce_counter > 0); if (atomic_fetch_dec(&bs->quiesce_counter) > 1) { return; } =20 bdrv_parent_drained_end(bs); + bdrv_drain_recurse(bs, false); aio_enable_external(bdrv_get_aio_context(bs)); } =20 @@ -350,7 +369,7 @@ void bdrv_drain_all_begin(void) aio_context_acquire(aio_context); for (bs =3D bdrv_first(&it); bs; bs =3D bdrv_next(&it)) { if (aio_context =3D=3D bdrv_get_aio_context(bs)) { - waited |=3D bdrv_drain_recurse(bs); + waited |=3D bdrv_drain_recurse(bs, true); } } aio_context_release(aio_context); @@ -371,6 +390,7 @@ void bdrv_drain_all_end(void) aio_context_acquire(aio_context); aio_enable_external(aio_context); bdrv_parent_drained_end(bs); + bdrv_drain_recurse(bs, false); aio_context_release(aio_context); } =20 --=20 2.11.0