arm64/mm: Enable color zero pages
Subsystems (2)
v2 cover arm64/mm: Enable color zero pages
The feature of color zero pages isn't enabled on arm64, meaning all read-only (anonymous) VM areas are backed up by same zero page. It leads pressure to L1 (data) cache on reading data from them. This tries to enable color zero pages.
PATCH[1/2] decouples the zero PGD table from zero page PATCH[2/2] allocates the needed zero pages according to L1 cache size
Changelog
=========
v2:
* Rebased to 5.9.rc6 (Gavin)
* Retrieve cache topology from ACPI/DT (Will/Robin)
Gavin Shan (2):
arm64/mm: Introduce zero PGD table
arm64/mm: Enable color zero pages
arch/arm64/include/asm/cache.h | 3 ++ arch/arm64/include/asm/mmu_context.h | 6 +-- arch/arm64/include/asm/pgtable.h | 11 ++++- arch/arm64/kernel/cacheinfo.c | 67 ++++++++++++++++++++++++++++ arch/arm64/kernel/setup.c | 2 +- arch/arm64/kernel/vmlinux.lds.S | 4 ++ arch/arm64/mm/init.c | 37 +++++++++++++++ arch/arm64/mm/mmu.c | 7 --- arch/arm64/mm/proc.S | 2 +- drivers/base/cacheinfo.c | 3 +- include/linux/cacheinfo.h | 6 +++ 11 files changed, 132 insertions(+), 16 deletions(-)
Signature · 1 line
2.23.0
PATCH v2 1/2 arm64/mm: Introduce zero PGD table
The zero PGD table is used when TTBR_EL1 is changed. It's exactly the zero page. As the zero page(s) will be allocated dynamically when colored zero page feature is enabled in subsequent patch. the zero page(s) aren't usable during early boot stage.
This introduces zero PGD table, which is decoupled from the zero page(s).
--- arch/arm64/include/asm/mmu_context.h | 6 +++--- arch/arm64/include/asm/pgtable.h | 2 ++ arch/arm64/kernel/setup.c | 2 +- arch/arm64/kernel/vmlinux.lds.S | 4 ++++ arch/arm64/mm/proc.S | 2 +- 5 files changed, 11 insertions(+), 5 deletions(-)
arch/arm64/include/asm/mmu_context.h +3 -3
@@ -36,11 +36,11 @@ static inline void contextidr_thread_switch(struct task_struct *next) }/* -* Set TTBR0 to empty_zero_page. No translations will be possible via TTBR0. +* Set TTBR0 to zero_pg_dir. No translations will be possible via TTBR0. */ static inline void cpu_set_reserved_ttbr0 (void ) { -unsigned long ttbr =phys_to_ttbr (__pa_symbol (empty_zero_page)); +unsigned long ttbr =phys_to_ttbr (__pa_symbol (zero_pg_dir));write_sysreg (ttbr, ttbr0_el1);isb ();
@@ -189,7 +189,7 @@ static inline void update_saved_ttbr0(struct task_struct *tsk,return ;if (mm == &init_mm) - ttbr =__pa_symbol (empty_zero_page); + ttbr =__pa_symbol (zero_pg_dir);else ttbr =virt_to_phys (mm->pgd) |ASID (mm) <<48 ;
arch/arm64/include/asm/pgtable.h +2 -0
@@ -474,6 +474,8 @@ static inline bool pud_table(pud_t pud) { return true; } PUD_TYPE_TABLE)#endif +extern pgd_t zero_pg_dir[PTRS_PER_PGD ]; +extern pgd_t zero_pg_end[];extern pgd_t init_pg_dir[PTRS_PER_PGD ];extern pgd_t init_pg_end[];extern pgd_t swapper_pg_dir[PTRS_PER_PGD ];
arch/arm64/kernel/setup.c +1 -1
@@ -366,7 +366,7 @@ void __init __no_sanitize_address setup_arch(char **cmdline_p)* faults in case uaccess_enable() is inadvertently called by the init * thread. */ - init_task.thread_info.ttbr0 =__pa_symbol (empty_zero_page); + init_task.thread_info.ttbr0 =__pa_symbol (zero_pg_dir);#endif if (boot_args[1 ] || boot_args[2 ] || boot_args[3 ]) {
arch/arm64/kernel/vmlinux.lds.S +4 -0
@@ -137,6 +137,10 @@ SECTIONS /* everything from this point to __init_begin will be marked RO NX */ RO_DATA(PAGE_SIZE) + zero_pg_dir = .; + . += PAGE_SIZE; + zero_pg_end = .; + idmap_pg_dir = .; . += IDMAP_DIR_SIZE; idmap_pg_end = .;
arch/arm64/mm/proc.S +1 -1
@@ -163,7 +163,7 @@ SYM_FUNC_END(cpu_do_resume) .pushsection ".idmap.text", "awx" .macro __idmap_cpu_set_reserved_ttbr1, tmp1, tmp2 - adrp \tmp1, empty_zero_page + adrp \tmp1, zero_pg_dir phys_to_ttbr \tmp2, \tmp1 offset_ttbr1 \tmp2, \tmp1 msr ttbr1_el1, \tmp2
Signature · 1 line
2.23.0
PATCH v2 2/2 arm64/mm: Enable color zero pages
In reply to Gavin ShanThis enables color zero pages by allocating contiguous page frames for it. The number of pages for this is determined by L1 dCache (or iCache) size, which is probbed from the hardware.
* Export cache_setup_of_node() so that the cache topology could be parsed from device-tree.
* Add cache_get_info() so that L1 dCache size can be retrieved.
* Implement setup_zero_pages(), which is called after the page allocator begins to work, to allocate the contiguous pages needed by color zero page.
* Reworked ZERO_PAGE() and define __HAVE_COLOR_ZERO_PAGE.
--- arch/arm64/include/asm/cache.h | 3 ++ arch/arm64/include/asm/pgtable.h | 9 ++++- arch/arm64/kernel/cacheinfo.c | 67 ++++++++++++++++++++++++++++++++ arch/arm64/mm/init.c | 37 ++++++++++++++++++ arch/arm64/mm/mmu.c | 7 ---- drivers/base/cacheinfo.c | 3 +- include/linux/cacheinfo.h | 6 +++ 7 files changed, 121 insertions(+), 11 deletions(-)
arch/arm64/include/asm/cache.h +3 -0
@@ -89,6 +89,9 @@ static inline int cache_line_size_of_cpu(void) }int cache_line_size (void ); +unsigned int cache_get_info (unsigned int level,unsigned int type, +unsigned int *sets,unsigned int *ways, +unsigned int *cl_size);/* * Read the effective value of CTR_EL0.
arch/arm64/include/asm/pgtable.h +7 -2
@@ -54,8 +54,13 @@ extern void __pgd_error(const char *file, int line, unsigned long val);* ZERO_PAGE is a global shared page that is always zero: used * for zero-mapped memory areas etc.. */ -extern unsigned long empty_zero_page[PAGE_SIZE /sizeof (unsigned long )]; -#define ZERO_PAGE (vaddr) phys_to_page(__pa_symbol(empty_zero_page)) +extern unsigned long empty_zero_page; +extern unsigned long zero_page_mask; + +#define __HAVE_COLOR_ZERO_PAGE +#define ZERO_PAGE (vaddr) \ + (virt_to_page((void *)(empty_zero_page + \ + (((unsigned long)(vaddr)) & zero_page_mask))))#define pte_ERROR (pte) __pte_error(__FILE__, __LINE__, pte_val(pte))
arch/arm64/kernel/cacheinfo.c +67 -0
73 lines not on this page · raw message
arch/arm64/mm/init.c +37 -0
@@ -18,6 +18,7 @@#include <linux/gfp.h> #include <linux/memblock.h> #include <linux/sort.h> +#include <linux/cacheinfo.h> #include <linux/of.h> #include <linux/of_fdt.h> #include <linux/dma-direct.h>
@@ -69,6 +70,11 @@ EXPORT_SYMBOL(vmemmap);phys_addr_t arm64_dma_phys_limit__ro_after_init ;static phys_addr_t arm64_dma32_phys_limit__ro_after_init ; +unsigned long empty_zero_page; +EXPORT_SYMBOL (empty_zero_page); +unsigned long zero_page_mask; +EXPORT_SYMBOL (zero_page_mask); +#ifdef CONFIG_KEXEC_CORE /* * reserve_crashkernel() - reserves memory for crash kernel
@@ -507,6 +513,36 @@ static void __init free_unused_memmap(void) }#endif /* !CONFIG_SPARSEMEM_VMEMMAP */ +static void __init setup_zero_pages (void ) +{ +struct page *page; +unsigned int size; +int order, i; + + size =cache_get_info (1 ,CACHE_TYPE_DATA ,NULL ,NULL ,NULL ); + order = size >0 ?get_order (PAGE_ALIGN (size)) :0 ; + order =min (order,MAX_ORDER -1 ); + +do { + empty_zero_page =__get_free_pages (GFP_KERNEL | __GFP_ZERO, + order); +if (empty_zero_page) +break ; + }while (--order >=0 ); + +if (!empty_zero_page) +panic ("%s: out of memory\n" , __func__); + + page =virt_to_page ((void *) empty_zero_page); +split_page (page, order); +for (i =1 << order; i >0 ; i--) { +mark_page_reserved (page); + page++; + } + + zero_page_mask = ((PAGE_SIZE << order) -1 ) &PAGE_MASK ; +} +/* * mem_init() marks the free areas in the mem_map and tells us how much memory * is free. This is done after various parts of the system have claimed their
@@ -527,6 +563,7 @@ void __init mem_init(void)#endif /* this will put all unused low memory onto the freelists */ memblock_free_all (); +setup_zero_pages ();mem_init_print_info (NULL );
arch/arm64/mm/mmu.c +0 -7
@@ -49,13 +49,6 @@ EXPORT_SYMBOL(vabits_actual);u64 kimage_voffset__ro_after_init ;EXPORT_SYMBOL (kimage_voffset); -/* -* Empty_zero_page is a special page that is used for zero-initialized data -* and COW. -*/ -unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss; -EXPORT_SYMBOL (empty_zero_page); - static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss; static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss __maybe_unused; static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused;
drivers/base/cacheinfo.c +1 -2
@@ -153,7 +153,7 @@ static void cache_of_set_props(struct cacheinfo *this_leaf,cache_associativity (this_leaf); } -static int cache_setup_of_node (unsigned int cpu) +int cache_setup_of_node (unsigned int cpu) {struct device_node *np;struct cacheinfo *this_leaf;
@@ -195,7 +195,6 @@ static int cache_setup_of_node(unsigned int cpu) return 0; }#else -static inline int cache_setup_of_node (unsigned int cpu) {return 0 ; }static inline bool cache_leaves_are_shared (struct cacheinfo *this_leaf,struct cacheinfo *sib_leaf) {
include/linux/cacheinfo.h +6 -0
@@ -100,6 +100,12 @@ struct cpu_cacheinfo *get_cpu_cacheinfo(unsigned int cpu);int init_cache_level (unsigned int cpu);int populate_cache_leaves (unsigned int cpu);int cache_setup_acpi (unsigned int cpu); +#ifdef CONFIG_OF +int cache_setup_of_node (unsigned int cpu); +#else +static inline int cache_setup_of_node (unsigned int cpu) {return 0 ; } +#endif +#ifndef CONFIG_ACPI_PPTT /* * acpi_find_last_cache_level is only called on ACPI enabled
Signature · 1 line
2.23.0
Catalin Marinas
maintainerHi Gavin,
Quoted 7 lines · Gavin Shan
The feature of color zero pages isn't enabled on arm64, meaning all read-only (anonymous) VM areas are backed up by same zero page. It leads pressure to L1 (data) cache on reading data from them. This tries to enable color zero pages.
PATCH[1/2] decouples the zero PGD table from zero page PATCH[2/2] allocates the needed zero pages according to L1 cache size
To save you (and potential reviewers) some time, please include in the cover letter details of a realistic workload/benchmark that is improved by this patchset, backed by numbers. Just because it's doable and the patches aren't too complex is not a good enough reason for merging.
Thanks.
Signature · 1 line
Catalin
Hi Catalin,
Quoted 6 lines · Catalin Marinas
Quoted 5 lines · Gavin Shan
The feature of color zero pages isn't enabled on arm64, meaning all read-only (anonymous) VM areas are backed up by same zero page. It leads pressure to L1 (data) cache on reading data from them. This tries to enable color zero pages.
Quoted 8 lines
Quoted 2 lines
PATCH[1/2] decouples the zero PGD table from zero page PATCH[2/2] allocates the needed zero pages according to L1 cache size
To save you (and potential reviewers) some time, please include in the cover letter details of a realistic workload/benchmark that is improved by this patchset, backed by numbers. Just because it's doable and the patches aren't too complex is not a good enough reason for merging.
Sure, Please ignore this (v2) series for now. I'll try to provide performance data in v3 even I'm not positive about that because it depends on CPU's L1 dCache size :)
Cheers,
Gavin