Skip to content

Commit bf25266

Browse files
ardbiesheuvelctmarinas
authored andcommitted
arm64/kernel: Always use level 2 or higher for early mappings
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. Cc: Anshuman Khandual <anshuman.khandual@arm.com> Reported-by: Ryan Roberts <ryan.roberts@arm.com> Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Reviewed-by: Anshuman Khandual <anshuman.khandual@arm.com> Reviewed-by: Ryan Roberts <ryan.roberts@arm.com> Link: https://lore.kernel.org/r/20250311073043.96795-2-ardb+git@google.com Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
1 parent 34e8e63 commit bf25266

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

arch/arm64/kernel/pi/map_range.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ void __init map_range(u64 *pte, u64 start, u64 end, u64 pa, pgprot_t prot,
4545
* clearing the mapping
4646
*/
4747
if (protval)
48-
protval |= (level < 3) ? PMD_TYPE_SECT : PTE_TYPE_PAGE;
48+
protval |= (level == 2) ? PMD_TYPE_SECT : PTE_TYPE_PAGE;
4949

5050
while (start < end) {
5151
u64 next = min((start | lmask) + 1, PAGE_ALIGN(end));
5252

53-
if (level < 3 && (start | next | pa) & lmask) {
53+
if (level < 2 || (level == 2 && (start | next | pa) & lmask)) {
5454
/*
5555
* This chunk needs a finer grained mapping. Create a
5656
* table mapping if necessary and recurse.

0 commit comments

Comments
 (0)