Skip to contentSkip to conversation

arm64/kernel: Always use level 2 or higher for early mappings

PATCH Superseded Ard Biesheuvel 4 participants 4 messages Mar 10, 2025 – Mar 11, 2025 arm64

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64/kernel: Always use level 2 or higher for early mappings

From: Ard Biesheuvel <[email protected]>

The page table population code in map_range() uses a recursive algorithm to create the early mappings of the kernel, the DTB and the ID mapped text and data pages, and this fails to take into account that the way these page tables may be constructed is not precisely the same at each level. In particular, block mappings are not permitted at each level, and the code as it exists today might inadvertently create such a forbidden block mapping if it were used to map a region of the appropriate size and alignment.

This never happens in practice, given the limited size of the assets being mapped by the early boot code. Nonetheless, it would be better if this code would behave correctly in all circumstances.

So only permit block mappings at level 2, and page mappings at level 3, for any page size, and use table mappings exclusively at all other levels. This change should have no impact in practice, but it makes the code more robust.

---
 arch/arm64/kernel/pi/map_range.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
arch/arm64/kernel/pi/map_range.c +9 -8
index 2b69e3beeef8..025bb9f0aa0b 100644
@@ -40,17 +40,10 @@ void __init map_range(u64 *pte, u64 start, u64 end, u64 pa, pgprot_t prot,
 	/* Advance tbl to the entry that covers start */
 	tbl += (start >> (lshift + PAGE_SHIFT)) % PTRS_PER_PTE;
 
-	/*
-	 * Set the right block/page bits for this level unless we are
-	 * clearing the mapping
-	 */
-	if (protval)
-		protval |= (level < 3) ? PMD_TYPE_SECT : PTE_TYPE_PAGE;
-
 	while (start < end) {
 		u64 next = min((start | lmask) + 1, PAGE_ALIGN(end));
 
-		if (level < 3 && (start | next | pa) & lmask) {
+		if (level < 2 || (level == 2 && (start | next | pa) & lmask)) {
 			/*
 			 * This chunk needs a finer grained mapping. Create a
 			 * table mapping if necessary and recurse.
@@ -64,6 +57,14 @@ void __init map_range(u64 *pte, u64 start, u64 end, u64 pa, pgprot_t prot,
 				  (pte_t *)(__pte_to_phys(*tbl) + va_offset),
 				  may_use_cont, va_offset);
 		} else {
+			/*
+			 * Set the right block/page bits for this level unless
+			 * we are clearing the mapping
+			 */
+			if (protval)
+				protval |= (level == 2) ? PMD_TYPE_SECT
+							: PTE_TYPE_PAGE;
+
 			/*
 			 * Start a contiguous range if start and pa are
 			 * suitably aligned
Signature · 1 line
2.49.0.rc0.332.g42c0ae87b1-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

The page table population code in map_range() uses a recursive algorithm to create the early mappings of the kernel, the DTB and the ID mapped text and data pages, and this fails to take into account that the way these page tables may be constructed is not precisely the same at each level. In particular, block mappings are not permitted at each level, and the code as it exists today might inadvertently create such a forbidden block mapping if it were used to map a region of the appropriate size and alignment.

Quoted 3 lines

This never happens in practice, given the limited size of the assets being mapped by the early boot code. Nonetheless, it would be better if this code would behave correctly in all circumstances.

ahha, yes I had a hunch we would be able to guarantee not to hit this condition in practice. Thanks for the explanation.

Quoted 9 lines

So only permit block mappings at level 2, and page mappings at level 3, for any page size, and use table mappings exclusively at all other levels. This change should have no impact in practice, but it makes the code more robust.

This looks good to me, and it's D128 ready!

Quoted 42 lines
---
 arch/arm64/kernel/pi/map_range.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
arch/arm64/kernel/pi/map_range.c +9 -8
index 2b69e3beeef8..025bb9f0aa0b 100644
@@ -40,17 +40,10 @@ void __init map_range(u64 *pte, u64 start, u64 end, u64 pa, pgprot_t prot,
 	/* Advance tbl to the entry that covers start */
 	tbl += (start >> (lshift + PAGE_SHIFT)) % PTRS_PER_PTE;
 
-	/*
-	 * Set the right block/page bits for this level unless we are
-	 * clearing the mapping
-	 */
-	if (protval)
-		protval |= (level < 3) ? PMD_TYPE_SECT : PTE_TYPE_PAGE;
-
 	while (start < end) {
 		u64 next = min((start | lmask) + 1, PAGE_ALIGN(end));
 
-		if (level < 3 && (start | next | pa) & lmask) {
+		if (level < 2 || (level == 2 && (start | next | pa) & lmask)) {
 			/*
 			 * This chunk needs a finer grained mapping. Create a
 			 * table mapping if necessary and recurse.
@@ -64,6 +57,14 @@ void __init map_range(u64 *pte, u64 start, u64 end, u64 pa, pgprot_t prot,
 				  (pte_t *)(__pte_to_phys(*tbl) + va_offset),
 				  may_use_cont, va_offset);
 		} else {
+			/*
+			 * Set the right block/page bits for this level unless
+			 * we are clearing the mapping
+			 */
+			if (protval)
+				protval |= (level == 2) ? PMD_TYPE_SECT
+							: PTE_TYPE_PAGE;
+
 			/*
 			 * Start a contiguous range if start and pa are
 			 * suitably aligned
Reviewed-by on v1 1/1
#2 lore raw
In reply to Ard Biesheuvel
Quoted 55 lines · Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

The page table population code in map_range() uses a recursive algorithm to create the early mappings of the kernel, the DTB and the ID mapped text and data pages, and this fails to take into account that the way these page tables may be constructed is not precisely the same at each level. In particular, block mappings are not permitted at each level, and the code as it exists today might inadvertently create such a forbidden block mapping if it were used to map a region of the appropriate size and alignment.

This never happens in practice, given the limited size of the assets being mapped by the early boot code. Nonetheless, it would be better if this code would behave correctly in all circumstances.

So only permit block mappings at level 2, and page mappings at level 3, for any page size, and use table mappings exclusively at all other levels. This change should have no impact in practice, but it makes the code more robust.

---
 arch/arm64/kernel/pi/map_range.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
arch/arm64/kernel/pi/map_range.c +2 -8
index 2b69e3beeef8..025bb9f0aa0b 100644
@@ -40,17 +40,10 @@ void __init map_range(u64 *pte, u64 start, u64 end, u64 pa, pgprot_t prot,
 	/* Advance tbl to the entry that covers start */
 	tbl += (start >> (lshift + PAGE_SHIFT)) % PTRS_PER_PTE;
 
-	/*
-	 * Set the right block/page bits for this level unless we are
-	 * clearing the mapping
-	 */
-	if (protval)
-		protval |= (level < 3) ? PMD_TYPE_SECT : PTE_TYPE_PAGE;
-
 	while (start < end) {
 		u64 next = min((start | lmask) + 1, PAGE_ALIGN(end));
 
-		if (level < 3 && (start | next | pa) & lmask) {
+		if (level < 2 || (level == 2 && (start | next | pa) & lmask)) {
 			/*
 			 * This chunk needs a finer grained mapping. Create a
 			 * table mapping if necessary and recurse.
@@ -64,6 +57,14 @@ void __init map_range(u64 *pte, u64 start, u64 end, u64 pa, pgprot_t prot,
 				  (pte_t *)(__pte_to_phys(*tbl) + va_offset),
 				  may_use_cont, va_offset);
 		} else {
+			/*

arch/arm64/kernel/pi/map_range.c void __init map_range(u64 *pte, u64 start, u64 end, u64 pa, pgprot_t prot, · line 70

Quoted 10 lines
arch/arm64/kernel/pi/map_range.c +7 -0
+			 * Set the right block/page bits for this level unless
+			 * we are clearing the mapping
+			 */
+			if (protval)
+				protval |= (level == 2) ? PMD_TYPE_SECT
+							: PTE_TYPE_PAGE;
+
 			/*
 			 * Start a contiguous range if start and pa are
 			 * suitably aligned

LGTM and as Ryan mentioned, D128 ready as well !

#3 lore raw
Quoted 72 lines · Anshuman Khandual
Quoted 55 lines · Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

The page table population code in map_range() uses a recursive algorithm to create the early mappings of the kernel, the DTB and the ID mapped text and data pages, and this fails to take into account that the way these page tables may be constructed is not precisely the same at each level. In particular, block mappings are not permitted at each level, and the code as it exists today might inadvertently create such a forbidden block mapping if it were used to map a region of the appropriate size and alignment.

This never happens in practice, given the limited size of the assets being mapped by the early boot code. Nonetheless, it would be better if this code would behave correctly in all circumstances.

So only permit block mappings at level 2, and page mappings at level 3, for any page size, and use table mappings exclusively at all other levels. This change should have no impact in practice, but it makes the code more robust.

---
 arch/arm64/kernel/pi/map_range.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
arch/arm64/kernel/pi/map_range.c +2 -8
index 2b69e3beeef8..025bb9f0aa0b 100644
@@ -40,17 +40,10 @@ void __init map_range(u64 *pte, u64 start, u64 end, u64 pa, pgprot_t prot,
      /* Advance tbl to the entry that covers start */
      tbl += (start >> (lshift + PAGE_SHIFT)) % PTRS_PER_PTE;
 
-     /*
-      * Set the right block/page bits for this level unless we are
-      * clearing the mapping
-      */
-     if (protval)
-             protval |= (level < 3) ? PMD_TYPE_SECT : PTE_TYPE_PAGE;
-
      while (start < end) {
              u64 next = min((start | lmask) + 1, PAGE_ALIGN(end));
 
-             if (level < 3 && (start | next | pa) & lmask) {
+             if (level < 2 || (level == 2 && (start | next | pa) & lmask)) {
                      /*
                       * This chunk needs a finer grained mapping. Create a
                       * table mapping if necessary and recurse.
@@ -64,6 +57,14 @@ void __init map_range(u64 *pte, u64 start, u64 end, u64 pa, pgprot_t prot,
                                (pte_t *)(__pte_to_phys(*tbl) + va_offset),
                                may_use_cont, va_offset);
              } else {
+                     /*

arch/arm64/kernel/pi/map_range.c void __init map_range(u64 *pte, u64 start, u64 end, u64 pa, pgprot_t prot, · line 70

Quoted 10 lines
arch/arm64/kernel/pi/map_range.c +7 -0
+                      * Set the right block/page bits for this level unless
+                      * we are clearing the mapping
+                      */
+                     if (protval)
+                             protval |= (level == 2) ? PMD_TYPE_SECT
+                                                     : PTE_TYPE_PAGE;
+
                      /*
                       * Start a contiguous range if start and pa are
                       * suitably aligned

LGTM and as Ryan mentioned, D128 ready as well !

Quoted 1 line

Thanks, both, but the patch is actually buggy so I'll need to respin this.