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.15553.1675951211850480313 for ; Thu, 09 Feb 2023 06:00:12 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ElX4PIPH; 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 4BC13B82081; Thu, 9 Feb 2023 14:00:10 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CC8F1C433A4; Thu, 9 Feb 2023 14:00:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1675951209; bh=k7tjal1o6OjyVsVxcbMb4b6aStbYKWeMB6airWAzzCY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ElX4PIPH40aYvoisMrzcuGM8nFBf2Xtr9uZfVzwuDUHm+ZImgOBN6aCpNJAjC1beC QKJHX4Cwc45yckaHW3UAGWIyGiphtep2Tb+EW0JOjJJDQ/5Zy99IJJq9+gRr4euQWV ADjpdY+KIg1NTzDnN94Gam1cP14rlbSXfOnYFYLdZesVuogYYmrjY1bXC0YpLN9plk J1Dn3HfzQqgoGNN0eh2tIzRNUEcmU738jJ+9Kb/6jRjWp6cERB3ZFLmVrF5QZeKH7C SWlrwi1vIKh1IUExv0f58l14oRK1o6ONLbnk3nh+zCPGfKtTKKTsUGzjH9J1BxkLK/ iyoTUFlapuWMQ== 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 v4 08/11] ArmPkg/ArmMmuLib: Avoid splitting block entries if possible Date: Thu, 9 Feb 2023 14:59:33 +0100 Message-Id: <20230209135936.789983-9-ardb@kernel.org> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230209135936.789983-1-ardb@kernel.org> References: <20230209135936.789983-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.1