From nobody Thu Dec 18 17:51:01 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 1520018460627206.36567754421492; Fri, 2 Mar 2018 11:21:00 -0800 (PST) Received: from localhost ([::1]:37022 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1erqEa-00071A-S2 for importer@patchew.org; Fri, 02 Mar 2018 14:20:48 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53055) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1erpqe-0001oq-Uz for qemu-devel@nongnu.org; Fri, 02 Mar 2018 13:56:06 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1erpqb-0003De-Kg for qemu-devel@nongnu.org; Fri, 02 Mar 2018 13:56:05 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:38108 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 1erpqY-00038Q-EX; Fri, 02 Mar 2018 13:55:58 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id EE106813F6FF; Fri, 2 Mar 2018 18:55:57 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-66.ams2.redhat.com [10.36.117.66]) by smtp.corp.redhat.com (Postfix) with ESMTP id CF689AB58B; Fri, 2 Mar 2018 18:55:56 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Fri, 2 Mar 2018 19:54:39 +0100 Message-Id: <20180302185448.6314-29-kwolf@redhat.com> In-Reply-To: <20180302185448.6314-1-kwolf@redhat.com> References: <20180302185448.6314-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Fri, 02 Mar 2018 18:55:57 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Fri, 02 Mar 2018 18:55:57 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'kwolf@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] [PULL 28/37] block: add BlockBackend->in_flight counter 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: kwolf@redhat.com, qemu-devel@nongnu.org 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" From: Stefan Hajnoczi BlockBackend currently relies on BlockDriverState->in_flight to track requests for blk_drain(). There is a corner case where BlockDriverState->in_flight cannot be used though: blk->root can be NULL when there is no medium. This results in a segfault when the NULL pointer is dereferenced. Introduce a BlockBackend->in_flight counter for aio requests so it works even when blk->root =3D=3D NULL. Based on a patch by Kevin Wolf . Signed-off-by: Kevin Wolf Signed-off-by: Stefan Hajnoczi Signed-off-by: Kevin Wolf --- block.c | 2 +- block/block-backend.c | 60 +++++++++++++++++++++++++++++++++++++++++++++--= ---- 2 files changed, 54 insertions(+), 8 deletions(-) diff --git a/block.c b/block.c index 9e4da81213..a83037c2a5 100644 --- a/block.c +++ b/block.c @@ -4713,7 +4713,7 @@ out: =20 AioContext *bdrv_get_aio_context(BlockDriverState *bs) { - return bs->aio_context; + return bs ? bs->aio_context : qemu_get_aio_context(); } =20 AioWait *bdrv_get_aio_wait(BlockDriverState *bs) diff --git a/block/block-backend.c b/block/block-backend.c index 0266ac990b..a775a3dd2f 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -73,6 +73,14 @@ struct BlockBackend { int quiesce_counter; VMChangeStateEntry *vmsh; bool force_allow_inactivate; + + /* Number of in-flight aio requests. BlockDriverState also counts + * in-flight requests but aio requests can exist even when blk->root is + * NULL, so we cannot rely on its counter for that case. + * Accessed with atomic ops. + */ + unsigned int in_flight; + AioWait wait; }; =20 typedef struct BlockBackendAIOCB { @@ -1225,11 +1233,22 @@ int blk_make_zero(BlockBackend *blk, BdrvRequestFla= gs flags) return bdrv_make_zero(blk->root, flags); } =20 +static void blk_inc_in_flight(BlockBackend *blk) +{ + atomic_inc(&blk->in_flight); +} + +static void blk_dec_in_flight(BlockBackend *blk) +{ + atomic_dec(&blk->in_flight); + aio_wait_kick(&blk->wait); +} + static void error_callback_bh(void *opaque) { struct BlockBackendAIOCB *acb =3D opaque; =20 - bdrv_dec_in_flight(acb->common.bs); + blk_dec_in_flight(acb->blk); acb->common.cb(acb->common.opaque, acb->ret); qemu_aio_unref(acb); } @@ -1240,7 +1259,7 @@ BlockAIOCB *blk_abort_aio_request(BlockBackend *blk, { struct BlockBackendAIOCB *acb; =20 - bdrv_inc_in_flight(blk_bs(blk)); + blk_inc_in_flight(blk); acb =3D blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque); acb->blk =3D blk; acb->ret =3D ret; @@ -1263,7 +1282,7 @@ static const AIOCBInfo blk_aio_em_aiocb_info =3D { static void blk_aio_complete(BlkAioEmAIOCB *acb) { if (acb->has_returned) { - bdrv_dec_in_flight(acb->common.bs); + blk_dec_in_flight(acb->rwco.blk); acb->common.cb(acb->common.opaque, acb->rwco.ret); qemu_aio_unref(acb); } @@ -1284,7 +1303,7 @@ static BlockAIOCB *blk_aio_prwv(BlockBackend *blk, in= t64_t offset, int bytes, BlkAioEmAIOCB *acb; Coroutine *co; =20 - bdrv_inc_in_flight(blk_bs(blk)); + blk_inc_in_flight(blk); acb =3D blk_aio_get(&blk_aio_em_aiocb_info, blk, cb, opaque); acb->rwco =3D (BlkRwCo) { .blk =3D blk, @@ -1521,14 +1540,41 @@ int blk_flush(BlockBackend *blk) =20 void blk_drain(BlockBackend *blk) { - if (blk_bs(blk)) { - bdrv_drain(blk_bs(blk)); + BlockDriverState *bs =3D blk_bs(blk); + + if (bs) { + bdrv_drained_begin(bs); + } + + /* We may have -ENOMEDIUM completions in flight */ + AIO_WAIT_WHILE(&blk->wait, + blk_get_aio_context(blk), + atomic_mb_read(&blk->in_flight) > 0); + + if (bs) { + bdrv_drained_end(bs); } } =20 void blk_drain_all(void) { - bdrv_drain_all(); + BlockBackend *blk =3D NULL; + + bdrv_drain_all_begin(); + + while ((blk =3D blk_all_next(blk)) !=3D NULL) { + AioContext *ctx =3D blk_get_aio_context(blk); + + aio_context_acquire(ctx); + + /* We may have -ENOMEDIUM completions in flight */ + AIO_WAIT_WHILE(&blk->wait, ctx, + atomic_mb_read(&blk->in_flight) > 0); + + aio_context_release(ctx); + } + + bdrv_drain_all_end(); } =20 void blk_set_on_error(BlockBackend *blk, BlockdevOnError on_read_error, --=20 2.13.6