Map larger kernels at early init
cover Map larger kernels at early init
The early pagetable creation code assumes that a single pgd, pud, pmd and pte are sufficient to map the kernel text for MMU bringup. For 16KB granules this is, unfortunately, rarely the case. Some kernels may be too big even for a 64KB granule employing this scheme.
This patch series addresses the problem in two steps: 1) re-order the reserved_ttbr0 to allow its address computation to be independent of swapper_pg_dir size, 2) re-write the early pgtable code to allow for multiple page table entries at each level.
Steve Capper (2):
arm64: Re-order reserved_ttbr0 in linker script
arm64: Extend early page table code to allow for larger kernels
arch/arm64/include/asm/asm-uaccess.h | 6 +- arch/arm64/include/asm/kernel-pgtable.h | 20 ++++- arch/arm64/include/asm/pgtable.h | 2 +- arch/arm64/include/asm/uaccess.h | 2 +- arch/arm64/kernel/head.S | 148 +++++++++++++++++++++++--------- arch/arm64/kernel/vmlinux.lds.S | 6 +- arch/arm64/mm/mmu.c | 3 +- 7 files changed, 137 insertions(+), 50 deletions(-)
Signature · 1 line
2.11.0
PATCH 1/2 arm64: Re-order reserved_ttbr0 in linker script
Currently one resolves the location of the reserved_ttbr0 for PAN by taking a positive offset from swapper_pg_dir. In a future patch we wish to extend the swapper s.t. its size is determined at link time rather than comile time, rendering SWAPPER_DIR_SIZE unsuitable for such a low level calculation.
In this patch we re-arrange the order of the linker script s.t. instead one computes reserved_ttbr0 by subtracting RESERVED_TTBR0_SIZE from swapper_pg_dir.
--- arch/arm64/include/asm/asm-uaccess.h | 6 +++--- arch/arm64/include/asm/uaccess.h | 2 +- arch/arm64/kernel/vmlinux.lds.S | 5 ++--- 3 files changed, 6 insertions(+), 7 deletions(-)
arch/arm64/include/asm/asm-uaccess.h +3 -3
@@ -11,9 +11,9 @@*/ #ifdef CONFIG_ARM64_SW_TTBR0_PAN .macro __uaccess_ttbr0_disable, tmp1 - mrs \tmp1, ttbr1_el1 // swapper_pg_dir - add \tmp1, \tmp1, #SWAPPER_DIR_SIZE // reserved_ttbr0 at the end of swapper_pg_dir - msr ttbr0_el1, \tmp1 // set reserved TTBR0_EL1 + mrs \tmp1, ttbr1_el1 // swapper_pg_dir + sub \tmp1, \tmp1, #RESERVED_TTBR0_SIZE // reserved_ttbr0 just before swapper_pg_dir + msr ttbr0_el1, \tmp1 // set reserved TTBR0_EL1 isb .endm
arch/arm64/include/asm/uaccess.h +1 -1
@@ -108,7 +108,7 @@ static inline void __uaccess_ttbr0_disable(void)unsigned long ttbr;/* reserved_ttbr0 placed at the end of swapper_pg_dir */ - ttbr =read_sysreg (ttbr1_el1) +SWAPPER_DIR_SIZE ; + ttbr =read_sysreg (ttbr1_el1) -RESERVED_TTBR0_SIZE ;write_sysreg (ttbr, ttbr0_el1);isb (); }
arch/arm64/kernel/vmlinux.lds.S +2 -3
@@ -205,13 +205,12 @@ SECTIONS . = ALIGN(PAGE_SIZE); idmap_pg_dir = .; . += IDMAP_DIR_SIZE; - swapper_pg_dir = .; - . += SWAPPER_DIR_SIZE; - #ifdef CONFIG_ARM64_SW_TTBR0_PAN reserved_ttbr0 = .; . += RESERVED_TTBR0_SIZE; #endif + swapper_pg_dir = .; + . += SWAPPER_DIR_SIZE; __pecoff_data_size = ABSOLUTE(. - __initdata_begin); _end = .;
Signature · 1 line
2.11.0
PATCH 2/2 arm64: Extend early page table code to allow for larger kernels
In reply to Steve CapperCurrently the early assembler page table code assumes that precisely 1xpgd, 1xpud, 1xpmd are sufficient to represent the early kernel text mappings.
Unfortunately this is rarely the case when running with a 16KB granule, and we also run into limits with 4KB granule when building much larger kernels.
This patch re-writes the early page table logic to compute indices of mappings for each level of page table, and if multiple indices are required, the next-level page table is scaled up accordingly.
Also the required size of the swapper_pg_dir is computed at link time to cover the mapping [KIMAGE_ADDR + VOFFSET, _end + SZ_2M] to allow for kaslr.
--- arch/arm64/include/asm/kernel-pgtable.h | 20 ++++- arch/arm64/include/asm/pgtable.h | 2 +- arch/arm64/kernel/head.S | 148 +++++++++++++++++++++++--------- arch/arm64/kernel/vmlinux.lds.S | 1 + arch/arm64/mm/mmu.c | 3 +- 5 files changed, 131 insertions(+), 43 deletions(-)
arch/arm64/include/asm/kernel-pgtable.h +19 -1
@@ -52,7 +52,25 @@#define IDMAP_PGTABLE_LEVELS (ARM64_HW_PGTABLE_LEVELS(PHYS_MASK_SHIFT))#endif -#define SWAPPER_DIR_SIZE (SWAPPER_PGTABLE_LEVELS * PAGE_SIZE) +#define EARLY_PGDS (vstart, vend) ((vend >> PGDIR_SHIFT) - (vstart >> PGDIR_SHIFT) + 1) + +#if SWAPPER_PGTABLE_LEVELS >3 +#define EARLY_PUDS (vstart, vend) ((vend >> PUD_SHIFT) - (vstart >> PUD_SHIFT) + 1) +#else +#define EARLY_PUDS (vstart, vend) (0) +#endif + +#if SWAPPER_PGTABLE_LEVELS >2 +#define EARLY_PMDS (vstart, vend) ((vend >> PMD_SHIFT) - (vstart >> PMD_SHIFT) + 1) +#else +#define EARLY_PMDS (vstart, vend) (0) +#endif + +#define EARLY_PAGES (vstart, vend) ( 1/* PGDIR page */ \ + + EARLY_PGDS((vstart), (vend)) /* each PGDIR needs a next level page table */ \ + + EARLY_PUDS((vstart), (vend)) /* each PUD needs a next level page table */ \ + + EARLY_PMDS((vstart), (vend))) /* each PMD needs a next level page table */ +#define SWAPPER_DIR_SIZE (PAGE_SIZE * EARLY_PAGES(KIMAGE_VADDR + TEXT_OFFSET, _end + SZ_2M))#define IDMAP_DIR_SIZE (IDMAP_PGTABLE_LEVELS * PAGE_SIZE)#ifdef CONFIG_ARM64_SW_TTBR0_PAN
arch/arm64/include/asm/pgtable.h +1 -1
@@ -667,7 +667,7 @@ static inline void pmdp_set_wrprotect(struct mm_struct *mm,extern pgd_t swapper_pg_dir[PTRS_PER_PGD ];extern pgd_t idmap_pg_dir[PTRS_PER_PGD ]; - +extern pgd_t swapper_pg_end[];/* * Encode and decode a swap entry: * bits 0-1: present (must be zero)
arch/arm64/kernel/head.S +108 -40
210 lines not on this page · raw message
arch/arm64/kernel/vmlinux.lds.S +1 -0
@@ -211,6 +211,7 @@ SECTIONS #endif swapper_pg_dir = .; . += SWAPPER_DIR_SIZE; + swapper_pg_end = .; __pecoff_data_size = ABSOLUTE(. - __initdata_begin); _end = .;
arch/arm64/mm/mmu.c +2 -1
@@ -612,7 +612,8 @@ void __init paging_init(void)* allocated with it. */ memblock_free (__pa_symbol (swapper_pg_dir) +PAGE_SIZE , -SWAPPER_DIR_SIZE -PAGE_SIZE ); +__pa_symbol (swapper_pg_end) -__pa_symbol (swapper_pg_dir) + -PAGE_SIZE ); }/*
Signature · 1 line
2.11.0
cover Map larger kernels at early init
In reply to Steve CapperQuoted 14 lines · Steve Capper
The early pagetable creation code assumes that a single pgd, pud, pmd and pte are sufficient to map the kernel text for MMU bringup. For 16KB granules this is, unfortunately, rarely the case. Some kernels may be too big even for a 64KB granule employing this scheme.
This patch series addresses the problem in two steps: 1) re-order the
reserved_ttbr0to allow its address computation to be independent ofswapper_pg_dirsize, 2) re-write the early pgtable code to allow for multiple page table entries at each level.Steve Capper (2):
arm64: Re-orderreserved_ttbr0in linker script
arm64: Extend early page table code to allow for larger kernels
Quoted 9 lines
arch/arm64/include/asm/asm-uaccess.h | 6 +- arch/arm64/include/asm/kernel-pgtable.h | 20 ++++- arch/arm64/include/asm/pgtable.h | 2 +- arch/arm64/include/asm/uaccess.h | 2 +- arch/arm64/kernel/head.S | 148 +++++++++++++++++++++++--------- arch/arm64/kernel/vmlinux.lds.S | 6 +- arch/arm64/mm/mmu.c | 3 +- 7 files changed, 137 insertions(+), 50 deletions(-)
Excellent!
This means we can also remove the workaround from the KASLR init code that takes care not to put the kernel image across a PMD/PUD boundary.
Mark Rutland
reviewerPATCH 1/2 arm64: Re-order reserved_ttbr0 in linker script
In reply to Steve CapperQuoted 11 lines · Steve Capper
Currently one resolves the location of the
reserved_ttbr0for PAN by taking a positive offset fromswapper_pg_dir. In a future patch we wish to extend the swapper s.t. its size is determined at link time rather than comile time, renderingSWAPPER_DIR_SIZEunsuitable for such a low level calculation.In this patch we re-arrange the order of the linker script s.t. instead one computes
reserved_ttbr0by subtractingRESERVED_TTBR0_SIZEfromswapper_pg_dir.Signed-off-by Steve Capper <[email protected]>
Looks sane to me. FWIW:
Mark.
Quoted 64 lines
--- arch/arm64/include/asm/asm-uaccess.h | 6 +++--- arch/arm64/include/asm/uaccess.h | 2 +- arch/arm64/kernel/vmlinux.lds.S | 5 ++--- 3 files changed, 6 insertions(+), 7 deletions(-)arch/arm64/include/asm/asm-uaccess.h +3
-3@@ -11,9 +11,9 @@*/ #ifdef CONFIG_ARM64_SW_TTBR0_PAN .macro __uaccess_ttbr0_disable, tmp1 - mrs \tmp1, ttbr1_el1 // swapper_pg_dir - add \tmp1, \tmp1, #SWAPPER_DIR_SIZE // reserved_ttbr0 at the end of swapper_pg_dir - msr ttbr0_el1, \tmp1 // set reserved TTBR0_EL1 + mrs \tmp1, ttbr1_el1 // swapper_pg_dir + sub \tmp1, \tmp1, #RESERVED_TTBR0_SIZE // reserved_ttbr0 just before swapper_pg_dir + msr ttbr0_el1, \tmp1 // set reserved TTBR0_EL1 isb .endmarch/arm64/include/asm/uaccess.h +1
-1@@ -108,7 +108,7 @@ static inline void __uaccess_ttbr0_disable(void)unsigned long ttbr;/* reserved_ttbr0 placed at the end of swapper_pg_dir */ - ttbr =read_sysreg (ttbr1_el1) +SWAPPER_DIR_SIZE ; + ttbr =read_sysreg (ttbr1_el1) -RESERVED_TTBR0_SIZE ;write_sysreg (ttbr, ttbr0_el1);isb (); }arch/arm64/kernel/vmlinux.lds.S +2
-3@@ -205,13 +205,12 @@ SECTIONS . = ALIGN(PAGE_SIZE); idmap_pg_dir = .; . += IDMAP_DIR_SIZE; - swapper_pg_dir = .; - . += SWAPPER_DIR_SIZE; - #ifdef CONFIG_ARM64_SW_TTBR0_PAN reserved_ttbr0 = .; . += RESERVED_TTBR0_SIZE; #endif + swapper_pg_dir = .; + . += SWAPPER_DIR_SIZE; __pecoff_data_size = ABSOLUTE(. - __initdata_begin); _end = .;Signature · 7 lines
2.11.0 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel at lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Mark Rutland
reviewerPATCH 2/2 arm64: Extend early page table code to allow for larger kernels
In reply to Steve CapperHi,
Quoted 11 lines · Steve Capper
arch/arm64/include/asm/kernel-pgtable.h +10
-1-#define SWAPPER_DIR_SIZE (SWAPPER_PGTABLE_LEVELS * PAGE_SIZE) +#define EARLY_PGDS (vstart, vend) ((vend >> PGDIR_SHIFT) - (vstart >> PGDIR_SHIFT) + 1) + +#if SWAPPER_PGTABLE_LEVELS >3 +#define EARLY_PUDS (vstart, vend) ((vend >> PUD_SHIFT) - (vstart >> PUD_SHIFT) + 1) +#else +#define EARLY_PUDS (vstart, vend) (0) +#endif + +#if SWAPPER_PGTABLE_LEVELS >2 +#define EARLY_PMDS (vstart, vend) ((vend >> PMD_SHIFT) - (vstart >> PMD_SHIFT) + 1)
arch/arm64/include/asm/kernel-pgtable.h line 74
Quoted 10 lines
arch/arm64/include/asm/kernel-pgtable.h +9
-0+#else +#define EARLY_PMDS (vstart, vend) (0) +#endif + +#define EARLY_PAGES (vstart, vend) ( 1/* PGDIR page */ \ + + EARLY_PGDS((vstart), (vend)) /* each PGDIR needs a next level page table */ \ + + EARLY_PUDS((vstart), (vend)) /* each PUD needs a next level page table */ \ + + EARLY_PMDS((vstart), (vend))) /* each PMD needs a next level page table */ +#define SWAPPER_DIR_SIZE (PAGE_SIZE * EARLY_PAGES(KIMAGE_VADDR + TEXT_OFFSET, _end + SZ_2M))#define IDMAP_DIR_SIZE (IDMAP_PGTABLE_LEVELS * PAGE_SIZE)
I'm currently struggling to convince myself as to whether 2M is necessary/sufficient slack space for all configurations.
At least w.r.t. Ard's comment, we'd need a bit more slack to allow KASLR to cross PGD/PUD/PMD boundaries.
For example with 3 levels of 64K pages, and a huge kernel that takes up SZ_512M - SZ_2M - TEXT_OFFSET. That perfectly fits into 1 PGDIR, 1 PGD, and 1 PMD. IIUC, the above would allocate us 3 pages for this case. If the kernel were to be relocated such that it straddled two PGDs, we'd need 2 PGDs and 2 PMDs, needing 5 pages total.
I'm not sure that we have a problem if we don't relax KASLR.
Thanks,
Mark.
PATCH 2/2 arm64: Extend early page table code to allow for larger kernels
Quoted 1 line · Mark Rutland
Hi,
Hi Mark,
Quoted 18 lines · Steve Capper
Quoted 16 lines
arch/arm64/include/asm/kernel-pgtable.h +15
-1-#define SWAPPER_DIR_SIZE (SWAPPER_PGTABLE_LEVELS * PAGE_SIZE) +#define EARLY_PGDS (vstart, vend) ((vend >> PGDIR_SHIFT) - (vstart >> PGDIR_SHIFT) + 1) + +#if SWAPPER_PGTABLE_LEVELS >3 +#define EARLY_PUDS (vstart, vend) ((vend >> PUD_SHIFT) - (vstart >> PUD_SHIFT) + 1) +#else +#define EARLY_PUDS (vstart, vend) (0) +#endif + +#if SWAPPER_PGTABLE_LEVELS >2 +#define EARLY_PMDS (vstart, vend) ((vend >> PMD_SHIFT) - (vstart >> PMD_SHIFT) + 1) +#else +#define EARLY_PMDS (vstart, vend) (0) +#endif + +#define EARLY_PAGES (vstart, vend) ( 1/* PGDIR page */ \
Quoted 8 lines
arch/arm64/include/asm/kernel-pgtable.h line 74
Quoted 5 lines
arch/arm64/include/asm/kernel-pgtable.h +4
-0+ + EARLY_PGDS((vstart), (vend)) /* each PGDIR needs a next level page table */ \ + + EARLY_PUDS((vstart), (vend)) /* each PUD needs a next level page table */ \ + + EARLY_PMDS((vstart), (vend))) /* each PMD needs a next level page table */ +#define SWAPPER_DIR_SIZE (PAGE_SIZE * EARLY_PAGES(KIMAGE_VADDR + TEXT_OFFSET, _end + SZ_2M))#define IDMAP_DIR_SIZE (IDMAP_PGTABLE_LEVELS * PAGE_SIZE)I'm currently struggling to convince myself as to whether 2M is necessary/sufficient slack space for all configurations.
Agreed, if the possible address range is changed outside these bounds then we need to extend them.
Quoted 4 lines
At least w.r.t. Ard's comment, we'd need a bit more slack to allow KASLR to cross PGD/PUD/PMD boundaries.
Quoted 8 lines
For example with 3 levels of 64K pages, and a huge kernel that takes up
SZ_512M-SZ_2M-TEXT_OFFSET. That perfectly fits into 1 PGDIR, 1 PGD, and 1 PMD. IIUC, the above would allocate us 3 pages for this case. If the kernel were to be relocated such that it straddled two PGDs, we'd need 2 PGDs and 2 PMDs, needing 5 pages total.I'm not sure that we have a problem if we don't relax KASLR.
The approach I've adopted is to compute which indices are required for PGDs, PUDs, PMDs to map the supplied address range, then count them. If we require two PGD entries then that means we need two pages containing PMD entries to be allocated.
If we consider just PGDs, for example, the only way I am aware of the kernel straddling more PGDs than previously computed is for the mapping to begin before vstart or end after vend (or both).
Should I refine the range specified in the SWAPPER_DIR_SIZE for the current KASLR? (I thought the random offset was < SZ_2M?)
Cheers,
Signature · 1 line
Steve
PATCH 1/2 arm64: Re-order reserved_ttbr0 in linker script
In reply to Mark RutlandQuoted 11 lines · Mark Rutland
Quoted 10 lines · Steve Capper
Currently one resolves the location of the
reserved_ttbr0for PAN by taking a positive offset fromswapper_pg_dir. In a future patch we wish to extend the swapper s.t. its size is determined at link time rather than comile time, renderingSWAPPER_DIR_SIZEunsuitable for such a low level calculation.In this patch we re-arrange the order of the linker script s.t. instead one computes
reserved_ttbr0by subtractingRESERVED_TTBR0_SIZEfromswapper_pg_dir.
Quoted 5 lines
Quoted 1 line
Signed-off-by Steve Capper <[email protected]>Looks sane to me. FWIW:
Acked-by Mark Rutland <[email protected]>
Thanks!
Signature · 1 line
Steve
PATCH 2/2 arm64: Extend early page table code to allow for larger kernels
In reply to Steve CapperQuoted 53 lines · Steve Capper
Quoted 1 line · Mark Rutland
Hi,
Hi Mark,
Quoted 18 lines · Steve Capper
Quoted 16 lines
arch/arm64/include/asm/kernel-pgtable.h +15
-1-#define SWAPPER_DIR_SIZE (SWAPPER_PGTABLE_LEVELS * PAGE_SIZE) +#define EARLY_PGDS (vstart, vend) ((vend >> PGDIR_SHIFT) - (vstart >> PGDIR_SHIFT) + 1) + +#if SWAPPER_PGTABLE_LEVELS >3 +#define EARLY_PUDS (vstart, vend) ((vend >> PUD_SHIFT) - (vstart >> PUD_SHIFT) + 1) +#else +#define EARLY_PUDS (vstart, vend) (0) +#endif + +#if SWAPPER_PGTABLE_LEVELS >2 +#define EARLY_PMDS (vstart, vend) ((vend >> PMD_SHIFT) - (vstart >> PMD_SHIFT) + 1) +#else +#define EARLY_PMDS (vstart, vend) (0) +#endif + +#define EARLY_PAGES (vstart, vend) ( 1/* PGDIR page */ \Quoted 8 lines
arch/arm64/include/asm/kernel-pgtable.h line 74
Quoted 5 lines
arch/arm64/include/asm/kernel-pgtable.h +4
-0+ + EARLY_PGDS((vstart), (vend)) /* each PGDIR needs a next level page table */ \ + + EARLY_PUDS((vstart), (vend)) /* each PUD needs a next level page table */ \ + + EARLY_PMDS((vstart), (vend))) /* each PMD needs a next level page table */ +#define SWAPPER_DIR_SIZE (PAGE_SIZE * EARLY_PAGES(KIMAGE_VADDR + TEXT_OFFSET, _end + SZ_2M))#define IDMAP_DIR_SIZE (IDMAP_PGTABLE_LEVELS * PAGE_SIZE)I'm currently struggling to convince myself as to whether 2M is necessary/sufficient slack space for all configurations.
Agreed, if the possible address range is changed outside these bounds then we need to extend them.
Quoted 4 lines
At least w.r.t. Ard's comment, we'd need a bit more slack to allow KASLR to cross PGD/PUD/PMD boundaries.
Quoted 8 lines
For example with 3 levels of 64K pages, and a huge kernel that takes up
SZ_512M-SZ_2M-TEXT_OFFSET. That perfectly fits into 1 PGDIR, 1 PGD, and 1 PMD. IIUC, the above would allocate us 3 pages for this case. If the kernel were to be relocated such that it straddled two PGDs, we'd need 2 PGDs and 2 PMDs, needing 5 pages total.I'm not sure that we have a problem if we don't relax KASLR.
The approach I've adopted is to compute which indices are required for PGDs, PUDs, PMDs to map the supplied address range, then count them. If we require two PGD entries then that means we need two pages containing PMD entries to be allocated.
Quoted 7 lines
If we consider just PGDs, for example, the only way I am aware of the kernel straddling more PGDs than previously computed is for the mapping to begin before vstart or end after vend (or both).
Should I refine the range specified in the
SWAPPER_DIR_SIZEfor the current KASLR? (I thought the random offset was <SZ_2M?)
Ahh, I see KASLR offset has the bottom 21 bits masked out, not confined to the 21 bottom bits :-).
It may be possible to do something with (vend - vstart) I will have a think about this.
Cheers,
Signature · 1 line
Steve
PATCH 2/2 arm64: Extend early page table code to allow for larger kernels
Quoted 62 lines · Steve Capper
Quoted 53 lines · Steve Capper
Quoted 1 line · Mark Rutland
Hi,
Hi Mark,
Quoted 18 lines · Steve Capper
Quoted 16 lines
arch/arm64/include/asm/kernel-pgtable.h +15
-1-#define SWAPPER_DIR_SIZE (SWAPPER_PGTABLE_LEVELS * PAGE_SIZE) +#define EARLY_PGDS (vstart, vend) ((vend >> PGDIR_SHIFT) - (vstart >> PGDIR_SHIFT) + 1) + +#if SWAPPER_PGTABLE_LEVELS >3 +#define EARLY_PUDS (vstart, vend) ((vend >> PUD_SHIFT) - (vstart >> PUD_SHIFT) + 1) +#else +#define EARLY_PUDS (vstart, vend) (0) +#endif + +#if SWAPPER_PGTABLE_LEVELS >2 +#define EARLY_PMDS (vstart, vend) ((vend >> PMD_SHIFT) - (vstart >> PMD_SHIFT) + 1) +#else +#define EARLY_PMDS (vstart, vend) (0) +#endif + +#define EARLY_PAGES (vstart, vend) ( 1/* PGDIR page */ \Quoted 8 lines
arch/arm64/include/asm/kernel-pgtable.h line 74
Quoted 5 lines
arch/arm64/include/asm/kernel-pgtable.h +4
-0+ +EARLY_PGDS ((vstart), (vend))/* each PGDIR needs a next level page table */ \ + +EARLY_PUDS ((vstart), (vend))/* each PUD needs a next level page table */ \ + + EARLY_PMDS((vstart), (vend))) /* each PMD needs a next level page table */ +#define SWAPPER_DIR_SIZE (PAGE_SIZE * EARLY_PAGES(KIMAGE_VADDR + TEXT_OFFSET, _end + SZ_2M))#define IDMAP_DIR_SIZE (IDMAP_PGTABLE_LEVELS * PAGE_SIZE)I'm currently struggling to convince myself as to whether 2M is necessary/sufficient slack space for all configurations.
Agreed, if the possible address range is changed outside these bounds then we need to extend them.
Quoted 4 lines
At least w.r.t. Ard's comment, we'd need a bit more slack to allow KASLR to cross PGD/PUD/PMD boundaries.
Quoted 8 lines
For example with 3 levels of 64K pages, and a huge kernel that takes up
SZ_512M-SZ_2M-TEXT_OFFSET. That perfectly fits into 1 PGDIR, 1 PGD, and 1 PMD. IIUC, the above would allocate us 3 pages for this case. If the kernel were to be relocated such that it straddled two PGDs, we'd need 2 PGDs and 2 PMDs, needing 5 pages total.I'm not sure that we have a problem if we don't relax KASLR.
The approach I've adopted is to compute which indices are required for PGDs, PUDs, PMDs to map the supplied address range, then count them. If we require two PGD entries then that means we need two pages containing PMD entries to be allocated.
Quoted 7 lines
If we consider just PGDs, for example, the only way I am aware of the kernel straddling more PGDs than previously computed is for the mapping to begin before vstart or end after vend (or both).
Should I refine the range specified in the
SWAPPER_DIR_SIZEfor the current KASLR? (I thought the random offset was <SZ_2M?)
Quoted 6 lines
Ahh, I see KASLR offset has the bottom 21 bits masked out, not confined to the 21 bottom bits :-).
It may be possible to do something with (vend - vstart) I will have a think about this.
Hi Steve,
Please be aware that it is slightly more complicated than that.
The VA randomization offset chosen by the KASLR code is made up of two separate values:
- the VA offset modulo 2 MB, which equals the PA offset modulo 2 MB, and is set by the EFI stub when it loads the kernel image into 1:1 mapped memory
- the VA offset in 2 MB increments, which is set by the kaslr_init call in head.S
The reason for this approach is that it allows randomization at 64 KB granularity without losing the ability to map the kernel using 2 MB block mappings or contiguous page mappings. On a 48-bit VA kernel, this gives us 30 bits of randomization.
PATCH 2/2 arm64: Extend early page table code to allow for larger kernels
Quoted 82 lines · Ard Biesheuvel
Quoted 62 lines · Steve Capper
Quoted 53 lines · Steve Capper
Quoted 1 line · Mark Rutland
Hi,
Hi Mark,
Quoted 18 lines · Steve Capper
Quoted 16 lines
arch/arm64/include/asm/kernel-pgtable.h +15
-1-#define SWAPPER_DIR_SIZE (SWAPPER_PGTABLE_LEVELS * PAGE_SIZE) +#define EARLY_PGDS (vstart, vend) ((vend >> PGDIR_SHIFT) - (vstart >> PGDIR_SHIFT) + 1) + +#if SWAPPER_PGTABLE_LEVELS >3 +#define EARLY_PUDS (vstart, vend) ((vend >> PUD_SHIFT) - (vstart >> PUD_SHIFT) + 1) +#else +#define EARLY_PUDS (vstart, vend) (0) +#endif + +#if SWAPPER_PGTABLE_LEVELS >2 +#define EARLY_PMDS (vstart, vend) ((vend >> PMD_SHIFT) - (vstart >> PMD_SHIFT) + 1) +#else +#define EARLY_PMDS (vstart, vend) (0) +#endif + +#define EARLY_PAGES (vstart, vend) ( 1/* PGDIR page */ \Quoted 8 lines
arch/arm64/include/asm/kernel-pgtable.h line 74
Quoted 5 lines
arch/arm64/include/asm/kernel-pgtable.h +4
-0+ +EARLY_PGDS ((vstart), (vend))/* each PGDIR needs a next level page table */ \ + +EARLY_PUDS ((vstart), (vend))/* each PUD needs a next level page table */ \ + + EARLY_PMDS((vstart), (vend))) /* each PMD needs a next level page table */ +#define SWAPPER_DIR_SIZE (PAGE_SIZE * EARLY_PAGES(KIMAGE_VADDR + TEXT_OFFSET, _end + SZ_2M))#define IDMAP_DIR_SIZE (IDMAP_PGTABLE_LEVELS * PAGE_SIZE)I'm currently struggling to convince myself as to whether 2M is necessary/sufficient slack space for all configurations.
Agreed, if the possible address range is changed outside these bounds then we need to extend them.
Quoted 4 lines
At least w.r.t. Ard's comment, we'd need a bit more slack to allow KASLR to cross PGD/PUD/PMD boundaries.
Quoted 8 lines
For example with 3 levels of 64K pages, and a huge kernel that takes up
SZ_512M-SZ_2M-TEXT_OFFSET. That perfectly fits into 1 PGDIR, 1 PGD, and 1 PMD. IIUC, the above would allocate us 3 pages for this case. If the kernel were to be relocated such that it straddled two PGDs, we'd need 2 PGDs and 2 PMDs, needing 5 pages total.I'm not sure that we have a problem if we don't relax KASLR.
The approach I've adopted is to compute which indices are required for PGDs, PUDs, PMDs to map the supplied address range, then count them. If we require two PGD entries then that means we need two pages containing PMD entries to be allocated.
Quoted 7 lines
If we consider just PGDs, for example, the only way I am aware of the kernel straddling more PGDs than previously computed is for the mapping to begin before vstart or end after vend (or both).
Should I refine the range specified in the
SWAPPER_DIR_SIZEfor the current KASLR? (I thought the random offset was <SZ_2M?)Quoted 6 lines
Ahh, I see KASLR offset has the bottom 21 bits masked out, not confined to the 21 bottom bits :-).
It may be possible to do something with (vend - vstart) I will have a think about this.
Hi Steve,
Please be aware that it is slightly more complicated than that.
The VA randomization offset chosen by the KASLR code is made up of two separate values:
- the VA offset modulo 2 MB, which equals the PA offset modulo 2 MB, and is set by the EFI stub when it loads the kernel image into 1:1 mapped memory
- the VA offset in 2 MB increments, which is set by thekaslr_initcall inhead.S
Quoted 4 lines
The reason for this approach is that it allows randomization at 64 KB granularity without losing the ability to map the kernel using 2 MB block mappings or contiguous page mappings. On a 48-bit VA kernel, this gives us 30 bits of randomization.
Thanks Ard!
So if I've understood correctly, it is valid for there to exist a VA KASLR offset K at runtime s.t.
K mod 2^SHIFT != 0
For SHIFT = PGDIR_SHIFT, PUD_SHIFT and SWAPPER_TABLE_SHIFT. (I need to correct my EARLY_PMDS macro to use SWAPPER_TABLE_SHIFT instead of PMD_SHIFT).
I've managed to convince myself that this will mean at most one extra page being needed for each strideable level to cover where we get unlucky with KASLR. This will make the kernel image up to 3 pages larger with KASLR enabled (but shouldn't affect runtime memory as this will be given back).
If (vend - vstart) mod 2^SHIFT == 0, then KASLR cannot affect that particular level, but we would need to map a much larger kernel for that identity to be true.
So I'll remove the 2MB end offset from SWAPPER_DIR_SIZE and add in an extra page to EARLY_P[GUM]DS when KASLR is enabled.
If I've understood things correctly, this should be safe with an updated KASLR that can cross PMD/PUD boundaries.
Cheers,
Signature · 1 line
Steve
PATCH 2/2 arm64: Extend early page table code to allow for larger kernels
Quoted 89 lines · Steve Capper
Quoted 82 lines · Ard Biesheuvel
Quoted 62 lines · Steve Capper
Quoted 53 lines · Steve Capper
Quoted 1 line · Mark Rutland
Hi,
Hi Mark,
Quoted 18 lines · Steve Capper
Quoted 16 lines
arch/arm64/include/asm/kernel-pgtable.h +15
-1-#define SWAPPER_DIR_SIZE (SWAPPER_PGTABLE_LEVELS * PAGE_SIZE) +#define EARLY_PGDS (vstart, vend) ((vend >> PGDIR_SHIFT) - (vstart >> PGDIR_SHIFT) + 1) + +#if SWAPPER_PGTABLE_LEVELS >3 +#define EARLY_PUDS (vstart, vend) ((vend >> PUD_SHIFT) - (vstart >> PUD_SHIFT) + 1) +#else +#define EARLY_PUDS (vstart, vend) (0) +#endif + +#if SWAPPER_PGTABLE_LEVELS >2 +#define EARLY_PMDS (vstart, vend) ((vend >> PMD_SHIFT) - (vstart >> PMD_SHIFT) + 1) +#else +#define EARLY_PMDS (vstart, vend) (0) +#endif + +#define EARLY_PAGES (vstart, vend) ( 1/* PGDIR page */ \Quoted 8 lines
arch/arm64/include/asm/kernel-pgtable.h line 74
Quoted 5 lines
arch/arm64/include/asm/kernel-pgtable.h +4
-0+ +EARLY_PGDS ((vstart), (vend))/* each PGDIR needs a next level page table */ \ + +EARLY_PUDS ((vstart), (vend))/* each PUD needs a next level page table */ \ + + EARLY_PMDS((vstart), (vend))) /* each PMD needs a next level page table */ +#define SWAPPER_DIR_SIZE (PAGE_SIZE * EARLY_PAGES(KIMAGE_VADDR + TEXT_OFFSET, _end + SZ_2M))#define IDMAP_DIR_SIZE (IDMAP_PGTABLE_LEVELS * PAGE_SIZE)I'm currently struggling to convince myself as to whether 2M is necessary/sufficient slack space for all configurations.
Agreed, if the possible address range is changed outside these bounds then we need to extend them.
Quoted 4 lines
At least w.r.t. Ard's comment, we'd need a bit more slack to allow KASLR to cross PGD/PUD/PMD boundaries.
Quoted 8 lines
For example with 3 levels of 64K pages, and a huge kernel that takes up
SZ_512M-SZ_2M-TEXT_OFFSET. That perfectly fits into 1 PGDIR, 1 PGD, and 1 PMD. IIUC, the above would allocate us 3 pages for this case. If the kernel were to be relocated such that it straddled two PGDs, we'd need 2 PGDs and 2 PMDs, needing 5 pages total.I'm not sure that we have a problem if we don't relax KASLR.
The approach I've adopted is to compute which indices are required for PGDs, PUDs, PMDs to map the supplied address range, then count them. If we require two PGD entries then that means we need two pages containing PMD entries to be allocated.
Quoted 7 lines
If we consider just PGDs, for example, the only way I am aware of the kernel straddling more PGDs than previously computed is for the mapping to begin before vstart or end after vend (or both).
Should I refine the range specified in the
SWAPPER_DIR_SIZEfor the current KASLR? (I thought the random offset was <SZ_2M?)Quoted 6 lines
Ahh, I see KASLR offset has the bottom 21 bits masked out, not confined to the 21 bottom bits :-).
It may be possible to do something with (vend - vstart) I will have a think about this.
Hi Steve,
Please be aware that it is slightly more complicated than that.
The VA randomization offset chosen by the KASLR code is made up of two separate values:
- the VA offset modulo 2 MB, which equals the PA offset modulo 2 MB, and is set by the EFI stub when it loads the kernel image into 1:1 mapped memory
- the VA offset in 2 MB increments, which is set by thekaslr_initcall inhead.SQuoted 4 lines
The reason for this approach is that it allows randomization at 64 KB granularity without losing the ability to map the kernel using 2 MB block mappings or contiguous page mappings. On a 48-bit VA kernel, this gives us 30 bits of randomization.
Quoted 8 lines
Thanks Ard!
So if I've understood correctly, it is valid for there to exist a VA KASLR offset K at runtime s.t.
K mod 2^SHIFT != 0
For SHIFT =
PGDIR_SHIFT,PUD_SHIFTandSWAPPER_TABLE_SHIFT.
The latter only for 4k and 16k pages, given that the KASLR offset granularity is 64k, and so K mod 64k must be 0.
Quoted 13 lines
(I need to correct my
EARLY_PMDSmacro to useSWAPPER_TABLE_SHIFTinstead ofPMD_SHIFT).I've managed to convince myself that this will mean at most one extra page being needed for each strideable level to cover where we get unlucky with KASLR. This will make the kernel image up to 3 pages larger with KASLR enabled (but shouldn't affect runtime memory as this will be given back).
If (vend - vstart) mod 2^SHIFT == 0, then KASLR cannot affect that particular level, but we would need to map a much larger kernel for that identity to be true.
Quoted 6 lines
So I'll remove the 2MB end offset from
SWAPPER_DIR_SIZEand add in an extra page to EARLY_P[GUM]DS when KASLR is enabled.If I've understood things correctly, this should be safe with an updated KASLR that can cross PMD/PUD boundaries.
Yes, I /think/ that is the case.