From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by mx.groups.io with SMTP id smtpd.web11.26473.1678727877734257378 for ; Mon, 13 Mar 2023 10:17:58 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ei5fncR0; spf=pass (domain: kernel.org, ip: 145.40.68.75, mailfrom: ardb@kernel.org) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 25897B811B1; Mon, 13 Mar 2023 17:17:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9BD37C433A1; Mon, 13 Mar 2023 17:17:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1678727874; bh=0Skr/KGDExn9tUzTdP4C5Lu1KRA9arxt7ZqMVmLGbFo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ei5fncR0Y0W/TBXspgFYt0sG7h+EYpTHR7kMUXZnFMXSD9zWoxIb3pSpZL2O02tNU lEkfKWTOUEntrTGwHlNOacmJW8YiZjpPJdSTF+hz9sNIjVYDPynRns4z/SHusUh4HR hM5TqcPg7MctiX90snuALjPQ4FCU7H04+wrsOxkF50HEbOd903Tn5Fcgt7bamnnwIY ww4+KM9GCc04Ag5JK2EUm7brX2rXaSDDhB3WtK+46EM2T5LO46/QrGnLvQ9kQEShFj 49ZChXOrPuzALkXJzFNaL/FmtpRwm2rqb4ifIykvgOEOVJqd1pGoRiHvdnfhDzuCBh WfL8l56nMIe8g== From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Michael Kinney , Liming Gao , Jiewen Yao , Michael Kubacki , Sean Brogan , Rebecca Cran , Leif Lindholm , Sami Mujawar , Taylor Beebe Subject: [PATCH v5 08/38] ArmPkg/ArmMmuLib: Avoid splitting block entries if possible Date: Mon, 13 Mar 2023 18:16:44 +0100 Message-Id: <20230313171714.3866151-9-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230313171714.3866151-1-ardb@kernel.org> References: <20230313171714.3866151-1-ardb@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Currently, the ARM MMU page table logic will break down any block entry that overlaps with the region being mapped, even if the block entry in question is using the same attributes as the new region. This means that creating a non-executable mapping inside a region that is already mapped non-executable at a coarser granularity may trigger a call to AllocatePages (), which may recurse back into the page table code to update the attributes on the newly allocated page tables. Let's avoid this, by preserving the block entry if it already covers the region being mapped with the correct attributes. Signed-off-by: Ard Biesheuvel --- ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuLibCore.c | 10 ++++++++++ ArmPkg/Library/ArmMmuLib/Arm/ArmMmuLibUpdate.c | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuLibCore.c b/ArmPkg/Libr= ary/ArmMmuLib/AArch64/ArmMmuLibCore.c index 6d21a2e41dd1..1ce200c43c72 100644 --- a/ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuLibCore.c +++ b/ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuLibCore.c @@ -251,6 +251,16 @@ UpdateRegionMappingRecursive ( ASSERT (Level < 3);=0D =0D if (!IsTableEntry (*Entry, Level)) {=0D + //=0D + // If the region we are trying to map is already covered by a bloc= k=0D + // entry with the right attributes, don't bother splitting it up.= =0D + //=0D + if (IsBlockEntry (*Entry, Level) &&=0D + ((*Entry & TT_ATTRIBUTES_MASK & ~AttributeClearMask) =3D=3D At= tributeSetMask))=0D + {=0D + continue;=0D + }=0D +=0D //=0D // No table entry exists yet, so we need to allocate a page table= =0D // for the next level.=0D diff --git a/ArmPkg/Library/ArmMmuLib/Arm/ArmMmuLibUpdate.c b/ArmPkg/Librar= y/ArmMmuLib/Arm/ArmMmuLibUpdate.c index 247cf87bf3d3..299d38ad07e8 100644 --- a/ArmPkg/Library/ArmMmuLib/Arm/ArmMmuLibUpdate.c +++ b/ArmPkg/Library/ArmMmuLib/Arm/ArmMmuLibUpdate.c @@ -170,6 +170,17 @@ UpdatePageEntries ( =0D // Does this descriptor need to be converted from section entry to 4K = pages?=0D if (!TT_DESCRIPTOR_SECTION_TYPE_IS_PAGE_TABLE (Descriptor)) {=0D + //=0D + // If the section mapping covers the requested region with the expec= ted=0D + // attributes, splitting it is unnecessary, and should be avoided as= it=0D + // may result in unbounded recursion when using a strict NX policy.= =0D + //=0D + if ((EntryValue & ~TT_DESCRIPTOR_PAGE_TYPE_MASK & EntryMask) =3D=3D= =0D + (ConvertSectionAttributesToPageAttributes (Descriptor) & EntryMa= sk))=0D + {=0D + continue;=0D + }=0D +=0D Status =3D ConvertSectionToPages (FirstLevelIdx << TT_DESCRIPTOR_SEC= TION_BASE_SHIFT);=0D if (EFI_ERROR (Status)) {=0D // Exit for loop=0D --=20 2.39.2