From nobody Fri May 3 04:21:50 2024 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=1679917275; cv=none; d=zohomail.com; s=zohoarc; b=Do0EsKtY6CcvDcZa4HBv3BTnjzoIJN3zacD6voPMyw2xF3W+Rdzz8J9rvLU2wBn+1f1hDdw61VDN4ycZFfokif/im6I9N5EmySDBA2O46aeJCWPSUBnUorkN67XnF3inu6RmNZvFMBRyY2/WnDJTnj/9r7/m2vjAjUZsPaW7O18= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1679917275; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=04jb2mEuGMYCu2cDIuynYzdYP2L58gnXOJT1cPTez8c=; b=lMv1swd4Qd0BGH/lebpCF4nee1kL28UL7waexD63gNboxSlyJTFpdEJ8oBu5YXxSQpDUUocUrpPuwByaRf5XEZvDEkn2Hd94O8BNpvdbU4rd8fYZEH5GDDuALMjPM3Q3wU/EEqmrfAflTKZ0OtOkFxoZ3oVYB1e+z5for7Otd1s= 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 1679917275710997.240430894054; Mon, 27 Mar 2023 04:41:15 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pglDG-0003Sy-1S; Mon, 27 Mar 2023 07:40:34 -0400 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 1pglDB-0003SF-Af for qemu-devel@nongnu.org; Mon, 27 Mar 2023 07:40:29 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pglD8-0004md-Jq for qemu-devel@nongnu.org; Mon, 27 Mar 2023 07:40:28 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-328-_3-H9O1uM5ec8Xk3hk3ILg-1; Mon, 27 Mar 2023 07:40:23 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 02AC73C025CE; Mon, 27 Mar 2023 11:40:23 +0000 (UTC) Received: from merkur.redhat.com (unknown [10.39.194.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1B275406AA66; Mon, 27 Mar 2023 11:40:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679917224; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=04jb2mEuGMYCu2cDIuynYzdYP2L58gnXOJT1cPTez8c=; b=VqufBrObZKIUlOa2u0efvVPLMFMb94cSVEjY8z1cSgi3udMM6Kle/xdc33LoNVegqL5nmG 6QObxs4F35Bkog8FOMILw5ah2bnBxFeH5REcAdOHQil5bT827ELQ80eJxqEYoEqWoVmNGr lBErbNYJaU06VriuIUh+KS5EPKlfNoI= X-MC-Unique: _3-H9O1uM5ec8Xk3hk3ILg-1 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, hreitz@redhat.com, eesposit@redhat.com, ldoktor@redhat.com, qemu-devel@nongnu.org Subject: [PATCH for-8.0] block/export: Fix graph locking in blk_get_geometry() call Date: Mon, 27 Mar 2023 13:39:59 +0200 Message-Id: <20230327113959.60071-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 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.133.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=ham 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: 1679917277287100003 blk_get_geometry() eventually calls bdrv_nb_sectors(), which is a co_wrapper_mixed_bdrv_rdlock. This means that when it is called from coroutine context, it already assume to have the graph locked. However, virtio_blk_sect_range_ok() in block/export/virtio-blk-handler.c (used by vhost-user-blk and VDUSE exports) runs in a coroutine, but doesn't take the graph lock - blk_*() functions are generally expected to do that internally. This causes an assertion failure when accessing an export for the first time if it runs in an iothread. This is an example of the crash: $ ./storage-daemon/qemu-storage-daemon --object iothread,id=3Dth0 --blockde= v file,filename=3D/home/kwolf/images/hd.img,node-name=3Ddisk --export vhost= -user-blk,addr.type=3Dunix,addr.path=3D/tmp/vhost.sock,node-name=3Ddisk,id= =3Dexp0,iothread=3Dth0 qemu-storage-daemon: ../block/graph-lock.c:268: void assert_bdrv_graph_read= able(void): Assertion `qemu_in_main_thread() || reader_count()' failed. (gdb) bt Fix this by creating a new blk_co_get_geometry() that takes the lock, and changing blk_get_geometry() to be a co_wrapper_mixed around it. To make the resulting code cleaner, virtio-blk-handler.c can directly call the coroutine version now (though that wouldn't be necessary for fixing the bug, taking the lock in blk_co_get_geometry() is what fixes it). Fixes: 8ab8140a04cf771d63e9754d6ba6c1e676bfe507 Reported-by: Luk=C3=A1=C5=A1 Doktor Signed-off-by: Kevin Wolf Reviewed-by: Emanuele Giuseppe Esposito --- include/block/block-io.h | 4 +++- include/sysemu/block-backend-io.h | 5 ++++- block.c | 5 +++-- block/block-backend.c | 7 +++++-- block/export/virtio-blk-handler.c | 7 ++++--- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/include/block/block-io.h b/include/block/block-io.h index 5da99d4d60..dbc034b728 100644 --- a/include/block/block-io.h +++ b/include/block/block-io.h @@ -89,7 +89,9 @@ int64_t co_wrapper bdrv_get_allocated_file_size(BlockDriv= erState *bs); =20 BlockMeasureInfo *bdrv_measure(BlockDriver *drv, QemuOpts *opts, BlockDriverState *in_bs, Error **errp); -void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr); + +void coroutine_fn GRAPH_RDLOCK +bdrv_co_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr); =20 int coroutine_fn GRAPH_RDLOCK bdrv_co_delete_file(BlockDriverState *bs, Error **errp); diff --git a/include/sysemu/block-backend-io.h b/include/sysemu/block-backe= nd-io.h index 40ab178719..c672b77247 100644 --- a/include/sysemu/block-backend-io.h +++ b/include/sysemu/block-backend-io.h @@ -70,7 +70,10 @@ void co_wrapper blk_eject(BlockBackend *blk, bool eject_= flag); int64_t coroutine_fn blk_co_getlength(BlockBackend *blk); int64_t co_wrapper_mixed blk_getlength(BlockBackend *blk); =20 -void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr); +void coroutine_fn blk_co_get_geometry(BlockBackend *blk, + uint64_t *nb_sectors_ptr); +void co_wrapper_mixed blk_get_geometry(BlockBackend *blk, + uint64_t *nb_sectors_ptr); =20 int64_t coroutine_fn blk_co_nb_sectors(BlockBackend *blk); int64_t co_wrapper_mixed blk_nb_sectors(BlockBackend *blk); diff --git a/block.c b/block.c index 0dd604d0f6..e0c6c648b1 100644 --- a/block.c +++ b/block.c @@ -5879,9 +5879,10 @@ int64_t coroutine_fn bdrv_co_getlength(BlockDriverSt= ate *bs) } =20 /* return 0 as number of sectors if no device present or error */ -void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) +void coroutine_fn bdrv_co_get_geometry(BlockDriverState *bs, + uint64_t *nb_sectors_ptr) { - int64_t nb_sectors =3D bdrv_nb_sectors(bs); + int64_t nb_sectors =3D bdrv_co_nb_sectors(bs); IO_CODE(); =20 *nb_sectors_ptr =3D nb_sectors < 0 ? 0 : nb_sectors; diff --git a/block/block-backend.c b/block/block-backend.c index 278b04ce69..2ee39229e4 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -1615,13 +1615,16 @@ int64_t coroutine_fn blk_co_getlength(BlockBackend = *blk) return bdrv_co_getlength(blk_bs(blk)); } =20 -void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr) +void coroutine_fn blk_co_get_geometry(BlockBackend *blk, + uint64_t *nb_sectors_ptr) { IO_CODE(); + GRAPH_RDLOCK_GUARD(); + if (!blk_bs(blk)) { *nb_sectors_ptr =3D 0; } else { - bdrv_get_geometry(blk_bs(blk), nb_sectors_ptr); + bdrv_co_get_geometry(blk_bs(blk), nb_sectors_ptr); } } =20 diff --git a/block/export/virtio-blk-handler.c b/block/export/virtio-blk-ha= ndler.c index 313666e8ab..bc1cec6757 100644 --- a/block/export/virtio-blk-handler.c +++ b/block/export/virtio-blk-handler.c @@ -22,8 +22,9 @@ struct virtio_blk_inhdr { unsigned char status; }; =20 -static bool virtio_blk_sect_range_ok(BlockBackend *blk, uint32_t block_siz= e, - uint64_t sector, size_t size) +static bool coroutine_fn +virtio_blk_sect_range_ok(BlockBackend *blk, uint32_t block_size, + uint64_t sector, size_t size) { uint64_t nb_sectors; uint64_t total_sectors; @@ -41,7 +42,7 @@ static bool virtio_blk_sect_range_ok(BlockBackend *blk, u= int32_t block_size, if ((sector << VIRTIO_BLK_SECTOR_BITS) % block_size) { return false; } - blk_get_geometry(blk, &total_sectors); + blk_co_get_geometry(blk, &total_sectors); if (sector > total_sectors || nb_sectors > total_sectors - sector) { return false; } --=20 2.39.2