From nobody Fri May 17 12:14:57 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=1676395030; cv=none; d=zohomail.com; s=zohoarc; b=WrqWxCnzEm8IhmBqBaiXt3/B5uva5drr83tUZCufFvGZX232AkmnzO5LgEnG8muF3u/+rcpFKi+QwT0quy0A9y/bzEFoqcUxh8jlVHxo+J/Jh+E9gGYHFtUY+NJRiM3y4j9h34uQ+xzrK3liSU4xxEB3876UJsXq7HJK7tPSmv0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1676395030; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=+xDnfroDKrE7MZIL3tNRjkakvJkhUC6sm9BW0qIJtbA=; b=IM8sR4+FU6rot8EMeI4yNjGmyOrNVQ4P3PofExF94mQc31QFJr4s9vCzgQo9dChrkdUwZoyRHp41e0GHDQzbwBqnPV8iSK2gNaeb5+0EEJqrkQq2iQWG53W38GSh51S7bDG80T1ylz68f4FD7V65KBrxVRLjmpItH2ZgZ+ioMRE= 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 1676395030062891.7895103058198; Tue, 14 Feb 2023 09:17:10 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pRyv4-0000kl-1B; Tue, 14 Feb 2023 12:16:42 -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 1pRyv2-0000kV-Bx for qemu-devel@nongnu.org; Tue, 14 Feb 2023 12:16:40 -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 1pRyuy-0004Ut-FT for qemu-devel@nongnu.org; Tue, 14 Feb 2023 12:16:38 -0500 Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-317-NvGvJLidMO6JFUjmaS9dDg-1; Tue, 14 Feb 2023 12:16:28 -0500 Received: by mail-qv1-f71.google.com with SMTP id jh2-20020a0562141fc200b004c74bbb0affso9110753qvb.21 for ; Tue, 14 Feb 2023 09:16:28 -0800 (PST) Received: from step1.redhat.com (host-79-46-200-90.retail.telecomitalia.it. [79.46.200.90]) by smtp.gmail.com with ESMTPSA id 3-20020ac85743000000b003b86d3ca969sm5432104qtx.51.2023.02.14.09.16.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Feb 2023 09:16:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676394995; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=+xDnfroDKrE7MZIL3tNRjkakvJkhUC6sm9BW0qIJtbA=; b=RbVpZVAqy0sCtUD+vuUVmsv+bOkkNX+94K0b2rP7VlHr490HxVvo6U3LTES3x5K3YlVe0o fiAX9EkDi+YhYSDkiXiRacW0KDcQj3N+kUiigRMr8D65bgojamR1SYrim75t1mMP2WKU6r 7a26GjutrTWEpvdlKH/lIAOLUzzddgk= X-MC-Unique: NvGvJLidMO6JFUjmaS9dDg-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=+xDnfroDKrE7MZIL3tNRjkakvJkhUC6sm9BW0qIJtbA=; b=amQKj9yAu7nHGKzHa4kQTj0WFPsRG2xIP4UX/7HcqG9Vv9dZhAlW7L2CFO57AzHheQ SeaWRL+hlXELaFJvS01kZnNBJ9RGuhVuuriTeM4bxs8Fz4wQlszPveq9P33sQ3ZJLSf/ Kk8ESkcJU47Fxgey1WzadRhdfyO4Kx12ZMb7nlvQNJ5LDVgou9vzdb+WLfUcuHEntGjI o5uRNKXCri7rvgdxu+vdAEDRXVWXXxe+7tNYy2wQ2N2UAmsAefzdIPN62ifcOjyG/x/M GvTSMAX1eOUEODagZbgd8zyf+AOE6rXgX9csIaOsl/Qq/pYwTpm+PRJZmaX3gF7tc7RG bi4g== X-Gm-Message-State: AO0yUKWm++OZf6gpV4iFQ/CWhCSRoxBn4L1GC+VflN+K9JPohu5ARk0K v7vXi9Vof07WpXd9r4zxXBFgrLHgrnjxd5AKxbwPgEEV/3g6ftYVIUeNyFza6Mhga3J5YyErW14 dsEaUoq/PmZOxQTXNcUyWapettgqcmUEFP8fVsK31KUAIpYZPnAs0nvCSYE48qpbLiHHndjEW X-Received: by 2002:a05:6214:c48:b0:56e:b21c:61e5 with SMTP id r8-20020a0562140c4800b0056eb21c61e5mr5028883qvj.47.1676394986635; Tue, 14 Feb 2023 09:16:26 -0800 (PST) X-Google-Smtp-Source: AK7set8cDwUez7gIRFbxs2IHqe//zIDFQ5BXdbDef4USZg8Vgoey+E3wKR7WDSuLTRw3K9FgDuxZ0w== X-Received: by 2002:a05:6214:c48:b0:56e:b21c:61e5 with SMTP id r8-20020a0562140c4800b0056eb21c61e5mr5028831qvj.47.1676394986207; Tue, 14 Feb 2023 09:16:26 -0800 (PST) From: Stefano Garzarella To: qemu-devel@nongnu.org Cc: Kevin Wolf , qemu-block@nongnu.org, Hanna Reitz , Stefano Garzarella , Aihua Liang Subject: [PATCH v2] block: temporarily hold the new AioContext of bs_top in bdrv_append() Date: Tue, 14 Feb 2023 18:16:21 +0100 Message-Id: <20230214171621.11574-1-sgarzare@redhat.com> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=sgarzare@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1676395031918100002 Content-Type: text/plain; charset="utf-8" bdrv_append() is called with bs_top AioContext held, but bdrv_attach_child_noperm() could change the AioContext of bs_top. bdrv_replace_node_noperm() calls bdrv_drained_begin() starting from commit 2398747128 ("block: Don't poll in bdrv_replace_child_noperm()"). bdrv_drained_begin() can call BDRV_POLL_WHILE that assumes the new lock is taken, so let's temporarily hold the new AioContext to prevent QEMU from failing in BDRV_POLL_WHILE when it tries to release the wrong AioContext. Buglink: https://bugzilla.redhat.com/show_bug.cgi?id=3D2168209 Reported-by: Aihua Liang Signed-off-by: Stefano Garzarella --- v2: - released the right lock in the error path [Kevin] - held the new lock until the end of the function [Kevin] v1: https://lore.kernel.org/qemu-devel/20230214105156.316586-1-sgarzare@red= hat.com/ --- block.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/block.c b/block.c index aa9062f2c1..036fce19e0 100644 --- a/block.c +++ b/block.c @@ -5266,6 +5266,8 @@ int bdrv_drop_filter(BlockDriverState *bs, Error **er= rp) * child. * * This function does not create any image files. + * + * The caller must hold the AioContext lock for @bs_top. */ int bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top, Error **errp) @@ -5273,11 +5275,14 @@ int bdrv_append(BlockDriverState *bs_new, BlockDriv= erState *bs_top, int ret; BdrvChild *child; Transaction *tran =3D tran_new(); + AioContext *old_context, *new_context =3D NULL; =20 GLOBAL_STATE_CODE(); =20 assert(!bs_new->backing); =20 + old_context =3D bdrv_get_aio_context(bs_top); + child =3D bdrv_attach_child_noperm(bs_new, bs_top, "backing", &child_of_bds, bdrv_backing_role(bs_n= ew), tran, errp); @@ -5286,6 +5291,19 @@ int bdrv_append(BlockDriverState *bs_new, BlockDrive= rState *bs_top, goto out; } =20 + /* + * bdrv_attach_child_noperm could change the AioContext of bs_top. + * bdrv_replace_node_noperm calls bdrv_drained_begin, so let's tempora= rily + * hold the new AioContext, since bdrv_drained_begin calls BDRV_POLL_W= HILE + * that assumes the new lock is taken. + */ + new_context =3D bdrv_get_aio_context(bs_top); + + if (old_context !=3D new_context) { + aio_context_release(old_context); + aio_context_acquire(new_context); + } + ret =3D bdrv_replace_node_noperm(bs_top, bs_new, true, tran, errp); if (ret < 0) { goto out; @@ -5297,6 +5315,11 @@ out: =20 bdrv_refresh_limits(bs_top, NULL, NULL); =20 + if (new_context && old_context !=3D new_context) { + aio_context_release(new_context); + aio_context_acquire(old_context); + } + return ret; } =20 --=20 2.39.1