From nobody Sun Apr 13 10:55:54 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1674217714; cv=none; d=zohomail.com; s=zohoarc; b=FBgOgUEcba2rGsJXvrS4Fp25pdYgzoP086GNkAOJo4eQcIr8rzPUdJCKS+Sr3dj4yP+aLdmRouoSYE2FSW0fTw+P3eIPhGyrcrERoY/j8j9fuoycNz+l/5EFYsAvLHMSSt0jrks/aXKGMcDvkQUV/3CLKB8x3cPRjj8wHOa2NQw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1674217714; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=oPstMRD7Ck0frUvQvIu1lnpBmt9YRIf6fF1BLgr1eHk=; b=cD/HXel1z69POXbvEYMyVAv8avgaREcsMtNFjpNQ0eHLEmLJvXxL4vSGy3AxlcveFnt404BeNl3gmwjKs5+0hL3V8aavW/ybWpRfcyaTASP410U5rOzhx/RHGCgUtgMha+/al68l4wEycZ4GjuFb7HTFwdIozocXVbRBBexCz8I= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 16742177143551018.5875412807374; Fri, 20 Jan 2023 04:28:34 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pIqU4-0002on-L1; Fri, 20 Jan 2023 07:27:04 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pIqU3-0002mU-0U for qemu-devel@nongnu.org; Fri, 20 Jan 2023 07:27:03 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pIqTz-0000Lz-A6 for qemu-devel@nongnu.org; Fri, 20 Jan 2023 07:27:02 -0500 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-533-FwqAoI8cMRG-hmnt1xFS0g-1; Fri, 20 Jan 2023 07:26:53 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 85F5718E0043; Fri, 20 Jan 2023 12:26:53 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.193.74]) by smtp.corp.redhat.com (Postfix) with ESMTP id BA0F440C6EC4; Fri, 20 Jan 2023 12:26:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1674217617; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oPstMRD7Ck0frUvQvIu1lnpBmt9YRIf6fF1BLgr1eHk=; b=Oz5gl6UjG06aV7jK/p1BQvog8q0+/g6/duh4JoDMZA9YLazFwEbb1S2fY0xgGWd/akRd7d P8btklWhnNAcyQ9quR9OnFmxNvNFxDDk1OAh00Blk7bSHKLo7NKfKVysYz2K7JIplquLKI ostK7khaRDnaK4A6FM+31/ax8DRysVA= X-MC-Unique: FwqAoI8cMRG-hmnt1xFS0g-1 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, peter.maydell@linaro.org, qemu-devel@nongnu.org Subject: [PULL 15/38] block: Convert bdrv_is_inserted() to co_wrapper Date: Fri, 20 Jan 2023 13:26:10 +0100 Message-Id: <20230120122633.84983-16-kwolf@redhat.com> In-Reply-To: <20230120122633.84983-1-kwolf@redhat.com> References: <20230120122633.84983-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1674217715640100008 Content-Type: text/plain; charset="utf-8" From: Emanuele Giuseppe Esposito bdrv_is_inserted() is categorized as an I/O function, and it currently doesn't run in a coroutine. We should let it take a graph rdlock since it traverses the block nodes graph, which however is only possible in a coroutine. Therefore turn it into a co_wrapper to move the actual function into a coroutine where the lock can be taken. At the same time, add also blk_is_inserted as co_wrapper_mixed, since it is called in both coroutine and non-coroutine contexts. Because now this function creates a new coroutine and polls, we need to take the AioContext lock where it is missing, for the only reason that internally c_w_mixed_bdrv_rdlock calls AIO_WAIT_WHILE and it expects to release the AioContext lock. Once the rwlock is ultimated and placed in every place it needs to be, we will poll using AIO_WAIT_WHILE_UNLOCKED and remove the AioContext lock. Signed-off-by: Emanuele Giuseppe Esposito Signed-off-by: Kevin Wolf Message-Id: <20230113204212.359076-5-kwolf@redhat.com> Reviewed-by: Emanuele Giuseppe Esposito Signed-off-by: Kevin Wolf --- include/block/block-io.h | 5 ++++- include/block/block_int-common.h | 2 +- include/sysemu/block-backend-io.h | 5 ++++- block.c | 8 ++++---- block/block-backend.c | 4 ++-- block/file-posix.c | 8 ++++---- block/io.c | 12 ++++++------ blockdev.c | 8 +++++++- 8 files changed, 32 insertions(+), 20 deletions(-) diff --git a/include/block/block-io.h b/include/block/block-io.h index 322c7b61c3..3ad053d60c 100644 --- a/include/block/block-io.h +++ b/include/block/block-io.h @@ -133,7 +133,10 @@ bool bdrv_is_read_only(BlockDriverState *bs); bool bdrv_is_writable(BlockDriverState *bs); bool bdrv_is_sg(BlockDriverState *bs); int bdrv_get_flags(BlockDriverState *bs); -bool bdrv_is_inserted(BlockDriverState *bs); + +bool coroutine_fn bdrv_co_is_inserted(BlockDriverState *bs); +bool co_wrapper bdrv_is_inserted(BlockDriverState *bs); + void bdrv_lock_medium(BlockDriverState *bs, bool locked); void bdrv_eject(BlockDriverState *bs, bool eject_flag); const char *bdrv_get_format_name(BlockDriverState *bs); diff --git a/include/block/block_int-common.h b/include/block/block_int-com= mon.h index 253df92509..e6229c64e6 100644 --- a/include/block/block_int-common.h +++ b/include/block/block_int-common.h @@ -708,7 +708,7 @@ struct BlockDriver { BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos); =20 /* removable device specific */ - bool (*bdrv_is_inserted)(BlockDriverState *bs); + bool coroutine_fn (*bdrv_co_is_inserted)(BlockDriverState *bs); void (*bdrv_eject)(BlockDriverState *bs, bool eject_flag); void (*bdrv_lock_medium)(BlockDriverState *bs, bool locked); =20 diff --git a/include/sysemu/block-backend-io.h b/include/sysemu/block-backe= nd-io.h index d8cc8d74f5..4358fc6476 100644 --- a/include/sysemu/block-backend-io.h +++ b/include/sysemu/block-backend-io.h @@ -53,7 +53,10 @@ BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned lo= ng int req, void *buf, =20 void blk_inc_in_flight(BlockBackend *blk); void blk_dec_in_flight(BlockBackend *blk); -bool blk_is_inserted(BlockBackend *blk); + +bool coroutine_fn blk_co_is_inserted(BlockBackend *blk); +bool co_wrapper_mixed blk_is_inserted(BlockBackend *blk); + bool blk_is_available(BlockBackend *blk); void blk_lock_medium(BlockBackend *blk, bool locked); void blk_eject(BlockBackend *blk, bool eject_flag); diff --git a/block.c b/block.c index 3f2bd12857..1bb20bbff6 100644 --- a/block.c +++ b/block.c @@ -6781,7 +6781,7 @@ out: /** * Return TRUE if the media is present */ -bool bdrv_is_inserted(BlockDriverState *bs) +bool coroutine_fn bdrv_co_is_inserted(BlockDriverState *bs) { BlockDriver *drv =3D bs->drv; BdrvChild *child; @@ -6790,11 +6790,11 @@ bool bdrv_is_inserted(BlockDriverState *bs) if (!drv) { return false; } - if (drv->bdrv_is_inserted) { - return drv->bdrv_is_inserted(bs); + if (drv->bdrv_co_is_inserted) { + return drv->bdrv_co_is_inserted(bs); } QLIST_FOREACH(child, &bs->children, next) { - if (!bdrv_is_inserted(child->bs)) { + if (!bdrv_co_is_inserted(child->bs)) { return false; } } diff --git a/block/block-backend.c b/block/block-backend.c index e9cc7d291e..7ba436811b 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -1983,12 +1983,12 @@ void blk_activate(BlockBackend *blk, Error **errp) bdrv_activate(bs, errp); } =20 -bool blk_is_inserted(BlockBackend *blk) +bool coroutine_fn blk_co_is_inserted(BlockBackend *blk) { BlockDriverState *bs =3D blk_bs(blk); IO_CODE(); =20 - return bs && bdrv_is_inserted(bs); + return bs && bdrv_co_is_inserted(bs); } =20 bool blk_is_available(BlockBackend *blk) diff --git a/block/file-posix.c b/block/file-posix.c index 37bf71c5a6..02a8703767 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -3756,7 +3756,7 @@ out: return prio; } =20 -static bool cdrom_is_inserted(BlockDriverState *bs) +static bool coroutine_fn cdrom_co_is_inserted(BlockDriverState *bs) { BDRVRawState *s =3D bs->opaque; int ret; @@ -3823,7 +3823,7 @@ static BlockDriver bdrv_host_cdrom =3D { =3D raw_get_allocated_file_size, =20 /* removable device support */ - .bdrv_is_inserted =3D cdrom_is_inserted, + .bdrv_co_is_inserted =3D cdrom_co_is_inserted, .bdrv_eject =3D cdrom_eject, .bdrv_lock_medium =3D cdrom_lock_medium, =20 @@ -3882,7 +3882,7 @@ static int cdrom_reopen(BlockDriverState *bs) return 0; } =20 -static bool cdrom_is_inserted(BlockDriverState *bs) +static bool coroutine_fn cdrom_co_is_inserted(BlockDriverState *bs) { return raw_getlength(bs) > 0; } @@ -3953,7 +3953,7 @@ static BlockDriver bdrv_host_cdrom =3D { =3D raw_get_allocated_file_size, =20 /* removable device support */ - .bdrv_is_inserted =3D cdrom_is_inserted, + .bdrv_co_is_inserted =3D cdrom_co_is_inserted, .bdrv_eject =3D cdrom_eject, .bdrv_lock_medium =3D cdrom_lock_medium, }; diff --git a/block/io.c b/block/io.c index 32c744d546..03becd32d2 100644 --- a/block/io.c +++ b/block/io.c @@ -1621,7 +1621,7 @@ int coroutine_fn bdrv_co_preadv_part(BdrvChild *child, =20 trace_bdrv_co_preadv_part(bs, offset, bytes, flags); =20 - if (!bdrv_is_inserted(bs)) { + if (!bdrv_co_is_inserted(bs)) { return -ENOMEDIUM; } =20 @@ -2066,7 +2066,7 @@ int coroutine_fn bdrv_co_pwritev_part(BdrvChild *chil= d, =20 trace_bdrv_co_pwritev_part(child->bs, offset, bytes, flags); =20 - if (!bdrv_is_inserted(bs)) { + if (!bdrv_co_is_inserted(bs)) { return -ENOMEDIUM; } =20 @@ -2834,7 +2834,7 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs) =20 bdrv_inc_in_flight(bs); =20 - if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs) || + if (!bdrv_co_is_inserted(bs) || bdrv_is_read_only(bs) || bdrv_is_sg(bs)) { goto early_exit; } @@ -2958,7 +2958,7 @@ int coroutine_fn bdrv_co_pdiscard(BdrvChild *child, i= nt64_t offset, BlockDriverState *bs =3D child->bs; IO_CODE(); =20 - if (!bs || !bs->drv || !bdrv_is_inserted(bs)) { + if (!bs || !bs->drv || !bdrv_co_is_inserted(bs)) { return -ENOMEDIUM; } =20 @@ -3240,7 +3240,7 @@ static int coroutine_fn bdrv_co_copy_range_internal( assert(!(read_flags & BDRV_REQ_NO_WAIT)); assert(!(write_flags & BDRV_REQ_NO_WAIT)); =20 - if (!dst || !dst->bs || !bdrv_is_inserted(dst->bs)) { + if (!dst || !dst->bs || !bdrv_co_is_inserted(dst->bs)) { return -ENOMEDIUM; } ret =3D bdrv_check_request32(dst_offset, bytes, NULL, 0); @@ -3251,7 +3251,7 @@ static int coroutine_fn bdrv_co_copy_range_internal( return bdrv_co_pwrite_zeroes(dst, dst_offset, bytes, write_flags); } =20 - if (!src || !src->bs || !bdrv_is_inserted(src->bs)) { + if (!src || !src->bs || !bdrv_co_is_inserted(src->bs)) { return -ENOMEDIUM; } ret =3D bdrv_check_request32(src_offset, bytes, NULL, 0); diff --git a/blockdev.c b/blockdev.c index ebf952cd21..ec9271ce49 100644 --- a/blockdev.c +++ b/blockdev.c @@ -1023,6 +1023,7 @@ fail: static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp) { BlockDriverState *bs; + AioContext *aio_context; =20 bs =3D bdrv_lookup_bs(name, name, errp); if (bs =3D=3D NULL) { @@ -1034,11 +1035,16 @@ static BlockDriverState *qmp_get_root_bs(const char= *name, Error **errp) return NULL; } =20 + aio_context =3D bdrv_get_aio_context(bs); + aio_context_acquire(aio_context); + if (!bdrv_is_inserted(bs)) { error_setg(errp, "Device has no medium"); - return NULL; + bs =3D NULL; } =20 + aio_context_release(aio_context); + return bs; } =20 --=20 2.38.1