arm64: mm: move zero page from .bss to right before swapper_pg_dir
PATCH v3 1/1 arm64: mm: move zero page from .bss to right before swapper_pg_dir
Move the statically allocated zero page from the .bss section to right before swapper_pg_dir. This allows us to refer to its physical address by simply reading TTBR1_EL1 (which always points to swapper_pg_dir and always has its ASID field cleared), and subtracting PAGE_SIZE.
To protect the zero page from inadvertent modification, carve out a segment that covers it as well as idmap_pg_dir[], and mark it read-only in both the primary and the linear mappings of the kernel.
---
v3: rebase onto for-next/core
add clarifying comments around cpu_set_reserved_ttbr0() reduce alignment of r/o region covering idmap_pg_dir and empty_zero_page to PAGE_SIZE
rename vmlinux_tail vm_struct to vmlinux_swapper
v2: make empty_zero_page[] read-only
make idmap_pg_dir[] read-only as well
fix issue in v1 with cpu_reserved_ttbr0()
arch/arm64/include/asm/mmu_context.h | 18 +++++-- arch/arm64/include/asm/sections.h | 1 + arch/arm64/kernel/vmlinux.lds.S | 9 ++++ arch/arm64/mm/mmu.c | 56 ++++++++++++-------- 4 files changed, 57 insertions(+), 27 deletions(-)
arch/arm64/include/asm/mmu_context.h +13 -5
@@ -41,12 +41,16 @@ static inline void contextidr_thread_switch(struct task_struct *next)/* * Set TTBR0 to empty_zero_page. No translations will be possible via TTBR0. +* In some cases (e.g. where cpu_replace_ttbr1 is used), TTBR1 may not +* point at swapper_pg_dir, and this helper cannot be used. */ static inline void cpu_set_reserved_ttbr0 (void ) { -unsigned long ttbr =virt_to_phys (empty_zero_page); - -write_sysreg (ttbr, ttbr0_el1); +/* +* The zero page is located right before swapper_pg_dir, whose +* physical address we can easily fetch from TTBR1_EL1. +*/ +write_sysreg (read_sysreg (ttbr1_el1) -PAGE_SIZE , ttbr0_el1);isb (); }
@@ -99,7 +103,9 @@ static inline void cpu_uninstall_idmap(void) {struct mm_struct *mm = current->active_mm; -cpu_set_reserved_ttbr0 (); +/* see cpu_set_reserved_ttbr0 */ +write_sysreg (virt_to_phys (empty_zero_page), ttbr0_el1); +isb ();local_flush_tlb_all ();cpu_set_default_tcr_t0sz ();
@@ -109,7 +115,9 @@ static inline void cpu_uninstall_idmap(void)static inline void cpu_install_idmap (void ) { -cpu_set_reserved_ttbr0 (); +/* see cpu_set_reserved_ttbr0 */ +write_sysreg (virt_to_phys (empty_zero_page), ttbr0_el1); +isb ();local_flush_tlb_all ();cpu_set_idmap_tcr_t0sz ();
arch/arm64/include/asm/sections.h +1 -0
@@ -26,5 +26,6 @@ extern char __hyp_text_start[], __hyp_text_end[];extern char __idmap_text_start[], __idmap_text_end[];extern char __irqentry_text_start[], __irqentry_text_end[];extern char __mmuoff_data_start[], __mmuoff_data_end[]; +extern char __robss_start[], __robss_end[];#endif /* __ASM_SECTIONS_H */
arch/arm64/kernel/vmlinux.lds.S +9 -0
@@ -210,8 +210,17 @@ SECTIONS BSS_SECTION(0, 0, 0) . = ALIGN(PAGE_SIZE); + __robss_start = .; idmap_pg_dir = .; . += IDMAP_DIR_SIZE; + /* + * Put the zero page right before swapper_pg_dir so we can + * easily obtain its physical address by subtracting PAGE_SIZE + * from the contents of TTBR1_EL1. + */ + empty_zero_page = .; + . += PAGE_SIZE; + __robss_end = .; swapper_pg_dir = .; . += SWAPPER_DIR_SIZE;
arch/arm64/mm/mmu.c +34 -22
97 lines not on this page · raw message
Signature · 1 line
2.7.4
Mark Rutland
reviewerPATCH v3 1/1 arm64: mm: move zero page from .bss to right before swapper_pg_dir
Hi,
Quoted 4 lines · Ard Biesheuvel
Move the statically allocated zero page from the .bss section to right before
swapper_pg_dir. This allows us to refer to its physical address by simply readingTTBR1_EL1(which always points toswapper_pg_dirand always has its ASID field cleared), and subtractingPAGE_SIZE.
It might be worth worth mentioning that we want to do this to make cpu_set_reserved_ttbr0() as cheap as possible for the TTBR0_SW_PAN stuff, as that'll mean we're calling it far more frequently.
Quoted 5 lines
To protect the zero page from inadvertent modification, carve out a segment that covers it as well as idmap_pg_dir[], and mark it read-only in both the primary and the linear mappings of the kernel.
Signed-off-by Ard Biesheuvel <[email protected]>
Otherwise, this looks good to me, builds without warnings, and works on Juno and Seattle without issue even when I throw the usual set of problematic config options at it. Which is to say:
Thanks,
Mark.
PATCH v3 1/1 arm64: mm: move zero page from .bss to right before swapper_pg_dir
Quoted 16 lines · Mark Rutland
Hi,
Quoted 4 lines · Ard Biesheuvel
Move the statically allocated zero page from the .bss section to right before
swapper_pg_dir. This allows us to refer to its physical address by simply readingTTBR1_EL1(which always points toswapper_pg_dirand always has its ASID field cleared), and subtractingPAGE_SIZE.It might be worth worth mentioning that we want to do this to make
cpu_set_reserved_ttbr0()as cheap as possible for theTTBR0_SW_PANstuff, as that'll mean we're calling it far more frequently.Quoted 4 lines
To protect the zero page from inadvertent modification, carve out a segment that covers it as well as idmap_pg_dir[], and mark it read-only in both the primary and the linear mappings of the kernel.
Quoted 9 lines
Quoted 1 line
Signed-off-by Ard Biesheuvel <[email protected]>Otherwise, this looks good to me, builds without warnings, and works on Juno and Seattle without issue even when I throw the usual set of problematic config options at it. Which is to say:
Reviewed-by Mark Rutland <[email protected]>Tested-by Mark Rutland <[email protected]>
Thanks. But actually, I think it makes sense to make the first swapper_pg_dir page read-only as well, given that it is only modified via the fixmap, and we can trivially extend the r/o bss region to end at 'swapper_pg_dir + PAGE_SIZE'
Thoughts?
Mark Rutland
reviewerPATCH v3 1/1 arm64: mm: move zero page from .bss to right before swapper_pg_dir
Quoted 7 lines · Ard Biesheuvel
On 13 September 2016 at 18:35, Mark Rutland <[email protected]> wrote:
Thanks. But actually, I think it makes sense to make the firstswapper_pg_dirpage read-only as well, given that it is only modified via the fixmap, and we can trivially extend the r/o bss region to end at 'swapper_pg_dir+PAGE_SIZE'Thoughts?
I thought that we lazy-allocated the vmalloc region at runtime, and initialised pgd level entries.
Quoted 1 line
From a quick dig it looks like a
vmalloc()could eventually call
pgd_populate(), which seems to set a pgd entry without using a fixmap slot.
Is there some reason that won't happen at runtime?
Thanks,
Mark.
PATCH v3 1/1 arm64: mm: move zero page from .bss to right before swapper_pg_dir
Quoted 9 lines · Mark Rutland
Quoted 7 lines · Ard Biesheuvel
On 13 September 2016 at 18:35, Mark Rutland <[email protected]> wrote:
Thanks. But actually, I think it makes sense to make the firstswapper_pg_dirpage read-only as well, given that it is only modified via the fixmap, and we can trivially extend the r/o bss region to end at 'swapper_pg_dir+PAGE_SIZE'Thoughts?
Quoted 8 lines
I thought that we lazy-allocated the vmalloc region at runtime, and initialised pgd level entries.
From a quick dig it looks like a
vmalloc()could eventually callpgd_populate(), which seems to set a pgd entry without using a fixmap slot.Is there some reason that won't happen at runtime?
Ah, right. I thought all swapper_pg_dir manipulations went via the arch64/mm/mmu.c routines that use the fixmap slots, but apparently this is not the case.
Mark Rutland
reviewerPATCH v3 1/1 arm64: mm: move zero page from .bss to right before swapper_pg_dir
Quoted 13 lines · Ard Biesheuvel
Quoted 12 lines · Mark Rutland
Quoted 7 lines · Ard Biesheuvel
On 13 September 2016 at 18:35, Mark Rutland <[email protected]> wrote:
Thanks. But actually, I think it makes sense to make the firstswapper_pg_dirpage read-only as well, given that it is only modified via the fixmap, and we can trivially extend the r/o bss region to end at 'swapper_pg_dir+PAGE_SIZE'Thoughts?
I thought that we lazy-allocated the vmalloc region at runtime, and initialised pgd level entries.
Quoted 8 lines
Quoted 4 lines
From a quick dig it looks like a
vmalloc()could eventually callpgd_populate(), which seems to set a pgd entry without using a fixmap slot.Is there some reason that won't happen at runtime?
Ah, right. I thought all
swapper_pg_dirmanipulations went via thearch64/mm/mmu.croutines that use the fixmap slots, but apparently this is not the case.
For better or worse, we only use a fixmap'd pgd under paging_init, and only for the temporary pgd, not the "real" swapper pgd.
Thanks,
Mark.
PATCH v3 1/1 arm64: mm: move zero page from .bss to right before swapper_pg_dir
In reply to Ard BiesheuvelQuoted 11 lines · Ard Biesheuvel
Move the statically allocated zero page from the .bss section to right before
swapper_pg_dir. This allows us to refer to its physical address by simply readingTTBR1_EL1(which always points toswapper_pg_dirand always has its ASID field cleared), and subtractingPAGE_SIZE.To protect the zero page from inadvertent modification, carve out a segment that covers it as well as idmap_pg_dir[], and mark it read-only in both the primary and the linear mappings of the kernel.
Signed-off-by Ard Biesheuvel <[email protected]>---
[...]
Quoted 4 lines
arch/arm64/mm/mmu.c +0
-0
[...]
Quoted 7 lines
arch/arm64/mm/mmu.c +2
-1@@ -424,13 +430,19 @@ static void __init map_kernel_segment(pgd_t *pgd, void *va_start, void *va_end,*/ static void __init map_kernel (pgd_t *pgd) { -static struct vm_struct vmlinux_text, vmlinux_rodata, vmlinux_init, vmlinux_data; +static struct vm_struct vmlinux_text, vmlinux_rodata, vmlinux_init, + vmlinux_data, vmlinux_robss, vmlinux_swapper;
arch/arm64/mm/mmu.c static void __init map_kernel_segment(pgd_t *pgd, void *va_start, void *va_end, · line 443
Quoted 10 lines
arch/arm64/mm/mmu.c +4
-1map_kernel_segment (pgd, _text, _etext,PAGE_KERNEL_EXEC , &vmlinux_text);map_kernel_segment (pgd, __start_rodata, __init_begin,PAGE_KERNEL , &vmlinux_rodata);map_kernel_segment (pgd, __init_begin, __init_end,PAGE_KERNEL_EXEC , &vmlinux_init); -map_kernel_segment (pgd, _data, _end,PAGE_KERNEL , &vmlinux_data); +map_kernel_segment (pgd, _data, __robss_start,PAGE_KERNEL , + &vmlinux_data); +map_kernel_segment (pgd, __robss_start, __robss_end,PAGE_KERNEL_RO , + &vmlinux_robss);
I realised it is actually unnecessary to map the idmap and the zero page into the kernel mapping, so we could drop this line.
Mark Rutland
reviewerPATCH v3 1/1 arm64: mm: move zero page from .bss to right before swapper_pg_dir
Quoted 9 lines · Ard Biesheuvel
Quoted 8 lines · Ard Biesheuvel
Move the statically allocated zero page from the .bss section to right before
swapper_pg_dir. This allows us to refer to its physical address by simply readingTTBR1_EL1(which always points toswapper_pg_dirand always has its ASID field cleared), and subtractingPAGE_SIZE.To protect the zero page from inadvertent modification, carve out a segment that covers it as well as idmap_pg_dir[], and mark it read-only in both the primary and the linear mappings of the kernel.
[...]
Quoted 8 lines
arch/arm64/mm/mmu.c static void __init map_kernel_segment(pgd_t *pgd, void *va_start, void *va_end, · line 443
Quoted 5 lines
arch/arm64/mm/mmu.c +4
-1-map_kernel_segment (pgd, _data, _end,PAGE_KERNEL , &vmlinux_data); +map_kernel_segment (pgd, _data, __robss_start,PAGE_KERNEL , + &vmlinux_data); +map_kernel_segment (pgd, __robss_start, __robss_end,PAGE_KERNEL_RO , + &vmlinux_robss);I realised it is actually unnecessary to map the idmap and the zero page into the kernel mapping, so we could drop this line.
Given that drivers use the zero page, I wouldn't be entirely surprised to see phys_to_virt(virt_to_phys(zero_page)) happen indirectly, and the end result read. Are we sure that doesn't happen anywhere?
For the idmap, I think we might walk that were we to take a fault (though perhaps we don't). Otherwise, unless we add a sysfs walker for it I guess we don't strictly need it in the linear map.
Thanks,
Mark.
PATCH v3 1/1 arm64: mm: move zero page from .bss to right before swapper_pg_dir
Quoted 19 lines · Mark Rutland
Quoted 9 lines · Ard Biesheuvel
Quoted 8 lines · Ard Biesheuvel
Move the statically allocated zero page from the .bss section to right before
swapper_pg_dir. This allows us to refer to its physical address by simply readingTTBR1_EL1(which always points toswapper_pg_dirand always has its ASID field cleared), and subtractingPAGE_SIZE.To protect the zero page from inadvertent modification, carve out a segment that covers it as well as idmap_pg_dir[], and mark it read-only in both the primary and the linear mappings of the kernel.
[...]
Quoted 6 lines
Quoted 5 lines
arch/arm64/mm/mmu.c +4
-1-map_kernel_segment (pgd, _data, _end,PAGE_KERNEL , &vmlinux_data); +map_kernel_segment (pgd, _data, __robss_start,PAGE_KERNEL , + &vmlinux_data); +map_kernel_segment (pgd, __robss_start, __robss_end,PAGE_KERNEL_RO , + &vmlinux_robss);
Quoted 7 lines
Quoted 2 lines
I realised it is actually unnecessary to map the idmap and the zero page into the kernel mapping, so we could drop this line.
Given that drivers use the zero page, I wouldn't be entirely surprised to see phys_to_virt(virt_to_phys(zero_page)) happen indirectly, and the end result read. Are we sure that doesn't happen anywhere?
That conversion would actually still work, it would be the direct reference that is left unmapped. But given that it is mapped R/O anyway (which is the whole point of the patch), it makes more sense to follow the principle of least surprise, and make the direct symbol dereference work as expected.
Quoted 4 lines
For the idmap, I think we might walk that were we to take a fault (though perhaps we don't). Otherwise, unless we add a sysfs walker for it I guess we don't strictly need it in the linear map.
Likewise, this is the kernel mapping not the linear mapping. But given how little this matters, please forget I said anything :-)