From nobody Sun Sep 28 16:35:43 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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=fail(p=reject dis=none) header.from=rsg.ci.i.u-tokyo.ac.jp Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1756401858943786.8773454465905; Thu, 28 Aug 2025 10:24:18 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1urgIh-00089Y-US; Thu, 28 Aug 2025 13:20:40 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1urdTL-0000kL-35; Thu, 28 Aug 2025 10:19:27 -0400 Received: from www3579.sakura.ne.jp ([49.212.243.89]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1urdTE-0005se-7w; Thu, 28 Aug 2025 10:19:26 -0400 Received: from [192.168.10.111] (p865013-ipoe.ipoe.ocn.ne.jp [153.242.222.12]) (authenticated bits=0) by www3579.sakura.ne.jp (8.16.1/8.16.1) with ESMTPSA id 57SEICLM097958 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Thu, 28 Aug 2025 23:18:25 +0900 (JST) (envelope-from odaki@rsg.ci.i.u-tokyo.ac.jp) DKIM-Signature: a=rsa-sha256; bh=qnvNBRk/k2ji9GR/TCV7p/ZPIyvw3f4h7mAHQM6jeUk=; c=relaxed/relaxed; d=rsg.ci.i.u-tokyo.ac.jp; h=From:Date:Subject:Message-Id:To; s=rs20250326; t=1756390705; v=1; b=jKY5colu/UHyKHD0tI50oy0BBtD12tbYfBlvM9ieVxW75UjXGP5efpOZVeibmCpn +OpgjTgeU41IoV0JH1mDcouNL1ND4JRLUk1Jdlwo6Xm2/yNlm6znKw7o1Meo6Olf DjZpwqaVFZbdopOJvq9iPhWSvDHb0XSD3Li1KVGxcqXO+wNFGPbG8cY8OPzvSdhH dgH4HTgml2YsQcyWY98R2I0RWrGQWbGEQ0CoyDkmrnFX0zONWOeNU14g8HJi4/Jp 0cCQP8A5iCKU7lTlT73/aYlZZIzbHVrMR2o2pHdafLIenkJG0tZ4ArGIj8GpHmDX Kul6c/9z0fT0PumAUNZ+YQ== From: Akihiko Odaki Date: Thu, 28 Aug 2025 23:18:12 +0900 Subject: [PATCH v9 2/2] memory: Do not create circular reference with subregion MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250828-san-v9-2-c0dff4b8a487@rsg.ci.i.u-tokyo.ac.jp> References: <20250828-san-v9-0-c0dff4b8a487@rsg.ci.i.u-tokyo.ac.jp> In-Reply-To: <20250828-san-v9-0-c0dff4b8a487@rsg.ci.i.u-tokyo.ac.jp> To: Eduardo Habkost , Marcel Apfelbaum , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Yanan Wang , John Snow , BALATON Zoltan , Jiaxun Yang , Nicholas Piggin , Daniel Henrique Barboza , David Gibson , Harsh Prateek Bora , Alexey Kardashevskiy , "Michael S. Tsirkin" , =?utf-8?q?Alex_Benn=C3=A9e?= , Peter Xu , Fabiano Rosas , Paolo Bonzini , David Hildenbrand , Thomas Huth , Laurent Vivier , Peter Maydell Cc: qemu-devel@nongnu.org, qemu-block@nongnu.org, qemu-ppc@nongnu.org, Akihiko Odaki X-Mailer: b4 0.14.2 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=49.212.243.89; envelope-from=odaki@rsg.ci.i.u-tokyo.ac.jp; helo=www3579.sakura.ne.jp X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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-ZM-MESSAGEID: 1756401864581124100 memory_region_update_container_subregions() used to call memory_region_ref(), which creates a reference to the owner of the subregion, on behalf of the owner of the container. This results in a circular reference if the subregion and container have the same owner. memory_region_ref() creates a reference to the owner instead of the memory region to match the lifetime of the owner and memory region. We do not need such a hack if the subregion and container have the same owner because the owner will be alive as long as the container is. Therefore, create a reference to the subregion itself instead ot its owner in such a case; the reference to the subregion is still necessary to ensure that the subregion gets finalized after the container. Signed-off-by: Akihiko Odaki --- system/memory.c | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/system/memory.c b/system/memory.c index 56465479406f4a264bfe13e6a2bc7d9b6565410f..1f4111fc24f5a8dda9ee8c277f6= 8f839f5277906 100644 --- a/system/memory.c +++ b/system/memory.c @@ -1848,6 +1848,31 @@ void memory_region_unref(MemoryRegion *mr) } } =20 +static void memory_region_ref_subregion(MemoryRegion *mr, + MemoryRegion *subregion) +{ + subregion->container =3D mr; + + if (mr->owner =3D=3D subregion->owner) { + object_ref(OBJECT(subregion)); + } else { + memory_region_ref(subregion); + } +} + +static void memory_region_unref_subregion(MemoryRegion *subregion) +{ + MemoryRegion *mr =3D subregion->container; + + subregion->container =3D NULL; + + if (mr->owner =3D=3D subregion->owner) { + object_unref(OBJECT(subregion)); + } else { + memory_region_unref(subregion); + } +} + uint64_t memory_region_size(MemoryRegion *mr) { if (int128_eq(mr->size, int128_2_64())) { @@ -2618,14 +2643,15 @@ void memory_region_del_eventfd(MemoryRegion *mr, memory_region_transaction_commit(); } =20 -static void memory_region_update_container_subregions(MemoryRegion *subreg= ion) +static void memory_region_update_container_subregions(MemoryRegion *mr, + MemoryRegion *subreg= ion) { - MemoryRegion *mr =3D subregion->container; MemoryRegion *other; =20 memory_region_transaction_begin(); =20 - memory_region_ref(subregion); + memory_region_ref_subregion(mr, subregion); + QTAILQ_FOREACH(other, &mr->subregions, subregions_link) { if (subregion->priority >=3D other->priority) { QTAILQ_INSERT_BEFORE(other, subregion, subregions_link); @@ -2645,12 +2671,11 @@ static void memory_region_add_subregion_common(Memo= ryRegion *mr, MemoryRegion *alias; =20 assert(!subregion->container); - subregion->container =3D mr; for (alias =3D subregion->alias; alias; alias =3D alias->alias) { alias->mapped_via_alias++; } subregion->addr =3D offset; - memory_region_update_container_subregions(subregion); + memory_region_update_container_subregions(mr, subregion); } =20 void memory_region_add_subregion(MemoryRegion *mr, @@ -2677,13 +2702,13 @@ void memory_region_del_subregion(MemoryRegion *mr, =20 memory_region_transaction_begin(); assert(subregion->container =3D=3D mr); - subregion->container =3D NULL; for (alias =3D subregion->alias; alias; alias =3D alias->alias) { alias->mapped_via_alias--; assert(alias->mapped_via_alias >=3D 0); } QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link); - memory_region_unref(subregion); + memory_region_unref_subregion(subregion); + memory_region_update_pending |=3D mr->enabled && subregion->enabled; memory_region_transaction_commit(); } --=20 2.51.0