diff mbox series

[v7,01/33] arm64: mm: Avoid SWAPPER_BLOCK_xxx constants in FDT fixmap logic

Message ID 20221111171201.2088501-2-ardb@kernel.org (mailing list archive)
State New, archived
Headers show
Series arm64: robustify boot sequence and add support for WXN | expand

Commit Message

Ard Biesheuvel Nov. 11, 2022, 5:11 p.m. UTC
The FDT is permitted to be up to 2 MiB in size, and is mapped in the
fixmap region using the same granularity as we use for the initial ID
map, and up until recently, as we use for the preliminary mapping of the
kernel in swapper_pg_dir.

However, even though the constants are the same, the motivation is
different: on 4k pagesize configurations, the fixmap region only has 2
MiB's worth of level 3 PTE slots to begin with, and so mapping the FDT
down to pages in the fixmap would be wasteful, and this is why we use
block mappings in this case. This is also documented in the boot
protocol, i.e., adjacent regions must not be used by the platform in a
away that could result in the need for memory attributes that conflict
with the cacheable attributes used for the FDT block mapping.

For larger page sizes, using block mappings is unnecessary, and given
the potential issues caused by rounding, undesirable, so we use page
mappings in that case.

So to convey that this granularity is unrelated to the swapper block
size, and to allow us to rename or remove the associated constants in a
subsequent patch, use our own constants to define the granularity.

No functional change intended, although the FDT fixmap virtual address
will no longer be 2 MiB aligned on non-4k pages configurations.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 arch/arm64/include/asm/fixmap.h |  5 ++--
 arch/arm64/mm/mmu.c             | 27 ++++++++++----------
 2 files changed, 17 insertions(+), 15 deletions(-)
diff mbox series

Patch

diff --git a/arch/arm64/include/asm/fixmap.h b/arch/arm64/include/asm/fixmap.h
index 71ed5fdf718bd0fd..d09654af5b1277c6 100644
--- a/arch/arm64/include/asm/fixmap.h
+++ b/arch/arm64/include/asm/fixmap.h
@@ -40,11 +40,12 @@  enum fixed_addresses {
 	 * maximum supported size, and put it at the top of the fixmap region.
 	 * The additional space ensures that any FDT that does not exceed
 	 * MAX_FDT_SIZE can be mapped regardless of whether it crosses any
-	 * 2 MB alignment boundaries.
+	 * 2 MB alignment boundaries on 4k pages configurations.
 	 *
 	 * Keep this at the top so it remains 2 MB aligned.
 	 */
-#define FIX_FDT_SIZE		(MAX_FDT_SIZE + SZ_2M)
+#define FIX_FDT_BSIZE		(MAX_FDT_SIZE >= PMD_SIZE ? PMD_SIZE : PAGE_SIZE)
+#define FIX_FDT_SIZE		(MAX_FDT_SIZE + FIX_FDT_BSIZE)
 	FIX_FDT_END,
 	FIX_FDT = FIX_FDT_END + FIX_FDT_SIZE / PAGE_SIZE - 1,
 
diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
index 9a7c38965154081e..757c2fe54d2e99f0 100644
--- a/arch/arm64/mm/mmu.c
+++ b/arch/arm64/mm/mmu.c
@@ -1373,22 +1373,23 @@  void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot)
 	 * allocate additional translation table pages, so that it is safe
 	 * to call create_mapping_noalloc() this early.
 	 *
-	 * On 64k pages, the FDT will be mapped using PTEs, so we need to
-	 * be in the same PMD as the rest of the fixmap.
-	 * On 4k pages, we'll use section mappings for the FDT so we only
-	 * have to be in the same PUD.
+	 * On 4k pages, the entire level 3 fixmap only covers 2 MiB, so we'll
+	 * need to use section mappings for the FDT, and these must be covered
+	 * by the same statically allocated PUD (bm_pud). Otherwise, the FDT
+	 * will be mapped using PTEs, so the entire mappings needs to fit into
+	 * a single PMD (bm_pmd).
 	 */
-	BUILD_BUG_ON(dt_virt_base % SZ_2M);
+	BUILD_BUG_ON(dt_virt_base % FIX_FDT_BSIZE);
 
-	BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> SWAPPER_TABLE_SHIFT !=
-		     __fix_to_virt(FIX_BTMAP_BEGIN) >> SWAPPER_TABLE_SHIFT);
+	BUILD_BUG_ON((__fix_to_virt(FIX_FDT_END) ^ __fix_to_virt(FIX_BTMAP_BEGIN))
+		     & ~((FIX_FDT_BSIZE << (PAGE_SHIFT - 3)) - 1));
 
-	offset = dt_phys % SWAPPER_BLOCK_SIZE;
+	offset = dt_phys % FIX_FDT_BSIZE;
 	dt_virt = (void *)dt_virt_base + offset;
 
 	/* map the first chunk so we can read the size from the header */
-	create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE),
-			dt_virt_base, SWAPPER_BLOCK_SIZE, prot);
+	create_mapping_noalloc(round_down(dt_phys, FIX_FDT_BSIZE),
+			       dt_virt_base, FIX_FDT_BSIZE, prot);
 
 	if (fdt_magic(dt_virt) != FDT_MAGIC)
 		return NULL;
@@ -1397,9 +1398,9 @@  void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot)
 	if (*size > MAX_FDT_SIZE)
 		return NULL;
 
-	if (offset + *size > SWAPPER_BLOCK_SIZE)
-		create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base,
-			       round_up(offset + *size, SWAPPER_BLOCK_SIZE), prot);
+	if (offset + *size > FIX_FDT_BSIZE)
+		create_mapping_noalloc(round_down(dt_phys, FIX_FDT_BSIZE), dt_virt_base,
+				       round_up(offset + *size, FIX_FDT_BSIZE), prot);
 
 	return dt_virt;
 }