From nobody Wed Nov 27 09:52:08 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=1699382807; cv=none; d=zohomail.com; s=zohoarc; b=g2Wre0XBYYEFBglS3xiaD7LtQfSSktJ/ylxIOulM+9U7cklNGOG8UtxiIwKJvyBSIPBsUoRKvK2BfLp/eGn9gVIc1xtyu9mIipmLrDkXRTESvafcBlSZaKd9tAEROZllUpSh2APmzXiN8eqyfGgMkV1mwOGDdDY8YTcl4BCS/3w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699382807; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=w5wXt420nfZuaBln/UwssBKJYACD2D3SInbpjRNYO6E=; b=OM920nLW1lXK+dyq3QFkV23fhTcdI86mP+/xkIrLI1vvpJYJeZHvI9YpuxNUDwihZXrrKMBIa200ysmrKPW6KFIgCOtvHWbSTSFOXRFLsqTVbTMM0qXrlg11ypEiZ87N3C46Z0K4GFYLURVBfl26fw3vq2fFD2vLs3foifuujo4= 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 1699382807184822.0334592973254; Tue, 7 Nov 2023 10:46:47 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0R5o-00041H-J2; Tue, 07 Nov 2023 13:46:31 -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 1r0R5d-0003uG-PR for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:18 -0500 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 1r0R5b-0001T4-Hq for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:17 -0500 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-538-nugyPZHSM_S_3P3NhrEQKw-1; Tue, 07 Nov 2023 13:46:08 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A6D3D1C068C9 for ; Tue, 7 Nov 2023 18:46:08 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3079FC1596F; Tue, 7 Nov 2023 18:46:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699382770; 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=w5wXt420nfZuaBln/UwssBKJYACD2D3SInbpjRNYO6E=; b=gqe1STnJHGn4lGpIvjmRpXCxC/OeJKK5QQSWnq5TrbqGF1E7cYl1eXLfC41+0/HlZWA9I3 CJlWAmPAK7TCfDIx1bWjyH1ahUWAmLsogVr3g+Rji3yNGHvbOTcn2CryKqNYzgPbb82OCX SqjWeL0Jb+2PouFCgVQLOX3auOgXTrc= X-MC-Unique: nugyPZHSM_S_3P3NhrEQKw-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 01/25] block: Mark bdrv_probe_blocksizes() and callers GRAPH_RDLOCK Date: Tue, 7 Nov 2023 19:45:41 +0100 Message-ID: <20231107184605.236540-2-kwolf@redhat.com> In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com> References: <20231107184605.236540-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1699382808916100007 Content-Type: text/plain; charset="utf-8" This adds GRAPH_RDLOCK annotations to declare that callers of bdrv_probe_blocksizes() need to hold a reader lock for the graph because it calls bdrv_filter_bs(), which accesses bs->file/backing. Signed-off-by: Kevin Wolf Message-ID: <20231027155333.420094-2-kwolf@redhat.com> Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- include/block/block-global-state.h | 2 +- include/block/block_int-common.h | 3 ++- block/block-backend.c | 2 ++ block/raw-format.c | 3 ++- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/include/block/block-global-state.h b/include/block/block-globa= l-state.h index 6bfafe781d..fca0a40dbd 100644 --- a/include/block/block-global-state.h +++ b/include/block/block-global-state.h @@ -281,7 +281,7 @@ bool bdrv_child_change_aio_context(BdrvChild *c, AioCon= text *ctx, int bdrv_try_change_aio_context(BlockDriverState *bs, AioContext *ctx, BdrvChild *ignore_child, Error **errp); =20 -int bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz); +int GRAPH_RDLOCK bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *b= sz); int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo); =20 void GRAPH_WRLOCK diff --git a/include/block/block_int-common.h b/include/block/block_int-com= mon.h index b8d9d24f39..8abdd2724b 100644 --- a/include/block/block_int-common.h +++ b/include/block/block_int-common.h @@ -386,7 +386,8 @@ struct BlockDriver { * On success, store them in @bsz and return zero. * On failure, return negative errno. */ - int (*bdrv_probe_blocksizes)(BlockDriverState *bs, BlockSizes *bsz); + int GRAPH_RDLOCK_PTR (*bdrv_probe_blocksizes)( + BlockDriverState *bs, BlockSizes *bsz); /** * Try to get @bs's geometry (cyls, heads, sectors) * On success, store them in @geo and return 0. diff --git a/block/block-backend.c b/block/block-backend.c index 39aac1bbce..53cf3bb8b8 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -2666,6 +2666,8 @@ int blk_load_vmstate(BlockBackend *blk, uint8_t *buf,= int64_t pos, int size) int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz) { GLOBAL_STATE_CODE(); + GRAPH_RDLOCK_GUARD_MAINLOOP(); + if (!blk_is_available(blk)) { return -ENOMEDIUM; } diff --git a/block/raw-format.c b/block/raw-format.c index 8ff03adfa4..3fb77b0097 100644 --- a/block/raw-format.c +++ b/block/raw-format.c @@ -543,7 +543,8 @@ static int raw_probe(const uint8_t *buf, int buf_size, = const char *filename) return 1; } =20 -static int raw_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz) +static int GRAPH_RDLOCK +raw_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz) { BDRVRawState *s =3D bs->opaque; int ret; --=20 2.41.0 From nobody Wed Nov 27 09:52:08 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=1699382941; cv=none; d=zohomail.com; s=zohoarc; b=BkDdpz7AC5KDG3rdl5lEdRZdo5oIn+5+dIyRZfZWTE0Bz7IwDw4tKW3dzY19InmBtVR9//F2htRYy+wu/idz3QTYgZ7bQE8I25yhl+1qCNMB2HhI44elEweDa1bnOYm7xU4/1WhPoPfUKhJo/okB4dpHi1uXbA2D3eLoDB3OFIQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699382941; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=tsO1hoc8KHZp/hJdF9DKjncWu0YA2pJ95srb5fxo7iQ=; b=nMXi+hbb+GFN3YFxnxGrAIalR3ZV3pk4L7c5ae7OM7bX3TFlzTh2shxsjr5tdtAhr73hRmynWMkb++Wdg7+V0B6K9s/9ImEWg0Bp7p3z/bRwV2Ou+RrYFvddkmkXiCTphmOzX8PIWfjTmh4mLqCVqFqMzMdqgJvAdSdGT6aTk0E= 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 1699382941907798.1239632873775; Tue, 7 Nov 2023 10:49:01 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0R68-0004HB-Pg; Tue, 07 Nov 2023 13:46:48 -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 1r0R5f-0003vJ-IR for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:23 -0500 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 1r0R5b-0001TP-Pd for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:18 -0500 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-564-HMXChmdxPUC6NKe4COhifQ-1; Tue, 07 Nov 2023 13:46:09 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5D68738143AC for ; Tue, 7 Nov 2023 18:46:09 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id DD761C1596F; Tue, 7 Nov 2023 18:46:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699382771; 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=tsO1hoc8KHZp/hJdF9DKjncWu0YA2pJ95srb5fxo7iQ=; b=gpTuPubOeP0HuNQVcSYM9sVJrsixdnh4hegsKRld18Z4UOjArjZQNXVHQLhXx6PdV2aVGr TXaHCCcdbbqUqlflKepBhTkKNp9R1fMh0tcihGXx8wETPkmmNviDvFWCZYenwzrJjXdIAg Mz7CzxrcZ94xwOB7t5sdnGsdFcPQfAI= X-MC-Unique: HMXChmdxPUC6NKe4COhifQ-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 02/25] block: Mark bdrv_has_zero_init() and callers GRAPH_RDLOCK Date: Tue, 7 Nov 2023 19:45:42 +0100 Message-ID: <20231107184605.236540-3-kwolf@redhat.com> In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com> References: <20231107184605.236540-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1699382943580100011 Content-Type: text/plain; charset="utf-8" This adds GRAPH_RDLOCK annotations to declare that callers of bdrv_has_zero_init() need to hold a reader lock for the graph because it calls bdrv_filter_bs(), which accesses bs->file/backing. Signed-off-by: Kevin Wolf Message-ID: <20231027155333.420094-3-kwolf@redhat.com> Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- include/block/block-global-state.h | 2 +- include/block/block_int-common.h | 2 +- block.c | 2 +- block/qcow2.c | 3 ++- block/raw-format.c | 2 +- block/vdi.c | 2 +- block/vhdx.c | 13 +++++++++---- block/vmdk.c | 2 +- block/vpc.c | 2 +- blockdev.c | 2 ++ qemu-img.c | 2 ++ 11 files changed, 22 insertions(+), 12 deletions(-) diff --git a/include/block/block-global-state.h b/include/block/block-globa= l-state.h index fca0a40dbd..3ae468ea15 100644 --- a/include/block/block-global-state.h +++ b/include/block/block-global-state.h @@ -189,7 +189,7 @@ void bdrv_drain_all(void); void bdrv_aio_cancel(BlockAIOCB *acb); =20 int bdrv_has_zero_init_1(BlockDriverState *bs); -int bdrv_has_zero_init(BlockDriverState *bs); +int coroutine_mixed_fn GRAPH_RDLOCK bdrv_has_zero_init(BlockDriverState *b= s); BlockDriverState *bdrv_find_node(const char *node_name); BlockDeviceInfoList *bdrv_named_nodes_list(bool flat, Error **errp); XDbgBlockGraph * GRAPH_RDLOCK bdrv_get_xdbg_block_graph(Error **errp); diff --git a/include/block/block_int-common.h b/include/block/block_int-com= mon.h index 8abdd2724b..c0db862de7 100644 --- a/include/block/block_int-common.h +++ b/include/block/block_int-common.h @@ -349,7 +349,7 @@ struct BlockDriver { * Returns 1 if newly created images are guaranteed to contain only * zeros, 0 otherwise. */ - int (*bdrv_has_zero_init)(BlockDriverState *bs); + int GRAPH_RDLOCK_PTR (*bdrv_has_zero_init)(BlockDriverState *bs); =20 /* * Remove fd handlers, timers, and other event loop callbacks so the e= vent diff --git a/block.c b/block.c index a527aa1a4c..1171f47570 100644 --- a/block.c +++ b/block.c @@ -6587,7 +6587,7 @@ int bdrv_has_zero_init_1(BlockDriverState *bs) return 1; } =20 -int bdrv_has_zero_init(BlockDriverState *bs) +int coroutine_mixed_fn bdrv_has_zero_init(BlockDriverState *bs) { BlockDriverState *filtered; GLOBAL_STATE_CODE(); diff --git a/block/qcow2.c b/block/qcow2.c index aa01d9e7b5..a1443a31aa 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -5302,7 +5302,8 @@ static ImageInfoSpecific *qcow2_get_specific_info(Blo= ckDriverState *bs, return spec_info; } =20 -static int coroutine_mixed_fn qcow2_has_zero_init(BlockDriverState *bs) +static int coroutine_mixed_fn GRAPH_RDLOCK +qcow2_has_zero_init(BlockDriverState *bs) { BDRVQcow2State *s =3D bs->opaque; bool preallocated; diff --git a/block/raw-format.c b/block/raw-format.c index 3fb77b0097..8ca74c1cf3 100644 --- a/block/raw-format.c +++ b/block/raw-format.c @@ -452,7 +452,7 @@ raw_co_ioctl(BlockDriverState *bs, unsigned long int re= q, void *buf) return bdrv_co_ioctl(bs->file->bs, req, buf); } =20 -static int raw_has_zero_init(BlockDriverState *bs) +static int GRAPH_RDLOCK raw_has_zero_init(BlockDriverState *bs) { return bdrv_has_zero_init(bs->file->bs); } diff --git a/block/vdi.c b/block/vdi.c index 7cfd12b50d..8e144ce523 100644 --- a/block/vdi.c +++ b/block/vdi.c @@ -990,7 +990,7 @@ static void vdi_close(BlockDriverState *bs) migrate_del_blocker(&s->migration_blocker); } =20 -static int vdi_has_zero_init(BlockDriverState *bs) +static int GRAPH_RDLOCK vdi_has_zero_init(BlockDriverState *bs) { BDRVVdiState *s =3D bs->opaque; =20 diff --git a/block/vhdx.c b/block/vhdx.c index a9d08742f9..e136ba1ae1 100644 --- a/block/vhdx.c +++ b/block/vhdx.c @@ -1695,7 +1695,7 @@ exit: * Fixed images: default state of the BAT is fully populated, with * file offsets and state PAYLOAD_BLOCK_FULLY_PRESENT. */ -static int coroutine_fn +static int coroutine_fn GRAPH_UNLOCKED vhdx_create_bat(BlockBackend *blk, BDRVVHDXState *s, uint64_t image_size, VHDXImageType type, bool use_zero_blocks, uint64_t file_offset, @@ -1708,6 +1708,7 @@ vhdx_create_bat(BlockBackend *blk, BDRVVHDXState *s, uint64_t unused; int block_state; VHDXSectorInfo sinfo; + bool has_zero_init; =20 assert(s->bat =3D=3D NULL); =20 @@ -1737,9 +1738,13 @@ vhdx_create_bat(BlockBackend *blk, BDRVVHDXState *s, goto exit; } =20 + bdrv_graph_co_rdlock(); + has_zero_init =3D bdrv_has_zero_init(blk_bs(blk)); + bdrv_graph_co_rdunlock(); + if (type =3D=3D VHDX_TYPE_FIXED || use_zero_blocks || - bdrv_has_zero_init(blk_bs(blk)) =3D=3D 0) { + has_zero_init =3D=3D 0) { /* for a fixed file, the default BAT entry is not zero */ s->bat =3D g_try_malloc0(length); if (length && s->bat =3D=3D NULL) { @@ -1782,7 +1787,7 @@ exit: * to create the BAT itself, we will also cause the BAT to be * created. */ -static int coroutine_fn +static int coroutine_fn GRAPH_UNLOCKED vhdx_create_new_region_table(BlockBackend *blk, uint64_t image_size, uint32_t block_size, uint32_t sector_size, uint32_t log_size, bool use_zero_blocks, @@ -2173,7 +2178,7 @@ static int coroutine_fn vhdx_co_check(BlockDriverStat= e *bs, return 0; } =20 -static int vhdx_has_zero_init(BlockDriverState *bs) +static int GRAPH_RDLOCK vhdx_has_zero_init(BlockDriverState *bs) { BDRVVHDXState *s =3D bs->opaque; int state; diff --git a/block/vmdk.c b/block/vmdk.c index 85864b8045..91ed7a8d93 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -2894,7 +2894,7 @@ vmdk_co_get_allocated_file_size(BlockDriverState *bs) return ret; } =20 -static int vmdk_has_zero_init(BlockDriverState *bs) +static int GRAPH_RDLOCK vmdk_has_zero_init(BlockDriverState *bs) { int i; BDRVVmdkState *s =3D bs->opaque; diff --git a/block/vpc.c b/block/vpc.c index aa1a48ae0e..483775103c 100644 --- a/block/vpc.c +++ b/block/vpc.c @@ -1170,7 +1170,7 @@ fail: } =20 =20 -static int vpc_has_zero_init(BlockDriverState *bs) +static int GRAPH_RDLOCK vpc_has_zero_init(BlockDriverState *bs) { BDRVVPCState *s =3D bs->opaque; =20 diff --git a/blockdev.c b/blockdev.c index e9b7e38dc4..148df99e00 100644 --- a/blockdev.c +++ b/blockdev.c @@ -3156,9 +3156,11 @@ void qmp_drive_mirror(DriveMirror *arg, Error **errp) return; } =20 + bdrv_graph_rdlock_main_loop(); zero_target =3D (arg->sync =3D=3D MIRROR_SYNC_MODE_FULL && (arg->mode =3D=3D NEW_IMAGE_MODE_EXISTING || !bdrv_has_zero_init(target_bs))); + bdrv_graph_rdunlock_main_loop(); =20 =20 /* Honor bdrv_try_change_aio_context() context acquisition requirement= s. */ diff --git a/qemu-img.c b/qemu-img.c index 369c2e8ddf..c061fd0634 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -2099,7 +2099,9 @@ static int convert_do_copy(ImgConvertState *s) /* Check whether we have zero initialisation or can get it efficiently= */ if (!s->has_zero_init && s->target_is_new && s->min_sparse && !s->target_has_backing) { + bdrv_graph_rdlock_main_loop(); s->has_zero_init =3D bdrv_has_zero_init(blk_bs(s->target)); + bdrv_graph_rdunlock_main_loop(); } =20 /* Allocate buffer for copied data. For compressed images, only one cl= uster --=20 2.41.0 From nobody Wed Nov 27 09:52:08 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=1699382927; cv=none; d=zohomail.com; s=zohoarc; b=JBkmb7eAHeYY+nBSJgPJrU+ZaxwgWh62BcyOVPRr64KkWx6tivp824krGbY7RGQoAO3Wcdc6QFGRezlBGrrKqiE1KISqtfVaCy7XxpOVX67xyzL6Yf/vzxgzDv+LehfcOOyLznwsNVVbS3lD502tvT/tFSxbszb2FBm8yIQUw0Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699382927; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=UFzMc3zv8P1+utKXWBljx4/dYa3FjPUL/pDvRRvZ4Mg=; b=LXPxOCHP461JcTXijaSdk7pdbCNCxL6lqkRbRHBK9iBUaC/UwEdfq2E3hJDjgZXij86GqZe+xzzxj5u1kPT7779fS7HuD0qS+eMOm3MMPjrvq43OCaeGyoEml+waCHoLHRxdATaWR8WdPPjhkpGSyZvZp1FrZ+Mr2B6svarOJLQ= 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 1699382927532634.3002407436439; Tue, 7 Nov 2023 10:48:47 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0R6B-0004Pr-A1; Tue, 07 Nov 2023 13:46:51 -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 1r0R5d-0003uI-QO for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:18 -0500 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 1r0R5b-0001TU-Ht for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:17 -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.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-578-T8Gd181fNWWj4wdrmhltgA-1; Tue, 07 Nov 2023 13:46:10 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 14A71101A529 for ; Tue, 7 Nov 2023 18:46:10 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id 937A4C1596F; Tue, 7 Nov 2023 18:46:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699382771; 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=UFzMc3zv8P1+utKXWBljx4/dYa3FjPUL/pDvRRvZ4Mg=; b=KF08zt9SLDZf5F0Ksg4zkBug6DYzHJwLCifHGQEIRpOt7BI/WHcP8GEkOeX34D9BNeU9pl 5/ZvjIxK8t/5dQtZtAuThnqpOrGqfeXmqQyRJwL4vipa8jaVgx64OJzsKZL4S6QVnbe8wd chl6aAodfGA6UkU6JDMlmK+D6lNGRJs= X-MC-Unique: T8Gd181fNWWj4wdrmhltgA-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 03/25] block: Mark bdrv_filter_bs() and callers GRAPH_RDLOCK Date: Tue, 7 Nov 2023 19:45:43 +0100 Message-ID: <20231107184605.236540-4-kwolf@redhat.com> In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com> References: <20231107184605.236540-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1699382929509100011 Content-Type: text/plain; charset="utf-8" This adds GRAPH_RDLOCK annotations to declare that callers of bdrv_filter_bs() need to hold a reader lock for the graph because it calls bdrv_filter_child(), which accesses bs->file/backing. Signed-off-by: Kevin Wolf Message-ID: <20231027155333.420094-4-kwolf@redhat.com> Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- include/block/block-io.h | 2 +- include/block/block_int-io.h | 3 ++- block.c | 9 +++++++-- block/stream.c | 2 ++ migration/block-dirty-bitmap.c | 4 ++++ 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/include/block/block-io.h b/include/block/block-io.h index ad270b6ad2..58c4cf50a0 100644 --- a/include/block/block-io.h +++ b/include/block/block-io.h @@ -183,7 +183,7 @@ bdrv_co_eject(BlockDriverState *bs, bool eject_flag); =20 const char *bdrv_get_format_name(BlockDriverState *bs); =20 -bool bdrv_supports_compressed_writes(BlockDriverState *bs); +bool GRAPH_RDLOCK bdrv_supports_compressed_writes(BlockDriverState *bs); const char *bdrv_get_node_name(const BlockDriverState *bs); =20 const char * GRAPH_RDLOCK diff --git a/include/block/block_int-io.h b/include/block/block_int-io.h index 34eac72d7a..26bff94e4e 100644 --- a/include/block/block_int-io.h +++ b/include/block/block_int-io.h @@ -143,7 +143,8 @@ static inline BlockDriverState *bdrv_cow_bs(BlockDriver= State *bs) return child_bs(bdrv_cow_child(bs)); } =20 -static inline BlockDriverState *bdrv_filter_bs(BlockDriverState *bs) +static inline BlockDriverState * GRAPH_RDLOCK +bdrv_filter_bs(BlockDriverState *bs) { IO_CODE(); return child_bs(bdrv_filter_child(bs)); diff --git a/block.c b/block.c index 1171f47570..d85738b7dc 100644 --- a/block.c +++ b/block.c @@ -820,12 +820,17 @@ int bdrv_probe_blocksizes(BlockDriverState *bs, Block= Sizes *bsz) int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo) { BlockDriver *drv =3D bs->drv; - BlockDriverState *filtered =3D bdrv_filter_bs(bs); + BlockDriverState *filtered; + GLOBAL_STATE_CODE(); + GRAPH_RDLOCK_GUARD_MAINLOOP(); =20 if (drv && drv->bdrv_probe_geometry) { return drv->bdrv_probe_geometry(bs, geo); - } else if (filtered) { + } + + filtered =3D bdrv_filter_bs(bs); + if (filtered) { return bdrv_probe_geometry(filtered, geo); } =20 diff --git a/block/stream.c b/block/stream.c index ddaab7dbbd..b22d9c236b 100644 --- a/block/stream.c +++ b/block/stream.c @@ -268,6 +268,8 @@ void stream_start(const char *job_id, BlockDriverState = *bs, assert(!bottom->drv->is_filter); base_overlay =3D above_base =3D bottom; } else { + GRAPH_RDLOCK_GUARD_MAINLOOP(); + base_overlay =3D bdrv_find_overlay(bs, base); if (!base_overlay) { error_setg(errp, "'%s' is not in the backing chain of '%s'", diff --git a/migration/block-dirty-bitmap.c b/migration/block-dirty-bitmap.c index 03cb2e72ee..24347ab0f7 100644 --- a/migration/block-dirty-bitmap.c +++ b/migration/block-dirty-bitmap.c @@ -607,6 +607,10 @@ static int init_dirty_bitmap_migration(DBMSaveState *s) BlockBackend *blk; GHashTable *alias_map =3D NULL; =20 + /* Runs in the migration thread, but holds the iothread lock */ + GLOBAL_STATE_CODE(); + GRAPH_RDLOCK_GUARD_MAINLOOP(); + if (migrate_has_block_bitmap_mapping()) { alias_map =3D construct_alias_map(migrate_block_bitmap_mapping(), = true, &error_abort); --=20 2.41.0 From nobody Wed Nov 27 09:52:08 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=1699382804; cv=none; d=zohomail.com; s=zohoarc; b=Rv8/V498Pc8NaOB+5czvEiHtBZH4A/B7AYcoMdcYlYMt/pvBRUpe8D6gHSAS/fu/zc0gY7akugr+uXgJhhE/ZkjSalX29vpG6K9vmW8kfWLhgs8iV/u1fbbLzKXr2LBAUb3e+BDXE2Sf51AFAbAI+ATiyiSFi/jD1mTNotkPxLQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699382804; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=tSDHhFfE5MUpEEQ9ubxFJ68KIm/3nj5cwYGsrXRHwGk=; b=Fr9RkijCMAB5icigGmNGMrx/EyRz+SF8yHs4HVR5Oua5Iq0svxEzfI6UFo+o5tFzJK8HVokUshpBGQ71fT0Wqnuongkd6WzsanAhaCOB2xv3MLsfcaFSYw5vt450kMC//X9PtV0ByttmrzaXIurVYk06hp3eH6565+U4spPyz+Y= 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 16993828047661006.483949911521; Tue, 7 Nov 2023 10:46:44 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0R5j-0003vt-Gb; Tue, 07 Nov 2023 13:46:23 -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 1r0R5d-0003uJ-R9 for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:18 -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 1r0R5b-0001TY-JI for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:17 -0500 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-651-10NxzuSEOru66IEy6wQ95A-1; Tue, 07 Nov 2023 13:46:11 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BE70E1C068D2 for ; Tue, 7 Nov 2023 18:46:10 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4A7ADC1596F; Tue, 7 Nov 2023 18:46:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699382772; 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=tSDHhFfE5MUpEEQ9ubxFJ68KIm/3nj5cwYGsrXRHwGk=; b=CqVBnhFvBUnTPoCj3PW3QoUBcobFAnWF3vdezE1WqG+GPZ8M0qYyqy/c1p93VxAsc0ozw/ 8MC35fg3dST3c3JIorfzZYxhVfkqXRdgdGu3xgEgOd1Ar2qvEiMKfz9m+ckK7yLuosB5qU la1vPR8+Ee9sMz7MZS1/uPs+55n29yI= X-MC-Unique: 10NxzuSEOru66IEy6wQ95A-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 04/25] block: Mark bdrv_root_attach_child() GRAPH_WRLOCK Date: Tue, 7 Nov 2023 19:45:44 +0100 Message-ID: <20231107184605.236540-5-kwolf@redhat.com> In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com> References: <20231107184605.236540-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1699382806940100003 Content-Type: text/plain; charset="utf-8" Instead of taking the writer lock internally, require callers to already hold it when calling bdrv_root_attach_child(). These callers will typically already hold the graph lock once the locking work is completed, which means that they can't call functions that take it internally. Signed-off-by: Kevin Wolf Message-ID: <20231027155333.420094-5-kwolf@redhat.com> Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- include/block/block_int-global-state.h | 13 +++++++------ block.c | 5 +---- block/block-backend.c | 2 ++ blockjob.c | 2 ++ 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/include/block/block_int-global-state.h b/include/block/block_i= nt-global-state.h index 074b677838..afce6c4416 100644 --- a/include/block/block_int-global-state.h +++ b/include/block/block_int-global-state.h @@ -196,12 +196,13 @@ BlockJob *backup_job_create(const char *job_id, Block= DriverState *bs, BlockCompletionFunc *cb, void *opaque, JobTxn *txn, Error **errp); =20 -BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs, - const char *child_name, - const BdrvChildClass *child_class, - BdrvChildRole child_role, - uint64_t perm, uint64_t shared_perm, - void *opaque, Error **errp); +BdrvChild * GRAPH_WRLOCK +bdrv_root_attach_child(BlockDriverState *child_bs, const char *child_name, + const BdrvChildClass *child_class, + BdrvChildRole child_role, + uint64_t perm, uint64_t shared_perm, + void *opaque, Error **errp); + void GRAPH_WRLOCK bdrv_root_unref_child(BdrvChild *child); =20 void GRAPH_RDLOCK bdrv_get_cumulative_perm(BlockDriverState *bs, uint64_t = *perm, diff --git a/block.c b/block.c index d85738b7dc..5f92eb4950 100644 --- a/block.c +++ b/block.c @@ -3214,8 +3214,6 @@ BdrvChild *bdrv_root_attach_child(BlockDriverState *c= hild_bs, =20 GLOBAL_STATE_CODE(); =20 - bdrv_graph_wrlock(child_bs); - child =3D bdrv_attach_child_common(child_bs, child_name, child_class, child_role, perm, shared_perm, opaque, tran, errp); @@ -3228,9 +3226,8 @@ BdrvChild *bdrv_root_attach_child(BlockDriverState *c= hild_bs, =20 out: tran_finalize(tran, ret); - bdrv_graph_wrunlock(); =20 - bdrv_unref(child_bs); + bdrv_schedule_unref(child_bs); =20 return ret < 0 ? NULL : child; } diff --git a/block/block-backend.c b/block/block-backend.c index 53cf3bb8b8..075a0dfa95 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -931,10 +931,12 @@ int blk_insert_bs(BlockBackend *blk, BlockDriverState= *bs, Error **errp) ThrottleGroupMember *tgm =3D &blk->public.throttle_group_member; GLOBAL_STATE_CODE(); bdrv_ref(bs); + bdrv_graph_wrlock(bs); blk->root =3D bdrv_root_attach_child(bs, "root", &child_root, BDRV_CHILD_FILTERED | BDRV_CHILD_PR= IMARY, blk->perm, blk->shared_perm, blk, errp); + bdrv_graph_wrunlock(); if (blk->root =3D=3D NULL) { return -EPERM; } diff --git a/blockjob.c b/blockjob.c index 5b24de356d..7920f6e500 100644 --- a/blockjob.c +++ b/blockjob.c @@ -248,8 +248,10 @@ int block_job_add_bdrv(BlockJob *job, const char *name= , BlockDriverState *bs, } aio_context_acquire(ctx); } + bdrv_graph_wrlock(bs); c =3D bdrv_root_attach_child(bs, name, &child_job, 0, perm, shared_per= m, job, errp); + bdrv_graph_wrunlock(); if (need_context_ops) { aio_context_release(ctx); if (job->job.aio_context !=3D qemu_get_aio_context()) { --=20 2.41.0 From nobody Wed Nov 27 09:52:08 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=1699382855; cv=none; d=zohomail.com; s=zohoarc; b=ZIH2LIK8nXz1ec/tF1zb5tXTbIQnhvvkh0oSYnTzEj4GOrKzqQvBi09+F8V4HtAf3B641/GilDcB2wuRQR9bFLqtubQISQZxbCKhp1JZPjMZAP/S0UMGoqZwH8Mf3rAQ+yc5dfu/Lb91PN/FZlzYR6BSEDQkxk1XwTZ0q4kzEPI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699382855; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=473Ecpc2faSQYEtUrg89rO0F58KFORSbfSxuL6EM7og=; b=IdGd5fE5JPdvfH5oOHaa+XCPmTWYv701pr3nt3Z5JbGjBL1AU/vrIM//0Uq35nG7ZKSKr1Ieo+FmVF4cc1dWAt+wRXtLwbR41ZIIKCTVsD68X34Ws4gv9Me/S5/YgH1maSg5kxYDAxXCf8L7RE0CbkvTx8WjhkBgC52omzFCzKE= 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 1699382855257947.9611652854801; Tue, 7 Nov 2023 10:47:35 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0R6D-0004Sm-JS; Tue, 07 Nov 2023 13:46:53 -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 1r0R5d-0003uK-VF for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:18 -0500 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 1r0R5b-0001Tc-I1 for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:17 -0500 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-636-DixG_fAGNn2qpweRM-P4Ug-1; Tue, 07 Nov 2023 13:46:11 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 779CF2803023 for ; Tue, 7 Nov 2023 18:46:11 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0151FC1290F; Tue, 7 Nov 2023 18:46:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699382773; 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=473Ecpc2faSQYEtUrg89rO0F58KFORSbfSxuL6EM7og=; b=IRHSU45oJbpG+SmTJmqLCPTECB2lQvszI183yY/gTozGpUAQFR2jjWARdquELtiBlmR3SR A9lm70sdMROcBFR6ZvezbQ8gST/SXnXCB04cSIBEDc/ZWBER7k9UoC5LGkmWm378WfUAXi JHiroPGz7zHEkAiNPm3VDK1ppPkYybE= X-MC-Unique: DixG_fAGNn2qpweRM-P4Ug-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 05/25] block: Mark block_job_add_bdrv() GRAPH_WRLOCK Date: Tue, 7 Nov 2023 19:45:45 +0100 Message-ID: <20231107184605.236540-6-kwolf@redhat.com> In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com> References: <20231107184605.236540-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1699382857148100003 Content-Type: text/plain; charset="utf-8" Instead of taking the writer lock internally, require callers to already hold it when calling block_job_add_bdrv(). These callers will typically already hold the graph lock once the locking work is completed, which means that they can't call functions that take it internally. Signed-off-by: Kevin Wolf Message-ID: <20231027155333.420094-6-kwolf@redhat.com> Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- include/block/blockjob.h | 5 +++-- include/block/blockjob_int.h | 9 +++++---- block/backup.c | 21 +++++++++++++++------ block/commit.c | 5 +++++ block/mirror.c | 5 +++++ block/stream.c | 4 ++++ blockjob.c | 8 +++++--- tests/unit/test-bdrv-drain.c | 3 +++ 8 files changed, 45 insertions(+), 15 deletions(-) diff --git a/include/block/blockjob.h b/include/block/blockjob.h index 95854f1477..e594c10d23 100644 --- a/include/block/blockjob.h +++ b/include/block/blockjob.h @@ -138,8 +138,9 @@ BlockJob *block_job_get_locked(const char *id); * @job. This means that all operations will be blocked on @bs while * @job exists. */ -int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *= bs, - uint64_t perm, uint64_t shared_perm, Error **errp); +int GRAPH_WRLOCK +block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs, + uint64_t perm, uint64_t shared_perm, Error **errp); =20 /** * block_job_remove_all_bdrv: diff --git a/include/block/blockjob_int.h b/include/block/blockjob_int.h index 18ee6f7bf0..4c3d2e25a2 100644 --- a/include/block/blockjob_int.h +++ b/include/block/blockjob_int.h @@ -111,10 +111,11 @@ struct BlockJobDriver { * This function is not part of the public job interface; it should be * called from a wrapper that is specific to the job type. */ -void *block_job_create(const char *job_id, const BlockJobDriver *driver, - JobTxn *txn, BlockDriverState *bs, uint64_t perm, - uint64_t shared_perm, int64_t speed, int flags, - BlockCompletionFunc *cb, void *opaque, Error **errp= ); +void * GRAPH_UNLOCKED +block_job_create(const char *job_id, const BlockJobDriver *driver, + JobTxn *txn, BlockDriverState *bs, uint64_t perm, + uint64_t shared_perm, int64_t speed, int flags, + BlockCompletionFunc *cb, void *opaque, Error **errp); =20 /** * block_job_free: diff --git a/block/backup.c b/block/backup.c index 9a3c4bdc82..5bad7d116f 100644 --- a/block/backup.c +++ b/block/backup.c @@ -374,7 +374,6 @@ BlockJob *backup_job_create(const char *job_id, BlockDr= iverState *bs, assert(bs); assert(target); GLOBAL_STATE_CODE(); - GRAPH_RDLOCK_GUARD_MAINLOOP(); =20 /* QMP interface protects us from these cases */ assert(sync_mode !=3D MIRROR_SYNC_MODE_INCREMENTAL); @@ -385,31 +384,33 @@ BlockJob *backup_job_create(const char *job_id, Block= DriverState *bs, return NULL; } =20 + bdrv_graph_rdlock_main_loop(); if (!bdrv_is_inserted(bs)) { error_setg(errp, "Device is not inserted: %s", bdrv_get_device_name(bs)); - return NULL; + goto error_rdlock; } =20 if (!bdrv_is_inserted(target)) { error_setg(errp, "Device is not inserted: %s", bdrv_get_device_name(target)); - return NULL; + goto error_rdlock; } =20 if (compress && !bdrv_supports_compressed_writes(target)) { error_setg(errp, "Compression is not supported for this drive %s", bdrv_get_device_name(target)); - return NULL; + goto error_rdlock; } =20 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { - return NULL; + goto error_rdlock; } =20 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_BACKUP_TARGET, errp)) { - return NULL; + goto error_rdlock; } + bdrv_graph_rdunlock_main_loop(); =20 if (perf->max_workers < 1 || perf->max_workers > INT_MAX) { error_setg(errp, "max-workers must be between 1 and %d", INT_MAX); @@ -437,6 +438,7 @@ BlockJob *backup_job_create(const char *job_id, BlockDr= iverState *bs, =20 len =3D bdrv_getlength(bs); if (len < 0) { + GRAPH_RDLOCK_GUARD_MAINLOOP(); error_setg_errno(errp, -len, "Unable to get length for '%s'", bdrv_get_device_or_node_name(bs)); goto error; @@ -444,6 +446,7 @@ BlockJob *backup_job_create(const char *job_id, BlockDr= iverState *bs, =20 target_len =3D bdrv_getlength(target); if (target_len < 0) { + GRAPH_RDLOCK_GUARD_MAINLOOP(); error_setg_errno(errp, -target_len, "Unable to get length for '%s'= ", bdrv_get_device_or_node_name(bs)); goto error; @@ -493,8 +496,10 @@ BlockJob *backup_job_create(const char *job_id, BlockD= riverState *bs, block_copy_set_speed(bcs, speed); =20 /* Required permissions are taken by copy-before-write filter target */ + bdrv_graph_wrlock(target); block_job_add_bdrv(&job->common, "target", target, 0, BLK_PERM_ALL, &error_abort); + bdrv_graph_wrunlock(); =20 return &job->common; =20 @@ -507,4 +512,8 @@ BlockJob *backup_job_create(const char *job_id, BlockDr= iverState *bs, } =20 return NULL; + +error_rdlock: + bdrv_graph_rdunlock_main_loop(); + return NULL; } diff --git a/block/commit.c b/block/commit.c index 43d1de7577..fc3ad79749 100644 --- a/block/commit.c +++ b/block/commit.c @@ -342,6 +342,7 @@ void commit_start(const char *job_id, BlockDriverState = *bs, */ iter_shared_perms =3D BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE; =20 + bdrv_graph_wrlock(top); for (iter =3D top; iter !=3D base; iter =3D bdrv_filter_or_cow_bs(iter= )) { if (iter =3D=3D filtered_base) { /* @@ -354,16 +355,20 @@ void commit_start(const char *job_id, BlockDriverStat= e *bs, ret =3D block_job_add_bdrv(&s->common, "intermediate node", iter, = 0, iter_shared_perms, errp); if (ret < 0) { + bdrv_graph_wrunlock(); goto fail; } } =20 if (bdrv_freeze_backing_chain(commit_top_bs, base, errp) < 0) { + bdrv_graph_wrunlock(); goto fail; } s->chain_frozen =3D true; =20 ret =3D block_job_add_bdrv(&s->common, "base", base, 0, BLK_PERM_ALL, = errp); + bdrv_graph_wrunlock(); + if (ret < 0) { goto fail; } diff --git a/block/mirror.c b/block/mirror.c index c839542774..a03247a31b 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -1888,11 +1888,13 @@ static BlockJob *mirror_start_job( */ bdrv_disable_dirty_bitmap(s->dirty_bitmap); =20 + bdrv_graph_wrlock(bs); ret =3D block_job_add_bdrv(&s->common, "source", bs, 0, BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE | BLK_PERM_CONSISTENT_READ, errp); if (ret < 0) { + bdrv_graph_wrunlock(); goto fail; } =20 @@ -1937,14 +1939,17 @@ static BlockJob *mirror_start_job( ret =3D block_job_add_bdrv(&s->common, "intermediate node", it= er, 0, iter_shared_perms, errp); if (ret < 0) { + bdrv_graph_wrunlock(); goto fail; } } =20 if (bdrv_freeze_backing_chain(mirror_top_bs, target, errp) < 0) { + bdrv_graph_wrunlock(); goto fail; } } + bdrv_graph_wrunlock(); =20 QTAILQ_INIT(&s->ops_in_flight); =20 diff --git a/block/stream.c b/block/stream.c index b22d9c236b..51333e460b 100644 --- a/block/stream.c +++ b/block/stream.c @@ -352,8 +352,10 @@ void stream_start(const char *job_id, BlockDriverState= *bs, * already have our own plans. Also don't allow resize as the image si= ze is * queried only at the job start and then cached. */ + bdrv_graph_wrlock(bs); if (block_job_add_bdrv(&s->common, "active node", bs, 0, basic_flags | BLK_PERM_WRITE, errp)) { + bdrv_graph_wrunlock(); goto fail; } =20 @@ -373,9 +375,11 @@ void stream_start(const char *job_id, BlockDriverState= *bs, ret =3D block_job_add_bdrv(&s->common, "intermediate node", iter, = 0, basic_flags, errp); if (ret < 0) { + bdrv_graph_wrunlock(); goto fail; } } + bdrv_graph_wrunlock(); =20 s->base_overlay =3D base_overlay; s->above_base =3D above_base; diff --git a/blockjob.c b/blockjob.c index 7920f6e500..af44322cbe 100644 --- a/blockjob.c +++ b/blockjob.c @@ -248,10 +248,8 @@ int block_job_add_bdrv(BlockJob *job, const char *name= , BlockDriverState *bs, } aio_context_acquire(ctx); } - bdrv_graph_wrlock(bs); c =3D bdrv_root_attach_child(bs, name, &child_job, 0, perm, shared_per= m, job, errp); - bdrv_graph_wrunlock(); if (need_context_ops) { aio_context_release(ctx); if (job->job.aio_context !=3D qemu_get_aio_context()) { @@ -515,7 +513,8 @@ void *block_job_create(const char *job_id, const BlockJ= obDriver *driver, BlockJob *job; int ret; GLOBAL_STATE_CODE(); - GRAPH_RDLOCK_GUARD_MAINLOOP(); + + bdrv_graph_wrlock(bs); =20 if (job_id =3D=3D NULL && !(flags & JOB_INTERNAL)) { job_id =3D bdrv_get_device_name(bs); @@ -524,6 +523,7 @@ void *block_job_create(const char *job_id, const BlockJ= obDriver *driver, job =3D job_create(job_id, &driver->job_driver, txn, bdrv_get_aio_cont= ext(bs), flags, cb, opaque, errp); if (job =3D=3D NULL) { + bdrv_graph_wrunlock(); return NULL; } =20 @@ -563,9 +563,11 @@ void *block_job_create(const char *job_id, const Block= JobDriver *driver, goto fail; } =20 + bdrv_graph_wrunlock(); return job; =20 fail: + bdrv_graph_wrunlock(); job_early_fail(&job->job); return NULL; } diff --git a/tests/unit/test-bdrv-drain.c b/tests/unit/test-bdrv-drain.c index f67e9df01c..40d17b4c5a 100644 --- a/tests/unit/test-bdrv-drain.c +++ b/tests/unit/test-bdrv-drain.c @@ -794,7 +794,10 @@ static void test_blockjob_common_drain_node(enum drain= _type drain_type, 0, 0, NULL, NULL, &error_abort); tjob->bs =3D src; job =3D &tjob->common; + + bdrv_graph_wrlock(target); block_job_add_bdrv(job, "target", target, 0, BLK_PERM_ALL, &error_abor= t); + bdrv_graph_wrunlock(); =20 switch (result) { case TEST_JOB_SUCCESS: --=20 2.41.0 From nobody Wed Nov 27 09:52:08 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=1699382905; cv=none; d=zohomail.com; s=zohoarc; b=WFfK3hFnquUEGROShdgrVFL+EmqnncNi1LWdTNU1c7hdAlqKAXB0+IvYl7ULiyMmLpWObHk2/PLBjzgg456Qjm10FeVuv6oylNSCq6XmHBNVv968PU0xkrAUHmNS9aF9/Xo5Er4BkcFDl2CDDTjXXncX6sgLv67iMEboCenK0I0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699382905; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Ikpzi/J9P2vxFqXxR3blvPQASsFGFzG0SWOjp/o3suY=; b=PJWfJLjBJtO2ha0tjRsMyj3sUhXvh4G77ee5VxtE9yEI9btfT39sJBHVJBNxKkKrldRfllm+DXc0T1ZuCxl140gUstNLRVKdmWdirmtDhGWRT1ZNy8pAWT139JgsOIWe3+TW1zMJ1a8WwBc7ZXNt4dqfY79KE1MVZk610Y5rWzc= 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 1699382905128440.90986394336073; Tue, 7 Nov 2023 10:48:25 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0R5w-00044n-TV; Tue, 07 Nov 2023 13:46:41 -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 1r0R5f-0003vI-I9 for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:23 -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 1r0R5c-0001Tl-CF for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:19 -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.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-317-M5-Sq2HwOTGWW8Q7-zPFNA-1; Tue, 07 Nov 2023 13:46:12 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 34DC0832D6E for ; Tue, 7 Nov 2023 18:46:12 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id AD970C1290F; Tue, 7 Nov 2023 18:46:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699382775; 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=Ikpzi/J9P2vxFqXxR3blvPQASsFGFzG0SWOjp/o3suY=; b=bzwmw63rTpMf4AG5f6I6NiR8R+c445yJMVVd0cHtT2SpQQNMJRRcZlTbM6gYZO10uUdZkZ 1EpCrJVT0HYmA24QPa+pq7yyGaAc2DKr8xjYTiATP2Sw4/ie75UVd5qkr79ViROoRZYmfM FNWFN40HtVr/V63+HkXV14tuHR9sOjE= X-MC-Unique: M5-Sq2HwOTGWW8Q7-zPFNA-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 06/25] block: Mark bdrv_filter_or_cow_bs() and callers GRAPH_RDLOCK Date: Tue, 7 Nov 2023 19:45:46 +0100 Message-ID: <20231107184605.236540-7-kwolf@redhat.com> In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com> References: <20231107184605.236540-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1699382907330100003 Content-Type: text/plain; charset="utf-8" This adds GRAPH_RDLOCK annotations to declare that callers of bdrv_filter_or_cow_bs() need to hold a reader lock for the graph because it calls bdrv_filter_or_cow_child(), which accesses bs->file/backing. Signed-off-by: Kevin Wolf Message-ID: <20231027155333.420094-7-kwolf@redhat.com> Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- include/block/block_int-io.h | 3 ++- block.c | 31 ++++++++++++++++++------------- block/stream.c | 4 ++++ blockdev.c | 2 +- nbd/server.c | 6 ++++++ 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/include/block/block_int-io.h b/include/block/block_int-io.h index 26bff94e4e..6800af7590 100644 --- a/include/block/block_int-io.h +++ b/include/block/block_int-io.h @@ -150,7 +150,8 @@ bdrv_filter_bs(BlockDriverState *bs) return child_bs(bdrv_filter_child(bs)); } =20 -static inline BlockDriverState *bdrv_filter_or_cow_bs(BlockDriverState *bs) +static inline BlockDriverState * GRAPH_RDLOCK +bdrv_filter_or_cow_bs(BlockDriverState *bs) { IO_CODE(); return child_bs(bdrv_filter_or_cow_child(bs)); diff --git a/block.c b/block.c index 5f92eb4950..a6060eddbc 100644 --- a/block.c +++ b/block.c @@ -5435,17 +5435,6 @@ static int bdrv_replace_node_common(BlockDriverState= *from, =20 GLOBAL_STATE_CODE(); =20 - if (detach_subchain) { - assert(bdrv_chain_contains(from, to)); - assert(from !=3D to); - for (to_cow_parent =3D from; - bdrv_filter_or_cow_bs(to_cow_parent) !=3D to; - to_cow_parent =3D bdrv_filter_or_cow_bs(to_cow_parent)) - { - ; - } - } - /* Make sure that @from doesn't go away until we have successfully att= ached * all of its parents to @to. */ bdrv_ref(from); @@ -5457,6 +5446,17 @@ static int bdrv_replace_node_common(BlockDriverState= *from, =20 bdrv_graph_wrlock(to); =20 + if (detach_subchain) { + assert(bdrv_chain_contains(from, to)); + assert(from !=3D to); + for (to_cow_parent =3D from; + bdrv_filter_or_cow_bs(to_cow_parent) !=3D to; + to_cow_parent =3D bdrv_filter_or_cow_bs(to_cow_parent)) + { + ; + } + } + /* * Do the replacement without permission update. * Replacement may influence the permissions, we should calculate new @@ -5504,10 +5504,14 @@ int bdrv_replace_node(BlockDriverState *from, Block= DriverState *to, =20 int bdrv_drop_filter(BlockDriverState *bs, Error **errp) { + BlockDriverState *child_bs; + GLOBAL_STATE_CODE(); + bdrv_graph_rdlock_main_loop(); + child_bs =3D bdrv_filter_or_cow_bs(bs); + bdrv_graph_rdunlock_main_loop(); =20 - return bdrv_replace_node_common(bs, bdrv_filter_or_cow_bs(bs), true, t= rue, - errp); + return bdrv_replace_node_common(bs, child_bs, true, true, errp); } =20 /* @@ -6509,6 +6513,7 @@ bool bdrv_chain_contains(BlockDriverState *top, Block= DriverState *base) { =20 GLOBAL_STATE_CODE(); + GRAPH_RDLOCK_GUARD_MAINLOOP(); =20 while (top && top !=3D base) { top =3D bdrv_filter_or_cow_bs(top); diff --git a/block/stream.c b/block/stream.c index 51333e460b..2781441191 100644 --- a/block/stream.c +++ b/block/stream.c @@ -60,6 +60,8 @@ static int stream_prepare(Job *job) Error *local_err =3D NULL; int ret =3D 0; =20 + GLOBAL_STATE_CODE(); + /* We should drop filter at this point, as filter hold the backing cha= in */ bdrv_cor_filter_drop(s->cor_filter_bs); s->cor_filter_bs =3D NULL; @@ -78,8 +80,10 @@ static int stream_prepare(Job *job) bdrv_drained_begin(unfiltered_bs_cow); } =20 + bdrv_graph_rdlock_main_loop(); base =3D bdrv_filter_or_cow_bs(s->above_base); unfiltered_base =3D bdrv_skip_filters(base); + bdrv_graph_rdunlock_main_loop(); =20 if (bdrv_cow_child(unfiltered_bs)) { const char *base_id =3D NULL, *base_fmt =3D NULL; diff --git a/blockdev.c b/blockdev.c index 148df99e00..4cb8e1d91a 100644 --- a/blockdev.c +++ b/blockdev.c @@ -2501,8 +2501,8 @@ void qmp_block_stream(const char *job_id, const char = *device, /* * Check for op blockers in the whole chain between bs and base (or bo= ttom) */ - iter_end =3D bottom ? bdrv_filter_or_cow_bs(bottom_bs) : base_bs; bdrv_graph_rdlock_main_loop(); + iter_end =3D bottom ? bdrv_filter_or_cow_bs(bottom_bs) : base_bs; for (iter =3D bs; iter && iter !=3D iter_end; iter =3D bdrv_filter_or_cow_bs(iter)) { diff --git a/nbd/server.c b/nbd/server.c index 859c163d19..895cf0a752 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -1689,6 +1689,7 @@ static int nbd_export_create(BlockExport *blk_exp, Bl= ockExportOptions *exp_args, size_t i; int ret; =20 + GLOBAL_STATE_CODE(); assert(exp_args->type =3D=3D BLOCK_EXPORT_TYPE_NBD); =20 if (!nbd_server_is_running()) { @@ -1743,6 +1744,8 @@ static int nbd_export_create(BlockExport *blk_exp, Bl= ockExportOptions *exp_args, } exp->size =3D QEMU_ALIGN_DOWN(size, BDRV_SECTOR_SIZE); =20 + bdrv_graph_rdlock_main_loop(); + for (bitmaps =3D arg->bitmaps; bitmaps; bitmaps =3D bitmaps->next) { exp->nr_export_bitmaps++; } @@ -1825,9 +1828,12 @@ static int nbd_export_create(BlockExport *blk_exp, B= lockExportOptions *exp_args, =20 QTAILQ_INSERT_TAIL(&exports, exp, next); =20 + bdrv_graph_rdunlock_main_loop(); + return 0; =20 fail: + bdrv_graph_rdunlock_main_loop(); g_free(exp->export_bitmaps); g_free(exp->name); g_free(exp->description); --=20 2.41.0 From nobody Wed Nov 27 09:52:08 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=1699382889; cv=none; d=zohomail.com; s=zohoarc; b=P5/pv6hPi7t1Ipn4LvkfJ7TLB9RDgKrlZFf7UTP6yBb9FPH8VvLsc+JO7ENeg7tHwhqNDa1t327RinxFLq7UnR5XXEyiWWHrgYbs0sGu3kYcaAFJKLhBcVSpuXrj6oxFsM5gsyoQAiKqDN5asLql7gsPrxeUoRAcTW3T9kX38sM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699382889; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=3Gysl0ZBHgyXFksr4RgfuRtuFuCtAWWPUOnQr7I7eCo=; b=C2AGb/IFCLNYq9Xb97vSnT/fZXs7ERHOFxOuursLR3kblYNuTj78gKblTOagUV7JSIcFSUOqGFPJ/VYyBsbzfS1SouE2ySYJgj+EkbirwxC/rBTO6bkobEjsYuSTYYMc5EdzkuHdOxBEK9dCsLMrlizYCjRbGp9SUvePwlCAAZc= 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 1699382889883229.8337947039555; Tue, 7 Nov 2023 10:48:09 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0R68-0004EQ-5D; Tue, 07 Nov 2023 13:46:48 -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 1r0R5f-0003vK-Il for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:23 -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 1r0R5b-0001Tg-VA for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:18 -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.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-627-EY1Qia7IPNa3A6rbKcPDRA-1; Tue, 07 Nov 2023 13:46:13 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E2B50832D69 for ; Tue, 7 Nov 2023 18:46:12 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6C9E4C1596F; Tue, 7 Nov 2023 18:46:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699382774; 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=3Gysl0ZBHgyXFksr4RgfuRtuFuCtAWWPUOnQr7I7eCo=; b=PP4iat+RbKIAGlnF364DrftNhDOoveqSeh/KC7hsfiWtR8UxczhODn+OkXbQSh5NWG9Zcb mkt8U1aIqirlXMwSAjgvRJ4Z+eSyclppXykAIL+9jG+7tkjC7gM1CHqcfCwNOp46vtymBP V2y8xpjKEc9aBLgJHt1AHnQQqVpZ6O0= X-MC-Unique: EY1Qia7IPNa3A6rbKcPDRA-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 07/25] block: Mark bdrv_skip_implicit_filters() and callers GRAPH_RDLOCK Date: Tue, 7 Nov 2023 19:45:47 +0100 Message-ID: <20231107184605.236540-8-kwolf@redhat.com> In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com> References: <20231107184605.236540-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1699382891279100001 Content-Type: text/plain; charset="utf-8" This adds GRAPH_RDLOCK annotations to declare that callers of bdrv_skip_implicit_filters() need to hold a reader lock for the graph because it calls bdrv_filter_child(), which accesses bs->file/backing. Signed-off-by: Kevin Wolf Message-ID: <20231027155333.420094-8-kwolf@redhat.com> Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- include/block/block_int-global-state.h | 3 ++- block.c | 28 +++++++++++++++++--------- block/monitor/block-hmp-cmds.c | 3 +++ blockdev.c | 14 +++++++------ 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/include/block/block_int-global-state.h b/include/block/block_i= nt-global-state.h index afce6c4416..ef31c58bb3 100644 --- a/include/block/block_int-global-state.h +++ b/include/block/block_int-global-state.h @@ -277,7 +277,8 @@ BdrvDirtyBitmap *block_dirty_bitmap_remove(const char *= node, const char *name, Error **errp); =20 =20 -BlockDriverState *bdrv_skip_implicit_filters(BlockDriverState *bs); +BlockDriverState * GRAPH_RDLOCK +bdrv_skip_implicit_filters(BlockDriverState *bs); =20 /** * bdrv_add_aio_context_notifier: diff --git a/block.c b/block.c index a6060eddbc..7e8b39711b 100644 --- a/block.c +++ b/block.c @@ -4778,6 +4778,8 @@ bdrv_reopen_parse_file_or_backing(BDRVReopenState *re= open_state, return 0; } =20 + bdrv_graph_rdlock_main_loop(); + switch (qobject_type(value)) { case QTYPE_QNULL: assert(is_backing); /* The 'file' option does not allow a null val= ue */ @@ -4787,17 +4789,16 @@ bdrv_reopen_parse_file_or_backing(BDRVReopenState *= reopen_state, str =3D qstring_get_str(qobject_to(QString, value)); new_child_bs =3D bdrv_lookup_bs(NULL, str, errp); if (new_child_bs =3D=3D NULL) { - return -EINVAL; + ret =3D -EINVAL; + goto out_rdlock; } =20 - bdrv_graph_rdlock_main_loop(); has_child =3D bdrv_recurse_has_child(new_child_bs, bs); - bdrv_graph_rdunlock_main_loop(); - if (has_child) { error_setg(errp, "Making '%s' a %s child of '%s' would create = a " "cycle", str, child_name, bs->node_name); - return -EINVAL; + ret =3D -EINVAL; + goto out_rdlock; } break; default: @@ -4809,18 +4810,21 @@ bdrv_reopen_parse_file_or_backing(BDRVReopenState *= reopen_state, } =20 if (old_child_bs =3D=3D new_child_bs) { - return 0; + ret =3D 0; + goto out_rdlock; } =20 if (old_child_bs) { if (bdrv_skip_implicit_filters(old_child_bs) =3D=3D new_child_bs) { - return 0; + ret =3D 0; + goto out_rdlock; } =20 if (old_child_bs->implicit) { error_setg(errp, "Cannot replace implicit %s child of %s", child_name, bs->node_name); - return -EPERM; + ret =3D -EPERM; + goto out_rdlock; } } =20 @@ -4831,7 +4835,8 @@ bdrv_reopen_parse_file_or_backing(BDRVReopenState *re= open_state, */ error_setg(errp, "'%s' is a %s filter node that does not support a= " "%s child", bs->node_name, bs->drv->format_name, child_= name); - return -EINVAL; + ret =3D -EINVAL; + goto out_rdlock; } =20 if (is_backing) { @@ -4852,6 +4857,7 @@ bdrv_reopen_parse_file_or_backing(BDRVReopenState *re= open_state, aio_context_acquire(ctx); } =20 + bdrv_graph_rdunlock_main_loop(); bdrv_graph_wrlock(new_child_bs); =20 ret =3D bdrv_set_file_or_backing_noperm(bs, new_child_bs, is_backing, @@ -4870,6 +4876,10 @@ bdrv_reopen_parse_file_or_backing(BDRVReopenState *r= eopen_state, } =20 return ret; + +out_rdlock: + bdrv_graph_rdunlock_main_loop(); + return ret; } =20 /* diff --git a/block/monitor/block-hmp-cmds.c b/block/monitor/block-hmp-cmds.c index 5b2c597e7a..c729cbf1eb 100644 --- a/block/monitor/block-hmp-cmds.c +++ b/block/monitor/block-hmp-cmds.c @@ -206,6 +206,9 @@ void hmp_commit(Monitor *mon, const QDict *qdict) BlockBackend *blk; int ret; =20 + GLOBAL_STATE_CODE(); + GRAPH_RDLOCK_GUARD_MAINLOOP(); + if (!strcmp(device, "all")) { ret =3D blk_commit_all(); } else { diff --git a/blockdev.c b/blockdev.c index 4cb8e1d91a..6cdf48beb1 100644 --- a/blockdev.c +++ b/blockdev.c @@ -1746,10 +1746,10 @@ static void drive_backup_action(DriveBackup *backup, assert(format); if (source) { /* Implicit filters should not appear in the filename */ - BlockDriverState *explicit_backing =3D - bdrv_skip_implicit_filters(source); + BlockDriverState *explicit_backing; =20 bdrv_graph_rdlock_main_loop(); + explicit_backing =3D bdrv_skip_implicit_filters(source); bdrv_refresh_filename(explicit_backing); bdrv_graph_rdunlock_main_loop(); =20 @@ -3108,16 +3108,18 @@ void qmp_drive_mirror(DriveMirror *arg, Error **err= p) bdrv_img_create(arg->target, format, NULL, NULL, NULL, size, flags, false, &local_err); } else { - /* Implicit filters should not appear in the filename */ - BlockDriverState *explicit_backing =3D - bdrv_skip_implicit_filters(target_backing_bs); + BlockDriverState *explicit_backing; =20 switch (arg->mode) { case NEW_IMAGE_MODE_EXISTING: break; case NEW_IMAGE_MODE_ABSOLUTE_PATHS: - /* create new image with backing file */ + /* + * Create new image with backing file. + * Implicit filters should not appear in the filename. + */ bdrv_graph_rdlock_main_loop(); + explicit_backing =3D bdrv_skip_implicit_filters(target_backing= _bs); bdrv_refresh_filename(explicit_backing); bdrv_graph_rdunlock_main_loop(); =20 --=20 2.41.0 From nobody Wed Nov 27 09:52:08 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=1699382871; cv=none; d=zohomail.com; s=zohoarc; b=IOWaeeviEUxpWiMVnItvL0Dg1lMJDS2FoR/w8Ix4uYb/xVYcJur75G9JRaWmd8hPjm1tHjn+o/g9HbJn3LdNzEp4N3aqrSfHaD1ZU2SzggG6K08zXSBZ3nAqMELrPJYBNtl7PfbKeOE51RpVrA3NIzArF70ZbGU5Q4/C1WY9f9M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699382871; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=7grqvMUm/2+tdpBgnAuIHfwKZ2t032uJUVPpIdY1xXM=; b=QWlffNhrlNjRD2AWWcxpXe9PCcDAg5iAVEyj0IshUzKSuO8+hz5mrFLahgz3WGHcW8T04vKhdMxkbKE16rJwDZ6wywEe50Unvfcx0qIMw3ImjZTG3tAq/Lh+92qT8rB9kWK0ap5M0xyPKNy4Rh1PCrEDJeuJlOuWho8yceyzbBI= 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 169938287161449.646598390361305; Tue, 7 Nov 2023 10:47:51 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0R6B-0004Qi-TL; Tue, 07 Nov 2023 13:46:51 -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 1r0R5h-0003vR-MZ for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:23 -0500 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 1r0R5d-0001Tz-Fc for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:19 -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.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-46-Oh7t5RkRM9uS4cKuhPu6HQ-1; Tue, 07 Nov 2023 13:46:14 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1337C101A529 for ; Tue, 7 Nov 2023 18:46:14 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id 25174C15983; Tue, 7 Nov 2023 18:46:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699382776; 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=7grqvMUm/2+tdpBgnAuIHfwKZ2t032uJUVPpIdY1xXM=; b=g+NlDoBfihR5Xy+1Z9Fe3EwBKKFSzUm5+1cphYggarU/sAnBNik15R0NrYVJxS4lW7XFXE GnDRiAoU5vtSSMbkeK3yJTjN3UgEq6A1FNBx9Sl3hjGUxmmUGooMEDbFnwBZOtZDbZzmFZ +WfBgUSTg21T7i3WuTK1Uksc02OEnxk= X-MC-Unique: Oh7t5RkRM9uS4cKuhPu6HQ-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 08/25] block: Mark bdrv_skip_filters() and callers GRAPH_RDLOCK Date: Tue, 7 Nov 2023 19:45:48 +0100 Message-ID: <20231107184605.236540-9-kwolf@redhat.com> In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com> References: <20231107184605.236540-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1699382873206100002 Content-Type: text/plain; charset="utf-8" This adds GRAPH_RDLOCK annotations to declare that callers of bdrv_skip_filters() need to hold a reader lock for the graph because it calls bdrv_filter_child(), which accesses bs->file/backing. Signed-off-by: Kevin Wolf Message-ID: <20231027155333.420094-9-kwolf@redhat.com> Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- include/block/block-global-state.h | 8 ++++--- include/block/block_int-io.h | 4 ++-- block/block-backend.c | 1 + block/block-copy.c | 9 +++++++- block/commit.c | 5 ++++- block/mirror.c | 34 +++++++++++++++++++++--------- block/stream.c | 22 ++++++++++++------- blockdev.c | 7 +++--- qemu-img.c | 18 +++++++++++++--- 9 files changed, 77 insertions(+), 31 deletions(-) diff --git a/include/block/block-global-state.h b/include/block/block-globa= l-state.h index 3ae468ea15..b6860ae43b 100644 --- a/include/block/block-global-state.h +++ b/include/block/block-global-state.h @@ -144,9 +144,11 @@ int bdrv_change_backing_file(BlockDriverState *bs, con= st char *backing_file, void bdrv_register(BlockDriver *bdrv); int bdrv_drop_intermediate(BlockDriverState *top, BlockDriverState *base, const char *backing_file_str); -BlockDriverState *bdrv_find_overlay(BlockDriverState *active, - BlockDriverState *bs); -BlockDriverState *bdrv_find_base(BlockDriverState *bs); + +BlockDriverState * GRAPH_RDLOCK +bdrv_find_overlay(BlockDriverState *active, BlockDriverState *bs); + +BlockDriverState * GRAPH_RDLOCK bdrv_find_base(BlockDriverState *bs); bool bdrv_is_backing_chain_frozen(BlockDriverState *bs, BlockDriverState *= base, Error **errp); int bdrv_freeze_backing_chain(BlockDriverState *bs, BlockDriverState *base, diff --git a/include/block/block_int-io.h b/include/block/block_int-io.h index 6800af7590..4e7bf57a5e 100644 --- a/include/block/block_int-io.h +++ b/include/block/block_int-io.h @@ -134,8 +134,8 @@ BdrvChild *bdrv_cow_child(BlockDriverState *bs); BdrvChild *bdrv_filter_child(BlockDriverState *bs); BdrvChild *bdrv_filter_or_cow_child(BlockDriverState *bs); BdrvChild * GRAPH_RDLOCK bdrv_primary_child(BlockDriverState *bs); -BlockDriverState *bdrv_skip_filters(BlockDriverState *bs); -BlockDriverState *bdrv_backing_chain_next(BlockDriverState *bs); +BlockDriverState * GRAPH_RDLOCK bdrv_skip_filters(BlockDriverState *bs); +BlockDriverState * GRAPH_RDLOCK bdrv_backing_chain_next(BlockDriverState *= bs); =20 static inline BlockDriverState *bdrv_cow_bs(BlockDriverState *bs) { diff --git a/block/block-backend.c b/block/block-backend.c index 075a0dfa95..4053134781 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -2730,6 +2730,7 @@ int blk_commit_all(void) { BlockBackend *blk =3D NULL; GLOBAL_STATE_CODE(); + GRAPH_RDLOCK_GUARD_MAINLOOP(); =20 while ((blk =3D blk_all_next(blk)) !=3D NULL) { AioContext *aio_context =3D blk_get_aio_context(blk); diff --git a/block/block-copy.c b/block/block-copy.c index 1c60368d72..6b2be3d204 100644 --- a/block/block-copy.c +++ b/block/block-copy.c @@ -313,7 +313,12 @@ static int64_t block_copy_calculate_cluster_size(Block= DriverState *target, { int ret; BlockDriverInfo bdi; - bool target_does_cow =3D bdrv_backing_chain_next(target); + bool target_does_cow; + + GLOBAL_STATE_CODE(); + GRAPH_RDLOCK_GUARD_MAINLOOP(); + + target_does_cow =3D bdrv_backing_chain_next(target); =20 /* * If there is no backing file on the target, we cannot rely on COW if= our @@ -355,6 +360,8 @@ BlockCopyState *block_copy_state_new(BdrvChild *source,= BdrvChild *target, BdrvDirtyBitmap *copy_bitmap; bool is_fleecing; =20 + GLOBAL_STATE_CODE(); + cluster_size =3D block_copy_calculate_cluster_size(target->bs, errp); if (cluster_size < 0) { return NULL; diff --git a/block/commit.c b/block/commit.c index fc3ad79749..05eb57d9ea 100644 --- a/block/commit.c +++ b/block/commit.c @@ -255,10 +255,13 @@ void commit_start(const char *job_id, BlockDriverStat= e *bs, GLOBAL_STATE_CODE(); =20 assert(top !=3D bs); + bdrv_graph_rdlock_main_loop(); if (bdrv_skip_filters(top) =3D=3D bdrv_skip_filters(base)) { error_setg(errp, "Invalid files for merge: top and base are the sa= me"); + bdrv_graph_rdunlock_main_loop(); return; } + bdrv_graph_rdunlock_main_loop(); =20 base_size =3D bdrv_getlength(base); if (base_size < 0) { @@ -324,6 +327,7 @@ void commit_start(const char *job_id, BlockDriverState = *bs, * this is the responsibility of the interface (i.e. whoever calls * commit_start()). */ + bdrv_graph_wrlock(top); s->base_overlay =3D bdrv_find_overlay(top, base); assert(s->base_overlay); =20 @@ -342,7 +346,6 @@ void commit_start(const char *job_id, BlockDriverState = *bs, */ iter_shared_perms =3D BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE; =20 - bdrv_graph_wrlock(top); for (iter =3D top; iter !=3D base; iter =3D bdrv_filter_or_cow_bs(iter= )) { if (iter =3D=3D filtered_base) { /* diff --git a/block/mirror.c b/block/mirror.c index a03247a31b..75e826dac8 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -714,7 +714,6 @@ static int mirror_exit_common(Job *job) bdrv_graph_rdlock_main_loop(); bdrv_child_refresh_perms(mirror_top_bs, mirror_top_bs->backing, &error_abort); - bdrv_graph_rdunlock_main_loop(); =20 if (!abort && s->backing_mode =3D=3D MIRROR_SOURCE_BACKING_CHAIN) { BlockDriverState *backing =3D s->is_none_mode ? src : s->base; @@ -737,6 +736,7 @@ static int mirror_exit_common(Job *job) local_err =3D NULL; } } + bdrv_graph_rdunlock_main_loop(); =20 if (s->to_replace) { replace_aio_context =3D bdrv_get_aio_context(s->to_replace); @@ -992,13 +992,13 @@ static int coroutine_fn mirror_run(Job *job, Error **= errp) } else { s->target_cluster_size =3D BDRV_SECTOR_SIZE; } - bdrv_graph_co_rdunlock(); if (backing_filename[0] && !bdrv_backing_chain_next(target_bs) && s->granularity < s->target_cluster_size) { s->buf_size =3D MAX(s->buf_size, s->target_cluster_size); s->cow_bitmap =3D bitmap_new(length); } s->max_iov =3D MIN(bs->bl.max_iov, target_bs->bl.max_iov); + bdrv_graph_co_rdunlock(); =20 s->buf =3D qemu_try_blockalign(bs, s->buf_size); if (s->buf =3D=3D NULL) { @@ -1744,12 +1744,15 @@ static BlockJob *mirror_start_job( buf_size =3D DEFAULT_MIRROR_BUF_SIZE; } =20 + bdrv_graph_rdlock_main_loop(); if (bdrv_skip_filters(bs) =3D=3D bdrv_skip_filters(target)) { error_setg(errp, "Can't mirror node into itself"); + bdrv_graph_rdunlock_main_loop(); return NULL; } =20 target_is_backing =3D bdrv_chain_contains(bs, target); + bdrv_graph_rdunlock_main_loop(); =20 /* In the case of active commit, add dummy driver to provide consistent * reads on the top, while disabling it in the intermediate nodes, and= make @@ -1832,14 +1835,19 @@ static BlockJob *mirror_start_job( } =20 target_shared_perms |=3D BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE; - } else if (bdrv_chain_contains(bs, bdrv_skip_filters(target))) { - /* - * We may want to allow this in the future, but it would - * require taking some extra care. - */ - error_setg(errp, "Cannot mirror to a filter on top of a node in th= e " - "source's backing chain"); - goto fail; + } else { + bdrv_graph_rdlock_main_loop(); + if (bdrv_chain_contains(bs, bdrv_skip_filters(target))) { + /* + * We may want to allow this in the future, but it would + * require taking some extra care. + */ + error_setg(errp, "Cannot mirror to a filter on top of a node i= n " + "the source's backing chain"); + bdrv_graph_rdunlock_main_loop(); + goto fail; + } + bdrv_graph_rdunlock_main_loop(); } =20 s->target =3D blk_new(s->common.job.aio_context, @@ -1860,6 +1868,7 @@ static BlockJob *mirror_start_job( blk_set_allow_aio_context_change(s->target, true); blk_set_disable_request_queuing(s->target, true); =20 + bdrv_graph_rdlock_main_loop(); s->replaces =3D g_strdup(replaces); s->on_source_error =3D on_source_error; s->on_target_error =3D on_target_error; @@ -1875,6 +1884,7 @@ static BlockJob *mirror_start_job( if (auto_complete) { s->should_complete =3D true; } + bdrv_graph_rdunlock_main_loop(); =20 s->dirty_bitmap =3D bdrv_create_dirty_bitmap(s->mirror_top_bs, granula= rity, NULL, errp); @@ -2007,8 +2017,12 @@ void mirror_start(const char *job_id, BlockDriverSta= te *bs, MirrorSyncMode_str(mode)); return; } + + bdrv_graph_rdlock_main_loop(); is_none_mode =3D mode =3D=3D MIRROR_SYNC_MODE_NONE; base =3D mode =3D=3D MIRROR_SYNC_MODE_TOP ? bdrv_backing_chain_next(bs= ) : NULL; + bdrv_graph_rdunlock_main_loop(); + mirror_start_job(job_id, bs, creation_flags, target, replaces, speed, granularity, buf_size, backing_mode, zero_targ= et, on_source_error, on_target_error, unmap, NULL, NULL, diff --git a/block/stream.c b/block/stream.c index 2781441191..5323a9976d 100644 --- a/block/stream.c +++ b/block/stream.c @@ -53,8 +53,8 @@ static int coroutine_fn stream_populate(BlockBackend *blk, static int stream_prepare(Job *job) { StreamBlockJob *s =3D container_of(job, StreamBlockJob, common.job); - BlockDriverState *unfiltered_bs =3D bdrv_skip_filters(s->target_bs); - BlockDriverState *unfiltered_bs_cow =3D bdrv_cow_bs(unfiltered_bs); + BlockDriverState *unfiltered_bs; + BlockDriverState *unfiltered_bs_cow; BlockDriverState *base; BlockDriverState *unfiltered_base; Error *local_err =3D NULL; @@ -62,6 +62,11 @@ static int stream_prepare(Job *job) =20 GLOBAL_STATE_CODE(); =20 + bdrv_graph_rdlock_main_loop(); + unfiltered_bs =3D bdrv_skip_filters(s->target_bs); + unfiltered_bs_cow =3D bdrv_cow_bs(unfiltered_bs); + bdrv_graph_rdunlock_main_loop(); + /* We should drop filter at this point, as filter hold the backing cha= in */ bdrv_cor_filter_drop(s->cor_filter_bs); s->cor_filter_bs =3D NULL; @@ -142,18 +147,19 @@ static void stream_clean(Job *job) static int coroutine_fn stream_run(Job *job, Error **errp) { StreamBlockJob *s =3D container_of(job, StreamBlockJob, common.job); - BlockDriverState *unfiltered_bs =3D bdrv_skip_filters(s->target_bs); + BlockDriverState *unfiltered_bs; int64_t len; int64_t offset =3D 0; int error =3D 0; int64_t n =3D 0; /* bytes */ =20 - if (unfiltered_bs =3D=3D s->base_overlay) { - /* Nothing to stream */ - return 0; - } - WITH_GRAPH_RDLOCK_GUARD() { + unfiltered_bs =3D bdrv_skip_filters(s->target_bs); + if (unfiltered_bs =3D=3D s->base_overlay) { + /* Nothing to stream */ + return 0; + } + len =3D bdrv_co_getlength(s->target_bs); if (len < 0) { return len; diff --git a/blockdev.c b/blockdev.c index 6cdf48beb1..5f15ea3b1d 100644 --- a/blockdev.c +++ b/blockdev.c @@ -1710,7 +1710,6 @@ static void drive_backup_action(DriveBackup *backup, bdrv_graph_rdunlock_main_loop(); goto out; } - bdrv_graph_rdunlock_main_loop(); =20 flags =3D bs->open_flags | BDRV_O_RDWR; =20 @@ -1735,6 +1734,7 @@ static void drive_backup_action(DriveBackup *backup, flags |=3D BDRV_O_NO_BACKING; set_backing_hd =3D true; } + bdrv_graph_rdunlock_main_loop(); =20 size =3D bdrv_getlength(bs); if (size < 0) { @@ -3054,7 +3054,6 @@ void qmp_drive_mirror(DriveMirror *arg, Error **errp) bdrv_graph_rdunlock_main_loop(); return; } - bdrv_graph_rdunlock_main_loop(); =20 aio_context =3D bdrv_get_aio_context(bs); aio_context_acquire(aio_context); @@ -3076,6 +3075,7 @@ void qmp_drive_mirror(DriveMirror *arg, Error **errp) if (arg->sync =3D=3D MIRROR_SYNC_MODE_NONE) { target_backing_bs =3D bs; } + bdrv_graph_rdunlock_main_loop(); =20 size =3D bdrv_getlength(bs); if (size < 0) { @@ -3450,15 +3450,16 @@ void qmp_change_backing_file(const char *device, goto out; } =20 + bdrv_graph_rdlock_main_loop(); if (bdrv_find_base(image_bs) =3D=3D image_bs) { error_setg(errp, "not allowing backing file change on an image " "without a backing file"); + bdrv_graph_rdunlock_main_loop(); goto out; } =20 /* even though we are not necessarily operating on bs, we need it to * determine if block ops are currently prohibited on the chain */ - bdrv_graph_rdlock_main_loop(); if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { bdrv_graph_rdunlock_main_loop(); goto out; diff --git a/qemu-img.c b/qemu-img.c index c061fd0634..33f3ab5fba 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1050,12 +1050,14 @@ static int img_commit(int argc, char **argv) qemu_progress_init(progress, 1.f); qemu_progress_print(0.f, 100); =20 + bdrv_graph_rdlock_main_loop(); if (base) { base_bs =3D bdrv_find_backing_image(bs, base); if (!base_bs) { error_setg(&local_err, "Did not find '%s' in the backing chain of '%s'", base, filename); + bdrv_graph_rdunlock_main_loop(); goto done; } } else { @@ -1065,9 +1067,11 @@ static int img_commit(int argc, char **argv) base_bs =3D bdrv_backing_chain_next(bs); if (!base_bs) { error_setg(&local_err, "Image does not have a backing file"); + bdrv_graph_rdunlock_main_loop(); goto done; } } + bdrv_graph_rdunlock_main_loop(); =20 cbi =3D (CommonBlockJobCBInfo){ .errp =3D &local_err, @@ -1713,7 +1717,8 @@ static void convert_select_part(ImgConvertState *s, i= nt64_t sector_num, } } =20 -static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_nu= m) +static int coroutine_mixed_fn GRAPH_RDLOCK +convert_iteration_sectors(ImgConvertState *s, int64_t sector_num) { int64_t src_cur_offset; int ret, n, src_cur; @@ -2115,7 +2120,9 @@ static int convert_do_copy(ImgConvertState *s) } =20 while (sector_num < s->total_sectors) { + bdrv_graph_rdlock_main_loop(); n =3D convert_iteration_sectors(s, sector_num); + bdrv_graph_rdunlock_main_loop(); if (n < 0) { return n; } @@ -2757,8 +2764,10 @@ static int img_convert(int argc, char **argv) * s.target_backing_sectors has to be negative, which it will * be automatically). The backing file length is used only * for optimizations, so such a case is not fatal. */ + bdrv_graph_rdlock_main_loop(); s.target_backing_sectors =3D bdrv_nb_sectors(bdrv_backing_chain_next(out_bs)); + bdrv_graph_rdunlock_main_loop(); } else { s.target_backing_sectors =3D -1; } @@ -3145,6 +3154,9 @@ static int get_block_status(BlockDriverState *bs, int= 64_t offset, int64_t map; char *filename =3D NULL; =20 + GLOBAL_STATE_CODE(); + GRAPH_RDLOCK_GUARD_MAINLOOP(); + /* As an optimization, we could cache the current range of unallocated * clusters in each file of the chain, and avoid querying the same * range repeatedly. @@ -3173,9 +3185,7 @@ static int get_block_status(BlockDriverState *bs, int= 64_t offset, has_offset =3D !!(ret & BDRV_BLOCK_OFFSET_VALID); =20 if (file && has_offset) { - bdrv_graph_rdlock_main_loop(); bdrv_refresh_filename(file); - bdrv_graph_rdunlock_main_loop(); filename =3D file->filename; } =20 @@ -3663,7 +3673,9 @@ static int img_rebase(int argc, char **argv) } bs =3D blk_bs(blk); =20 + bdrv_graph_rdlock_main_loop(); unfiltered_bs =3D bdrv_skip_filters(bs); + bdrv_graph_rdunlock_main_loop(); =20 if (compress && !block_driver_can_compress(unfiltered_bs->drv)) { error_report("Compression not supported for this file format"); --=20 2.41.0 From nobody Wed Nov 27 09:52:08 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=1699382921; cv=none; d=zohomail.com; s=zohoarc; b=PGw2cRAhlC0Zz9+YWlEw2uqn05TcJ7JseABF42bacC5xoW7fzlTR7MWGfFYCU1ZppVAeOtqDjJNLnJyQQFoH2rW734tjijUzldE8EK1JcgqMWynvmdWv/06y+jv7N8c/zyQssnqNVDcdzfNUN8C3xJzOsER/8GyYgHdG3B8l57Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699382921; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=2rmk37/1+MaLUl82vdTAMrHS8+OOW11NeH/kDFXrXvY=; b=aiMNC52LZJ1xsQS4xH7HEhsr6JrRQhYYCeI7E+zbxZRm0z5VIG7J+S5uFieERmsMisVw/0E/t7BlBzsrNU3HWTieJ1oVVa76PoDBPjydzSSBExsE7Ye2NHwl9cFDCJJ/JFjk5MSCkJRMwgzabErTqaBsSx/tp8nPQDYAiVp5CsE= 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 1699382921695838.2949640768924; Tue, 7 Nov 2023 10:48:41 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0R6E-0004Vf-Pj; Tue, 07 Nov 2023 13:46:54 -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 1r0R5h-0003vP-Mw for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:23 -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 1r0R5f-0001UN-AC for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:21 -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.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-633-cb2aN3iJOneWMHoUzXuteg-1; Tue, 07 Nov 2023 13:46:15 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C0018834FC2 for ; Tue, 7 Nov 2023 18:46:14 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4A9FBC1290F; Tue, 7 Nov 2023 18:46:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699382777; 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=2rmk37/1+MaLUl82vdTAMrHS8+OOW11NeH/kDFXrXvY=; b=g6Z+72aw9InzFCPYJE4Ru6q6GQmJNxP1nsJ/LY+BSQsC24juw8a8L+eptkQ2VJl5Ty5C1Q 0geoFHOmdJYcrDjFsL+GuKSUifn0Gz0pxqSBwEGQlDLJkanEFNsnbUr0UJHusK1cH/EiLL rzfWEF6E1DaINygPdshbAg+/uShP6Kk= X-MC-Unique: cb2aN3iJOneWMHoUzXuteg-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 09/25] block: Mark bdrv_(un)freeze_backing_chain() and callers GRAPH_RDLOCK Date: Tue, 7 Nov 2023 19:45:49 +0100 Message-ID: <20231107184605.236540-10-kwolf@redhat.com> In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com> References: <20231107184605.236540-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1699382923392100009 Content-Type: text/plain; charset="utf-8" This adds GRAPH_RDLOCK annotations to declare that callers of bdrv_(un)freeze_backing_chain() need to hold a reader lock for the graph because it calls bdrv_filter_or_cow_child(), which accesses bs->file/backing. Use the opportunity to make bdrv_is_backing_chain_frozen() static, it has no external callers. Signed-off-by: Kevin Wolf Message-ID: <20231027155333.420094-10-kwolf@redhat.com> Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- block/copy-on-read.h | 3 ++- include/block/block-global-state.h | 11 ++++++----- block.c | 5 +++-- block/commit.c | 6 ++++++ block/copy-on-read.c | 19 +++++++++++++++---- block/mirror.c | 3 +++ block/stream.c | 16 +++++++++++----- 7 files changed, 46 insertions(+), 17 deletions(-) diff --git a/block/copy-on-read.h b/block/copy-on-read.h index 1d8ad38c74..72f9b378ea 100644 --- a/block/copy-on-read.h +++ b/block/copy-on-read.h @@ -27,6 +27,7 @@ =20 #include "block/block_int.h" =20 -void bdrv_cor_filter_drop(BlockDriverState *cor_filter_bs); +void no_coroutine_fn GRAPH_UNLOCKED +bdrv_cor_filter_drop(BlockDriverState *cor_filter_bs); =20 #endif /* BLOCK_COPY_ON_READ_H */ diff --git a/include/block/block-global-state.h b/include/block/block-globa= l-state.h index b6860ae43b..545708c35a 100644 --- a/include/block/block-global-state.h +++ b/include/block/block-global-state.h @@ -149,11 +149,12 @@ BlockDriverState * GRAPH_RDLOCK bdrv_find_overlay(BlockDriverState *active, BlockDriverState *bs); =20 BlockDriverState * GRAPH_RDLOCK bdrv_find_base(BlockDriverState *bs); -bool bdrv_is_backing_chain_frozen(BlockDriverState *bs, BlockDriverState *= base, - Error **errp); -int bdrv_freeze_backing_chain(BlockDriverState *bs, BlockDriverState *base, - Error **errp); -void bdrv_unfreeze_backing_chain(BlockDriverState *bs, BlockDriverState *b= ase); + +int GRAPH_RDLOCK +bdrv_freeze_backing_chain(BlockDriverState *bs, BlockDriverState *base, + Error **errp); +void GRAPH_RDLOCK +bdrv_unfreeze_backing_chain(BlockDriverState *bs, BlockDriverState *base); =20 /* * The units of offset and total_work_size may be chosen arbitrarily by the diff --git a/block.c b/block.c index 7e8b39711b..dc1980ee42 100644 --- a/block.c +++ b/block.c @@ -5843,8 +5843,9 @@ BlockDriverState *bdrv_find_base(BlockDriverState *bs) * between @bs and @base is frozen. @errp is set if that's the case. * @base must be reachable from @bs, or NULL. */ -bool bdrv_is_backing_chain_frozen(BlockDriverState *bs, BlockDriverState *= base, - Error **errp) +static bool GRAPH_RDLOCK +bdrv_is_backing_chain_frozen(BlockDriverState *bs, BlockDriverState *base, + Error **errp) { BlockDriverState *i; BdrvChild *child; diff --git a/block/commit.c b/block/commit.c index 05eb57d9ea..d92af02ead 100644 --- a/block/commit.c +++ b/block/commit.c @@ -48,8 +48,10 @@ static int commit_prepare(Job *job) { CommitBlockJob *s =3D container_of(job, CommitBlockJob, common.job); =20 + bdrv_graph_rdlock_main_loop(); bdrv_unfreeze_backing_chain(s->commit_top_bs, s->base_bs); s->chain_frozen =3D false; + bdrv_graph_rdunlock_main_loop(); =20 /* Remove base node parent that still uses BLK_PERM_WRITE/RESIZE before * the normal backing chain can be restored. */ @@ -68,7 +70,9 @@ static void commit_abort(Job *job) BlockDriverState *top_bs =3D blk_bs(s->top); =20 if (s->chain_frozen) { + bdrv_graph_rdlock_main_loop(); bdrv_unfreeze_backing_chain(s->commit_top_bs, s->base_bs); + bdrv_graph_rdunlock_main_loop(); } =20 /* Make sure commit_top_bs and top stay around until bdrv_replace_node= () */ @@ -404,7 +408,9 @@ void commit_start(const char *job_id, BlockDriverState = *bs, =20 fail: if (s->chain_frozen) { + bdrv_graph_rdlock_main_loop(); bdrv_unfreeze_backing_chain(commit_top_bs, base); + bdrv_graph_rdunlock_main_loop(); } if (s->base) { blk_unref(s->base); diff --git a/block/copy-on-read.c b/block/copy-on-read.c index 5149fcf63a..6f245b629a 100644 --- a/block/copy-on-read.c +++ b/block/copy-on-read.c @@ -35,8 +35,8 @@ typedef struct BDRVStateCOR { } BDRVStateCOR; =20 =20 -static int cor_open(BlockDriverState *bs, QDict *options, int flags, - Error **errp) +static int GRAPH_UNLOCKED +cor_open(BlockDriverState *bs, QDict *options, int flags, Error **errp) { BlockDriverState *bottom_bs =3D NULL; BDRVStateCOR *state =3D bs->opaque; @@ -44,6 +44,8 @@ static int cor_open(BlockDriverState *bs, QDict *options,= int flags, const char *bottom_node =3D qdict_get_try_str(options, "bottom"); int ret; =20 + GLOBAL_STATE_CODE(); + ret =3D bdrv_open_file_child(NULL, options, "file", bs, errp); if (ret < 0) { return ret; @@ -59,6 +61,8 @@ static int cor_open(BlockDriverState *bs, QDict *options,= int flags, bs->file->bs->supported_zero_flags); =20 if (bottom_node) { + GRAPH_RDLOCK_GUARD_MAINLOOP(); + bottom_bs =3D bdrv_find_node(bottom_node); if (!bottom_bs) { error_setg(errp, "Bottom node '%s' not found", bottom_node); @@ -227,13 +231,17 @@ cor_co_lock_medium(BlockDriverState *bs, bool locked) } =20 =20 -static void cor_close(BlockDriverState *bs) +static void GRAPH_UNLOCKED cor_close(BlockDriverState *bs) { BDRVStateCOR *s =3D bs->opaque; =20 + GLOBAL_STATE_CODE(); + if (s->chain_frozen) { + bdrv_graph_rdlock_main_loop(); s->chain_frozen =3D false; bdrv_unfreeze_backing_chain(bs, s->bottom_bs); + bdrv_graph_rdunlock_main_loop(); } =20 bdrv_unref(s->bottom_bs); @@ -263,12 +271,15 @@ static BlockDriver bdrv_copy_on_read =3D { }; =20 =20 -void bdrv_cor_filter_drop(BlockDriverState *cor_filter_bs) +void no_coroutine_fn bdrv_cor_filter_drop(BlockDriverState *cor_filter_bs) { BDRVStateCOR *s =3D cor_filter_bs->opaque; =20 + GLOBAL_STATE_CODE(); + /* unfreeze, as otherwise bdrv_replace_node() will fail */ if (s->chain_frozen) { + GRAPH_RDLOCK_GUARD_MAINLOOP(); s->chain_frozen =3D false; bdrv_unfreeze_backing_chain(cor_filter_bs, s->bottom_bs); } diff --git a/block/mirror.c b/block/mirror.c index 75e826dac8..f8e439371c 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -678,6 +678,7 @@ static int mirror_exit_common(Job *job) s->prepared =3D true; =20 aio_context_acquire(qemu_get_aio_context()); + bdrv_graph_rdlock_main_loop(); =20 mirror_top_bs =3D s->mirror_top_bs; bs_opaque =3D mirror_top_bs->opaque; @@ -696,6 +697,8 @@ static int mirror_exit_common(Job *job) bdrv_ref(mirror_top_bs); bdrv_ref(target_bs); =20 + bdrv_graph_rdunlock_main_loop(); + /* * Remove target parent that still uses BLK_PERM_WRITE/RESIZE before * inserting target_bs at s->to_replace, where we might not be able to= get diff --git a/block/stream.c b/block/stream.c index 5323a9976d..c32c98339a 100644 --- a/block/stream.c +++ b/block/stream.c @@ -266,6 +266,8 @@ void stream_start(const char *job_id, BlockDriverState = *bs, assert(!(base && bottom)); assert(!(backing_file_str && bottom)); =20 + bdrv_graph_rdlock_main_loop(); + if (bottom) { /* * New simple interface. The code is written in terms of old inter= face @@ -278,13 +280,11 @@ void stream_start(const char *job_id, BlockDriverStat= e *bs, assert(!bottom->drv->is_filter); base_overlay =3D above_base =3D bottom; } else { - GRAPH_RDLOCK_GUARD_MAINLOOP(); - base_overlay =3D bdrv_find_overlay(bs, base); if (!base_overlay) { error_setg(errp, "'%s' is not in the backing chain of '%s'", base->node_name, bs->node_name); - return; + goto out_rdlock; } =20 /* @@ -306,7 +306,7 @@ void stream_start(const char *job_id, BlockDriverState = *bs, if (bs_read_only) { /* Hold the chain during reopen */ if (bdrv_freeze_backing_chain(bs, above_base, errp) < 0) { - return; + goto out_rdlock; } =20 ret =3D bdrv_reopen_set_read_only(bs, false, errp); @@ -315,10 +315,12 @@ void stream_start(const char *job_id, BlockDriverStat= e *bs, bdrv_unfreeze_backing_chain(bs, above_base); =20 if (ret < 0) { - return; + goto out_rdlock; } } =20 + bdrv_graph_rdunlock_main_loop(); + opts =3D qdict_new(); =20 qdict_put_str(opts, "driver", "copy-on-read"); @@ -413,4 +415,8 @@ fail: if (bs_read_only) { bdrv_reopen_set_read_only(bs, true, NULL); } + return; + +out_rdlock: + bdrv_graph_rdunlock_main_loop(); } --=20 2.41.0 From nobody Wed Nov 27 09:52:08 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=1699382926; cv=none; d=zohomail.com; s=zohoarc; b=HYD4/qZYyx3tOD08dVpJfjesMI0F9olq5m7PqWydjG/Nt15E7dCDOJmYjojmsgtd+Jytmc/wtM5EWDtYwAYcWqbkQhIlvg2zPwSNm+OpBOh6TUG/i+wKU9b/tMPQH/zxPlW+cTavVRORNOCV5SnhgzL9FwISiTba7xWpL33kPvA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699382926; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Drxgkp0ROJENIrnPXu5nTdA0hV4Rbo4Y1Phpl0DAVFc=; b=oAtVirhsGZtjvC6cLTE8o90EEoJ3K18SEFZ9Je6IOpxaFzi2+EUnfF35qdbzBPGAADxawcyigAoH7CMz4BUr0jc799jwuQzDIUelt2HUcBXKPVXhJcNMzKBa/THZ0NAsTW8lNbLudz9Zc0uq8SxQ0bOCq2biVubG+JI2Mwtz2ug= 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 1699382926978387.5798036807031; Tue, 7 Nov 2023 10:48:46 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0R69-0004Ln-L7; Tue, 07 Nov 2023 13:46:49 -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 1r0R5h-0003vS-SL for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:23 -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 1r0R5f-0001UH-Ab for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:21 -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.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-639-H-3So__5NB2fZkqyJMTt7g-1; Tue, 07 Nov 2023 13:46:15 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 77F41101AA42 for ; Tue, 7 Nov 2023 18:46:15 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id 023D0C1290F; Tue, 7 Nov 2023 18:46:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699382777; 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=Drxgkp0ROJENIrnPXu5nTdA0hV4Rbo4Y1Phpl0DAVFc=; b=dbYk8n264owuIk/efYtGjuy+Lb4pV7VGICWkSoRxMK3nfPxYQeKP50XyGVqa27HiSst9w2 uQg290Q5vci/7bsojgIT9+F0RX8a0K0nOLNi/Hsp0Af7biZmZHYjqz3Km0bToIECkDMaQl b5CdLONLKBmbIScUglcagdvLKo1hudU= X-MC-Unique: H-3So__5NB2fZkqyJMTt7g-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 10/25] block: Mark bdrv_chain_contains() and callers GRAPH_RDLOCK Date: Tue, 7 Nov 2023 19:45:50 +0100 Message-ID: <20231107184605.236540-11-kwolf@redhat.com> In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com> References: <20231107184605.236540-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1699382927379100001 Content-Type: text/plain; charset="utf-8" This adds GRAPH_RDLOCK annotations to declare that callers of bdrv_chain_contains() need to hold a reader lock for the graph because it calls bdrv_filter_or_cow_bs(), which accesses bs->file/backing. Signed-off-by: Kevin Wolf Message-ID: <20231027155333.420094-11-kwolf@redhat.com> Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- include/block/block-global-state.h | 4 ++- block.c | 1 - block/block-copy.c | 2 ++ blockdev.c | 48 +++++++++++++++++------------- 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/include/block/block-global-state.h b/include/block/block-globa= l-state.h index 545708c35a..9a33bd7ef9 100644 --- a/include/block/block-global-state.h +++ b/include/block/block-global-state.h @@ -199,7 +199,9 @@ XDbgBlockGraph * GRAPH_RDLOCK bdrv_get_xdbg_block_graph= (Error **errp); BlockDriverState *bdrv_lookup_bs(const char *device, const char *node_name, Error **errp); -bool bdrv_chain_contains(BlockDriverState *top, BlockDriverState *base); +bool GRAPH_RDLOCK +bdrv_chain_contains(BlockDriverState *top, BlockDriverState *base); + BlockDriverState *bdrv_next_node(BlockDriverState *bs); BlockDriverState *bdrv_next_all_states(BlockDriverState *bs); =20 diff --git a/block.c b/block.c index dc1980ee42..bb322df7d8 100644 --- a/block.c +++ b/block.c @@ -6524,7 +6524,6 @@ bool bdrv_chain_contains(BlockDriverState *top, Block= DriverState *base) { =20 GLOBAL_STATE_CODE(); - GRAPH_RDLOCK_GUARD_MAINLOOP(); =20 while (top && top !=3D base) { top =3D bdrv_filter_or_cow_bs(top); diff --git a/block/block-copy.c b/block/block-copy.c index 6b2be3d204..9ee3dd7ef5 100644 --- a/block/block-copy.c +++ b/block/block-copy.c @@ -399,7 +399,9 @@ BlockCopyState *block_copy_state_new(BdrvChild *source,= BdrvChild *target, * For more information see commit f8d59dfb40bb and test * tests/qemu-iotests/222 */ + bdrv_graph_rdlock_main_loop(); is_fleecing =3D bdrv_chain_contains(target->bs, source->bs); + bdrv_graph_rdunlock_main_loop(); =20 s =3D g_new(BlockCopyState, 1); *s =3D (BlockCopyState) { diff --git a/blockdev.c b/blockdev.c index 5f15ea3b1d..f04faf6373 100644 --- a/blockdev.c +++ b/blockdev.c @@ -2450,11 +2450,12 @@ void qmp_block_stream(const char *job_id, const cha= r *device, aio_context =3D bdrv_get_aio_context(bs); aio_context_acquire(aio_context); =20 + bdrv_graph_rdlock_main_loop(); if (base) { base_bs =3D bdrv_find_backing_image(bs, base); if (base_bs =3D=3D NULL) { error_setg(errp, "Can't find '%s' in the backing chain", base); - goto out; + goto out_rdlock; } assert(bdrv_get_aio_context(base_bs) =3D=3D aio_context); } @@ -2462,38 +2463,36 @@ void qmp_block_stream(const char *job_id, const cha= r *device, if (base_node) { base_bs =3D bdrv_lookup_bs(NULL, base_node, errp); if (!base_bs) { - goto out; + goto out_rdlock; } if (bs =3D=3D base_bs || !bdrv_chain_contains(bs, base_bs)) { error_setg(errp, "Node '%s' is not a backing image of '%s'", base_node, device); - goto out; + goto out_rdlock; } assert(bdrv_get_aio_context(base_bs) =3D=3D aio_context); =20 - bdrv_graph_rdlock_main_loop(); bdrv_refresh_filename(base_bs); - bdrv_graph_rdunlock_main_loop(); } =20 if (bottom) { bottom_bs =3D bdrv_lookup_bs(NULL, bottom, errp); if (!bottom_bs) { - goto out; + goto out_rdlock; } if (!bottom_bs->drv) { error_setg(errp, "Node '%s' is not open", bottom); - goto out; + goto out_rdlock; } if (bottom_bs->drv->is_filter) { error_setg(errp, "Node '%s' is a filter, use a non-filter node= " "as 'bottom'", bottom); - goto out; + goto out_rdlock; } if (!bdrv_chain_contains(bs, bottom_bs)) { error_setg(errp, "Node '%s' is not in a chain starting from '%= s'", bottom, device); - goto out; + goto out_rdlock; } assert(bdrv_get_aio_context(bottom_bs) =3D=3D aio_context); } @@ -2501,14 +2500,12 @@ void qmp_block_stream(const char *job_id, const cha= r *device, /* * Check for op blockers in the whole chain between bs and base (or bo= ttom) */ - bdrv_graph_rdlock_main_loop(); iter_end =3D bottom ? bdrv_filter_or_cow_bs(bottom_bs) : base_bs; for (iter =3D bs; iter && iter !=3D iter_end; iter =3D bdrv_filter_or_cow_bs(iter)) { if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) { - bdrv_graph_rdunlock_main_loop(); - goto out; + goto out_rdlock; } } bdrv_graph_rdunlock_main_loop(); @@ -2540,6 +2537,11 @@ void qmp_block_stream(const char *job_id, const char= *device, =20 out: aio_context_release(aio_context); + return; + +out_rdlock: + bdrv_graph_rdunlock_main_loop(); + aio_context_release(aio_context); } =20 void qmp_block_commit(const char *job_id, const char *device, @@ -3439,39 +3441,38 @@ void qmp_change_backing_file(const char *device, aio_context =3D bdrv_get_aio_context(bs); aio_context_acquire(aio_context); =20 + bdrv_graph_rdlock_main_loop(); + image_bs =3D bdrv_lookup_bs(NULL, image_node_name, &local_err); if (local_err) { error_propagate(errp, local_err); - goto out; + goto out_rdlock; } =20 if (!image_bs) { error_setg(errp, "image file not found"); - goto out; + goto out_rdlock; } =20 - bdrv_graph_rdlock_main_loop(); if (bdrv_find_base(image_bs) =3D=3D image_bs) { error_setg(errp, "not allowing backing file change on an image " "without a backing file"); - bdrv_graph_rdunlock_main_loop(); - goto out; + goto out_rdlock; } =20 /* even though we are not necessarily operating on bs, we need it to * determine if block ops are currently prohibited on the chain */ if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { - bdrv_graph_rdunlock_main_loop(); - goto out; + goto out_rdlock; } - bdrv_graph_rdunlock_main_loop(); =20 /* final sanity check */ if (!bdrv_chain_contains(bs, image_bs)) { error_setg(errp, "'%s' and image file are not in the same chain", device); - goto out; + goto out_rdlock; } + bdrv_graph_rdunlock_main_loop(); =20 /* if not r/w, reopen to make r/w */ ro =3D bdrv_is_read_only(image_bs); @@ -3499,6 +3500,11 @@ void qmp_change_backing_file(const char *device, =20 out: aio_context_release(aio_context); + return; + +out_rdlock: + bdrv_graph_rdunlock_main_loop(); + aio_context_release(aio_context); } =20 void qmp_blockdev_add(BlockdevOptions *options, Error **errp) --=20 2.41.0 From nobody Wed Nov 27 09:52:08 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=1699382816; cv=none; d=zohomail.com; s=zohoarc; b=U3/fZpuOemsy1KHPclOKH0IWDGKTI77oIbwFTorn0UPhez9fDlAJ/L6Uee+lcEdRkZsbi281gFXS6jjxAyraJPKt8xUpK5PcG8OUcLH/V7scXuhDk5BosrWgyUq0tkk7zcQ5M1WAHkKnoKeJ4mRurEplFjI8EgwnWIratJqHzTU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699382816; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=bR2VYRJOr0FUz5LyixeiagYhQMQG2K629OQ/c03qinA=; b=Nsel0uezPvmklIZ9cLhynsO+8x82gLwZ5hvNcd+THJQXck10SQ0u/ALRQrV+f2zDL48OgP6vPXapOxMRXSvpAIxke36jjfZSnYt8op9zUDdLdMqR5DO1SAOT63ZUKThM8XxYpU47acoDOaMPNDhV2ygFjC1imnsFYQ7U3udYFIQ= 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 1699382816394262.76807962487806; Tue, 7 Nov 2023 10:46:56 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0R5r-00041K-JD; Tue, 07 Nov 2023 13:46:32 -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 1r0R5h-0003vQ-Mz for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:23 -0500 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 1r0R5f-0001Ue-9m for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:20 -0500 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-190-Bhf95x6DOk66MQiKLlKq1w-1; Tue, 07 Nov 2023 13:46:16 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3704D1C068D4 for ; Tue, 7 Nov 2023 18:46:16 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id AF3C2C1290F; Tue, 7 Nov 2023 18:46:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699382777; 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=bR2VYRJOr0FUz5LyixeiagYhQMQG2K629OQ/c03qinA=; b=eX1HMo3UDhpukVkYnDeR9SnyvEDy2pK+5ufo7jeUX6cIYohd/KvcuDjDQA5yz3S7gpXDom ixE06R2AHUVM7LEdr9sDnOim90trWK9p32ItCBR5FtvwTkW6iCpIo8MwpQAesvMJkv7Y/m mKrUzqDDJdiBlOFgo/D9tSEPF1IoflM= X-MC-Unique: Bhf95x6DOk66MQiKLlKq1w-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 11/25] block: Mark bdrv_filter_child() and callers GRAPH_RDLOCK Date: Tue, 7 Nov 2023 19:45:51 +0100 Message-ID: <20231107184605.236540-12-kwolf@redhat.com> In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com> References: <20231107184605.236540-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1699382816953100001 Content-Type: text/plain; charset="utf-8" This adds GRAPH_RDLOCK annotations to declare that callers of bdrv_filter_child() need to hold a reader lock for the graph because it accesses bs->file/backing. Signed-off-by: Kevin Wolf Message-ID: <20231027155333.420094-12-kwolf@redhat.com> Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- include/block/block_int-io.h | 4 ++-- block.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/block/block_int-io.h b/include/block/block_int-io.h index 4e7bf57a5e..17547a2dab 100644 --- a/include/block/block_int-io.h +++ b/include/block/block_int-io.h @@ -131,8 +131,8 @@ int co_wrapper_mixed_bdrv_rdlock bdrv_refresh_total_sectors(BlockDriverState *bs, int64_t hint); =20 BdrvChild *bdrv_cow_child(BlockDriverState *bs); -BdrvChild *bdrv_filter_child(BlockDriverState *bs); -BdrvChild *bdrv_filter_or_cow_child(BlockDriverState *bs); +BdrvChild * GRAPH_RDLOCK bdrv_filter_child(BlockDriverState *bs); +BdrvChild * GRAPH_RDLOCK bdrv_filter_or_cow_child(BlockDriverState *bs); BdrvChild * GRAPH_RDLOCK bdrv_primary_child(BlockDriverState *bs); BlockDriverState * GRAPH_RDLOCK bdrv_skip_filters(BlockDriverState *bs); BlockDriverState * GRAPH_RDLOCK bdrv_backing_chain_next(BlockDriverState *= bs); diff --git a/block.c b/block.c index bb322df7d8..499b147315 100644 --- a/block.c +++ b/block.c @@ -8491,8 +8491,8 @@ BdrvChild *bdrv_primary_child(BlockDriverState *bs) return found; } =20 -static BlockDriverState *bdrv_do_skip_filters(BlockDriverState *bs, - bool stop_on_explicit_filter) +static BlockDriverState * GRAPH_RDLOCK +bdrv_do_skip_filters(BlockDriverState *bs, bool stop_on_explicit_filter) { BdrvChild *c; =20 --=20 2.41.0 From nobody Wed Nov 27 09:52:08 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=1699382939; cv=none; d=zohomail.com; s=zohoarc; b=Ft/3Hl6kqQWCUAvv2sjySySdn0Pe8jlI66jcm2LmqDP8PQr8EPu/m5/1dFvWTGH/Fpm3clLWOFXL3R51q7t2/6oE8Ac8uJ3OKzpJDOyZGdY0Bi8zOoh6k6M3ukM1gv9oIwOo4RTWBetswk3e2JxwmLsaPnEbzyLI69mIoGyAv1g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699382939; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=6sfjBtg43d8qTRgHVSUXIOWhcEaXJBXhjGAOPyBdwgo=; b=mcxWE6f0YqymaaALlweUC2lXJsZG38H/dndwye9w7EAuSPoIiI0wLGVBQxHMDtf5ISajzrULLI0JZGl/2Y2z80Q5V+AtaGznIfKBQyYX4+GwfbqBSCFTZIcJj9KF2lKfTIH2kzUKMvdxhYtE4X9E4eEhSBvVtMDUxNBkbSyX2ZE= 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 1699382939402231.37370811171854; Tue, 7 Nov 2023 10:48:59 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0R68-0004Fn-Lq; Tue, 07 Nov 2023 13:46:48 -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 1r0R5j-0003vb-7b for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:23 -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 1r0R5h-0001Uw-31 for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:22 -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.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-17-IXIPfhyGOrGwN5MaOltwRg-1; Tue, 07 Nov 2023 13:46:17 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E428F834FC3 for ; Tue, 7 Nov 2023 18:46:16 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6B46EC1290F; Tue, 7 Nov 2023 18:46:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699382779; 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=6sfjBtg43d8qTRgHVSUXIOWhcEaXJBXhjGAOPyBdwgo=; b=OnP4lVErIml7B1UOUopUBWVIQWAgkazxQTgJtj0lrfLh9+o0Tpt2H+25mGGMTllvaYfiOq rMADFYsziouMULkj+nSwzObnyauT+BgmGlc/IrS8sOM4GQspyOt14LOVo/4KuJsiVMBrOb 5kjp5cbtAZSgduQjizrumGpkHItVVdw= X-MC-Unique: IXIPfhyGOrGwN5MaOltwRg-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 12/25] block: Mark bdrv_cow_child() and callers GRAPH_RDLOCK Date: Tue, 7 Nov 2023 19:45:52 +0100 Message-ID: <20231107184605.236540-13-kwolf@redhat.com> In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com> References: <20231107184605.236540-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1699382941439100005 Content-Type: text/plain; charset="utf-8" This adds GRAPH_RDLOCK annotations to declare that callers of bdrv_cow_child() need to hold a reader lock for the graph because it accesses bs->backing. Signed-off-by: Kevin Wolf Message-ID: <20231027155333.420094-13-kwolf@redhat.com> Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- include/block/block_int-io.h | 5 +++-- block/stream.c | 2 +- qemu-img.c | 11 ++++++++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/include/block/block_int-io.h b/include/block/block_int-io.h index 17547a2dab..4a7cf2b4fd 100644 --- a/include/block/block_int-io.h +++ b/include/block/block_int-io.h @@ -130,14 +130,15 @@ bdrv_co_refresh_total_sectors(BlockDriverState *bs, i= nt64_t hint); int co_wrapper_mixed_bdrv_rdlock bdrv_refresh_total_sectors(BlockDriverState *bs, int64_t hint); =20 -BdrvChild *bdrv_cow_child(BlockDriverState *bs); +BdrvChild * GRAPH_RDLOCK bdrv_cow_child(BlockDriverState *bs); BdrvChild * GRAPH_RDLOCK bdrv_filter_child(BlockDriverState *bs); BdrvChild * GRAPH_RDLOCK bdrv_filter_or_cow_child(BlockDriverState *bs); BdrvChild * GRAPH_RDLOCK bdrv_primary_child(BlockDriverState *bs); BlockDriverState * GRAPH_RDLOCK bdrv_skip_filters(BlockDriverState *bs); BlockDriverState * GRAPH_RDLOCK bdrv_backing_chain_next(BlockDriverState *= bs); =20 -static inline BlockDriverState *bdrv_cow_bs(BlockDriverState *bs) +static inline BlockDriverState * GRAPH_RDLOCK +bdrv_cow_bs(BlockDriverState *bs) { IO_CODE(); return child_bs(bdrv_cow_child(bs)); diff --git a/block/stream.c b/block/stream.c index c32c98339a..3f5d773535 100644 --- a/block/stream.c +++ b/block/stream.c @@ -90,7 +90,7 @@ static int stream_prepare(Job *job) unfiltered_base =3D bdrv_skip_filters(base); bdrv_graph_rdunlock_main_loop(); =20 - if (bdrv_cow_child(unfiltered_bs)) { + if (unfiltered_bs_cow) { const char *base_id =3D NULL, *base_fmt =3D NULL; if (unfiltered_base) { base_id =3D s->backing_file_str ?: unfiltered_base->filename; diff --git a/qemu-img.c b/qemu-img.c index 33f3ab5fba..5a77f67719 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -3541,7 +3541,7 @@ static int img_rebase(int argc, char **argv) uint8_t *buf_old =3D NULL; uint8_t *buf_new =3D NULL; BlockDriverState *bs =3D NULL, *prefix_chain_bs =3D NULL; - BlockDriverState *unfiltered_bs; + BlockDriverState *unfiltered_bs, *unfiltered_bs_cow; BlockDriverInfo bdi =3D {0}; char *filename; const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg; @@ -3675,6 +3675,7 @@ static int img_rebase(int argc, char **argv) =20 bdrv_graph_rdlock_main_loop(); unfiltered_bs =3D bdrv_skip_filters(bs); + unfiltered_bs_cow =3D bdrv_cow_bs(unfiltered_bs); bdrv_graph_rdunlock_main_loop(); =20 if (compress && !block_driver_can_compress(unfiltered_bs->drv)) { @@ -3710,7 +3711,11 @@ static int img_rebase(int argc, char **argv) /* For safe rebasing we need to compare old and new backing file */ if (!unsafe) { QDict *options =3D NULL; - BlockDriverState *base_bs =3D bdrv_cow_bs(unfiltered_bs); + BlockDriverState *base_bs; + + bdrv_graph_rdlock_main_loop(); + base_bs =3D bdrv_cow_bs(unfiltered_bs); + bdrv_graph_rdunlock_main_loop(); =20 if (base_bs) { blk_old_backing =3D blk_new(qemu_get_aio_context(), @@ -3876,7 +3881,7 @@ static int img_rebase(int argc, char **argv) * If cluster wasn't changed since prefix_chain, we don't = need * to take action */ - ret =3D bdrv_is_allocated_above(bdrv_cow_bs(unfiltered_bs), + ret =3D bdrv_is_allocated_above(unfiltered_bs_cow, prefix_chain_bs, false, offset, n, &n); if (ret < 0) { --=20 2.41.0 From nobody Wed Nov 27 09:52:08 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=1699383000; cv=none; d=zohomail.com; s=zohoarc; b=NtMWWibjJ166LEwDLcUvyTeU3E468jEFbxPcqKwPR0lZn5minenRpsGW+i4pjiTpClCJk37JBeaJ2k1mbniEphiOL0cmxlD7SM/jbIr4ZEHyLQkNQjxaHLYlQ0AFuSlsT1W47bwxj+u/hu2zlzS//r8ltlFKzxhcbCHHv25pzzs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699383000; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=kcuOf9Rl0aKadUvT8nleBmaG7Y6sOaD9QAYnICflMR0=; b=KkbwvbhCsHpXC88ynE4FcAvwK/jqftsVAo80BIGoxyJtAGPJzLKDxMkohWPfrzndGUJYdxOlXb5hF+XLbKS2JfGRjALHBUF/1T2VyuAKxf9G3r9ky8qEDQNpHU7AEE74zXaT9Ke8eWHDsi3QVtrsTI3xvorrscCJ3bFzLSmOsJw= 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 1699383000425395.8928067121202; Tue, 7 Nov 2023 10:50:00 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0R6H-0004aM-64; Tue, 07 Nov 2023 13:46:57 -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 1r0R5m-0003zP-Hk for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:26 -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 1r0R5h-0001VL-Ic for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:25 -0500 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-685-C8YesaxwOROfZaud8W5whw-1; Tue, 07 Nov 2023 13:46:17 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9B62C3C1CC2C for ; Tue, 7 Nov 2023 18:46:17 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id 264EFC1290F; Tue, 7 Nov 2023 18:46:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699382779; 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=kcuOf9Rl0aKadUvT8nleBmaG7Y6sOaD9QAYnICflMR0=; b=gDqsOVsKnFJUwvHRLRFz3++2nnzsXV/MLuarlcBF0Po1zLUYoEiaNjZF412mVXd+3hZDR8 QETiKVmV+ZbEiSJLOJqawNnPwl8HU+7fx+ZdG9GZkfn1vM95XWjtbpQEOP/dzRgEoy4PiL ZUmnLRz/tFnaAXgkqgBwpjm2n6x+MJo= X-MC-Unique: C8YesaxwOROfZaud8W5whw-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 13/25] block: Mark bdrv_set_backing_hd_drained() GRAPH_WRLOCK Date: Tue, 7 Nov 2023 19:45:53 +0100 Message-ID: <20231107184605.236540-14-kwolf@redhat.com> In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com> References: <20231107184605.236540-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1699383001669100003 Content-Type: text/plain; charset="utf-8" Instead of taking the writer lock internally, require callers to already hold it when calling bdrv_set_backing_hd_drained(). Basically everthing in the function needs the lock and its callers may already want to hold the graph lock and so wouldn't be able to call functions that take it internally. Signed-off-by: Kevin Wolf Message-ID: <20231027155333.420094-14-kwolf@redhat.com> Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- include/block/block-global-state.h | 7 ++++--- block.c | 4 ++-- block/stream.c | 2 ++ 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/include/block/block-global-state.h b/include/block/block-globa= l-state.h index 9a33bd7ef9..a1fd70ec97 100644 --- a/include/block/block-global-state.h +++ b/include/block/block-global-state.h @@ -101,9 +101,10 @@ bdrv_co_open_blockdev_ref(BlockdevRef *ref, Error **er= rp); =20 int bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd, Error **errp); -int bdrv_set_backing_hd_drained(BlockDriverState *bs, - BlockDriverState *backing_hd, - Error **errp); +int GRAPH_WRLOCK +bdrv_set_backing_hd_drained(BlockDriverState *bs, BlockDriverState *backin= g_hd, + Error **errp); + int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options, const char *bdref_key, Error **errp); =20 diff --git a/block.c b/block.c index 499b147315..d79a6f41f9 100644 --- a/block.c +++ b/block.c @@ -3557,7 +3557,6 @@ int bdrv_set_backing_hd_drained(BlockDriverState *bs, if (bs->backing) { assert(bs->backing->bs->quiesce_counter > 0); } - bdrv_graph_wrlock(backing_hd); =20 ret =3D bdrv_set_backing_noperm(bs, backing_hd, tran, errp); if (ret < 0) { @@ -3567,7 +3566,6 @@ int bdrv_set_backing_hd_drained(BlockDriverState *bs, ret =3D bdrv_refresh_perms(bs, tran, errp); out: tran_finalize(tran, ret); - bdrv_graph_wrunlock(); return ret; } =20 @@ -3580,7 +3578,9 @@ int bdrv_set_backing_hd(BlockDriverState *bs, BlockDr= iverState *backing_hd, =20 bdrv_ref(drain_bs); bdrv_drained_begin(drain_bs); + bdrv_graph_wrlock(backing_hd); ret =3D bdrv_set_backing_hd_drained(bs, backing_hd, errp); + bdrv_graph_wrunlock(); bdrv_drained_end(drain_bs); bdrv_unref(drain_bs); =20 diff --git a/block/stream.c b/block/stream.c index 3f5d773535..0b92410c00 100644 --- a/block/stream.c +++ b/block/stream.c @@ -99,7 +99,9 @@ static int stream_prepare(Job *job) } } =20 + bdrv_graph_wrlock(base); bdrv_set_backing_hd_drained(unfiltered_bs, base, &local_err); + bdrv_graph_wrunlock(); =20 /* * This call will do I/O, so the graph can change again from here = on. --=20 2.41.0 From nobody Wed Nov 27 09:52:08 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=1699382920; cv=none; d=zohomail.com; s=zohoarc; b=WbfoUGKUolWTC+Ri1glrpeh1mS/llt3EThT50qRRt/Y5g8lxevGN8nx3oohuE6WjXv6kb5NH/Fzftx8qb1ZJdGnBQdyD71u+68XZOxVlr1Um6nHKVh0nlVpH7lqVuAh1AgvNMG75zPDRjNaHR/LxBLzhijmFtldXKXG6G4QGmx0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699382920; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=INYB7n+QawKoYeQfVvQsSGeZKTEYgzBISam7H2frffc=; b=Jv91G+pCM7Oj8cVhgzig9YbnJ6BE2Ro6VIAbRgESkELac8g6QJB3KytBcTlHOOVPdQuCgTk1J5IkWW6Zin7VprjSM7VmjVKit8wQjikFkA83qeb3WfDLC4Pwc+KPSkGoQb+4Fd00sZy5Mn3iPISPqUFcJxlNydx3EFE6V7RTZjw= 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 1699382920002732.0788383843128; Tue, 7 Nov 2023 10:48:40 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0R6B-0004Q0-DY; Tue, 07 Nov 2023 13:46:51 -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 1r0R5j-0003vc-7y for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:23 -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 1r0R5h-0001Va-Dz for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:22 -0500 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-691-vM39sbkrNi2sUA0vfhb2Tg-1; Tue, 07 Nov 2023 13:46:18 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 515433C1CC27 for ; Tue, 7 Nov 2023 18:46:18 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id D1173C1290F; Tue, 7 Nov 2023 18:46:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699382779; 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=INYB7n+QawKoYeQfVvQsSGeZKTEYgzBISam7H2frffc=; b=VQBzcN9zJTPCqy9pKAWsLuokV/ebCHdMl3p6J/f9+zvjhqNUbBw3tndnYh+KpEkSum0uuL QAMbWNYUYNmIMR9UzSmy9ITz1pQvGuu2vYus4NF9wXp+CMtXm07qS/vXbyKkfjdJgbNoWO 1qm0+PJqdxjIExChKKB6ncOFfMyoqc4= X-MC-Unique: vM39sbkrNi2sUA0vfhb2Tg-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 14/25] block: Inline bdrv_set_backing_noperm() Date: Tue, 7 Nov 2023 19:45:54 +0100 Message-ID: <20231107184605.236540-15-kwolf@redhat.com> In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com> References: <20231107184605.236540-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1699382921357100001 Content-Type: text/plain; charset="utf-8" It's only a single line and has a single caller. Inlining makes things a bit easier to follow. Signed-off-by: Kevin Wolf Message-ID: <20231027155333.420094-15-kwolf@redhat.com> Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- block.c | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/block.c b/block.c index d79a6f41f9..dc43e36f69 100644 --- a/block.c +++ b/block.c @@ -3532,19 +3532,7 @@ out: * * If a backing child is already present (i.e. we're detaching a node), th= at * child node must be drained. - * - * After calling this function, the transaction @tran may only be completed - * while holding a writer lock for the graph. */ -static int GRAPH_WRLOCK -bdrv_set_backing_noperm(BlockDriverState *bs, - BlockDriverState *backing_hd, - Transaction *tran, Error **errp) -{ - GLOBAL_STATE_CODE(); - return bdrv_set_file_or_backing_noperm(bs, backing_hd, true, tran, err= p); -} - int bdrv_set_backing_hd_drained(BlockDriverState *bs, BlockDriverState *backing_hd, Error **errp) @@ -3558,7 +3546,7 @@ int bdrv_set_backing_hd_drained(BlockDriverState *bs, assert(bs->backing->bs->quiesce_counter > 0); } =20 - ret =3D bdrv_set_backing_noperm(bs, backing_hd, tran, errp); + ret =3D bdrv_set_file_or_backing_noperm(bs, backing_hd, true, tran, er= rp); if (ret < 0) { goto out; } --=20 2.41.0 From nobody Wed Nov 27 09:52:08 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=1699382898; cv=none; d=zohomail.com; s=zohoarc; b=akG0Y6UaDBYtIVTTG+C+83oive47oGQWgUUG5kcQAvDC3ZsmmvYDAEFGhiMz0jRKYiCZyNjFjzeoazFL4n8rrGE58KEPIeEexpGuhPX0FInpKdgdGyPAJfnrR3wJmDtdzJYnXosvLbDJkkZQ3hw7x9c55ezHP3D+e8q5lrWeg0o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699382898; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=qA6LD5FvtIcm4HXAg7hKc9KfBeIebVmoNokBPYIOAZw=; b=LyhA4NxoGfiORbLRrWl19SRCadcbLSu/834ji5mJ61QIzPItHfxoJNJIhsaGzmFmjN6zYPxpVF1JEJ66VFUvIWI2TFA26Bhpv4dQEts+Iy5qJD23RdTYWJ5ZyY4qWXX1hRTH037S8LKPNKVeqRLGN10jQIJuG95mcCPA+RbNEQI= 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 1699382898678810.3618483596408; Tue, 7 Nov 2023 10:48:18 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0R6F-0004WM-G1; Tue, 07 Nov 2023 13:46:55 -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 1r0R5p-00041Z-A1 for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:31 -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 1r0R5m-0001WW-V7 for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:28 -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.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-41-9Iqsv_JAP9azlHBbgzYUhQ-1; Tue, 07 Nov 2023 13:46:19 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 082B2185A788 for ; Tue, 7 Nov 2023 18:46:19 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id 87799C1290F; Tue, 7 Nov 2023 18:46:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699382783; 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=qA6LD5FvtIcm4HXAg7hKc9KfBeIebVmoNokBPYIOAZw=; b=HcuzCufm5+ExQAWjmnHnF/oPWkpaTB8KYVArlh3Q3lk4ERi78xWp+Q93mC3l+y+Qife/xe VOIwLWTzwupxRdicYtqaP6hNhYF7sthFF4mO0q75mkcRW0tIUz/FpKN5kKUnFX7e6Dz2Xr EUatwIjCajOUG9lI3EB+pFL6bOKGD9g= X-MC-Unique: 9Iqsv_JAP9azlHBbgzYUhQ-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 15/25] block: Mark bdrv_replace_node_common() GRAPH_WRLOCK Date: Tue, 7 Nov 2023 19:45:55 +0100 Message-ID: <20231107184605.236540-16-kwolf@redhat.com> In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com> References: <20231107184605.236540-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1699382899290100001 Content-Type: text/plain; charset="utf-8" Instead of taking the writer lock internally, require callers to already hold it when calling bdrv_replace_node_common(). Basically everthing in the function needs the lock and its callers may already want to hold the graph lock and so wouldn't be able to call functions that take it internally. Signed-off-by: Kevin Wolf Message-ID: <20231027155333.420094-16-kwolf@redhat.com> Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- block.c | 68 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 25 deletions(-) diff --git a/block.c b/block.c index dc43e36f69..c7409cf658 100644 --- a/block.c +++ b/block.c @@ -5412,6 +5412,9 @@ bdrv_replace_node_noperm(BlockDriverState *from, } =20 /* + * Switch all parents of @from to point to @to instead. @from and @to must= be in + * the same AioContext and both must be drained. + * * With auto_skip=3Dtrue bdrv_replace_node_common skips updating from pare= nts * if it creates a parent-child relation loop or if parent is block-job. * @@ -5421,10 +5424,9 @@ bdrv_replace_node_noperm(BlockDriverState *from, * With @detach_subchain=3Dtrue @to must be in a backing chain of @from. I= n this * case backing link of the cow-parent of @to is removed. */ -static int bdrv_replace_node_common(BlockDriverState *from, - BlockDriverState *to, - bool auto_skip, bool detach_subchain, - Error **errp) +static int GRAPH_WRLOCK +bdrv_replace_node_common(BlockDriverState *from, BlockDriverState *to, + bool auto_skip, bool detach_subchain, Error **err= p) { Transaction *tran =3D tran_new(); g_autoptr(GSList) refresh_list =3D NULL; @@ -5433,16 +5435,9 @@ static int bdrv_replace_node_common(BlockDriverState= *from, =20 GLOBAL_STATE_CODE(); =20 - /* Make sure that @from doesn't go away until we have successfully att= ached - * all of its parents to @to. */ - bdrv_ref(from); - - assert(qemu_get_current_aio_context() =3D=3D qemu_get_aio_context()); + assert(from->quiesce_counter); + assert(to->quiesce_counter); assert(bdrv_get_aio_context(from) =3D=3D bdrv_get_aio_context(to)); - bdrv_drained_begin(from); - bdrv_drained_begin(to); - - bdrv_graph_wrlock(to); =20 if (detach_subchain) { assert(bdrv_chain_contains(from, to)); @@ -5483,33 +5478,51 @@ static int bdrv_replace_node_common(BlockDriverStat= e *from, =20 out: tran_finalize(tran, ret); - bdrv_graph_wrunlock(); - - bdrv_drained_end(to); - bdrv_drained_end(from); - bdrv_unref(from); - return ret; } =20 int bdrv_replace_node(BlockDriverState *from, BlockDriverState *to, Error **errp) { + int ret; + GLOBAL_STATE_CODE(); =20 - return bdrv_replace_node_common(from, to, true, false, errp); + /* Make sure that @from doesn't go away until we have successfully att= ached + * all of its parents to @to. */ + bdrv_ref(from); + bdrv_drained_begin(from); + bdrv_drained_begin(to); + bdrv_graph_wrlock(to); + + ret =3D bdrv_replace_node_common(from, to, true, false, errp); + + bdrv_graph_wrunlock(); + bdrv_drained_end(to); + bdrv_drained_end(from); + bdrv_unref(from); + + return ret; } =20 int bdrv_drop_filter(BlockDriverState *bs, Error **errp) { BlockDriverState *child_bs; + int ret; =20 GLOBAL_STATE_CODE(); + bdrv_graph_rdlock_main_loop(); child_bs =3D bdrv_filter_or_cow_bs(bs); bdrv_graph_rdunlock_main_loop(); =20 - return bdrv_replace_node_common(bs, child_bs, true, true, errp); + bdrv_drained_begin(child_bs); + bdrv_graph_wrlock(bs); + ret =3D bdrv_replace_node_common(bs, child_bs, true, true, errp); + bdrv_graph_wrunlock(); + bdrv_drained_end(child_bs); + + return ret; } =20 /* @@ -5957,15 +5970,15 @@ int bdrv_drop_intermediate(BlockDriverState *top, B= lockDriverState *base, =20 bdrv_ref(top); bdrv_drained_begin(base); - bdrv_graph_rdlock_main_loop(); + bdrv_graph_wrlock(base); =20 if (!top->drv || !base->drv) { - goto exit; + goto exit_wrlock; } =20 /* Make sure that base is in the backing chain of top */ if (!bdrv_chain_contains(top, base)) { - goto exit; + goto exit_wrlock; } =20 /* If 'base' recursively inherits from 'top' then we should set @@ -5997,6 +6010,8 @@ int bdrv_drop_intermediate(BlockDriverState *top, Blo= ckDriverState *base, * That's a FIXME. */ bdrv_replace_node_common(top, base, false, false, &local_err); + bdrv_graph_wrunlock(); + if (local_err) { error_report_err(local_err); goto exit; @@ -6029,8 +6044,11 @@ int bdrv_drop_intermediate(BlockDriverState *top, Bl= ockDriverState *base, } =20 ret =3D 0; + goto exit; + +exit_wrlock: + bdrv_graph_wrunlock(); exit: - bdrv_graph_rdunlock_main_loop(); bdrv_drained_end(base); bdrv_unref(top); return ret; --=20 2.41.0 From nobody Wed Nov 27 09:52:08 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=1699382943; cv=none; d=zohomail.com; s=zohoarc; b=QNcdRt+K2nJFZ7XftAeZzzc1gGs1Suv/ZXFhDu+wmOoq3cciWUB+e69TUFTbpyVNjt6FRKtfkhn9qqEqPv7ulIGe+7EnwpSLtw9vdbnQmutqkikqurrWby13fW31B18QbJqQbxNkZnEwPj1R73F+jEd9btIhFXqcb2wpgHnSbIc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699382943; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=w0Pw14MMEFDUie5Z7nWMO05syP8WwrvNVlewbsAACAU=; b=DipXZIsifcG6F4OnHZSu0lcNZBAhRclQavR50PyqEqG6VNHXdwmwCESFyrVlqeVHW+b5kqffcBdbuwfp1nkw7I5gkN/zIbiuXCSJ+Ma6D/PrwV1pjPw46a+wmjBaHn/haH41ggSUXXQtgQnwEGBDvSbM5pC+ccrxz0xWic7gYv4= 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 1699382943491651.1995119618113; Tue, 7 Nov 2023 10:49:03 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0R6E-0004Vb-My; Tue, 07 Nov 2023 13:46:54 -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 1r0R5n-00041W-Cq for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:28 -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 1r0R5j-0001Vy-TL for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:27 -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.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-63-Xfj5dljJPVGg9KoO0Z4whA-1; Tue, 07 Nov 2023 13:46:20 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F2B36101AA75 for ; Tue, 7 Nov 2023 18:46:19 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3E811C1596F; Tue, 7 Nov 2023 18:46:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699382781; 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=w0Pw14MMEFDUie5Z7nWMO05syP8WwrvNVlewbsAACAU=; b=XdAVZLsA8i8COSV93H9gxKucgqQc7jckitF5muSbmHYF+mREk5kwhQlaH0xapMcLPO/eMt 3vcR0Qz/7OZeDyDTT0/0CuEUiU2eUa5/QqgazfcXgLOm6HdrCM/nt4E8ikXc5KmBADBid4 G7GnQAag6KDh9T1mzro9hbYzLyfglRA= X-MC-Unique: Xfj5dljJPVGg9KoO0Z4whA-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 16/25] block: Mark bdrv_replace_node() GRAPH_WRLOCK Date: Tue, 7 Nov 2023 19:45:56 +0100 Message-ID: <20231107184605.236540-17-kwolf@redhat.com> In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com> References: <20231107184605.236540-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1699382945518100015 Content-Type: text/plain; charset="utf-8" Instead of taking the writer lock internally, require callers to already hold it when calling bdrv_replace_node(). Its callers may already want to hold the graph lock and so wouldn't be able to call functions that take it internally. Signed-off-by: Kevin Wolf Message-ID: <20231027155333.420094-17-kwolf@redhat.com> Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- include/block/block-global-state.h | 6 ++++-- block.c | 30 +++++++++++------------------- block/commit.c | 13 +++++++++++-- block/mirror.c | 26 ++++++++++++++++---------- blockdev.c | 5 +++++ tests/unit/test-bdrv-drain.c | 6 ++++++ tests/unit/test-bdrv-graph-mod.c | 13 +++++++++++-- 7 files changed, 64 insertions(+), 35 deletions(-) diff --git a/include/block/block-global-state.h b/include/block/block-globa= l-state.h index a1fd70ec97..9e0ccc1c32 100644 --- a/include/block/block-global-state.h +++ b/include/block/block-global-state.h @@ -71,8 +71,10 @@ bdrv_co_create_file(const char *filename, QemuOpts *opts= , Error **errp); BlockDriverState *bdrv_new(void); int bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top, Error **errp); -int bdrv_replace_node(BlockDriverState *from, BlockDriverState *to, - Error **errp); + +int GRAPH_WRLOCK +bdrv_replace_node(BlockDriverState *from, BlockDriverState *to, Error **er= rp); + int bdrv_replace_child_bs(BdrvChild *child, BlockDriverState *new_bs, Error **errp); BlockDriverState *bdrv_insert_node(BlockDriverState *bs, QDict *node_optio= ns, diff --git a/block.c b/block.c index c7409cf658..cac517ab8b 100644 --- a/block.c +++ b/block.c @@ -5484,25 +5484,7 @@ out: int bdrv_replace_node(BlockDriverState *from, BlockDriverState *to, Error **errp) { - int ret; - - GLOBAL_STATE_CODE(); - - /* Make sure that @from doesn't go away until we have successfully att= ached - * all of its parents to @to. */ - bdrv_ref(from); - bdrv_drained_begin(from); - bdrv_drained_begin(to); - bdrv_graph_wrlock(to); - - ret =3D bdrv_replace_node_common(from, to, true, false, errp); - - bdrv_graph_wrunlock(); - bdrv_drained_end(to); - bdrv_drained_end(from); - bdrv_unref(from); - - return ret; + return bdrv_replace_node_common(from, to, true, false, errp); } =20 int bdrv_drop_filter(BlockDriverState *bs, Error **errp) @@ -5717,9 +5699,19 @@ BlockDriverState *bdrv_insert_node(BlockDriverState = *bs, QDict *options, goto fail; } =20 + /* + * Make sure that @bs doesn't go away until we have successfully attac= hed + * all of its parents to @new_node_bs and undrained it again. + */ + bdrv_ref(bs); bdrv_drained_begin(bs); + bdrv_drained_begin(new_node_bs); + bdrv_graph_wrlock(new_node_bs); ret =3D bdrv_replace_node(bs, new_node_bs, errp); + bdrv_graph_wrunlock(); + bdrv_drained_end(new_node_bs); bdrv_drained_end(bs); + bdrv_unref(bs); =20 if (ret < 0) { error_prepend(errp, "Could not replace node: "); diff --git a/block/commit.c b/block/commit.c index d92af02ead..30bc082edc 100644 --- a/block/commit.c +++ b/block/commit.c @@ -68,6 +68,7 @@ static void commit_abort(Job *job) { CommitBlockJob *s =3D container_of(job, CommitBlockJob, common.job); BlockDriverState *top_bs =3D blk_bs(s->top); + BlockDriverState *commit_top_backing_bs; =20 if (s->chain_frozen) { bdrv_graph_rdlock_main_loop(); @@ -94,8 +95,12 @@ static void commit_abort(Job *job) * XXX Can (or should) we somehow keep 'consistent read' blocked even * after the failed/cancelled commit job is gone? If we already wrote * something to base, the intermediate images aren't valid any more. */ - bdrv_replace_node(s->commit_top_bs, s->commit_top_bs->backing->bs, - &error_abort); + commit_top_backing_bs =3D s->commit_top_bs->backing->bs; + bdrv_drained_begin(commit_top_backing_bs); + bdrv_graph_wrlock(commit_top_backing_bs); + bdrv_replace_node(s->commit_top_bs, commit_top_backing_bs, &error_abor= t); + bdrv_graph_wrunlock(); + bdrv_drained_end(commit_top_backing_bs); =20 bdrv_unref(s->commit_top_bs); bdrv_unref(top_bs); @@ -425,7 +430,11 @@ fail: /* commit_top_bs has to be replaced after deleting the block job, * otherwise this would fail because of lack of permissions. */ if (commit_top_bs) { + bdrv_drained_begin(top); + bdrv_graph_wrlock(top); bdrv_replace_node(commit_top_bs, top, &error_abort); + bdrv_graph_wrunlock(); + bdrv_drained_end(top); } } =20 diff --git a/block/mirror.c b/block/mirror.c index f8e439371c..dfc1c416e8 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -712,6 +712,7 @@ static int mirror_exit_common(Job *job) * these permissions any more means that we can't allow any new reques= ts on * mirror_top_bs from now on, so keep it drained. */ bdrv_drained_begin(mirror_top_bs); + bdrv_drained_begin(target_bs); bs_opaque->stop =3D true; =20 bdrv_graph_rdlock_main_loop(); @@ -757,15 +758,13 @@ static int mirror_exit_common(Job *job) /* The mirror job has no requests in flight any more, but we need = to * drain potential other users of the BDS before changing the grap= h. */ assert(s->in_drain); - bdrv_drained_begin(target_bs); + bdrv_drained_begin(to_replace); /* * Cannot use check_to_replace_node() here, because that would * check for an op blocker on @to_replace, and we have our own * there. - * - * TODO Pull out the writer lock from bdrv_replace_node() to here */ - bdrv_graph_rdlock_main_loop(); + bdrv_graph_wrlock(target_bs); if (bdrv_recurse_can_replace(src, to_replace)) { bdrv_replace_node(to_replace, target_bs, &local_err); } else { @@ -774,8 +773,8 @@ static int mirror_exit_common(Job *job) "would not lead to an abrupt change of visible data= ", to_replace->node_name, target_bs->node_name); } - bdrv_graph_rdunlock_main_loop(); - bdrv_drained_end(target_bs); + bdrv_graph_wrunlock(); + bdrv_drained_end(to_replace); if (local_err) { error_report_err(local_err); ret =3D -EPERM; @@ -790,7 +789,6 @@ static int mirror_exit_common(Job *job) aio_context_release(replace_aio_context); } g_free(s->replaces); - bdrv_unref(target_bs); =20 /* * Remove the mirror filter driver from the graph. Before this, get ri= d of @@ -798,7 +796,12 @@ static int mirror_exit_common(Job *job) * valid. */ block_job_remove_all_bdrv(bjob); + bdrv_graph_wrlock(mirror_top_bs); bdrv_replace_node(mirror_top_bs, mirror_top_bs->backing->bs, &error_ab= ort); + bdrv_graph_wrunlock(); + + bdrv_drained_end(target_bs); + bdrv_unref(target_bs); =20 bs_opaque->job =3D NULL; =20 @@ -1987,11 +1990,14 @@ fail: } =20 bs_opaque->stop =3D true; - bdrv_graph_rdlock_main_loop(); + bdrv_drained_begin(bs); + bdrv_graph_wrlock(bs); + assert(mirror_top_bs->backing->bs =3D=3D bs); bdrv_child_refresh_perms(mirror_top_bs, mirror_top_bs->backing, &error_abort); - bdrv_graph_rdunlock_main_loop(); - bdrv_replace_node(mirror_top_bs, mirror_top_bs->backing->bs, &error_ab= ort); + bdrv_replace_node(mirror_top_bs, bs, &error_abort); + bdrv_graph_wrunlock(); + bdrv_drained_end(bs); =20 bdrv_unref(mirror_top_bs); =20 diff --git a/blockdev.c b/blockdev.c index f04faf6373..5bc921236c 100644 --- a/blockdev.c +++ b/blockdev.c @@ -1610,7 +1610,12 @@ static void external_snapshot_abort(void *opaque) aio_context_acquire(aio_context); } =20 + bdrv_drained_begin(state->new_bs); + bdrv_graph_wrlock(state->old_bs); bdrv_replace_node(state->new_bs, state->old_bs, &error_abort); + bdrv_graph_wrunlock(); + bdrv_drained_end(state->new_bs); + bdrv_unref(state->old_bs); /* bdrv_replace_node() ref'ed old_b= s */ =20 aio_context_release(aio_context); diff --git a/tests/unit/test-bdrv-drain.c b/tests/unit/test-bdrv-drain.c index 40d17b4c5a..b16f831c23 100644 --- a/tests/unit/test-bdrv-drain.c +++ b/tests/unit/test-bdrv-drain.c @@ -2000,7 +2000,13 @@ static void do_test_replace_child_mid_drain(int old_= drain_count, parent_s->was_undrained =3D false; =20 g_assert(parent_bs->quiesce_counter =3D=3D old_drain_count); + bdrv_drained_begin(old_child_bs); + bdrv_drained_begin(new_child_bs); + bdrv_graph_wrlock(NULL); bdrv_replace_node(old_child_bs, new_child_bs, &error_abort); + bdrv_graph_wrunlock(); + bdrv_drained_end(new_child_bs); + bdrv_drained_end(old_child_bs); g_assert(parent_bs->quiesce_counter =3D=3D new_drain_count); =20 if (!old_drain_count && !new_drain_count) { diff --git a/tests/unit/test-bdrv-graph-mod.c b/tests/unit/test-bdrv-graph-= mod.c index 8609f7f42b..22d4cd83f6 100644 --- a/tests/unit/test-bdrv-graph-mod.c +++ b/tests/unit/test-bdrv-graph-mod.c @@ -234,11 +234,16 @@ static void test_parallel_exclusive_write(void) BlockDriverState *fl1 =3D pass_through_node("fl1"); BlockDriverState *fl2 =3D pass_through_node("fl2"); =20 + bdrv_drained_begin(fl1); + bdrv_drained_begin(fl2); + /* * bdrv_attach_child() eats child bs reference, so we need two @base - * references for two filters: + * references for two filters. We also need an additional @fl1 referen= ce so + * that it still exists when we want to undrain it. */ bdrv_ref(base); + bdrv_ref(fl1); =20 bdrv_graph_wrlock(NULL); bdrv_attach_child(top, fl1, "backing", &child_of_bds, @@ -250,10 +255,14 @@ static void test_parallel_exclusive_write(void) bdrv_attach_child(fl2, base, "backing", &child_of_bds, BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY, &error_abort); - bdrv_graph_wrunlock(); =20 bdrv_replace_node(fl1, fl2, &error_abort); + bdrv_graph_wrunlock(); + + bdrv_drained_end(fl2); + bdrv_drained_end(fl1); =20 + bdrv_unref(fl1); bdrv_unref(fl2); bdrv_unref(top); } --=20 2.41.0 From nobody Wed Nov 27 09:52:08 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=1699383054; cv=none; d=zohomail.com; s=zohoarc; b=l2Erkgp+YwHQ4LJWmUNhPvMmOavDIKUra8n+rKFVK7raj7LMmlOJ1KX7QPyy4O2LP67xYaKbLYQiClVl2nGnwiGGlwcA+3usWlVGNxPMVQI5/ZXeqI2u6BnXzyHFlpQlc5GPTKlqW/vBqmle/m2QB2yUucpjG+okSh8taMUaEn0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699383054; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=qJh17qxd6wqEUju66ePHaqYMHY8xUudxbx/imAnQR6I=; b=VjbPhTgDQBnxVuJjNjh8rUfgg/h1xZDuQleA5K7hnCUjDfi/Fe/mR6KnKosePxxUCVixsbWZfBPZ7tAy9dPMJAi4XLg9hnP5dNnosvWDFIFTAuUB4EPlukl2AL+bM8w9MwROZ/8TMKkRo0YTLFjLUqljyvoNXquI9B3e5CYb8VA= 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 1699383054013944.0832880116596; Tue, 7 Nov 2023 10:50:54 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0R6N-0004cJ-4o; Tue, 07 Nov 2023 13:47:03 -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 1r0R5p-00041a-A4 for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:31 -0500 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 1r0R5j-0001W3-5r for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:28 -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.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-302-pnEtLUI4NSWBRCq9LIoU4A-1; Tue, 07 Nov 2023 13:46:20 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id AD292101A529 for ; Tue, 7 Nov 2023 18:46:20 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id 37702C1596F; Tue, 7 Nov 2023 18:46:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699382782; 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=qJh17qxd6wqEUju66ePHaqYMHY8xUudxbx/imAnQR6I=; b=gvr+jfhgXs6xFC/bzSgAPFVM8Ln/HUcIRRzakhmfW+Vj70XD8LD0dcEm+PbyCY9knfFkbr CEhe/4kWiQZ6GU+mV1pSiwETlbePTOaYJNrrL/N5MqavF1CEEnSbkgmR+X1/3Ckq8+5nOH KUkJDouZ1TTJn8p2Gj/YUND1c/fzN8c= X-MC-Unique: pnEtLUI4NSWBRCq9LIoU4A-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 17/25] block: Protect bs->backing with graph_lock Date: Tue, 7 Nov 2023 19:45:57 +0100 Message-ID: <20231107184605.236540-18-kwolf@redhat.com> In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com> References: <20231107184605.236540-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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, 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_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01, T_SPF_HELO_TEMPERROR=0.01 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: 1699383055954100003 Content-Type: text/plain; charset="utf-8" Almost all functions that access bs->backing already take the graph lock now. Add locking to the remaining users and finally annotate the struct field itself as protected by the graph lock. Signed-off-by: Kevin Wolf Message-ID: <20231027155333.420094-18-kwolf@redhat.com> Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- include/block/block_int-common.h | 2 +- block.c | 27 +++++++++++++++++---------- block/commit.c | 5 ++++- block/mirror.c | 17 ++++++++++++----- block/qed.c | 2 +- block/replication.c | 7 ++++++- block/vmdk.c | 7 ++++--- tests/unit/test-bdrv-drain.c | 22 ++++++++++++++++++---- tests/unit/test-bdrv-graph-mod.c | 5 ++++- 9 files changed, 67 insertions(+), 27 deletions(-) diff --git a/include/block/block_int-common.h b/include/block/block_int-com= mon.h index c0db862de7..ed6066929a 100644 --- a/include/block/block_int-common.h +++ b/include/block/block_int-common.h @@ -1178,7 +1178,7 @@ struct BlockDriverState { * are connected with BdrvChildRole. */ QLIST_HEAD(, BdrvChild GRAPH_RDLOCK_PTR) children; - BdrvChild *backing; + BdrvChild * GRAPH_RDLOCK_PTR backing; BdrvChild *file; =20 QLIST_HEAD(, BdrvChild GRAPH_RDLOCK_PTR) parents; diff --git a/block.c b/block.c index cac517ab8b..ed0afdffbd 100644 --- a/block.c +++ b/block.c @@ -3560,10 +3560,14 @@ out: int bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd, Error **errp) { - BlockDriverState *drain_bs =3D bs->backing ? bs->backing->bs : bs; + BlockDriverState *drain_bs; int ret; GLOBAL_STATE_CODE(); =20 + bdrv_graph_rdlock_main_loop(); + drain_bs =3D bs->backing ? bs->backing->bs : bs; + bdrv_graph_rdunlock_main_loop(); + bdrv_ref(drain_bs); bdrv_drained_begin(drain_bs); bdrv_graph_wrlock(backing_hd); @@ -3602,6 +3606,7 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDic= t *parent_options, Error *local_err =3D NULL; =20 GLOBAL_STATE_CODE(); + GRAPH_RDLOCK_GUARD_MAINLOOP(); =20 if (bs->backing !=3D NULL) { goto free_exit; @@ -3643,10 +3648,7 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDi= ct *parent_options, implicit_backing =3D !strcmp(bs->auto_backing_file, bs->backin= g_file); } =20 - bdrv_graph_rdlock_main_loop(); backing_filename =3D bdrv_get_full_backing_filename(bs, &local_err= ); - bdrv_graph_rdunlock_main_loop(); - if (local_err) { ret =3D -EINVAL; error_propagate(errp, local_err); @@ -3677,9 +3679,7 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDic= t *parent_options, } =20 if (implicit_backing) { - bdrv_graph_rdlock_main_loop(); bdrv_refresh_filename(backing_hd); - bdrv_graph_rdunlock_main_loop(); pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file), backing_hd->filename); } @@ -4750,8 +4750,8 @@ bdrv_reopen_parse_file_or_backing(BDRVReopenState *re= open_state, { BlockDriverState *bs =3D reopen_state->bs; BlockDriverState *new_child_bs; - BlockDriverState *old_child_bs =3D is_backing ? child_bs(bs->backing) : - child_bs(bs->file); + BlockDriverState *old_child_bs; + const char *child_name =3D is_backing ? "backing" : "file"; QObject *value; const char *str; @@ -4797,6 +4797,7 @@ bdrv_reopen_parse_file_or_backing(BDRVReopenState *re= open_state, g_assert_not_reached(); } =20 + old_child_bs =3D is_backing ? child_bs(bs->backing) : child_bs(bs->fil= e); if (old_child_bs =3D=3D new_child_bs) { ret =3D 0; goto out_rdlock; @@ -5008,13 +5009,16 @@ bdrv_reopen_prepare(BDRVReopenState *reopen_state, = BlockReopenQueue *queue, * file or if the image file has a backing file name as part of * its metadata. Otherwise the 'backing' option can be omitted. */ + bdrv_graph_rdlock_main_loop(); if (drv->supports_backing && reopen_state->backing_missing && (reopen_state->bs->backing || reopen_state->bs->backing_file[0])) { error_setg(errp, "backing is missing for '%s'", reopen_state->bs->node_name); + bdrv_graph_rdunlock_main_loop(); ret =3D -EINVAL; goto error; } + bdrv_graph_rdunlock_main_loop(); =20 /* * Allow changing the 'backing' option. The new value can be @@ -5204,10 +5208,11 @@ static void bdrv_close(BlockDriverState *bs) QLIST_FOREACH_SAFE(child, &bs->children, next, next) { bdrv_unref_child(bs, child); } - bdrv_graph_wrunlock(); =20 assert(!bs->backing); assert(!bs->file); + bdrv_graph_wrunlock(); + g_free(bs->opaque); bs->opaque =3D NULL; qatomic_set(&bs->copy_on_read, 0); @@ -5531,7 +5536,9 @@ int bdrv_append(BlockDriverState *bs_new, BlockDriver= State *bs_top, =20 GLOBAL_STATE_CODE(); =20 + bdrv_graph_rdlock_main_loop(); assert(!bs_new->backing); + bdrv_graph_rdunlock_main_loop(); =20 old_context =3D bdrv_get_aio_context(bs_top); bdrv_drained_begin(bs_top); @@ -8115,7 +8122,7 @@ static bool append_strong_runtime_options(QDict *d, B= lockDriverState *bs) /* Note: This function may return false positives; it may return true * even if opening the backing file specified by bs's image header * would result in exactly bs->backing. */ -static bool bdrv_backing_overridden(BlockDriverState *bs) +static bool GRAPH_RDLOCK bdrv_backing_overridden(BlockDriverState *bs) { GLOBAL_STATE_CODE(); if (bs->backing) { diff --git a/block/commit.c b/block/commit.c index 30bc082edc..eb3dc01f45 100644 --- a/block/commit.c +++ b/block/commit.c @@ -95,7 +95,10 @@ static void commit_abort(Job *job) * XXX Can (or should) we somehow keep 'consistent read' blocked even * after the failed/cancelled commit job is gone? If we already wrote * something to base, the intermediate images aren't valid any more. */ + bdrv_graph_rdlock_main_loop(); commit_top_backing_bs =3D s->commit_top_bs->backing->bs; + bdrv_graph_rdunlock_main_loop(); + bdrv_drained_begin(commit_top_backing_bs); bdrv_graph_wrlock(commit_top_backing_bs); bdrv_replace_node(s->commit_top_bs, commit_top_backing_bs, &error_abor= t); @@ -219,7 +222,7 @@ bdrv_commit_top_preadv(BlockDriverState *bs, int64_t of= fset, int64_t bytes, return bdrv_co_preadv(bs->backing, offset, bytes, qiov, flags); } =20 -static void bdrv_commit_top_refresh_filename(BlockDriverState *bs) +static GRAPH_RDLOCK void bdrv_commit_top_refresh_filename(BlockDriverState= *bs) { pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), bs->backing->bs->filename); diff --git a/block/mirror.c b/block/mirror.c index dfc1c416e8..aa5302aaae 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -479,7 +479,7 @@ static unsigned mirror_perform(MirrorBlockJob *s, int64= _t offset, return bytes_handled; } =20 -static void coroutine_fn mirror_iteration(MirrorBlockJob *s) +static void coroutine_fn GRAPH_RDLOCK mirror_iteration(MirrorBlockJob *s) { BlockDriverState *source =3D s->mirror_top_bs->backing->bs; MirrorOp *pseudo_op; @@ -839,14 +839,18 @@ static void coroutine_fn mirror_throttle(MirrorBlockJ= ob *s) } } =20 -static int coroutine_fn mirror_dirty_init(MirrorBlockJob *s) +static int coroutine_fn GRAPH_UNLOCKED mirror_dirty_init(MirrorBlockJob *s) { int64_t offset; - BlockDriverState *bs =3D s->mirror_top_bs->backing->bs; + BlockDriverState *bs; BlockDriverState *target_bs =3D blk_bs(s->target); int ret; int64_t count; =20 + bdrv_graph_co_rdlock(); + bs =3D s->mirror_top_bs->backing->bs; + bdrv_graph_co_rdunlock(); + if (s->zero_target) { if (!bdrv_can_write_zeroes_with_unmap(target_bs)) { bdrv_set_dirty_bitmap(s->dirty_bitmap, 0, s->bdev_length); @@ -926,7 +930,7 @@ static int coroutine_fn mirror_flush(MirrorBlockJob *s) static int coroutine_fn mirror_run(Job *job, Error **errp) { MirrorBlockJob *s =3D container_of(job, MirrorBlockJob, common.job); - BlockDriverState *bs =3D s->mirror_top_bs->backing->bs; + BlockDriverState *bs; MirrorBDSOpaque *mirror_top_opaque =3D s->mirror_top_bs->opaque; BlockDriverState *target_bs =3D blk_bs(s->target); bool need_drain =3D true; @@ -943,6 +947,7 @@ static int coroutine_fn mirror_run(Job *job, Error **er= rp) } =20 bdrv_graph_co_rdlock(); + bs =3D bdrv_filter_bs(s->mirror_top_bs); s->bdev_length =3D bdrv_co_getlength(bs); bdrv_graph_co_rdunlock(); =20 @@ -1070,7 +1075,9 @@ static int coroutine_fn mirror_run(Job *job, Error **= errp) mirror_wait_for_free_in_flight_slot(s); continue; } else if (cnt !=3D 0) { + bdrv_graph_co_rdlock(); mirror_iteration(s); + bdrv_graph_co_rdunlock(); } } =20 @@ -1640,7 +1647,7 @@ bdrv_mirror_top_pdiscard(BlockDriverState *bs, int64_= t offset, int64_t bytes) offset, bytes, NULL, 0); } =20 -static void bdrv_mirror_top_refresh_filename(BlockDriverState *bs) +static void GRAPH_RDLOCK bdrv_mirror_top_refresh_filename(BlockDriverState= *bs) { if (bs->backing =3D=3D NULL) { /* we can be here after failed bdrv_attach_child in diff --git a/block/qed.c b/block/qed.c index 45ae320290..686ad711f7 100644 --- a/block/qed.c +++ b/block/qed.c @@ -1138,7 +1138,7 @@ out: /** * Check if the QED_F_NEED_CHECK bit should be set during allocating write */ -static bool qed_should_set_need_check(BDRVQEDState *s) +static bool GRAPH_RDLOCK qed_should_set_need_check(BDRVQEDState *s) { /* The flush before L2 update path ensures consistency */ if (s->bs->backing) { diff --git a/block/replication.c b/block/replication.c index d522c7396f..49ecc608b2 100644 --- a/block/replication.c +++ b/block/replication.c @@ -363,6 +363,9 @@ static void reopen_backing_file(BlockDriverState *bs, b= ool writable, BdrvChild *hidden_disk, *secondary_disk; BlockReopenQueue *reopen_queue =3D NULL; =20 + GLOBAL_STATE_CODE(); + GRAPH_RDLOCK_GUARD_MAINLOOP(); + /* * s->hidden_disk and s->secondary_disk may not be set yet, as they wi= ll * only be set after the children are writable. @@ -496,9 +499,11 @@ static void replication_start(ReplicationState *rs, Re= plicationMode mode, case REPLICATION_MODE_PRIMARY: break; case REPLICATION_MODE_SECONDARY: + bdrv_graph_rdlock_main_loop(); active_disk =3D bs->file; if (!active_disk || !active_disk->bs || !active_disk->bs->backing)= { error_setg(errp, "Active disk doesn't have backing file"); + bdrv_graph_rdunlock_main_loop(); aio_context_release(aio_context); return; } @@ -506,11 +511,11 @@ static void replication_start(ReplicationState *rs, R= eplicationMode mode, hidden_disk =3D active_disk->bs->backing; if (!hidden_disk->bs || !hidden_disk->bs->backing) { error_setg(errp, "Hidden disk doesn't have backing file"); + bdrv_graph_rdunlock_main_loop(); aio_context_release(aio_context); return; } =20 - bdrv_graph_rdlock_main_loop(); secondary_disk =3D hidden_disk->bs->backing; if (!secondary_disk->bs || !bdrv_has_blk(secondary_disk->bs)) { error_setg(errp, "The secondary disk doesn't have block backen= d"); diff --git a/block/vmdk.c b/block/vmdk.c index 91ed7a8d93..5c789bb65b 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -380,7 +380,7 @@ out: return ret; } =20 -static int coroutine_fn vmdk_is_cid_valid(BlockDriverState *bs) +static int coroutine_fn GRAPH_RDLOCK vmdk_is_cid_valid(BlockDriverState *b= s) { BDRVVmdkState *s =3D bs->opaque; uint32_t cur_pcid; @@ -3044,8 +3044,9 @@ vmdk_co_get_info(BlockDriverState *bs, BlockDriverInf= o *bdi) return 0; } =20 -static void vmdk_gather_child_options(BlockDriverState *bs, QDict *target, - bool backing_overridden) +static void GRAPH_RDLOCK +vmdk_gather_child_options(BlockDriverState *bs, QDict *target, + bool backing_overridden) { /* No children but file and backing can be explicitly specified (TODO)= */ qdict_put(target, "file", diff --git a/tests/unit/test-bdrv-drain.c b/tests/unit/test-bdrv-drain.c index b16f831c23..ba4e42b197 100644 --- a/tests/unit/test-bdrv-drain.c +++ b/tests/unit/test-bdrv-drain.c @@ -218,8 +218,14 @@ static void do_drain_end_unlocked(enum drain_type drai= n_type, BlockDriverState * } } =20 -static void test_drv_cb_common(BlockBackend *blk, enum drain_type drain_ty= pe, - bool recursive) +/* + * Locking the block graph would be a bit cumbersome here because this fun= ction + * is called both in coroutine and non-coroutine context. We know this is = a test + * and nothing else is running, so don't bother with TSA. + */ +static void coroutine_mixed_fn TSA_NO_TSA +test_drv_cb_common(BlockBackend *blk, enum drain_type drain_type, + bool recursive) { BlockDriverState *bs =3D blk_bs(blk); BlockDriverState *backing =3D bs->backing->bs; @@ -307,8 +313,14 @@ static void test_drv_cb_co_drain(void) blk_unref(blk); } =20 -static void test_quiesce_common(BlockBackend *blk, enum drain_type drain_t= ype, - bool recursive) +/* + * Locking the block graph would be a bit cumbersome here because this fun= ction + * is called both in coroutine and non-coroutine context. We know this is = a test + * and nothing else is running, so don't bother with TSA. + */ +static void coroutine_mixed_fn TSA_NO_TSA +test_quiesce_common(BlockBackend *blk, enum drain_type drain_type, + bool recursive) { BlockDriverState *bs =3D blk_bs(blk); BlockDriverState *backing =3D bs->backing->bs; @@ -1868,6 +1880,8 @@ static void bdrv_replace_test_drain_end(BlockDriverSt= ate *bs) { BDRVReplaceTestState *s =3D bs->opaque; =20 + GRAPH_RDLOCK_GUARD_MAINLOOP(); + if (!s->setup_completed) { return; } diff --git a/tests/unit/test-bdrv-graph-mod.c b/tests/unit/test-bdrv-graph-= mod.c index 22d4cd83f6..878544dbd5 100644 --- a/tests/unit/test-bdrv-graph-mod.c +++ b/tests/unit/test-bdrv-graph-mod.c @@ -206,15 +206,18 @@ static void test_should_update_child(void) =20 bdrv_set_backing_hd(target, bs, &error_abort); =20 - g_assert(target->backing->bs =3D=3D bs); bdrv_graph_wrlock(NULL); + g_assert(target->backing->bs =3D=3D bs); bdrv_attach_child(filter, target, "target", &child_of_bds, BDRV_CHILD_DATA, &error_abort); bdrv_graph_wrunlock(); aio_context_acquire(qemu_get_aio_context()); bdrv_append(filter, bs, &error_abort); aio_context_release(qemu_get_aio_context()); + + bdrv_graph_rdlock_main_loop(); g_assert(target->backing->bs =3D=3D bs); + bdrv_graph_rdunlock_main_loop(); =20 bdrv_unref(filter); bdrv_unref(bs); --=20 2.41.0 From nobody Wed Nov 27 09:52:08 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=1699382921; cv=none; d=zohomail.com; s=zohoarc; b=kRm6tguIRNT7jA2vO/L3SA0X0KyD6aFUGiA40a2pxGTZHcY7AtVaitJibOgtCOphDk3e6a4JmuP1fN7MdEJKEaVTcVloqYDvNa1owwa6hVtVihAPKjNruP07p9ZgxjxNoODxRI/z8py+a+UcBvRa4eiRDQeknR7hKtvCYIu+lNM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699382921; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=s0j71Fw8yaxWSmaLTaMFdVftfT/1eKnJ1ubozTtAKmE=; b=dL1/3x5yCy7RImVdJgdY1mXsKx3IUHWk/swBXsLXwp7b4Oyxynva3sv+u3CU2uiXOEqc2F+7D7dDzpO4ZQCsQQ7XVd2GXsvBbf4pK5XsiAwn1vaWBemIXvVySv+r71vwg8lzMi/iaMDXttlvXCWu+Otjta/9glJxZqgJNetZhfE= 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 16993829216061006.1664825530983; Tue, 7 Nov 2023 10:48:41 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0R6J-0004c8-B5; Tue, 07 Nov 2023 13:46:59 -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 1r0R5n-00041V-6h for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:28 -0500 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 1r0R5k-0001WG-1t for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:26 -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.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-374-Ru98Rq2SN7OU4q9W6FGaJw-1; Tue, 07 Nov 2023 13:46:21 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 68843185A784 for ; Tue, 7 Nov 2023 18:46:21 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id E5EF1C1596F; Tue, 7 Nov 2023 18:46:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699382783; 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=s0j71Fw8yaxWSmaLTaMFdVftfT/1eKnJ1ubozTtAKmE=; b=Rxt0xe+ClmIeTzPA/Ww3hGCW7rq0I+LfKE5G8B3JSzhp4KsrKJZygn5sC5wjXTFa4r5rHp gRWv4NH3OxqIdYmUztZzJbeFB1ZreR3/dAWoyvBbWSDxm/y23hZf5rE0BbjrI7OxkRa249 DfL/J1Ls2xGCD4in08tZPsZD16RlSI4= X-MC-Unique: Ru98Rq2SN7OU4q9W6FGaJw-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 18/25] blkverify: Add locking for request_fn Date: Tue, 7 Nov 2023 19:45:58 +0100 Message-ID: <20231107184605.236540-19-kwolf@redhat.com> In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com> References: <20231107184605.236540-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1699382923353100007 Content-Type: text/plain; charset="utf-8" This is either bdrv_co_preadv() or bdrv_co_pwritev() which both need to have the graph locked. Annotate the function pointer accordingly and add locking to its callers. This shouldn't actually have resulted in a bug because the graph lock is already held by blkverify_co_prwv(), which waits for the coroutines to terminate. Annotate with GRAPH_RDLOCK as well to make this clearer. Signed-off-by: Kevin Wolf Message-ID: <20231027155333.420094-19-kwolf@redhat.com> Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- block/blkverify.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/block/blkverify.c b/block/blkverify.c index dae9716a26..9ea2cb4cc5 100644 --- a/block/blkverify.c +++ b/block/blkverify.c @@ -33,8 +33,8 @@ typedef struct BlkverifyRequest { uint64_t bytes; int flags; =20 - int (*request_fn)(BdrvChild *, int64_t, int64_t, QEMUIOVector *, - BdrvRequestFlags); + int GRAPH_RDLOCK_PTR (*request_fn)( + BdrvChild *, int64_t, int64_t, QEMUIOVector *, BdrvRequestFlags); =20 int ret; /* test image result */ int raw_ret; /* raw image result */ @@ -170,8 +170,11 @@ static void coroutine_fn blkverify_do_test_req(void *o= paque) BlkverifyRequest *r =3D opaque; BDRVBlkverifyState *s =3D r->bs->opaque; =20 + bdrv_graph_co_rdlock(); r->ret =3D r->request_fn(s->test_file, r->offset, r->bytes, r->qiov, r->flags); + bdrv_graph_co_rdunlock(); + r->done++; qemu_coroutine_enter_if_inactive(r->co); } @@ -180,13 +183,16 @@ static void coroutine_fn blkverify_do_raw_req(void *o= paque) { BlkverifyRequest *r =3D opaque; =20 + bdrv_graph_co_rdlock(); r->raw_ret =3D r->request_fn(r->bs->file, r->offset, r->bytes, r->raw_= qiov, r->flags); + bdrv_graph_co_rdunlock(); + r->done++; qemu_coroutine_enter_if_inactive(r->co); } =20 -static int coroutine_fn +static int coroutine_fn GRAPH_RDLOCK blkverify_co_prwv(BlockDriverState *bs, BlkverifyRequest *r, uint64_t offs= et, uint64_t bytes, QEMUIOVector *qiov, QEMUIOVector *raw_qi= ov, int flags, bool is_write) @@ -222,7 +228,7 @@ blkverify_co_prwv(BlockDriverState *bs, BlkverifyReques= t *r, uint64_t offset, return r->ret; } =20 -static int coroutine_fn +static int coroutine_fn GRAPH_RDLOCK blkverify_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes, QEMUIOVector *qiov, BdrvRequestFlags flags) { @@ -251,7 +257,7 @@ blkverify_co_preadv(BlockDriverState *bs, int64_t offse= t, int64_t bytes, return ret; } =20 -static int coroutine_fn +static int coroutine_fn GRAPH_RDLOCK blkverify_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes, QEMUIOVector *qiov, BdrvRequestFlags flags) { --=20 2.41.0 From nobody Wed Nov 27 09:52:08 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=1699382998; cv=none; d=zohomail.com; s=zohoarc; b=i8LFtlyCtK11lAgTrzBjA615lO3dBvbpD0G2kh2TpPcZ/z+HtT3A09v+UOOnYQ3F8Q9F9d1adY0KGCkEec6cSQ069zu1vA+jvGfBfTLpmbe0BvODdbAdHHhzHBA6oJedTrHnxau9fRHkMv319yj5T5kCzl+qYXSjp1Pze5a222M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699382998; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=+zE+HS+uIwx6yB7BDyqVm7l1cutd3G7/ZUNns4aax3w=; b=aiYIw5tm+SAal0eOBVmcvQ+Oh5DuhIOlVG1hXTkwgozx41RoiMYX/+TaySYrLcJaUbTfQnhd2jO/Mv89d9bw7snJAkkEZPGt8eLZab7f2j3PN8peQHCYbn4Kv6UqREbbEQPL+J7ZH0tKaq4SCKtLny2wbwna8lx1hnogM5LUa48= 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 1699382998692654.0454451252193; Tue, 7 Nov 2023 10:49:58 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0R7J-0000oZ-1m; Tue, 07 Nov 2023 13:48:01 -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 1r0R7H-0000iK-AI for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:47:59 -0500 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 1r0R7E-0001gw-Sq for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:47:59 -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.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-136-HIz2Iz5ANXCbHMVNjt41XA-1; Tue, 07 Nov 2023 13:46:31 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 24D67811E7E for ; Tue, 7 Nov 2023 18:46:22 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9EFB6C1596F; Tue, 7 Nov 2023 18:46:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699382875; 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=+zE+HS+uIwx6yB7BDyqVm7l1cutd3G7/ZUNns4aax3w=; b=bHvA6D6AqBvchKs4QxwEkxZCx5/XTpFti6J2DAKFwHl89BAaw1aSOlwlLVmhKVpRK+jUEE q9EDuq4zdxJbohseU+ib90aUU++ECVsREKUSBfxz2BRNSUnlFJWKOqmdEvx1ESCzADAwPf qbbwcKQmXbVigeKQ00e8Mlbid4YdxpU= X-MC-Unique: HIz2Iz5ANXCbHMVNjt41XA-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 19/25] block: Introduce bdrv_co_change_backing_file() Date: Tue, 7 Nov 2023 19:45:59 +0100 Message-ID: <20231107184605.236540-20-kwolf@redhat.com> In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com> References: <20231107184605.236540-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1699382999790100001 Content-Type: text/plain; charset="utf-8" bdrv_change_backing_file() is called both inside and outside coroutine context. This makes it difficult for it to take the graph lock internally. It also means that driver implementations need to be able to run outside of coroutines, too. Switch it to the usual model with a coroutine based implementation and a co_wrapper instead. The new function is marked GRAPH_RDLOCK. As the co_wrapper now runs the function in the AioContext of the node (as it should always have done), this is not GLOBAL_STATE_CODE() any more. Signed-off-by: Kevin Wolf Message-ID: <20231027155333.420094-20-kwolf@redhat.com> Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- include/block/block-global-state.h | 3 +- include/block/block-io.h | 8 ++ include/block/block_int-common.h | 5 +- block.c | 11 +-- block/qcow2.c | 128 +++++++++++++++-------------- block/qed.c | 64 +++++++-------- tests/unit/test-bdrv-drain.c | 8 +- 7 files changed, 120 insertions(+), 107 deletions(-) diff --git a/include/block/block-global-state.h b/include/block/block-globa= l-state.h index 9e0ccc1c32..6b21fbc73f 100644 --- a/include/block/block-global-state.h +++ b/include/block/block-global-state.h @@ -142,8 +142,7 @@ bdrv_refresh_limits(BlockDriverState *bs, Transaction *= tran, Error **errp); =20 int bdrv_commit(BlockDriverState *bs); int GRAPH_RDLOCK bdrv_make_empty(BdrvChild *c, Error **errp); -int bdrv_change_backing_file(BlockDriverState *bs, const char *backing_fil= e, - const char *backing_fmt, bool warn); + void bdrv_register(BlockDriver *bdrv); int bdrv_drop_intermediate(BlockDriverState *top, BlockDriverState *base, const char *backing_file_str); diff --git a/include/block/block-io.h b/include/block/block-io.h index 58c4cf50a0..f8729ccc55 100644 --- a/include/block/block-io.h +++ b/include/block/block-io.h @@ -210,6 +210,14 @@ void bdrv_round_to_subclusters(BlockDriverState *bs, void bdrv_get_backing_filename(BlockDriverState *bs, char *filename, int filename_size); =20 +int coroutine_fn GRAPH_RDLOCK +bdrv_co_change_backing_file(BlockDriverState *bs, const char *backing_file, + const char *backing_fmt, bool warn); + +int co_wrapper_bdrv_rdlock +bdrv_change_backing_file(BlockDriverState *bs, const char *backing_file, + const char *backing_fmt, bool warn); + int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, int64_t pos, int size); =20 diff --git a/include/block/block_int-common.h b/include/block/block_int-com= mon.h index ed6066929a..59f6d7f195 100644 --- a/include/block/block_int-common.h +++ b/include/block/block_int-common.h @@ -331,8 +331,9 @@ struct BlockDriver { const char *name, Error **errp); =20 - int (*bdrv_change_backing_file)(BlockDriverState *bs, - const char *backing_file, const char *backing_fmt); + int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_change_backing_file)( + BlockDriverState *bs, const char *backing_file, + const char *backing_fmt); =20 /* TODO Better pass a option string/QDict/QemuOpts to add any rule? */ int (*bdrv_debug_breakpoint)(BlockDriverState *bs, const char *event, diff --git a/block.c b/block.c index ed0afdffbd..4910b95d7d 100644 --- a/block.c +++ b/block.c @@ -5764,13 +5764,14 @@ int coroutine_fn bdrv_co_check(BlockDriverState *bs, * image file header * -ENOTSUP - format driver doesn't support changing the backing file */ -int bdrv_change_backing_file(BlockDriverState *bs, const char *backing_fil= e, - const char *backing_fmt, bool require) +int coroutine_fn +bdrv_co_change_backing_file(BlockDriverState *bs, const char *backing_file, + const char *backing_fmt, bool require) { BlockDriver *drv =3D bs->drv; int ret; =20 - GLOBAL_STATE_CODE(); + IO_CODE(); =20 if (!drv) { return -ENOMEDIUM; @@ -5785,8 +5786,8 @@ int bdrv_change_backing_file(BlockDriverState *bs, co= nst char *backing_file, return -EINVAL; } =20 - if (drv->bdrv_change_backing_file !=3D NULL) { - ret =3D drv->bdrv_change_backing_file(bs, backing_file, backing_fm= t); + if (drv->bdrv_co_change_backing_file !=3D NULL) { + ret =3D drv->bdrv_co_change_backing_file(bs, backing_file, backing= _fmt); } else { ret =3D -ENOTSUP; } diff --git a/block/qcow2.c b/block/qcow2.c index a1443a31aa..875e613ea9 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -3155,8 +3155,9 @@ fail: return ret; } =20 -static int qcow2_change_backing_file(BlockDriverState *bs, - const char *backing_file, const char *backing_fmt) +static int coroutine_fn GRAPH_RDLOCK +qcow2_co_change_backing_file(BlockDriverState *bs, const char *backing_fil= e, + const char *backing_fmt) { BDRVQcow2State *s =3D bs->opaque; =20 @@ -3816,8 +3817,11 @@ qcow2_co_create(BlockdevCreateOptions *create_option= s, Error **errp) backing_format =3D BlockdevDriver_str(qcow2_opts->backing_fmt); } =20 - ret =3D bdrv_change_backing_file(blk_bs(blk), qcow2_opts->backing_= file, - backing_format, false); + bdrv_graph_co_rdlock(); + ret =3D bdrv_co_change_backing_file(blk_bs(blk), qcow2_opts->backi= ng_file, + backing_format, false); + bdrv_graph_co_rdunlock(); + if (ret < 0) { error_setg_errno(errp, -ret, "Could not assign backing file '%= s' " "with format '%s'", qcow2_opts->backing_file, @@ -6115,64 +6119,64 @@ static const char *const qcow2_strong_runtime_opts[= ] =3D { }; =20 BlockDriver bdrv_qcow2 =3D { - .format_name =3D "qcow2", - .instance_size =3D sizeof(BDRVQcow2State), - .bdrv_probe =3D qcow2_probe, - .bdrv_open =3D qcow2_open, - .bdrv_close =3D qcow2_close, - .bdrv_reopen_prepare =3D qcow2_reopen_prepare, - .bdrv_reopen_commit =3D qcow2_reopen_commit, - .bdrv_reopen_commit_post =3D qcow2_reopen_commit_post, - .bdrv_reopen_abort =3D qcow2_reopen_abort, - .bdrv_join_options =3D qcow2_join_options, - .bdrv_child_perm =3D bdrv_default_perms, - .bdrv_co_create_opts =3D qcow2_co_create_opts, - .bdrv_co_create =3D qcow2_co_create, - .bdrv_has_zero_init =3D qcow2_has_zero_init, - .bdrv_co_block_status =3D qcow2_co_block_status, - - .bdrv_co_preadv_part =3D qcow2_co_preadv_part, - .bdrv_co_pwritev_part =3D qcow2_co_pwritev_part, - .bdrv_co_flush_to_os =3D qcow2_co_flush_to_os, - - .bdrv_co_pwrite_zeroes =3D qcow2_co_pwrite_zeroes, - .bdrv_co_pdiscard =3D qcow2_co_pdiscard, - .bdrv_co_copy_range_from =3D qcow2_co_copy_range_from, - .bdrv_co_copy_range_to =3D qcow2_co_copy_range_to, - .bdrv_co_truncate =3D qcow2_co_truncate, - .bdrv_co_pwritev_compressed_part =3D qcow2_co_pwritev_compressed_part, - .bdrv_make_empty =3D qcow2_make_empty, - - .bdrv_snapshot_create =3D qcow2_snapshot_create, - .bdrv_snapshot_goto =3D qcow2_snapshot_goto, - .bdrv_snapshot_delete =3D qcow2_snapshot_delete, - .bdrv_snapshot_list =3D qcow2_snapshot_list, - .bdrv_snapshot_load_tmp =3D qcow2_snapshot_load_tmp, - .bdrv_measure =3D qcow2_measure, - .bdrv_co_get_info =3D qcow2_co_get_info, - .bdrv_get_specific_info =3D qcow2_get_specific_info, - - .bdrv_co_save_vmstate =3D qcow2_co_save_vmstate, - .bdrv_co_load_vmstate =3D qcow2_co_load_vmstate, - - .is_format =3D true, - .supports_backing =3D true, - .bdrv_change_backing_file =3D qcow2_change_backing_file, - - .bdrv_refresh_limits =3D qcow2_refresh_limits, - .bdrv_co_invalidate_cache =3D qcow2_co_invalidate_cache, - .bdrv_inactivate =3D qcow2_inactivate, - - .create_opts =3D &qcow2_create_opts, - .amend_opts =3D &qcow2_amend_opts, - .strong_runtime_opts =3D qcow2_strong_runtime_opts, - .mutable_opts =3D mutable_opts, - .bdrv_co_check =3D qcow2_co_check, - .bdrv_amend_options =3D qcow2_amend_options, - .bdrv_co_amend =3D qcow2_co_amend, - - .bdrv_detach_aio_context =3D qcow2_detach_aio_context, - .bdrv_attach_aio_context =3D qcow2_attach_aio_context, + .format_name =3D "qcow2", + .instance_size =3D sizeof(BDRVQcow2State), + .bdrv_probe =3D qcow2_probe, + .bdrv_open =3D qcow2_open, + .bdrv_close =3D qcow2_close, + .bdrv_reopen_prepare =3D qcow2_reopen_prepare, + .bdrv_reopen_commit =3D qcow2_reopen_commit, + .bdrv_reopen_commit_post =3D qcow2_reopen_commit_post, + .bdrv_reopen_abort =3D qcow2_reopen_abort, + .bdrv_join_options =3D qcow2_join_options, + .bdrv_child_perm =3D bdrv_default_perms, + .bdrv_co_create_opts =3D qcow2_co_create_opts, + .bdrv_co_create =3D qcow2_co_create, + .bdrv_has_zero_init =3D qcow2_has_zero_init, + .bdrv_co_block_status =3D qcow2_co_block_status, + + .bdrv_co_preadv_part =3D qcow2_co_preadv_part, + .bdrv_co_pwritev_part =3D qcow2_co_pwritev_part, + .bdrv_co_flush_to_os =3D qcow2_co_flush_to_os, + + .bdrv_co_pwrite_zeroes =3D qcow2_co_pwrite_zeroes, + .bdrv_co_pdiscard =3D qcow2_co_pdiscard, + .bdrv_co_copy_range_from =3D qcow2_co_copy_range_from, + .bdrv_co_copy_range_to =3D qcow2_co_copy_range_to, + .bdrv_co_truncate =3D qcow2_co_truncate, + .bdrv_co_pwritev_compressed_part =3D qcow2_co_pwritev_compressed_pa= rt, + .bdrv_make_empty =3D qcow2_make_empty, + + .bdrv_snapshot_create =3D qcow2_snapshot_create, + .bdrv_snapshot_goto =3D qcow2_snapshot_goto, + .bdrv_snapshot_delete =3D qcow2_snapshot_delete, + .bdrv_snapshot_list =3D qcow2_snapshot_list, + .bdrv_snapshot_load_tmp =3D qcow2_snapshot_load_tmp, + .bdrv_measure =3D qcow2_measure, + .bdrv_co_get_info =3D qcow2_co_get_info, + .bdrv_get_specific_info =3D qcow2_get_specific_info, + + .bdrv_co_save_vmstate =3D qcow2_co_save_vmstate, + .bdrv_co_load_vmstate =3D qcow2_co_load_vmstate, + + .is_format =3D true, + .supports_backing =3D true, + .bdrv_co_change_backing_file =3D qcow2_co_change_backing_file, + + .bdrv_refresh_limits =3D qcow2_refresh_limits, + .bdrv_co_invalidate_cache =3D qcow2_co_invalidate_cache, + .bdrv_inactivate =3D qcow2_inactivate, + + .create_opts =3D &qcow2_create_opts, + .amend_opts =3D &qcow2_amend_opts, + .strong_runtime_opts =3D qcow2_strong_runtime_opts, + .mutable_opts =3D mutable_opts, + .bdrv_co_check =3D qcow2_co_check, + .bdrv_amend_options =3D qcow2_amend_options, + .bdrv_co_amend =3D qcow2_co_amend, + + .bdrv_detach_aio_context =3D qcow2_detach_aio_context, + .bdrv_attach_aio_context =3D qcow2_attach_aio_context, =20 .bdrv_supports_persistent_dirty_bitmap =3D qcow2_supports_persistent_dirty_bitmap, diff --git a/block/qed.c b/block/qed.c index 686ad711f7..996aa384fe 100644 --- a/block/qed.c +++ b/block/qed.c @@ -1498,9 +1498,9 @@ bdrv_qed_co_get_info(BlockDriverState *bs, BlockDrive= rInfo *bdi) return 0; } =20 -static int bdrv_qed_change_backing_file(BlockDriverState *bs, - const char *backing_file, - const char *backing_fmt) +static int coroutine_fn GRAPH_RDLOCK +bdrv_qed_co_change_backing_file(BlockDriverState *bs, const char *backing_= file, + const char *backing_fmt) { BDRVQEDState *s =3D bs->opaque; QEDHeader new_header, le_header; @@ -1562,7 +1562,7 @@ static int bdrv_qed_change_backing_file(BlockDriverSt= ate *bs, } =20 /* Write new header */ - ret =3D bdrv_pwrite_sync(bs->file, 0, buffer_len, buffer, 0); + ret =3D bdrv_co_pwrite_sync(bs->file, 0, buffer_len, buffer, 0); g_free(buffer); if (ret =3D=3D 0) { memcpy(&s->header, &new_header, sizeof(new_header)); @@ -1636,34 +1636,34 @@ static QemuOptsList qed_create_opts =3D { }; =20 static BlockDriver bdrv_qed =3D { - .format_name =3D "qed", - .instance_size =3D sizeof(BDRVQEDState), - .create_opts =3D &qed_create_opts, - .is_format =3D true, - .supports_backing =3D true, - - .bdrv_probe =3D bdrv_qed_probe, - .bdrv_open =3D bdrv_qed_open, - .bdrv_close =3D bdrv_qed_close, - .bdrv_reopen_prepare =3D bdrv_qed_reopen_prepare, - .bdrv_child_perm =3D bdrv_default_perms, - .bdrv_co_create =3D bdrv_qed_co_create, - .bdrv_co_create_opts =3D bdrv_qed_co_create_opts, - .bdrv_has_zero_init =3D bdrv_has_zero_init_1, - .bdrv_co_block_status =3D bdrv_qed_co_block_status, - .bdrv_co_readv =3D bdrv_qed_co_readv, - .bdrv_co_writev =3D bdrv_qed_co_writev, - .bdrv_co_pwrite_zeroes =3D bdrv_qed_co_pwrite_zeroes, - .bdrv_co_truncate =3D bdrv_qed_co_truncate, - .bdrv_co_getlength =3D bdrv_qed_co_getlength, - .bdrv_co_get_info =3D bdrv_qed_co_get_info, - .bdrv_refresh_limits =3D bdrv_qed_refresh_limits, - .bdrv_change_backing_file =3D bdrv_qed_change_backing_file, - .bdrv_co_invalidate_cache =3D bdrv_qed_co_invalidate_cache, - .bdrv_co_check =3D bdrv_qed_co_check, - .bdrv_detach_aio_context =3D bdrv_qed_detach_aio_context, - .bdrv_attach_aio_context =3D bdrv_qed_attach_aio_context, - .bdrv_drain_begin =3D bdrv_qed_drain_begin, + .format_name =3D "qed", + .instance_size =3D sizeof(BDRVQEDState), + .create_opts =3D &qed_create_opts, + .is_format =3D true, + .supports_backing =3D true, + + .bdrv_probe =3D bdrv_qed_probe, + .bdrv_open =3D bdrv_qed_open, + .bdrv_close =3D bdrv_qed_close, + .bdrv_reopen_prepare =3D bdrv_qed_reopen_prepare, + .bdrv_child_perm =3D bdrv_default_perms, + .bdrv_co_create =3D bdrv_qed_co_create, + .bdrv_co_create_opts =3D bdrv_qed_co_create_opts, + .bdrv_has_zero_init =3D bdrv_has_zero_init_1, + .bdrv_co_block_status =3D bdrv_qed_co_block_status, + .bdrv_co_readv =3D bdrv_qed_co_readv, + .bdrv_co_writev =3D bdrv_qed_co_writev, + .bdrv_co_pwrite_zeroes =3D bdrv_qed_co_pwrite_zeroes, + .bdrv_co_truncate =3D bdrv_qed_co_truncate, + .bdrv_co_getlength =3D bdrv_qed_co_getlength, + .bdrv_co_get_info =3D bdrv_qed_co_get_info, + .bdrv_refresh_limits =3D bdrv_qed_refresh_limits, + .bdrv_co_change_backing_file =3D bdrv_qed_co_change_backing_file, + .bdrv_co_invalidate_cache =3D bdrv_qed_co_invalidate_cache, + .bdrv_co_check =3D bdrv_qed_co_check, + .bdrv_detach_aio_context =3D bdrv_qed_detach_aio_context, + .bdrv_attach_aio_context =3D bdrv_qed_attach_aio_context, + .bdrv_drain_begin =3D bdrv_qed_drain_begin, }; =20 static void bdrv_qed_init(void) diff --git a/tests/unit/test-bdrv-drain.c b/tests/unit/test-bdrv-drain.c index ba4e42b197..8d05538bf6 100644 --- a/tests/unit/test-bdrv-drain.c +++ b/tests/unit/test-bdrv-drain.c @@ -96,9 +96,9 @@ static int coroutine_fn bdrv_test_co_preadv(BlockDriverSt= ate *bs, return 0; } =20 -static int bdrv_test_change_backing_file(BlockDriverState *bs, - const char *backing_file, - const char *backing_fmt) +static int bdrv_test_co_change_backing_file(BlockDriverState *bs, + const char *backing_file, + const char *backing_fmt) { return 0; } @@ -116,7 +116,7 @@ static BlockDriver bdrv_test =3D { =20 .bdrv_child_perm =3D bdrv_default_perms, =20 - .bdrv_change_backing_file =3D bdrv_test_change_backing_file, + .bdrv_co_change_backing_file =3D bdrv_test_co_change_backing_file, }; =20 static void aio_ret_cb(void *opaque, int ret) --=20 2.41.0 From nobody Wed Nov 27 09:52:08 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=1699382949; cv=none; d=zohomail.com; s=zohoarc; b=Z8wKRusvcSFj/DZQV3Yw53Upl6h3qay4U8+9W5ctAnRp7R4dHekDpoTxaK1x9fVb+wbo2NlsYnySABl7NoG+c8gRPludPlH5plzQKrqm7ydKBxA8MwHQ0zGBn4Wx8YdJJwBGcmBHCvyjzIUszG14aYDDrleMMqB5TK45ds2Ip4o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699382949; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=ptm7hwSXzPsMJ4jyJutgAOEotLG1+sYIdfkKXYKk4Go=; b=SiVKq/5eYnVar0cYarYj8a0ppHPABGephvChgTffDvZ2HW6iwxT0lNZD+xMUdvVJmjGb726bCxSI2lVEDEQJek3Kr3M/OeBrsF70pNi/5kPWYND/L7wG//Cir6yZWaomgGoOGX86q3qU59K0jrkqHKQE+6Ls2eRL7xtiu66qLBs= 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 1699382949385304.00313708455815; Tue, 7 Nov 2023 10:49:09 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0R6Q-0004kI-G6; Tue, 07 Nov 2023 13:47:06 -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 1r0R5r-00044k-J0 for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:32 -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 1r0R5n-0001Wo-Jc for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:31 -0500 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-597-4yidqTvpM7mYPi2lsbPItw-1; Tue, 07 Nov 2023 13:46:24 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D032A38143A2 for ; Tue, 7 Nov 2023 18:46:22 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5B6B7C1596F; Tue, 7 Nov 2023 18:46:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699382785; 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=ptm7hwSXzPsMJ4jyJutgAOEotLG1+sYIdfkKXYKk4Go=; b=ZtoDEEYLzdLUjHGHPDIZGgjYkmUFm50bNpgJsMChkvuDsgeeijZN+E0K40/BqVjBxffryQ KA95jz/ifewjJ70BaY09apaobco0GMx8LOhUt1eg5Il0vjvJ3jamDe6grp7eXFrFoSSvzM QwbhqLi0AUI8C4ZKYv/pDyzOH/veAWA= X-MC-Unique: 4yidqTvpM7mYPi2lsbPItw-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 20/25] block: Add missing GRAPH_RDLOCK annotations Date: Tue, 7 Nov 2023 19:46:00 +0100 Message-ID: <20231107184605.236540-21-kwolf@redhat.com> In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com> References: <20231107184605.236540-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1699382951520100003 Content-Type: text/plain; charset="utf-8" This adds GRAPH_RDLOCK to some driver callbacks that are already called with the graph lock held, and which will need the annotation because they access bs->file, but don't have it yet. This also covers a few callbacks that were not marked GRAPH_RDLOCK before, but where updating BlockDriver is trivially possible. Signed-off-by: Kevin Wolf Message-ID: <20231027155333.420094-21-kwolf@redhat.com> Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- block/qcow2.h | 11 ++++++----- include/block/block_int-common.h | 17 +++++++++-------- block/blkdebug.c | 13 +++++-------- block/blkverify.c | 2 +- block/copy-before-write.c | 2 +- block/filter-compress.c | 3 ++- block/io.c | 2 ++ block/parallels.c | 12 ++++-------- block/preallocate.c | 4 ++-- block/qcow.c | 2 +- block/qcow2.c | 4 ++-- block/qed.c | 10 ++++------ block/raw-format.c | 16 ++++++++-------- block/snapshot-access.c | 2 +- block/vdi.c | 9 ++++----- block/vhdx.c | 6 +++--- 16 files changed, 55 insertions(+), 60 deletions(-) diff --git a/block/qcow2.h b/block/qcow2.h index 29958c512b..948335979f 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -972,11 +972,12 @@ int GRAPH_RDLOCK qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id, const char *name, Error **errp); =20 -int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab); -int qcow2_snapshot_load_tmp(BlockDriverState *bs, - const char *snapshot_id, - const char *name, - Error **errp); +int GRAPH_RDLOCK +qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab); + +int GRAPH_RDLOCK +qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_id, + const char *name, Error **errp); =20 void qcow2_free_snapshots(BlockDriverState *bs); int coroutine_fn GRAPH_RDLOCK diff --git a/include/block/block_int-common.h b/include/block/block_int-com= mon.h index 59f6d7f195..63bc523d7c 100644 --- a/include/block/block_int-common.h +++ b/include/block/block_int-common.h @@ -310,7 +310,7 @@ struct BlockDriver { * One example usage is to avoid waiting for an nbd target node reconn= ect * timeout during job-cancel with force=3Dtrue. */ - void (*bdrv_cancel_in_flight)(BlockDriverState *bs); + void GRAPH_RDLOCK_PTR (*bdrv_cancel_in_flight)(BlockDriverState *bs); =20 int GRAPH_RDLOCK_PTR (*bdrv_inactivate)(BlockDriverState *bs); =20 @@ -324,12 +324,12 @@ struct BlockDriver { BlockDriverState *bs, const char *snapshot_id, const char *name, Error **errp); =20 - int (*bdrv_snapshot_list)(BlockDriverState *bs, - QEMUSnapshotInfo **psn_info); - int (*bdrv_snapshot_load_tmp)(BlockDriverState *bs, - const char *snapshot_id, - const char *name, - Error **errp); + int GRAPH_RDLOCK_PTR (*bdrv_snapshot_list)( + BlockDriverState *bs, QEMUSnapshotInfo **psn_info); + + int GRAPH_RDLOCK_PTR (*bdrv_snapshot_load_tmp)( + BlockDriverState *bs, const char *snapshot_id, const char *name, + Error **errp); =20 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_change_backing_file)( BlockDriverState *bs, const char *backing_file, @@ -396,7 +396,8 @@ struct BlockDriver { * Only drivers that want to override guest geometry implement this * callback; see hd_geometry_guess(). */ - int (*bdrv_probe_geometry)(BlockDriverState *bs, HDGeometry *geo); + int GRAPH_RDLOCK_PTR (*bdrv_probe_geometry)( + BlockDriverState *bs, HDGeometry *geo); =20 void GRAPH_WRLOCK_PTR (*bdrv_add_child)( BlockDriverState *parent, BlockDriverState *child, Error **errp); diff --git a/block/blkdebug.c b/block/blkdebug.c index addad914b3..230efa9e8d 100644 --- a/block/blkdebug.c +++ b/block/blkdebug.c @@ -746,13 +746,10 @@ blkdebug_co_pdiscard(BlockDriverState *bs, int64_t of= fset, int64_t bytes) return bdrv_co_pdiscard(bs->file, offset, bytes); } =20 -static int coroutine_fn blkdebug_co_block_status(BlockDriverState *bs, - bool want_zero, - int64_t offset, - int64_t bytes, - int64_t *pnum, - int64_t *map, - BlockDriverState **file) +static int coroutine_fn GRAPH_RDLOCK +blkdebug_co_block_status(BlockDriverState *bs, bool want_zero, int64_t off= set, + int64_t bytes, int64_t *pnum, int64_t *map, + BlockDriverState **file) { int err; =20 @@ -973,7 +970,7 @@ blkdebug_co_getlength(BlockDriverState *bs) return bdrv_co_getlength(bs->file->bs); } =20 -static void blkdebug_refresh_filename(BlockDriverState *bs) +static void GRAPH_RDLOCK blkdebug_refresh_filename(BlockDriverState *bs) { BDRVBlkdebugState *s =3D bs->opaque; const QDictEntry *e; diff --git a/block/blkverify.c b/block/blkverify.c index 9ea2cb4cc5..a96905db35 100644 --- a/block/blkverify.c +++ b/block/blkverify.c @@ -288,7 +288,7 @@ blkverify_recurse_can_replace(BlockDriverState *bs, bdrv_recurse_can_replace(s->test_file->bs, to_replace); } =20 -static void blkverify_refresh_filename(BlockDriverState *bs) +static void GRAPH_RDLOCK blkverify_refresh_filename(BlockDriverState *bs) { BDRVBlkverifyState *s =3D bs->opaque; =20 diff --git a/block/copy-before-write.c b/block/copy-before-write.c index 4ffabc5ca2..8193d3a4cd 100644 --- a/block/copy-before-write.c +++ b/block/copy-before-write.c @@ -335,7 +335,7 @@ cbw_co_pdiscard_snapshot(BlockDriverState *bs, int64_t = offset, int64_t bytes) return bdrv_co_pdiscard(s->target, offset, bytes); } =20 -static void cbw_refresh_filename(BlockDriverState *bs) +static void GRAPH_RDLOCK cbw_refresh_filename(BlockDriverState *bs) { pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), bs->file->bs->filename); diff --git a/block/filter-compress.c b/block/filter-compress.c index 320d9576fa..e3fc82f322 100644 --- a/block/filter-compress.c +++ b/block/filter-compress.c @@ -97,7 +97,8 @@ compress_co_pdiscard(BlockDriverState *bs, int64_t offset= , int64_t bytes) } =20 =20 -static void compress_refresh_limits(BlockDriverState *bs, Error **errp) +static void GRAPH_RDLOCK +compress_refresh_limits(BlockDriverState *bs, Error **errp) { BlockDriverInfo bdi; int ret; diff --git a/block/io.c b/block/io.c index 527a1de04e..7e62fabbf5 100644 --- a/block/io.c +++ b/block/io.c @@ -3685,6 +3685,8 @@ out: void bdrv_cancel_in_flight(BlockDriverState *bs) { GLOBAL_STATE_CODE(); + GRAPH_RDLOCK_GUARD_MAINLOOP(); + if (!bs || !bs->drv) { return; } diff --git a/block/parallels.c b/block/parallels.c index 6318dd02e7..9b9bd1f993 100644 --- a/block/parallels.c +++ b/block/parallels.c @@ -415,14 +415,10 @@ parallels_co_flush_to_os(BlockDriverState *bs) return 0; } =20 - -static int coroutine_fn parallels_co_block_status(BlockDriverState *bs, - bool want_zero, - int64_t offset, - int64_t bytes, - int64_t *pnum, - int64_t *map, - BlockDriverState **file) +static int coroutine_fn GRAPH_RDLOCK +parallels_co_block_status(BlockDriverState *bs, bool want_zero, int64_t of= fset, + int64_t bytes, int64_t *pnum, int64_t *map, + BlockDriverState **file) { BDRVParallelsState *s =3D bs->opaque; int count; diff --git a/block/preallocate.c b/block/preallocate.c index bfb638d8b1..4e0c891ab2 100644 --- a/block/preallocate.c +++ b/block/preallocate.c @@ -541,8 +541,8 @@ static void preallocate_drop_resize_bh(void *opaque) preallocate_drop_resize(opaque, NULL); } =20 -static void preallocate_set_perm(BlockDriverState *bs, - uint64_t perm, uint64_t shared) +static void GRAPH_RDLOCK +preallocate_set_perm(BlockDriverState *bs, uint64_t perm, uint64_t shared) { BDRVPreallocateState *s =3D bs->opaque; =20 diff --git a/block/qcow.c b/block/qcow.c index eab68e387c..1e2835f1f4 100644 --- a/block/qcow.c +++ b/block/qcow.c @@ -1024,7 +1024,7 @@ fail: return ret; } =20 -static int qcow_make_empty(BlockDriverState *bs) +static int GRAPH_RDLOCK qcow_make_empty(BlockDriverState *bs) { BDRVQcowState *s =3D bs->opaque; uint32_t l1_length =3D s->l1_size * sizeof(uint64_t); diff --git a/block/qcow2.c b/block/qcow2.c index 875e613ea9..7ab399ffc5 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -5226,8 +5226,8 @@ qcow2_co_get_info(BlockDriverState *bs, BlockDriverIn= fo *bdi) return 0; } =20 -static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs, - Error **errp) +static ImageInfoSpecific * GRAPH_RDLOCK +qcow2_get_specific_info(BlockDriverState *bs, Error **errp) { BDRVQcow2State *s =3D bs->opaque; ImageInfoSpecific *spec_info; diff --git a/block/qed.c b/block/qed.c index 996aa384fe..f4c1628a81 100644 --- a/block/qed.c +++ b/block/qed.c @@ -1443,12 +1443,10 @@ bdrv_qed_co_pwrite_zeroes(BlockDriverState *bs, int= 64_t offset, int64_t bytes, QED_AIOCB_WRITE | QED_AIOCB_ZERO); } =20 -static int coroutine_fn bdrv_qed_co_truncate(BlockDriverState *bs, - int64_t offset, - bool exact, - PreallocMode prealloc, - BdrvRequestFlags flags, - Error **errp) +static int coroutine_fn GRAPH_RDLOCK +bdrv_qed_co_truncate(BlockDriverState *bs, int64_t offset, bool exact, + PreallocMode prealloc, BdrvRequestFlags flags, + Error **errp) { BDRVQEDState *s =3D bs->opaque; uint64_t old_image_size; diff --git a/block/raw-format.c b/block/raw-format.c index 8ca74c1cf3..2640d54801 100644 --- a/block/raw-format.c +++ b/block/raw-format.c @@ -279,11 +279,10 @@ fail: return ret; } =20 -static int coroutine_fn raw_co_block_status(BlockDriverState *bs, - bool want_zero, int64_t offset, - int64_t bytes, int64_t *pnum, - int64_t *map, - BlockDriverState **file) +static int coroutine_fn GRAPH_RDLOCK +raw_co_block_status(BlockDriverState *bs, bool want_zero, int64_t offset, + int64_t bytes, int64_t *pnum, int64_t *map, + BlockDriverState **file) { BDRVRawState *s =3D bs->opaque; *pnum =3D bytes; @@ -397,7 +396,7 @@ raw_co_get_info(BlockDriverState *bs, BlockDriverInfo *= bdi) return bdrv_co_get_info(bs->file->bs, bdi); } =20 -static void raw_refresh_limits(BlockDriverState *bs, Error **errp) +static void GRAPH_RDLOCK raw_refresh_limits(BlockDriverState *bs, Error **= errp) { bs->bl.has_variable_length =3D bs->file->bs->bl.has_variable_length; =20 @@ -561,7 +560,8 @@ raw_probe_blocksizes(BlockDriverState *bs, BlockSizes *= bsz) return 0; } =20 -static int raw_probe_geometry(BlockDriverState *bs, HDGeometry *geo) +static int GRAPH_RDLOCK +raw_probe_geometry(BlockDriverState *bs, HDGeometry *geo) { BDRVRawState *s =3D bs->opaque; if (s->offset || s->has_size) { @@ -611,7 +611,7 @@ static const char *const raw_strong_runtime_opts[] =3D { NULL }; =20 -static void raw_cancel_in_flight(BlockDriverState *bs) +static void GRAPH_RDLOCK raw_cancel_in_flight(BlockDriverState *bs) { bdrv_cancel_in_flight(bs->file->bs); } diff --git a/block/snapshot-access.c b/block/snapshot-access.c index 8d4e8932b8..7c45739eb1 100644 --- a/block/snapshot-access.c +++ b/block/snapshot-access.c @@ -73,7 +73,7 @@ snapshot_access_co_pwritev_part(BlockDriverState *bs, } =20 =20 -static void snapshot_access_refresh_filename(BlockDriverState *bs) +static void GRAPH_RDLOCK snapshot_access_refresh_filename(BlockDriverState= *bs) { pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), bs->file->bs->filename); diff --git a/block/vdi.c b/block/vdi.c index 8e144ce523..84150180ec 100644 --- a/block/vdi.c +++ b/block/vdi.c @@ -520,11 +520,10 @@ static int vdi_reopen_prepare(BDRVReopenState *state, return 0; } =20 -static int coroutine_fn vdi_co_block_status(BlockDriverState *bs, - bool want_zero, - int64_t offset, int64_t bytes, - int64_t *pnum, int64_t *map, - BlockDriverState **file) +static int coroutine_fn GRAPH_RDLOCK +vdi_co_block_status(BlockDriverState *bs, bool want_zero, int64_t offset, + int64_t bytes, int64_t *pnum, int64_t *map, + BlockDriverState **file) { BDRVVdiState *s =3D (BDRVVdiState *)bs->opaque; size_t bmap_index =3D offset / s->block_size; diff --git a/block/vhdx.c b/block/vhdx.c index e136ba1ae1..ac4d52e8c6 100644 --- a/block/vhdx.c +++ b/block/vhdx.c @@ -2163,9 +2163,9 @@ fail: * r/w and any log has already been replayed, so there is nothing (current= ly) * for us to do here */ -static int coroutine_fn vhdx_co_check(BlockDriverState *bs, - BdrvCheckResult *result, - BdrvCheckMode fix) +static int coroutine_fn GRAPH_RDLOCK +vhdx_co_check(BlockDriverState *bs, BdrvCheckResult *result, + BdrvCheckMode fix) { BDRVVHDXState *s =3D bs->opaque; =20 --=20 2.41.0 From nobody Wed Nov 27 09:52:08 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=1699383032; cv=none; d=zohomail.com; s=zohoarc; b=esLwA57eRxWRAj/BWbp8xj5sPBKbBlnuQU89Gyx0dIvchX93DxdnqprtLLoilXk4amNFFAPSQRSe+LzFkKljPQCYOUqLmtjQFZim4B3EFgNNiMMs/SrlnWCkPMtWe2w384G2xaOuAbIGhSFE/KRqeHWhmYFeQsZjb0kB9wNAxZQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699383032; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=EFmRSZZ1tGJRCUwb25czO0aO3P+HawN92fGv1bSfreg=; b=E7dv3fyH4l4Zpv1Na8lCzGxxR0HesDa9VdxeLQ0op3dnl5ugQ77tpoh5zUwHfOfZWSN6hS4zCXkoVlD5a5BZgPGDj5STA6zLVo7VpqT4pIO7BP4mxGOkFHGG3/JF+Jm6+ZLW3rGbwPYNLcuAcyPI1A3EDpkbYwvX3n2rWw/FvD4= 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 1699383032063606.6021631372452; Tue, 7 Nov 2023 10:50:32 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0R6H-0004a2-21; Tue, 07 Nov 2023 13:46:57 -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 1r0R5p-00041Y-8y for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:31 -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 1r0R5m-0001Wl-96 for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:28 -0500 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-255-Vld49_u-PNS-ojBmldC1QQ-1; Tue, 07 Nov 2023 13:46:23 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 87153280302A for ; Tue, 7 Nov 2023 18:46:23 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id 12CCCC1596F; Tue, 7 Nov 2023 18:46:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699382785; 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=EFmRSZZ1tGJRCUwb25czO0aO3P+HawN92fGv1bSfreg=; b=CIs5fLN8Fxsr6iauZLLPr0DFiFcTFo1GzqYRWmDWMPh81iUhFMNZ4SUHGB0g08Ez0v53Lm 1+Fy5suvyEIYiO4NY1z93gyfQc6YhJyl4Qx9YZSd16kHceuZtAb6AC2HOmu/rh7QWkEvbu BjGAPlyD8DscBJ6SrFXx8wg3vorbOZg= X-MC-Unique: Vld49_u-PNS-ojBmldC1QQ-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 21/25] qcow2: Take locks for accessing bs->file Date: Tue, 7 Nov 2023 19:46:01 +0100 Message-ID: <20231107184605.236540-22-kwolf@redhat.com> In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com> References: <20231107184605.236540-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1699383033867100003 Content-Type: text/plain; charset="utf-8" This updates the qcow2 code to add GRAPH_RDLOCK annotations for all places that read bs->file. Signed-off-by: Kevin Wolf Message-ID: <20231027155333.420094-22-kwolf@redhat.com> Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- block/qcow2.h | 48 ++++++++++++++++++++++++++----------------- block/qcow2-bitmap.c | 14 +++++++------ block/qcow2-cluster.c | 25 +++++++++++----------- block/qcow2.c | 13 ++++++++---- 4 files changed, 59 insertions(+), 41 deletions(-) diff --git a/block/qcow2.h b/block/qcow2.h index 948335979f..a9e3481c6e 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -641,7 +641,7 @@ static inline void set_l2_bitmap(BDRVQcow2State *s, uin= t64_t *l2_slice, l2_slice[idx + 1] =3D cpu_to_be64(bitmap); } =20 -static inline bool has_data_file(BlockDriverState *bs) +static inline bool GRAPH_RDLOCK has_data_file(BlockDriverState *bs) { BDRVQcow2State *s =3D bs->opaque; return (s->data_file !=3D bs->file); @@ -709,8 +709,8 @@ static inline int64_t qcow2_vm_state_offset(BDRVQcow2St= ate *s) return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits); } =20 -static inline QCow2ClusterType qcow2_get_cluster_type(BlockDriverState *bs, - uint64_t l2_entry) +static inline QCow2ClusterType GRAPH_RDLOCK +qcow2_get_cluster_type(BlockDriverState *bs, uint64_t l2_entry) { BDRVQcow2State *s =3D bs->opaque; =20 @@ -743,7 +743,7 @@ static inline QCow2ClusterType qcow2_get_cluster_type(B= lockDriverState *bs, * (this checks the whole entry and bitmap, not only the bits related * to subcluster @sc_index). */ -static inline +static inline GRAPH_RDLOCK QCow2SubclusterType qcow2_get_subcluster_type(BlockDriverState *bs, uint64_t l2_entry, uint64_t l2_bitmap, @@ -834,9 +834,9 @@ int64_t qcow2_refcount_metadata_size(int64_t clusters, = size_t cluster_size, int refcount_order, bool generous_inc= rease, uint64_t *refblock_count); =20 -int qcow2_mark_dirty(BlockDriverState *bs); -int qcow2_mark_corrupt(BlockDriverState *bs); -int qcow2_update_header(BlockDriverState *bs); +int GRAPH_RDLOCK qcow2_mark_dirty(BlockDriverState *bs); +int GRAPH_RDLOCK qcow2_mark_corrupt(BlockDriverState *bs); +int GRAPH_RDLOCK qcow2_update_header(BlockDriverState *bs); =20 void GRAPH_RDLOCK qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset, @@ -890,10 +890,11 @@ int GRAPH_RDLOCK qcow2_write_caches(BlockDriverState = *bs); int coroutine_fn qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResu= lt *res, BdrvCheckMode fix); =20 -void qcow2_process_discards(BlockDriverState *bs, int ret); +void GRAPH_RDLOCK qcow2_process_discards(BlockDriverState *bs, int ret); =20 -int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t of= fset, - int64_t size); +int GRAPH_RDLOCK +qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset, + int64_t size); int GRAPH_RDLOCK qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offse= t, int64_t size, bool data_file); @@ -939,8 +940,9 @@ qcow2_alloc_host_offset(BlockDriverState *bs, uint64_t = offset, int coroutine_fn GRAPH_RDLOCK qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs, uint64_t offse= t, int compressed_size, uint64_t *host_= offset); -void qcow2_parse_compressed_l2_entry(BlockDriverState *bs, uint64_t l2_ent= ry, - uint64_t *coffset, int *csize); +void GRAPH_RDLOCK +qcow2_parse_compressed_l2_entry(BlockDriverState *bs, uint64_t l2_entry, + uint64_t *coffset, int *csize); =20 int coroutine_fn GRAPH_RDLOCK qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m); @@ -993,8 +995,9 @@ qcow2_check_fix_snapshot_table(BlockDriverState *bs, Bd= rvCheckResult *result, BdrvCheckMode fix); =20 /* qcow2-cache.c functions */ -Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables, - unsigned table_size); +Qcow2Cache * GRAPH_RDLOCK +qcow2_cache_create(BlockDriverState *bs, int num_tables, unsigned table_si= ze); + int qcow2_cache_destroy(Qcow2Cache *c); =20 void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table); @@ -1020,17 +1023,24 @@ void *qcow2_cache_is_table_offset(Qcow2Cache *c, ui= nt64_t offset); void qcow2_cache_discard(Qcow2Cache *c, void *table); =20 /* qcow2-bitmap.c functions */ -int coroutine_fn +int coroutine_fn GRAPH_RDLOCK qcow2_check_bitmaps_refcounts(BlockDriverState *bs, BdrvCheckResult *res, void **refcount_table, int64_t *refcount_table_size); + bool coroutine_fn GRAPH_RDLOCK -qcow2_load_dirty_bitmaps(BlockDriverState *bs, bool *header_updated, Error= **errp); -bool qcow2_get_bitmap_info_list(BlockDriverState *bs, - Qcow2BitmapInfoList **info_list, Error **e= rrp); +qcow2_load_dirty_bitmaps(BlockDriverState *bs, bool *header_updated, + Error **errp); + +bool GRAPH_RDLOCK +qcow2_get_bitmap_info_list(BlockDriverState *bs, + Qcow2BitmapInfoList **info_list, Error **errp); + int GRAPH_RDLOCK qcow2_reopen_bitmaps_rw(BlockDriverState *bs, Error **err= p); int GRAPH_RDLOCK qcow2_reopen_bitmaps_ro(BlockDriverState *bs, Error **err= p); -int coroutine_fn qcow2_truncate_bitmaps_check(BlockDriverState *bs, Error = **errp); + +int coroutine_fn GRAPH_RDLOCK +qcow2_truncate_bitmaps_check(BlockDriverState *bs, Error **errp); =20 bool GRAPH_RDLOCK qcow2_store_persistent_dirty_bitmaps(BlockDriverState *bs, bool release_st= ored, diff --git a/block/qcow2-bitmap.c b/block/qcow2-bitmap.c index 3058309c47..0e567ed588 100644 --- a/block/qcow2-bitmap.c +++ b/block/qcow2-bitmap.c @@ -105,7 +105,7 @@ static inline bool can_write(BlockDriverState *bs) return !bdrv_is_read_only(bs) && !(bdrv_get_flags(bs) & BDRV_O_INACTIV= E); } =20 -static int update_header_sync(BlockDriverState *bs) +static int GRAPH_RDLOCK update_header_sync(BlockDriverState *bs) { int ret; =20 @@ -221,8 +221,9 @@ clear_bitmap_table(BlockDriverState *bs, uint64_t *bitm= ap_table, } } =20 -static int bitmap_table_load(BlockDriverState *bs, Qcow2BitmapTable *tb, - uint64_t **bitmap_table) +static int GRAPH_RDLOCK +bitmap_table_load(BlockDriverState *bs, Qcow2BitmapTable *tb, + uint64_t **bitmap_table) { int ret; BDRVQcow2State *s =3D bs->opaque; @@ -551,8 +552,9 @@ static uint32_t bitmap_list_count(Qcow2BitmapList *bm_l= ist) * Get bitmap list from qcow2 image. Actually reads bitmap directory, * checks it and convert to bitmap list. */ -static Qcow2BitmapList *bitmap_list_load(BlockDriverState *bs, uint64_t of= fset, - uint64_t size, Error **errp) +static Qcow2BitmapList * GRAPH_RDLOCK +bitmap_list_load(BlockDriverState *bs, uint64_t offset, uint64_t size, + Error **errp) { int ret; BDRVQcow2State *s =3D bs->opaque; @@ -961,7 +963,7 @@ static void set_readonly_helper(gpointer bitmap, gpoint= er value) * If header_updated is not NULL then it is set appropriately regardless of * the return value. */ -bool coroutine_fn GRAPH_RDLOCK +bool coroutine_fn qcow2_load_dirty_bitmaps(BlockDriverState *bs, bool *header_updated, Error **errp) { diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 5af439bd11..ce8c0076b3 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -391,11 +391,10 @@ fail: * If the L2 entry is invalid return -errno and set @type to * QCOW2_SUBCLUSTER_INVALID. */ -static int qcow2_get_subcluster_range_type(BlockDriverState *bs, - uint64_t l2_entry, - uint64_t l2_bitmap, - unsigned sc_from, - QCow2SubclusterType *type) +static int GRAPH_RDLOCK +qcow2_get_subcluster_range_type(BlockDriverState *bs, uint64_t l2_entry, + uint64_t l2_bitmap, unsigned sc_from, + QCow2SubclusterType *type) { BDRVQcow2State *s =3D bs->opaque; uint32_t val; @@ -442,9 +441,10 @@ static int qcow2_get_subcluster_range_type(BlockDriver= State *bs, * On failure return -errno and update @l2_index to point to the * invalid entry. */ -static int count_contiguous_subclusters(BlockDriverState *bs, int nb_clust= ers, - unsigned sc_index, uint64_t *l2_sl= ice, - unsigned *l2_index) +static int GRAPH_RDLOCK +count_contiguous_subclusters(BlockDriverState *bs, int nb_clusters, + unsigned sc_index, uint64_t *l2_slice, + unsigned *l2_index) { BDRVQcow2State *s =3D bs->opaque; int i, count =3D 0; @@ -1329,7 +1329,8 @@ calculate_l2_meta(BlockDriverState *bs, uint64_t host= _cluster_offset, * requires a new allocation (that is, if the cluster is unallocated * or has refcount > 1 and therefore cannot be written in-place). */ -static bool cluster_needs_new_alloc(BlockDriverState *bs, uint64_t l2_entr= y) +static bool GRAPH_RDLOCK +cluster_needs_new_alloc(BlockDriverState *bs, uint64_t l2_entry) { switch (qcow2_get_cluster_type(bs, l2_entry)) { case QCOW2_CLUSTER_NORMAL: @@ -1360,9 +1361,9 @@ static bool cluster_needs_new_alloc(BlockDriverState = *bs, uint64_t l2_entry) * allocated and can be overwritten in-place (this includes clusters * of type QCOW2_CLUSTER_ZERO_ALLOC). */ -static int count_single_write_clusters(BlockDriverState *bs, int nb_cluste= rs, - uint64_t *l2_slice, int l2_index, - bool new_alloc) +static int GRAPH_RDLOCK +count_single_write_clusters(BlockDriverState *bs, int nb_clusters, + uint64_t *l2_slice, int l2_index, bool new_all= oc) { BDRVQcow2State *s =3D bs->opaque; uint64_t l2_entry =3D get_l2_entry(s, l2_slice, l2_index); diff --git a/block/qcow2.c b/block/qcow2.c index 7ab399ffc5..cf2468858f 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -95,9 +95,10 @@ static int qcow2_probe(const uint8_t *buf, int buf_size,= const char *filename) } =20 =20 -static int qcow2_crypto_hdr_read_func(QCryptoBlock *block, size_t offset, - uint8_t *buf, size_t buflen, - void *opaque, Error **errp) +static int GRAPH_RDLOCK +qcow2_crypto_hdr_read_func(QCryptoBlock *block, size_t offset, + uint8_t *buf, size_t buflen, + void *opaque, Error **errp) { BlockDriverState *bs =3D opaque; BDRVQcow2State *s =3D bs->opaque; @@ -156,7 +157,7 @@ qcow2_crypto_hdr_init_func(QCryptoBlock *block, size_t = headerlen, void *opaque, =20 =20 /* The graph lock must be held when called in coroutine context */ -static int coroutine_mixed_fn +static int coroutine_mixed_fn GRAPH_RDLOCK qcow2_crypto_hdr_write_func(QCryptoBlock *block, size_t offset, const uint8_t *buf, size_t buflen, void *opaque, Error **errp) @@ -2029,6 +2030,8 @@ static void qcow2_reopen_commit(BDRVReopenState *stat= e) { BDRVQcow2State *s =3D state->bs->opaque; =20 + GRAPH_RDLOCK_GUARD_MAINLOOP(); + qcow2_update_options_commit(state->bs, state->opaque); if (!s->data_file) { /* @@ -2064,6 +2067,8 @@ static void qcow2_reopen_abort(BDRVReopenState *state) { BDRVQcow2State *s =3D state->bs->opaque; =20 + GRAPH_RDLOCK_GUARD_MAINLOOP(); + if (!s->data_file) { /* * If we don't have an external data file, s->data_file was cleare= d by --=20 2.41.0 From nobody Wed Nov 27 09:52:08 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=1699382939; cv=none; d=zohomail.com; s=zohoarc; b=Ycy5MTjl71ZWmnHFCNvYwmRko9puNE8QSD5JlrW4Ve2cuG+2gH+rnG0952N6KlMhxJ+FFjNzTtNfEFHAcx0wKpWmeRzBDU4cWS1VpcyRcVB2iCVc7fWiHfQQ2AKCX4ZqINAGrI76soaqpyrQ6jiCccUnxAXEYdMNWQtFbdk1S5o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699382939; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=0zgrx3emowLAPrH+CreSd27HfEg1LDuFajKXT8/DjsY=; b=FDO+0aDzqVDhkoG8NAdZ60Ooznrg3zn6wmXVBOkoWkggJHGZw0cf7mtYBYwWa1GOJx3s0xkoQWQz1tT6hMgQwUCMvjZTIC/Atq01/4NDD4Dn7gElGOVOmxu25QyauKGfpw0QWxilcltgzYM8yUDXoVcWnimBVUWdGPfsnLGTAmg= 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 1699382939634116.99030722624025; Tue, 7 Nov 2023 10:48:59 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0R6J-0004bv-7S; Tue, 07 Nov 2023 13:46:59 -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 1r0R5r-00044l-Jp for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:34 -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 1r0R5n-0001X0-JO for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:30 -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.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-335-TqrQaqy-MMKD-s1Uu5YmKA-1; Tue, 07 Nov 2023 13:46:24 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 44E29811E91 for ; Tue, 7 Nov 2023 18:46:24 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id BFFF8C1596F; Tue, 7 Nov 2023 18:46:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699382786; 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=0zgrx3emowLAPrH+CreSd27HfEg1LDuFajKXT8/DjsY=; b=cMFaqHW7TnsiCxmAXRdKQEzC2S0w2d0sn5W/AFWNWx6MqGtVxXTBqOMhD6H4kkeW7soEtW uV8e/ejxIwkay/YUtugdbOS7OFmkpaNBvU4FHmVtWv7AMCtH7IZ1WCt6ili5OffMU/NN5Q cmp2K0s+X3tmB+gyrjFnZ50cFlmRk+k= X-MC-Unique: TqrQaqy-MMKD-s1Uu5YmKA-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 22/25] vhdx: Take locks for accessing bs->file Date: Tue, 7 Nov 2023 19:46:02 +0100 Message-ID: <20231107184605.236540-23-kwolf@redhat.com> In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com> References: <20231107184605.236540-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1699382941451100006 Content-Type: text/plain; charset="utf-8" This updates the vhdx code to add GRAPH_RDLOCK annotations for all places that read bs->file. Signed-off-by: Kevin Wolf Message-ID: <20231027155333.420094-23-kwolf@redhat.com> Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- block/vhdx.h | 9 ++++++--- block/vhdx-log.c | 40 ++++++++++++++++++++++------------------ block/vhdx.c | 18 +++++++++++------- 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/block/vhdx.h b/block/vhdx.h index 85594a5380..c6dd4d6040 100644 --- a/block/vhdx.h +++ b/block/vhdx.h @@ -401,8 +401,9 @@ typedef struct BDRVVHDXState { =20 void vhdx_guid_generate(MSGUID *guid); =20 -int vhdx_update_headers(BlockDriverState *bs, BDRVVHDXState *s, bool rw, - MSGUID *log_guid); +int GRAPH_RDLOCK +vhdx_update_headers(BlockDriverState *bs, BDRVVHDXState *s, bool rw, + MSGUID *log_guid); =20 uint32_t vhdx_update_checksum(uint8_t *buf, size_t size, int crc_offset); uint32_t vhdx_checksum_calc(uint32_t crc, uint8_t *buf, size_t size, @@ -448,6 +449,8 @@ void vhdx_metadata_header_le_import(VHDXMetadataTableHe= ader *hdr); void vhdx_metadata_header_le_export(VHDXMetadataTableHeader *hdr); void vhdx_metadata_entry_le_import(VHDXMetadataTableEntry *e); void vhdx_metadata_entry_le_export(VHDXMetadataTableEntry *e); -int vhdx_user_visible_write(BlockDriverState *bs, BDRVVHDXState *s); + +int GRAPH_RDLOCK +vhdx_user_visible_write(BlockDriverState *bs, BDRVVHDXState *s); =20 #endif diff --git a/block/vhdx-log.c b/block/vhdx-log.c index d8ed651b70..4385a2d4f6 100644 --- a/block/vhdx-log.c +++ b/block/vhdx-log.c @@ -55,8 +55,9 @@ static const MSGUID zero_guid =3D { 0 }; =20 /* Allow peeking at the hdr entry at the beginning of the current * read index, without advancing the read index */ -static int vhdx_log_peek_hdr(BlockDriverState *bs, VHDXLogEntries *log, - VHDXLogEntryHeader *hdr) +static int GRAPH_RDLOCK +vhdx_log_peek_hdr(BlockDriverState *bs, VHDXLogEntries *log, + VHDXLogEntryHeader *hdr) { int ret =3D 0; uint64_t offset; @@ -107,7 +108,7 @@ static int vhdx_log_inc_idx(uint32_t idx, uint64_t leng= th) =20 =20 /* Reset the log to empty */ -static void vhdx_log_reset(BlockDriverState *bs, BDRVVHDXState *s) +static void GRAPH_RDLOCK vhdx_log_reset(BlockDriverState *bs, BDRVVHDXStat= e *s) { MSGUID guid =3D { 0 }; s->log.read =3D s->log.write =3D 0; @@ -127,9 +128,10 @@ static void vhdx_log_reset(BlockDriverState *bs, BDRVV= HDXState *s) * not modified. * * 0 is returned on success, -errno otherwise. */ -static int vhdx_log_read_sectors(BlockDriverState *bs, VHDXLogEntries *log, - uint32_t *sectors_read, void *buffer, - uint32_t num_sectors, bool peek) +static int GRAPH_RDLOCK +vhdx_log_read_sectors(BlockDriverState *bs, VHDXLogEntries *log, + uint32_t *sectors_read, void *buffer, + uint32_t num_sectors, bool peek) { int ret =3D 0; uint64_t offset; @@ -333,9 +335,9 @@ static int vhdx_compute_desc_sectors(uint32_t desc_cnt) * will allocate all the space for buffer, which must be NULL when * passed into this function. Each descriptor will also be validated, * and error returned if any are invalid. */ -static int vhdx_log_read_desc(BlockDriverState *bs, BDRVVHDXState *s, - VHDXLogEntries *log, VHDXLogDescEntries **bu= ffer, - bool convert_endian) +static int GRAPH_RDLOCK +vhdx_log_read_desc(BlockDriverState *bs, BDRVVHDXState *s, VHDXLogEntries = *log, + VHDXLogDescEntries **buffer, bool convert_endian) { int ret =3D 0; uint32_t desc_sectors; @@ -412,8 +414,9 @@ exit: * For a zero descriptor, it may describe multiple sectors to fill with ze= roes. * In this case, it should be noted that zeroes are written to disk, and t= he * image file is not extended as a sparse file. */ -static int vhdx_log_flush_desc(BlockDriverState *bs, VHDXLogDescriptor *de= sc, - VHDXLogDataSector *data) +static int GRAPH_RDLOCK +vhdx_log_flush_desc(BlockDriverState *bs, VHDXLogDescriptor *desc, + VHDXLogDataSector *data) { int ret =3D 0; uint64_t seq, file_offset; @@ -484,8 +487,8 @@ exit: * file, and then set the log to 'empty' status once complete. * * The log entries should be validate prior to flushing */ -static int vhdx_log_flush(BlockDriverState *bs, BDRVVHDXState *s, - VHDXLogSequence *logs) +static int GRAPH_RDLOCK +vhdx_log_flush(BlockDriverState *bs, BDRVVHDXState *s, VHDXLogSequence *lo= gs) { int ret =3D 0; int i; @@ -584,9 +587,10 @@ exit: return ret; } =20 -static int vhdx_validate_log_entry(BlockDriverState *bs, BDRVVHDXState *s, - VHDXLogEntries *log, uint64_t seq, - bool *valid, VHDXLogEntryHeader *entry) +static int GRAPH_RDLOCK +vhdx_validate_log_entry(BlockDriverState *bs, BDRVVHDXState *s, + VHDXLogEntries *log, uint64_t seq, + bool *valid, VHDXLogEntryHeader *entry) { int ret =3D 0; VHDXLogEntryHeader hdr; @@ -663,8 +667,8 @@ free_and_exit: /* Search through the log circular buffer, and find the valid, active * log sequence, if any exists * */ -static int vhdx_log_search(BlockDriverState *bs, BDRVVHDXState *s, - VHDXLogSequence *logs) +static int GRAPH_RDLOCK +vhdx_log_search(BlockDriverState *bs, BDRVVHDXState *s, VHDXLogSequence *l= ogs) { int ret =3D 0; uint32_t tail; diff --git a/block/vhdx.c b/block/vhdx.c index ac4d52e8c6..5aa1a13506 100644 --- a/block/vhdx.c +++ b/block/vhdx.c @@ -353,8 +353,9 @@ exit: * * - non-current header is updated with largest sequence number */ -static int vhdx_update_header(BlockDriverState *bs, BDRVVHDXState *s, - bool generate_data_write_guid, MSGUID *log_g= uid) +static int GRAPH_RDLOCK +vhdx_update_header(BlockDriverState *bs, BDRVVHDXState *s, + bool generate_data_write_guid, MSGUID *log_guid) { int ret =3D 0; int hdr_idx =3D 0; @@ -416,8 +417,8 @@ int vhdx_update_headers(BlockDriverState *bs, BDRVVHDXS= tate *s, } =20 /* opens the specified header block from the VHDX file header section */ -static void vhdx_parse_header(BlockDriverState *bs, BDRVVHDXState *s, - Error **errp) +static void GRAPH_RDLOCK +vhdx_parse_header(BlockDriverState *bs, BDRVVHDXState *s, Error **errp) { int ret; VHDXHeader *header1; @@ -517,7 +518,8 @@ exit: } =20 =20 -static int vhdx_open_region_tables(BlockDriverState *bs, BDRVVHDXState *s) +static int GRAPH_RDLOCK +vhdx_open_region_tables(BlockDriverState *bs, BDRVVHDXState *s) { int ret =3D 0; uint8_t *buffer; @@ -634,7 +636,8 @@ fail: * Also, if the File Parameters indicate this is a differencing file, * we must also look for the Parent Locator metadata item. */ -static int vhdx_parse_metadata(BlockDriverState *bs, BDRVVHDXState *s) +static int GRAPH_RDLOCK +vhdx_parse_metadata(BlockDriverState *bs, BDRVVHDXState *s) { int ret =3D 0; uint8_t *buffer; @@ -885,7 +888,8 @@ static void vhdx_calc_bat_entries(BDRVVHDXState *s) =20 } =20 -static int vhdx_check_bat_entries(BlockDriverState *bs, int *errcnt) +static int coroutine_mixed_fn GRAPH_RDLOCK +vhdx_check_bat_entries(BlockDriverState *bs, int *errcnt) { BDRVVHDXState *s =3D bs->opaque; int64_t image_file_size =3D bdrv_getlength(bs->file->bs); --=20 2.41.0 From nobody Wed Nov 27 09:52:08 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=1699382927; cv=none; d=zohomail.com; s=zohoarc; b=mNoURWwF2URDr6vO3us4lJhyGOhKhbEwaXm9fAs67GWAmiOHIGLUnqp4qSO8y7fu+L+B0bv9PB976w6OyiMq0ZLE1Ztwli334Vpt+kKfwpMWLEt9d4hn1SanXy74V0cDv8TBqWdJ3kSBGtjvnHICrUFc/sKycfmLUZqmk3ls4sA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699382927; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=cSMlr2c0p2zRk0oSfDiiW6SzlCn95BF8E0oL/jh5VI4=; b=l8LULHEqo6QK/pkIs22J6bTKOc3C6nco5KxZ1lQXbC2+39QG/mqx14j65j2JZIjgdDkjVYUU6vdj0x24BKkEOgQeUXA2yYmnbmm1aFJakNPYPYhMzUpgreusP6YxI4wZYmke7bW4qTDqnGzQJiZlCrb0xmit+vLyrf7fMVp22VQ= 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 1699382927143826.3039106642043; Tue, 7 Nov 2023 10:48:47 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0R6F-0004WU-PP; Tue, 07 Nov 2023 13:46:56 -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 1r0R5p-00041m-LP for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:31 -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 1r0R5n-0001Wv-9U for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:29 -0500 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-139-WF6yjdX6MP2QzbH0b-lesQ-1; Tue, 07 Nov 2023 13:46:25 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F161A1C068DD for ; Tue, 7 Nov 2023 18:46:24 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7BD35C1290F; Tue, 7 Nov 2023 18:46:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699382786; 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=cSMlr2c0p2zRk0oSfDiiW6SzlCn95BF8E0oL/jh5VI4=; b=JIEg2eI5ySaz/oOVdboAdjndg6SMig6aAG1r77cjKGC6QO+Cd0BgGT1Dc3sT2L6vZ3SIGU UO8Ka1mpRRpHF6ErU1I54kTo+s7QjTeByDvJD82Qd6UpujNqy3QpjBeLHNZiecpEPNz2ZP 1pVZ52W5vE+gPAVyD8ougcr6ALtFBCI= X-MC-Unique: WF6yjdX6MP2QzbH0b-lesQ-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 23/25] block: Take graph lock for most of .bdrv_open Date: Tue, 7 Nov 2023 19:46:03 +0100 Message-ID: <20231107184605.236540-24-kwolf@redhat.com> In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com> References: <20231107184605.236540-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1699382927406100003 Content-Type: text/plain; charset="utf-8" Most implementations of .bdrv_open first open their file child (which is an operation that internally takes the write lock and therefore we shouldn't hold the graph lock while calling it), and afterwards many operations that require holding the graph lock, e.g. for accessing bs->file. This changes block drivers that follow this pattern to take the graph lock after opening the child node. Signed-off-by: Kevin Wolf Message-ID: <20231027155333.420094-24-kwolf@redhat.com> Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- block/blkdebug.c | 16 ++++++++++------ block/bochs.c | 4 ++++ block/cloop.c | 4 ++++ block/copy-before-write.c | 2 ++ block/copy-on-read.c | 4 ++-- block/crypto.c | 4 ++++ block/dmg.c | 5 +++++ block/filter-compress.c | 2 ++ block/parallels.c | 4 ++-- block/preallocate.c | 4 ++++ block/qcow.c | 11 +++++++---- block/raw-format.c | 6 ++++-- block/snapshot-access.c | 3 +++ block/throttle.c | 3 +++ block/vdi.c | 4 ++-- block/vpc.c | 4 ++-- 16 files changed, 60 insertions(+), 20 deletions(-) diff --git a/block/blkdebug.c b/block/blkdebug.c index 230efa9e8d..9da8c9eddc 100644 --- a/block/blkdebug.c +++ b/block/blkdebug.c @@ -508,6 +508,8 @@ static int blkdebug_open(BlockDriverState *bs, QDict *o= ptions, int flags, goto out; } =20 + bdrv_graph_rdlock_main_loop(); + bs->supported_write_flags =3D BDRV_REQ_WRITE_UNCHANGED | (BDRV_REQ_FUA & bs->file->bs->supported_write_flags); bs->supported_zero_flags =3D BDRV_REQ_WRITE_UNCHANGED | @@ -520,7 +522,7 @@ static int blkdebug_open(BlockDriverState *bs, QDict *o= ptions, int flags, if (s->align && (s->align >=3D INT_MAX || !is_power_of_2(s->align))) { error_setg(errp, "Cannot meet constraints with align %" PRIu64, s->align); - goto out; + goto out_rdlock; } align =3D MAX(s->align, bs->file->bs->bl.request_alignment); =20 @@ -530,7 +532,7 @@ static int blkdebug_open(BlockDriverState *bs, QDict *o= ptions, int flags, !QEMU_IS_ALIGNED(s->max_transfer, align))) { error_setg(errp, "Cannot meet constraints with max-transfer %" PRI= u64, s->max_transfer); - goto out; + goto out_rdlock; } =20 s->opt_write_zero =3D qemu_opt_get_size(opts, "opt-write-zero", 0); @@ -539,7 +541,7 @@ static int blkdebug_open(BlockDriverState *bs, QDict *o= ptions, int flags, !QEMU_IS_ALIGNED(s->opt_write_zero, align))) { error_setg(errp, "Cannot meet constraints with opt-write-zero %" P= RIu64, s->opt_write_zero); - goto out; + goto out_rdlock; } =20 s->max_write_zero =3D qemu_opt_get_size(opts, "max-write-zero", 0); @@ -549,7 +551,7 @@ static int blkdebug_open(BlockDriverState *bs, QDict *o= ptions, int flags, MAX(s->opt_write_zero, align)))) { error_setg(errp, "Cannot meet constraints with max-write-zero %" P= RIu64, s->max_write_zero); - goto out; + goto out_rdlock; } =20 s->opt_discard =3D qemu_opt_get_size(opts, "opt-discard", 0); @@ -558,7 +560,7 @@ static int blkdebug_open(BlockDriverState *bs, QDict *o= ptions, int flags, !QEMU_IS_ALIGNED(s->opt_discard, align))) { error_setg(errp, "Cannot meet constraints with opt-discard %" PRIu= 64, s->opt_discard); - goto out; + goto out_rdlock; } =20 s->max_discard =3D qemu_opt_get_size(opts, "max-discard", 0); @@ -568,12 +570,14 @@ static int blkdebug_open(BlockDriverState *bs, QDict = *options, int flags, MAX(s->opt_discard, align)))) { error_setg(errp, "Cannot meet constraints with max-discard %" PRIu= 64, s->max_discard); - goto out; + goto out_rdlock; } =20 bdrv_debug_event(bs, BLKDBG_NONE); =20 ret =3D 0; +out_rdlock: + bdrv_graph_rdunlock_main_loop(); out: if (ret < 0) { qemu_mutex_destroy(&s->lock); diff --git a/block/bochs.c b/block/bochs.c index 8c659fa9b9..b099fb52fe 100644 --- a/block/bochs.c +++ b/block/bochs.c @@ -105,6 +105,8 @@ static int bochs_open(BlockDriverState *bs, QDict *opti= ons, int flags, struct bochs_header bochs; int ret; =20 + GLOBAL_STATE_CODE(); + /* No write support yet */ bdrv_graph_rdlock_main_loop(); ret =3D bdrv_apply_auto_read_only(bs, NULL, errp); @@ -118,6 +120,8 @@ static int bochs_open(BlockDriverState *bs, QDict *opti= ons, int flags, return ret; } =20 + GRAPH_RDLOCK_GUARD_MAINLOOP(); + ret =3D bdrv_pread(bs->file, 0, sizeof(bochs), &bochs, 0); if (ret < 0) { return ret; diff --git a/block/cloop.c b/block/cloop.c index 773d7918be..443af1444e 100644 --- a/block/cloop.c +++ b/block/cloop.c @@ -67,6 +67,8 @@ static int cloop_open(BlockDriverState *bs, QDict *option= s, int flags, uint32_t offsets_size, max_compressed_block_size =3D 1, i; int ret; =20 + GLOBAL_STATE_CODE(); + bdrv_graph_rdlock_main_loop(); ret =3D bdrv_apply_auto_read_only(bs, NULL, errp); bdrv_graph_rdunlock_main_loop(); @@ -79,6 +81,8 @@ static int cloop_open(BlockDriverState *bs, QDict *option= s, int flags, return ret; } =20 + GRAPH_RDLOCK_GUARD_MAINLOOP(); + /* read header */ ret =3D bdrv_pread(bs->file, 128, 4, &s->block_size, 0); if (ret < 0) { diff --git a/block/copy-before-write.c b/block/copy-before-write.c index 8193d3a4cd..ad3b73cc4a 100644 --- a/block/copy-before-write.c +++ b/block/copy-before-write.c @@ -433,6 +433,8 @@ static int cbw_open(BlockDriverState *bs, QDict *option= s, int flags, return -EINVAL; } =20 + GRAPH_RDLOCK_GUARD_MAINLOOP(); + ctx =3D bdrv_get_aio_context(bs); aio_context_acquire(ctx); =20 diff --git a/block/copy-on-read.c b/block/copy-on-read.c index 6f245b629a..c36f253d16 100644 --- a/block/copy-on-read.c +++ b/block/copy-on-read.c @@ -51,6 +51,8 @@ cor_open(BlockDriverState *bs, QDict *options, int flags,= Error **errp) return ret; } =20 + GRAPH_RDLOCK_GUARD_MAINLOOP(); + bs->supported_read_flags =3D BDRV_REQ_PREFETCH; =20 bs->supported_write_flags =3D BDRV_REQ_WRITE_UNCHANGED | @@ -61,8 +63,6 @@ cor_open(BlockDriverState *bs, QDict *options, int flags,= Error **errp) bs->file->bs->supported_zero_flags); =20 if (bottom_node) { - GRAPH_RDLOCK_GUARD_MAINLOOP(); - bottom_bs =3D bdrv_find_node(bottom_node); if (!bottom_bs) { error_setg(errp, "Bottom node '%s' not found", bottom_node); diff --git a/block/crypto.c b/block/crypto.c index b3f0233d53..6ee0cac4b6 100644 --- a/block/crypto.c +++ b/block/crypto.c @@ -263,11 +263,15 @@ static int block_crypto_open_generic(QCryptoBlockForm= at format, unsigned int cflags =3D 0; QDict *cryptoopts =3D NULL; =20 + GLOBAL_STATE_CODE(); + ret =3D bdrv_open_file_child(NULL, options, "file", bs, errp); if (ret < 0) { return ret; } =20 + GRAPH_RDLOCK_GUARD_MAINLOOP(); + bs->supported_write_flags =3D BDRV_REQ_FUA & bs->file->bs->supported_write_flags; =20 diff --git a/block/dmg.c b/block/dmg.c index 38ee72bbe5..853ad36a00 100644 --- a/block/dmg.c +++ b/block/dmg.c @@ -452,6 +452,8 @@ static int dmg_open(BlockDriverState *bs, QDict *option= s, int flags, int64_t offset; int ret; =20 + GLOBAL_STATE_CODE(); + bdrv_graph_rdlock_main_loop(); ret =3D bdrv_apply_auto_read_only(bs, NULL, errp); bdrv_graph_rdunlock_main_loop(); @@ -463,6 +465,9 @@ static int dmg_open(BlockDriverState *bs, QDict *option= s, int flags, if (ret < 0) { return ret; } + + GRAPH_RDLOCK_GUARD_MAINLOOP(); + /* * NB: if uncompress submodules are absent, * ie block_module_load return value =3D=3D 0, the function pointers diff --git a/block/filter-compress.c b/block/filter-compress.c index e3fc82f322..9b68a2be64 100644 --- a/block/filter-compress.c +++ b/block/filter-compress.c @@ -36,6 +36,8 @@ static int compress_open(BlockDriverState *bs, QDict *opt= ions, int flags, return ret; } =20 + GRAPH_RDLOCK_GUARD_MAINLOOP(); + if (!bs->file->bs->drv || !block_driver_can_compress(bs->file->bs->drv= )) { error_setg(errp, "Compression is not supported for underlying format: %s= ", diff --git a/block/parallels.c b/block/parallels.c index 9b9bd1f993..a04f836cc2 100644 --- a/block/parallels.c +++ b/block/parallels.c @@ -1255,6 +1255,8 @@ static int parallels_open(BlockDriverState *bs, QDict= *options, int flags, return ret; } =20 + GRAPH_RDLOCK_GUARD_MAINLOOP(); + file_nb_sectors =3D bdrv_nb_sectors(bs->file->bs); if (file_nb_sectors < 0) { return -EINVAL; @@ -1359,11 +1361,9 @@ static int parallels_open(BlockDriverState *bs, QDic= t *options, int flags, bitmap_new(DIV_ROUND_UP(s->header_size, s->bat_dirty_block)); =20 /* Disable migration until bdrv_activate method is added */ - bdrv_graph_rdlock_main_loop(); error_setg(&s->migration_blocker, "The Parallels format used by node '= %s' " "does not support live migration", bdrv_get_device_or_node_name(bs)); - bdrv_graph_rdunlock_main_loop(); =20 ret =3D migrate_add_blocker_normal(&s->migration_blocker, errp); if (ret < 0) { diff --git a/block/preallocate.c b/block/preallocate.c index 4e0c891ab2..f302d17d3f 100644 --- a/block/preallocate.c +++ b/block/preallocate.c @@ -143,6 +143,8 @@ static int preallocate_open(BlockDriverState *bs, QDict= *options, int flags, BDRVPreallocateState *s =3D bs->opaque; int ret; =20 + GLOBAL_STATE_CODE(); + /* * s->data_end and friends should be initialized on permission update. * For this to work, mark them invalid. @@ -155,6 +157,8 @@ static int preallocate_open(BlockDriverState *bs, QDict= *options, int flags, return ret; } =20 + GRAPH_RDLOCK_GUARD_MAINLOOP(); + if (!preallocate_absorb_opts(&s->opts, options, bs->file->bs, errp)) { return -EINVAL; } diff --git a/block/qcow.c b/block/qcow.c index 1e2835f1f4..c6d0e15f1e 100644 --- a/block/qcow.c +++ b/block/qcow.c @@ -124,9 +124,11 @@ static int qcow_open(BlockDriverState *bs, QDict *opti= ons, int flags, =20 ret =3D bdrv_open_file_child(NULL, options, "file", bs, errp); if (ret < 0) { - goto fail; + goto fail_unlocked; } =20 + bdrv_graph_rdlock_main_loop(); + ret =3D bdrv_pread(bs->file, 0, sizeof(header), &header, 0); if (ret < 0) { goto fail; @@ -301,11 +303,9 @@ static int qcow_open(BlockDriverState *bs, QDict *opti= ons, int flags, } =20 /* Disable migration when qcow images are used */ - bdrv_graph_rdlock_main_loop(); error_setg(&s->migration_blocker, "The qcow format used by node '%s' " "does not support live migration", bdrv_get_device_or_node_name(bs)); - bdrv_graph_rdunlock_main_loop(); =20 ret =3D migrate_add_blocker_normal(&s->migration_blocker, errp); if (ret < 0) { @@ -315,9 +315,12 @@ static int qcow_open(BlockDriverState *bs, QDict *opti= ons, int flags, qobject_unref(encryptopts); qapi_free_QCryptoBlockOpenOptions(crypto_opts); qemu_co_mutex_init(&s->lock); + bdrv_graph_rdunlock_main_loop(); return 0; =20 - fail: +fail: + bdrv_graph_rdunlock_main_loop(); +fail_unlocked: g_free(s->l1_table); qemu_vfree(s->l2_cache); g_free(s->cluster_cache); diff --git a/block/raw-format.c b/block/raw-format.c index 2640d54801..66741be954 100644 --- a/block/raw-format.c +++ b/block/raw-format.c @@ -473,6 +473,8 @@ static int raw_open(BlockDriverState *bs, QDict *option= s, int flags, BdrvChildRole file_role; int ret; =20 + GLOBAL_STATE_CODE(); + ret =3D raw_read_options(options, &offset, &has_size, &size, errp); if (ret < 0) { return ret; @@ -490,6 +492,8 @@ static int raw_open(BlockDriverState *bs, QDict *option= s, int flags, =20 bdrv_open_child(NULL, options, "file", bs, &child_of_bds, file_role, false, errp); + + GRAPH_RDLOCK_GUARD_MAINLOOP(); if (!bs->file) { return -EINVAL; } @@ -504,9 +508,7 @@ static int raw_open(BlockDriverState *bs, QDict *option= s, int flags, BDRV_REQ_ZERO_WRITE; =20 if (bs->probed && !bdrv_is_read_only(bs)) { - bdrv_graph_rdlock_main_loop(); bdrv_refresh_filename(bs->file->bs); - bdrv_graph_rdunlock_main_loop(); fprintf(stderr, "WARNING: Image format was not specified for '%s' and prob= ing " "guessed raw.\n" diff --git a/block/snapshot-access.c b/block/snapshot-access.c index 7c45739eb1..84d0d13f86 100644 --- a/block/snapshot-access.c +++ b/block/snapshot-access.c @@ -85,6 +85,9 @@ static int snapshot_access_open(BlockDriverState *bs, QDi= ct *options, int flags, bdrv_open_child(NULL, options, "file", bs, &child_of_bds, BDRV_CHILD_DATA | BDRV_CHILD_PRIMARY, false, errp); + + GRAPH_RDLOCK_GUARD_MAINLOOP(); + if (!bs->file) { return -EINVAL; } diff --git a/block/throttle.c b/block/throttle.c index 1098a4ae9a..97972d1f15 100644 --- a/block/throttle.c +++ b/block/throttle.c @@ -84,6 +84,9 @@ static int throttle_open(BlockDriverState *bs, QDict *opt= ions, if (ret < 0) { return ret; } + + GRAPH_RDLOCK_GUARD_MAINLOOP(); + bs->supported_write_flags =3D bs->file->bs->supported_write_flags | BDRV_REQ_WRITE_UNCHANGED; bs->supported_zero_flags =3D bs->file->bs->supported_zero_flags | diff --git a/block/vdi.c b/block/vdi.c index 84150180ec..3b57becb9f 100644 --- a/block/vdi.c +++ b/block/vdi.c @@ -383,6 +383,8 @@ static int vdi_open(BlockDriverState *bs, QDict *option= s, int flags, return ret; } =20 + GRAPH_RDLOCK_GUARD_MAINLOOP(); + logout("\n"); =20 ret =3D bdrv_pread(bs->file, 0, sizeof(header), &header, 0); @@ -492,11 +494,9 @@ static int vdi_open(BlockDriverState *bs, QDict *optio= ns, int flags, } =20 /* Disable migration when vdi images are used */ - bdrv_graph_rdlock_main_loop(); error_setg(&s->migration_blocker, "The vdi format used by node '%s' " "does not support live migration", bdrv_get_device_or_node_name(bs)); - bdrv_graph_rdunlock_main_loop(); =20 ret =3D migrate_add_blocker_normal(&s->migration_blocker, errp); if (ret < 0) { diff --git a/block/vpc.c b/block/vpc.c index 483775103c..d95a204612 100644 --- a/block/vpc.c +++ b/block/vpc.c @@ -238,6 +238,8 @@ static int vpc_open(BlockDriverState *bs, QDict *option= s, int flags, return ret; } =20 + GRAPH_RDLOCK_GUARD_MAINLOOP(); + opts =3D qemu_opts_create(&vpc_runtime_opts, NULL, 0, &error_abort); if (!qemu_opts_absorb_qdict(opts, options, errp)) { ret =3D -EINVAL; @@ -446,11 +448,9 @@ static int vpc_open(BlockDriverState *bs, QDict *optio= ns, int flags, } =20 /* Disable migration when VHD images are used */ - bdrv_graph_rdlock_main_loop(); error_setg(&s->migration_blocker, "The vpc format used by node '%s' " "does not support live migration", bdrv_get_device_or_node_name(bs)); - bdrv_graph_rdunlock_main_loop(); =20 ret =3D migrate_add_blocker_normal(&s->migration_blocker, errp); if (ret < 0) { --=20 2.41.0 From nobody Wed Nov 27 09:52:08 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=1699382840; cv=none; d=zohomail.com; s=zohoarc; b=ZWPWzkF4JrPzMhFmxR+sCBqtDXliFPAGnbNOZ7piF08wJzgSTIRnau8xfzmy83qdzImgzT9yvVraQXgV8jI8ePuKeH7o1iOT+36ZJvaWqoYcDzT+kCf1VGhc45dpz4ChQdgI6Qc14Hvj8AjrcfLOouKbofgWpJyhJoX4rB0ikns= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699382840; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=cT14BSQd1gW1kW2hRnWi57CSh+BfdQ7NnNQ/i3wEQmc=; b=TsETc71Lzbl3qqeKwY5rIV+Kmr5UwoVVFnQx4a7tucyWcPiCpIzBqjFf4zFsaJ5PCrjsEPWqdLzG9ZznQWzK98OBMLOElMhij+8fqa5OEcEc3gZFpzx5LLXrya1LmBPZfF6CJmQqZ7E8UE8w5z1NkJo88ReMqxHNgAeOdPZt7zQ= 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 1699382840863440.98226457193925; Tue, 7 Nov 2023 10:47:20 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0R69-0004N0-Rk; Tue, 07 Nov 2023 13:46:49 -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 1r0R5r-00045d-RP for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:34 -0500 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 1r0R5o-0001XI-Rv for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:31 -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.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-378-MNSIhuw4NReLM3sE3venVA-1; Tue, 07 Nov 2023 13:46:26 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A9BE0185A789 for ; Tue, 7 Nov 2023 18:46:25 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id 342B6C12911; Tue, 7 Nov 2023 18:46:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699382787; 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=cT14BSQd1gW1kW2hRnWi57CSh+BfdQ7NnNQ/i3wEQmc=; b=Myal/J+/uCjvVvtbZTUZpkKaa/TYhGz78MEO+6ojYNAKDi5fYNPvARiGm/4jhAOcqnLdQc OvET1ki/vqIw/uUb1LoDVUU9H1JyyP53a5epRLJq+eHUaqno2Sc2gcdT9qFdyy+YCUZ6zR 1+F/Ii7No/5oUHj+gQMyCCYctpZFXZ0= X-MC-Unique: MNSIhuw4NReLM3sE3venVA-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 24/25] block: Protect bs->file with graph_lock Date: Tue, 7 Nov 2023 19:46:04 +0100 Message-ID: <20231107184605.236540-25-kwolf@redhat.com> In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com> References: <20231107184605.236540-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1699382843129100003 Content-Type: text/plain; charset="utf-8" Almost all functions that access bs->file already take the graph lock now. Add locking to the remaining users and finally annotate the struct field itself as protected by the graph lock. Signed-off-by: Kevin Wolf Message-ID: <20231027155333.420094-25-kwolf@redhat.com> Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- block/parallels.h | 5 +++-- block/qed.h | 2 +- include/block/block_int-common.h | 2 +- block.c | 11 ++++++++--- block/blkreplay.c | 8 +++++++- block/copy-before-write.c | 2 +- block/crypto.c | 6 ++++++ block/dmg.c | 16 ++++++++++------ block/parallels-ext.c | 21 ++++++++++----------- block/parallels.c | 6 ++++-- block/preallocate.c | 19 ++++++++++++++----- block/qed.c | 12 ++++++++++-- block/raw-format.c | 9 ++++++--- block/replication.c | 5 ++++- block/vmdk.c | 14 ++++++++++++-- 15 files changed, 97 insertions(+), 41 deletions(-) diff --git a/block/parallels.h b/block/parallels.h index 6b199443cf..423b2ad727 100644 --- a/block/parallels.h +++ b/block/parallels.h @@ -90,7 +90,8 @@ typedef struct BDRVParallelsState { Error *migration_blocker; } BDRVParallelsState; =20 -int parallels_read_format_extension(BlockDriverState *bs, - int64_t ext_off, Error **errp); +int GRAPH_RDLOCK +parallels_read_format_extension(BlockDriverState *bs, int64_t ext_off, + Error **errp); =20 #endif diff --git a/block/qed.h b/block/qed.h index 988654cb86..26d4bf038c 100644 --- a/block/qed.h +++ b/block/qed.h @@ -185,7 +185,7 @@ enum { /** * Header functions */ -int qed_write_header_sync(BDRVQEDState *s); +int GRAPH_RDLOCK qed_write_header_sync(BDRVQEDState *s); =20 /** * L2 cache functions diff --git a/include/block/block_int-common.h b/include/block/block_int-com= mon.h index 63bc523d7c..4e31d161c5 100644 --- a/include/block/block_int-common.h +++ b/include/block/block_int-common.h @@ -1181,7 +1181,7 @@ struct BlockDriverState { */ QLIST_HEAD(, BdrvChild GRAPH_RDLOCK_PTR) children; BdrvChild * GRAPH_RDLOCK_PTR backing; - BdrvChild *file; + BdrvChild * GRAPH_RDLOCK_PTR file; =20 QLIST_HEAD(, BdrvChild GRAPH_RDLOCK_PTR) parents; =20 diff --git a/block.c b/block.c index 4910b95d7d..eac105a504 100644 --- a/block.c +++ b/block.c @@ -1707,12 +1707,14 @@ bdrv_open_driver(BlockDriverState *bs, BlockDriver = *drv, const char *node_name, return 0; open_failed: bs->drv =3D NULL; + + bdrv_graph_wrlock(NULL); if (bs->file !=3D NULL) { - bdrv_graph_wrlock(NULL); bdrv_unref_child(bs, bs->file); - bdrv_graph_wrunlock(); assert(!bs->file); } + bdrv_graph_wrunlock(); + g_free(bs->opaque); bs->opaque =3D NULL; return ret; @@ -1854,9 +1856,12 @@ static int bdrv_open_common(BlockDriverState *bs, Bl= ockBackend *file, Error *local_err =3D NULL; bool ro; =20 + GLOBAL_STATE_CODE(); + + bdrv_graph_rdlock_main_loop(); assert(bs->file =3D=3D NULL); assert(options !=3D NULL && bs->options !=3D options); - GLOBAL_STATE_CODE(); + bdrv_graph_rdunlock_main_loop(); =20 opts =3D qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort); if (!qemu_opts_absorb_qdict(opts, options, errp)) { diff --git a/block/blkreplay.c b/block/blkreplay.c index 04f53eea41..792d980aa9 100644 --- a/block/blkreplay.c +++ b/block/blkreplay.c @@ -130,7 +130,13 @@ static int coroutine_fn GRAPH_RDLOCK blkreplay_co_flus= h(BlockDriverState *bs) static int blkreplay_snapshot_goto(BlockDriverState *bs, const char *snapshot_id) { - return bdrv_snapshot_goto(bs->file->bs, snapshot_id, NULL); + BlockDriverState *file_bs; + + bdrv_graph_rdlock_main_loop(); + file_bs =3D bs->file->bs; + bdrv_graph_rdunlock_main_loop(); + + return bdrv_snapshot_goto(file_bs, snapshot_id, NULL); } =20 static BlockDriver bdrv_blkreplay =3D { diff --git a/block/copy-before-write.c b/block/copy-before-write.c index ad3b73cc4a..13972879b1 100644 --- a/block/copy-before-write.c +++ b/block/copy-before-write.c @@ -203,7 +203,7 @@ static int coroutine_fn GRAPH_RDLOCK cbw_co_flush(Block= DriverState *bs) * It's guaranteed that guest writes will not interact in the region until * cbw_snapshot_read_unlock() called. */ -static coroutine_fn BlockReq * +static BlockReq * coroutine_fn GRAPH_RDLOCK cbw_snapshot_read_lock(BlockDriverState *bs, int64_t offset, int64_t bytes, int64_t *pnum, BdrvChild **file) { diff --git a/block/crypto.c b/block/crypto.c index 6ee0cac4b6..921933a5e5 100644 --- a/block/crypto.c +++ b/block/crypto.c @@ -65,6 +65,9 @@ static int block_crypto_read_func(QCryptoBlock *block, BlockDriverState *bs =3D opaque; ssize_t ret; =20 + GLOBAL_STATE_CODE(); + GRAPH_RDLOCK_GUARD_MAINLOOP(); + ret =3D bdrv_pread(bs->file, offset, buflen, buf, 0); if (ret < 0) { error_setg_errno(errp, -ret, "Could not read encryption header"); @@ -83,6 +86,9 @@ static int block_crypto_write_func(QCryptoBlock *block, BlockDriverState *bs =3D opaque; ssize_t ret; =20 + GLOBAL_STATE_CODE(); + GRAPH_RDLOCK_GUARD_MAINLOOP(); + ret =3D bdrv_pwrite(bs->file, offset, buflen, buf, 0); if (ret < 0) { error_setg_errno(errp, -ret, "Could not write encryption header"); diff --git a/block/dmg.c b/block/dmg.c index 853ad36a00..33dcb3a349 100644 --- a/block/dmg.c +++ b/block/dmg.c @@ -70,7 +70,8 @@ static int dmg_probe(const uint8_t *buf, int buf_size, co= nst char *filename) return 0; } =20 -static int read_uint64(BlockDriverState *bs, int64_t offset, uint64_t *res= ult) +static int GRAPH_RDLOCK +read_uint64(BlockDriverState *bs, int64_t offset, uint64_t *result) { uint64_t buffer; int ret; @@ -84,7 +85,8 @@ static int read_uint64(BlockDriverState *bs, int64_t offs= et, uint64_t *result) return 0; } =20 -static int read_uint32(BlockDriverState *bs, int64_t offset, uint32_t *res= ult) +static int GRAPH_RDLOCK +read_uint32(BlockDriverState *bs, int64_t offset, uint32_t *result) { uint32_t buffer; int ret; @@ -321,8 +323,9 @@ fail: return ret; } =20 -static int dmg_read_resource_fork(BlockDriverState *bs, DmgHeaderState *ds, - uint64_t info_begin, uint64_t info_lengt= h) +static int GRAPH_RDLOCK +dmg_read_resource_fork(BlockDriverState *bs, DmgHeaderState *ds, + uint64_t info_begin, uint64_t info_length) { BDRVDMGState *s =3D bs->opaque; int ret; @@ -388,8 +391,9 @@ fail: return ret; } =20 -static int dmg_read_plist_xml(BlockDriverState *bs, DmgHeaderState *ds, - uint64_t info_begin, uint64_t info_length) +static int GRAPH_RDLOCK +dmg_read_plist_xml(BlockDriverState *bs, DmgHeaderState *ds, + uint64_t info_begin, uint64_t info_length) { BDRVDMGState *s =3D bs->opaque; int ret; diff --git a/block/parallels-ext.c b/block/parallels-ext.c index 4d8ecf5047..b4e14c88f2 100644 --- a/block/parallels-ext.c +++ b/block/parallels-ext.c @@ -59,11 +59,10 @@ typedef struct ParallelsDirtyBitmapFeature { } QEMU_PACKED ParallelsDirtyBitmapFeature; =20 /* Given L1 table read bitmap data from the image and populate @bitmap */ -static int parallels_load_bitmap_data(BlockDriverState *bs, - const uint64_t *l1_table, - uint32_t l1_size, - BdrvDirtyBitmap *bitmap, - Error **errp) +static int GRAPH_RDLOCK +parallels_load_bitmap_data(BlockDriverState *bs, const uint64_t *l1_table, + uint32_t l1_size, BdrvDirtyBitmap *bitmap, + Error **errp) { BDRVParallelsState *s =3D bs->opaque; int ret =3D 0; @@ -120,10 +119,9 @@ finish: * @data buffer (of @data_size size) is the Dirty bitmaps feature which * consists of ParallelsDirtyBitmapFeature followed by L1 table. */ -static BdrvDirtyBitmap *parallels_load_bitmap(BlockDriverState *bs, - uint8_t *data, - size_t data_size, - Error **errp) +static BdrvDirtyBitmap * GRAPH_RDLOCK +parallels_load_bitmap(BlockDriverState *bs, uint8_t *data, size_t data_siz= e, + Error **errp) { int ret; ParallelsDirtyBitmapFeature bf; @@ -183,8 +181,9 @@ static BdrvDirtyBitmap *parallels_load_bitmap(BlockDriv= erState *bs, return bitmap; } =20 -static int parallels_parse_format_extension(BlockDriverState *bs, - uint8_t *ext_cluster, Error **= errp) +static int GRAPH_RDLOCK +parallels_parse_format_extension(BlockDriverState *bs, uint8_t *ext_cluste= r, + Error **errp) { BDRVParallelsState *s =3D bs->opaque; int ret; diff --git a/block/parallels.c b/block/parallels.c index a04f836cc2..9205a0864f 100644 --- a/block/parallels.c +++ b/block/parallels.c @@ -200,7 +200,7 @@ static int mark_used(BlockDriverState *bs, unsigned lon= g *bitmap, * bitmap anyway, as much as we can. This information will be used for * error resolution. */ -static int parallels_fill_used_bitmap(BlockDriverState *bs) +static int GRAPH_RDLOCK parallels_fill_used_bitmap(BlockDriverState *bs) { BDRVParallelsState *s =3D bs->opaque; int64_t payload_bytes; @@ -1185,7 +1185,7 @@ static int parallels_probe(const uint8_t *buf, int bu= f_size, return 0; } =20 -static int parallels_update_header(BlockDriverState *bs) +static int GRAPH_RDLOCK parallels_update_header(BlockDriverState *bs) { BDRVParallelsState *s =3D bs->opaque; unsigned size =3D MAX(bdrv_opt_mem_align(bs->file->bs), @@ -1428,6 +1428,8 @@ static void parallels_close(BlockDriverState *bs) { BDRVParallelsState *s =3D bs->opaque; =20 + GRAPH_RDLOCK_GUARD_MAINLOOP(); + if ((bs->open_flags & BDRV_O_RDWR) && !(bs->open_flags & BDRV_O_INACTI= VE)) { s->header->inuse =3D 0; parallels_update_header(bs); diff --git a/block/preallocate.c b/block/preallocate.c index f302d17d3f..d215bc5d6d 100644 --- a/block/preallocate.c +++ b/block/preallocate.c @@ -173,7 +173,8 @@ static int preallocate_open(BlockDriverState *bs, QDict= *options, int flags, return 0; } =20 -static int preallocate_truncate_to_real_size(BlockDriverState *bs, Error *= *errp) +static int GRAPH_RDLOCK +preallocate_truncate_to_real_size(BlockDriverState *bs, Error **errp) { BDRVPreallocateState *s =3D bs->opaque; int ret; @@ -204,6 +205,9 @@ static void preallocate_close(BlockDriverState *bs) { BDRVPreallocateState *s =3D bs->opaque; =20 + GLOBAL_STATE_CODE(); + GRAPH_RDLOCK_GUARD_MAINLOOP(); + qemu_bh_cancel(s->drop_resize_bh); qemu_bh_delete(s->drop_resize_bh); =20 @@ -227,6 +231,9 @@ static int preallocate_reopen_prepare(BDRVReopenState *= reopen_state, PreallocateOpts *opts =3D g_new0(PreallocateOpts, 1); int ret; =20 + GLOBAL_STATE_CODE(); + GRAPH_RDLOCK_GUARD_MAINLOOP(); + if (!preallocate_absorb_opts(opts, reopen_state->options, reopen_state->bs->file->bs, errp)) { g_free(opts); @@ -287,7 +294,7 @@ static bool can_write_resize(uint64_t perm) return (perm & BLK_PERM_WRITE) && (perm & BLK_PERM_RESIZE); } =20 -static bool has_prealloc_perms(BlockDriverState *bs) +static bool GRAPH_RDLOCK has_prealloc_perms(BlockDriverState *bs) { BDRVPreallocateState *s =3D bs->opaque; =20 @@ -503,7 +510,8 @@ preallocate_co_getlength(BlockDriverState *bs) return ret; } =20 -static int preallocate_drop_resize(BlockDriverState *bs, Error **errp) +static int GRAPH_RDLOCK +preallocate_drop_resize(BlockDriverState *bs, Error **errp) { BDRVPreallocateState *s =3D bs->opaque; int ret; @@ -529,15 +537,16 @@ static int preallocate_drop_resize(BlockDriverState *= bs, Error **errp) */ s->data_end =3D s->file_end =3D s->zero_start =3D -EINVAL; =20 - bdrv_graph_rdlock_main_loop(); bdrv_child_refresh_perms(bs, bs->file, NULL); - bdrv_graph_rdunlock_main_loop(); =20 return 0; } =20 static void preallocate_drop_resize_bh(void *opaque) { + GLOBAL_STATE_CODE(); + GRAPH_RDLOCK_GUARD_MAINLOOP(); + /* * In case of errors, we'll simply keep the exclusive lock on the image * indefinitely. diff --git a/block/qed.c b/block/qed.c index f4c1628a81..bc2f0a61c0 100644 --- a/block/qed.c +++ b/block/qed.c @@ -612,7 +612,7 @@ static int bdrv_qed_reopen_prepare(BDRVReopenState *sta= te, return 0; } =20 -static void bdrv_qed_close(BlockDriverState *bs) +static void GRAPH_RDLOCK bdrv_qed_do_close(BlockDriverState *bs) { BDRVQEDState *s =3D bs->opaque; =20 @@ -631,6 +631,14 @@ static void bdrv_qed_close(BlockDriverState *bs) qemu_vfree(s->l1_table); } =20 +static void GRAPH_UNLOCKED bdrv_qed_close(BlockDriverState *bs) +{ + GLOBAL_STATE_CODE(); + GRAPH_RDLOCK_GUARD_MAINLOOP(); + + bdrv_qed_do_close(bs); +} + static int coroutine_fn GRAPH_UNLOCKED bdrv_qed_co_create(BlockdevCreateOptions *opts, Error **errp) { @@ -1574,7 +1582,7 @@ bdrv_qed_co_invalidate_cache(BlockDriverState *bs, Er= ror **errp) BDRVQEDState *s =3D bs->opaque; int ret; =20 - bdrv_qed_close(bs); + bdrv_qed_do_close(bs); =20 bdrv_qed_init_state(bs); qemu_co_mutex_lock(&s->table_lock); diff --git a/block/raw-format.c b/block/raw-format.c index 66741be954..1111dffd54 100644 --- a/block/raw-format.c +++ b/block/raw-format.c @@ -95,9 +95,9 @@ end: return ret; } =20 -static int raw_apply_options(BlockDriverState *bs, BDRVRawState *s, - uint64_t offset, bool has_size, uint64_t size, - Error **errp) +static int GRAPH_RDLOCK +raw_apply_options(BlockDriverState *bs, BDRVRawState *s, uint64_t offset, + bool has_size, uint64_t size, Error **errp) { int64_t real_size =3D 0; =20 @@ -145,6 +145,9 @@ static int raw_reopen_prepare(BDRVReopenState *reopen_s= tate, uint64_t offset, size; int ret; =20 + GLOBAL_STATE_CODE(); + GRAPH_RDLOCK_GUARD_MAINLOOP(); + assert(reopen_state !=3D NULL); assert(reopen_state->bs !=3D NULL); =20 diff --git a/block/replication.c b/block/replication.c index 49ecc608b2..43e259444b 100644 --- a/block/replication.c +++ b/block/replication.c @@ -311,7 +311,7 @@ static void GRAPH_UNLOCKED secondary_do_checkpoint(BlockDriverState *bs, Error **errp) { BDRVReplicationState *s =3D bs->opaque; - BdrvChild *active_disk =3D bs->file; + BdrvChild *active_disk; Error *local_err =3D NULL; int ret; =20 @@ -328,6 +328,7 @@ secondary_do_checkpoint(BlockDriverState *bs, Error **e= rrp) return; } =20 + active_disk =3D bs->file; if (!active_disk->bs->drv) { error_setg(errp, "Active disk %s is ejected", active_disk->bs->node_name); @@ -755,11 +756,13 @@ static void replication_stop(ReplicationState *rs, bo= ol failover, Error **errp) return; } =20 + bdrv_graph_rdlock_main_loop(); s->stage =3D BLOCK_REPLICATION_FAILOVER; s->commit_job =3D commit_active_start( NULL, bs->file->bs, s->secondary_disk->bs, JOB_INTERNAL, 0, BLOCKDEV_ON_ERROR_REPORT, NULL, replication_done, bs, true, errp); + bdrv_graph_rdunlock_main_loop(); break; default: aio_context_release(aio_context); diff --git a/block/vmdk.c b/block/vmdk.c index 5c789bb65b..dda783f06b 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -300,7 +300,8 @@ static void vmdk_free_last_extent(BlockDriverState *bs) } =20 /* Return -ve errno, or 0 on success and write CID into *pcid. */ -static int vmdk_read_cid(BlockDriverState *bs, int parent, uint32_t *pcid) +static int GRAPH_RDLOCK +vmdk_read_cid(BlockDriverState *bs, int parent, uint32_t *pcid) { char *desc; uint32_t cid; @@ -415,6 +416,9 @@ static int vmdk_reopen_prepare(BDRVReopenState *state, BDRVVmdkReopenState *rs; int i; =20 + GLOBAL_STATE_CODE(); + GRAPH_RDLOCK_GUARD_MAINLOOP(); + assert(state !=3D NULL); assert(state->bs !=3D NULL); assert(state->opaque =3D=3D NULL); @@ -451,6 +455,9 @@ static void vmdk_reopen_commit(BDRVReopenState *state) BDRVVmdkReopenState *rs =3D state->opaque; int i; =20 + GLOBAL_STATE_CODE(); + GRAPH_RDLOCK_GUARD_MAINLOOP(); + for (i =3D 0; i < s->num_extents; i++) { if (rs->extents_using_bs_file[i]) { s->extents[i].file =3D state->bs->file; @@ -465,7 +472,7 @@ static void vmdk_reopen_abort(BDRVReopenState *state) vmdk_reopen_clean(state); } =20 -static int vmdk_parent_open(BlockDriverState *bs) +static int GRAPH_RDLOCK vmdk_parent_open(BlockDriverState *bs) { char *p_name; char *desc; @@ -2547,7 +2554,10 @@ vmdk_co_do_create(int64_t size, ret =3D -EINVAL; goto exit; } + + bdrv_graph_co_rdlock(); ret =3D vmdk_read_cid(blk_bs(backing), 0, &parent_cid); + bdrv_graph_co_rdunlock(); blk_co_unref(backing); if (ret) { error_setg(errp, "Failed to read parent CID"); --=20 2.41.0 From nobody Wed Nov 27 09:52:08 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=1699383040; cv=none; d=zohomail.com; s=zohoarc; b=AESkcu1D1+OdQCvleNHXLRQkFNta1JzlD5F3q8yMPY/NXcda5+mhxjicPRcVpaxbNe1i8dIH6zs4CmkfFYv8O8VpK20ZXH8Xuab5KubUQm+gxuHOonpawirwo/ayJ/PpafM2/nB4Mp+3zVtVrA/tt9mKxgk5SIwBSzxyyLP0plM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699383040; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=FwHUC7S3Z5nYb5gUNIYR3N7FkjaPlVamBaOvDLLfJ6w=; b=Uj21uhLQ3z6ZvnCCjClTx5sHzPlxynS+7tbQ722xmCqlV61MdXy5FcL/faDi31pkw5hnKlEYQPYFKn5qvPvGgbY3nMclReZ1pXVtLjvcOVnMSyhZhnf3xa1wp1zlc2W0mGdeRie1+PIL0FfKK4uGQdnQ2Z2oEhGWFsC2ueqIOl4= 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 1699383040792513.7205056381201; Tue, 7 Nov 2023 10:50:40 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r0R6N-0004cH-4E; Tue, 07 Nov 2023 13:47:03 -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 1r0R5u-00046E-Ft for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:36 -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 1r0R5s-0001Y5-Tk for qemu-devel@nongnu.org; Tue, 07 Nov 2023 13:46:34 -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.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-481-zOuXovR6Ow6sJ_3LN3Vd0Q-1; Tue, 07 Nov 2023 13:46:26 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 613FE83BA87 for ; Tue, 7 Nov 2023 18:46:26 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id E10E3C1290F; Tue, 7 Nov 2023 18:46:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699382791; 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: in-reply-to:in-reply-to:references:references; bh=FwHUC7S3Z5nYb5gUNIYR3N7FkjaPlVamBaOvDLLfJ6w=; b=ho6Ce3raTg1H4NqpJvlhgzc0VM2Wr7AQUFmSfnzKcUskcmHf4RAMFkhdFbZKAmP2UutE4w 9f7/kSvlLDobwZS48ADIirKRbW6mhdxgOaDAUblp5oqVzEzZ6stggRGOTXrfw89MlVqYus ZNbGWsHB1qHPNu9cdJdnoFGcvEnQiqg= X-MC-Unique: zOuXovR6Ow6sJ_3LN3Vd0Q-1 From: Kevin Wolf To: qemu-devel@nongnu.org Cc: kwolf@redhat.com Subject: [PULL 25/25] hw/ide/ahci: trigger either error IRQ or regular IRQ, not both Date: Tue, 7 Nov 2023 19:46:05 +0100 Message-ID: <20231107184605.236540-26-kwolf@redhat.com> In-Reply-To: <20231107184605.236540-1-kwolf@redhat.com> References: <20231107184605.236540-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.4.1 on 10.11.54.8 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_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1699383041850100003 From: Niklas Cassel According to AHCI 1.3.1, 5.3.8.1 RegFIS:Entry, if ERR_STAT is set, we jump to state ERR:FatalTaskfile, which will raise a TFES IRQ unconditionally, regardless if the I bit is set in the FIS or not. Thus, we should never raise a normal IRQ after having sent an error IRQ. NOTE: for QEMU platforms that use SeaBIOS, this patch depends on QEMU commit 784155cdcb02 ("seabios: update submodule to git snapshot"), and QEMU commit 14f5a7bae4cb ("seabios: update binaries to git snapshot"), which update SeaBIOS to a version that contains SeaBIOS commit 1281e340 ("ahci: handle TFES irq correctly"). Signed-off-by: Niklas Cassel Message-ID: <20231011131220.1992064-1-nks@flawful.org> Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Kevin Wolf Signed-off-by: Kevin Wolf --- hw/ide/ahci.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c index fcc5476e9e..7676e2d871 100644 --- a/hw/ide/ahci.c +++ b/hw/ide/ahci.c @@ -897,11 +897,10 @@ static bool ahci_write_fis_d2h(AHCIDevice *ad, bool d= 2h_fis_i) pr->tfdata =3D (ad->port.ifs[0].error << 8) | ad->port.ifs[0].status; =20 + /* TFES IRQ is always raised if ERR_STAT is set, regardless of I bit. = */ if (d2h_fis[2] & ERR_STAT) { ahci_trigger_irq(ad->hba, ad, AHCI_PORT_IRQ_BIT_TFES); - } - - if (d2h_fis_i) { + } else if (d2h_fis_i) { ahci_trigger_irq(ad->hba, ad, AHCI_PORT_IRQ_BIT_DHRS); } =20 --=20 2.41.0