Skip to content

Commit 9026857

Browse files
mrutland-armctmarinas
authored andcommitted
arm64: head: avoid over-mapping in map_memory
The `compute_indices` and `populate_entries` macros operate on inclusive bounds, and thus the `map_memory` macro which uses them also operates on inclusive bounds. We pass `_end` and `_idmap_text_end` to `map_memory`, but these are exclusive bounds, and if one of these is sufficiently aligned (as a result of kernel configuration, physical placement, and KASLR), then: * In `compute_indices`, the computed `iend` will be in the page/block *after* the final byte of the intended mapping. * In `populate_entries`, an unnecessary entry will be created at the end of each level of table. At the leaf level, this entry will map up to SWAPPER_BLOCK_SIZE bytes of physical addresses that we did not intend to map. As we may map up to SWAPPER_BLOCK_SIZE bytes more than intended, we may violate the boot protocol and map physical address past the 2MiB-aligned end address we are permitted to map. As we map these with Normal memory attributes, this may result in further problems depending on what these physical addresses correspond to. The final entry at each level may require an additional table at that level. As EARLY_ENTRIES() calculates an inclusive bound, we allocate enough memory for this. Avoid the extraneous mapping by having map_memory convert the exclusive end address to an inclusive end address by subtracting one, and do likewise in EARLY_ENTRIES() when calculating the number of required tables. For clarity, comments are updated to more clearly document which boundaries the macros operate on. For consistency with the other macros, the comments in map_memory are also updated to describe `vstart` and `vend` as virtual addresses. Fixes: 0370b31 ("arm64: Extend early page table code to allow for larger kernels") Cc: <[email protected]> # 4.16.x Signed-off-by: Mark Rutland <[email protected]> Cc: Anshuman Khandual <[email protected]> Cc: Ard Biesheuvel <[email protected]> Cc: Steve Capper <[email protected]> Cc: Will Deacon <[email protected]> Acked-by: Will Deacon <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Catalin Marinas <[email protected]>
1 parent 04fa17d commit 9026857

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

arch/arm64/include/asm/kernel-pgtable.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@
6565
#define EARLY_KASLR (0)
6666
#endif
6767

68-
#define EARLY_ENTRIES(vstart, vend, shift) (((vend) >> (shift)) \
69-
- ((vstart) >> (shift)) + 1 + EARLY_KASLR)
68+
#define EARLY_ENTRIES(vstart, vend, shift) \
69+
((((vend) - 1) >> (shift)) - ((vstart) >> (shift)) + 1 + EARLY_KASLR)
7070

7171
#define EARLY_PGDS(vstart, vend) (EARLY_ENTRIES(vstart, vend, PGDIR_SHIFT))
7272

arch/arm64/kernel/head.S

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ SYM_CODE_END(preserve_boot_args)
177177
* to be composed of multiple pages. (This effectively scales the end index).
178178
*
179179
* vstart: virtual address of start of range
180-
* vend: virtual address of end of range
180+
* vend: virtual address of end of range - we map [vstart, vend]
181181
* shift: shift used to transform virtual address into index
182182
* ptrs: number of entries in page table
183183
* istart: index in table corresponding to vstart
@@ -214,17 +214,18 @@ SYM_CODE_END(preserve_boot_args)
214214
*
215215
* tbl: location of page table
216216
* rtbl: address to be used for first level page table entry (typically tbl + PAGE_SIZE)
217-
* vstart: start address to map
218-
* vend: end address to map - we map [vstart, vend]
217+
* vstart: virtual address of start of range
218+
* vend: virtual address of end of range - we map [vstart, vend - 1]
219219
* flags: flags to use to map last level entries
220220
* phys: physical address corresponding to vstart - physical memory is contiguous
221221
* pgds: the number of pgd entries
222222
*
223223
* Temporaries: istart, iend, tmp, count, sv - these need to be different registers
224-
* Preserves: vstart, vend, flags
225-
* Corrupts: tbl, rtbl, istart, iend, tmp, count, sv
224+
* Preserves: vstart, flags
225+
* Corrupts: tbl, rtbl, vend, istart, iend, tmp, count, sv
226226
*/
227227
.macro map_memory, tbl, rtbl, vstart, vend, flags, phys, pgds, istart, iend, tmp, count, sv
228+
sub \vend, \vend, #1
228229
add \rtbl, \tbl, #PAGE_SIZE
229230
mov \sv, \rtbl
230231
mov \count, #0

0 commit comments

Comments
 (0)