mm: folio_zero_user: clear contiguous pages
Subsystems (23)
v8 cover mm: folio_zero_user: clear contiguous pages
This series adds clearing of contiguous page ranges for hugepages, improving on the current page-at-a-time approach in two ways:
- amortizes the per-page setup cost over a larger extent
- when using string instructions, exposes the real region size to the processor.
A processor could use a knowledge of the extent to optimize the clearing. AMD Zen uarchs, as an example, elide allocation of cachelines for regions larger than L3-size.
Demand faulting a 64GB region shows performance improvements:
$ perf bench mem map -p $pg-sz -f demand -s 64GB -l 5
baseline +series change
(GB/s +- %stdev) (GB/s +- %stdev)
pg-sz=2MB 12.92 +- 2.55% 17.03 +- 0.70% + 31.8% preempt=*
pg-sz=1GB 17.14 +- 2.27% 18.04 +- 1.05% [#] + 5.2% preempt=none|voluntary pg-sz=1GB 17.26 +- 1.24% 42.17 +- 4.21% +144.3% preempt=full|lazy
[#] Milan uses a threshold of LLC-size (~32MB) for eliding cacheline allocation, which is higher than the maximum extent used on x86 (ARCH_CONTIG_PAGE_NR=8MB), so preempt=none|voluntary sees no improvement with pg-sz=1GB.
The anon-w-seq test in the vm-scalability benchmark, however, does show worse performance with utime increasing by ~9%:
stime utime
baseline 1654.63 ( +- 3.84% ) 811.00 ( +- 3.84% ) +series 1630.32 ( +- 2.73% ) 886.37 ( +- 5.19% )
In part this is because anon-w-seq runs with 384 processes zeroing anonymously mapped memory which they then access sequentially. As such this is a likely uncommon pattern where the memory bandwidth is saturated while also being cache limited because we access the entire region.
Raghavendra also tested previous version of the series on AMD Genoa [1].
Changelog:
v8:
- make clear_user_highpages(), clear_user_pages() and clear_pages() more robust across architectures. (Thanks David!)
- split up folio_zero_user() changes into ones for clearing contiguous regions and those for maintaining temporal locality since they have different performance profiles (Suggested by Andrew Morton.)
- added Raghavendra's Reviewed-by, Tested-by.
- get rid of nth_page()
- perf related patches have been pulled already. Remove them.
v7:
- interface cleanups, comments for clear_user_highpages(), clear_user_pages(), clear_pages().
- fixed build errors flagged by kernel test robot
(https://lore.kernel.org/lkml/[email protected]/)
v6:
- perf bench mem: update man pages and other cleanups (Namhyung Kim)
- unify folio_zero_user() for HIGHMEM, !HIGHMEM options instead of working through a new config option (David Hildenbrand).
- cleanups and simlification around that.
(https://lore.kernel.org/lkml/[email protected]/)
v5:
- move the non HIGHMEM implementation of folio_zero_user() from x86 to common code (Dave Hansen)
- Minor naming cleanups, commit messages etc
(https://lore.kernel.org/lkml/[email protected]/)
v4:
- adds perf bench workloads to exercise mmap() populate/demand-fault (Ingo)
- inline stosb etc (PeterZ)
- handle cooperative preemption models (Ingo)
- interface and other cleanups all over (Ingo)
(https://lore.kernel.org/lkml/[email protected]/)
v3:
- get rid of preemption dependency (TIF_ALLOW_RESCHED); this version was limited to preempt=full|lazy.
- override folio_zero_user() (Linus)
(https://lore.kernel.org/lkml/[email protected]/)
v2:
- addressed review comments from peterz, tglx.
- Removed clear_user_pages(), and CONFIG_X86_32:clear_pages()
- General code cleanup
(https://lore.kernel.org/lkml/[email protected]/)
Comments appreciated!
Also at:
github.com/terminus/linux clear-pages.v7
[1] https://lore.kernel.org/lkml/[email protected]/
Ankur Arora (6):
mm: introduce clear_pages() and clear_user_pages() mm/highmem: introduce clear_user_highpages() x86/mm: Simplify clear_page_* x86/clear_page: Introduce clear_pages() mm, folio_zero_user: support clearing page ranges
mm: folio_zero_user: cache neighbouring pages
David Hildenbrand (1):
treewide: provide a generic clear_user_page() variant
arch/alpha/include/asm/page.h | 1 - arch/arc/include/asm/page.h | 2 + arch/arm/include/asm/page-nommu.h | 1 - arch/arm64/include/asm/page.h | 1 - arch/csky/abiv1/inc/abi/page.h | 1 + arch/csky/abiv2/inc/abi/page.h | 7 --- arch/hexagon/include/asm/page.h | 1 - arch/loongarch/include/asm/page.h | 1 - arch/m68k/include/asm/page_mm.h | 1 + arch/m68k/include/asm/page_no.h | 1 - arch/microblaze/include/asm/page.h | 1 - arch/mips/include/asm/page.h | 1 + arch/nios2/include/asm/page.h | 1 + arch/openrisc/include/asm/page.h | 1 - arch/parisc/include/asm/page.h | 1 - arch/powerpc/include/asm/page.h | 1 + arch/riscv/include/asm/page.h | 1 - arch/s390/include/asm/page.h | 1 - arch/sparc/include/asm/page_32.h | 2 + arch/sparc/include/asm/page_64.h | 1 + arch/um/include/asm/page.h | 1 - arch/x86/include/asm/page.h | 6 --- arch/x86/include/asm/page_32.h | 6 +++ arch/x86/include/asm/page_64.h | 64 ++++++++++++++++++----- arch/x86/lib/clear_page_64.S | 39 +++----------- arch/xtensa/include/asm/page.h | 1 - include/linux/highmem.h | 29 +++++++++++ include/linux/mm.h | 69 +++++++++++++++++++++++++ mm/memory.c | 82 ++++++++++++++++++++++-------- mm/util.c | 13 +++++ 30 files changed, 247 insertions(+), 91 deletions(-)
Signature · 1 line
2.43.5
PATCH v8 1/7 treewide: provide a generic clear_user_page() variant
From: David Hildenbrand <[email protected]>
Let's drop all variants that effectively map to clear_page() and provide it in a generic variant instead.
We'll use __HAVE_ARCH_CLEAR_USER_PAGE, similar to __HAVE_ARCH_COPY_USER_HIGHPAGE, to indicate whether an architecture provides it's own variant.
We have to be a bit careful if an architecture provides a custom clear_user_highpage(), because then it's very likely that some special flushing magic is happening behind the scenes.
Maybe at some point these should be CONFIG_ options.
Note that for parisc, clear_page() and clear_user_page() map to clear_page_asm(), so we can just get rid of the custom clear_user_page() implementation. There is a clear_user_page_asm() function on parisc, that seems to be unused. Not sure what's up with that.
--- arch/alpha/include/asm/page.h | 1 - arch/arc/include/asm/page.h | 2 ++ arch/arm/include/asm/page-nommu.h | 1 - arch/arm64/include/asm/page.h | 1 - arch/csky/abiv1/inc/abi/page.h | 1 + arch/csky/abiv2/inc/abi/page.h | 7 ------- arch/hexagon/include/asm/page.h | 1 - arch/loongarch/include/asm/page.h | 1 - arch/m68k/include/asm/page_mm.h | 1 + arch/m68k/include/asm/page_no.h | 1 - arch/microblaze/include/asm/page.h | 1 - arch/mips/include/asm/page.h | 1 + arch/nios2/include/asm/page.h | 1 + arch/openrisc/include/asm/page.h | 1 - arch/parisc/include/asm/page.h | 1 - arch/powerpc/include/asm/page.h | 1 + arch/riscv/include/asm/page.h | 1 - arch/s390/include/asm/page.h | 1 - arch/sparc/include/asm/page_32.h | 2 ++ arch/sparc/include/asm/page_64.h | 1 + arch/um/include/asm/page.h | 1 - arch/x86/include/asm/page.h | 6 ------ arch/xtensa/include/asm/page.h | 1 - include/linux/mm.h | 22 ++++++++++++++++++++++ 24 files changed, 32 insertions(+), 26 deletions(-)
arch/alpha/include/asm/page.h +0 -1
@@ -11,7 +11,6 @@#define STRICT_MM_TYPECHECKS extern void clear_page (void *page); -#define clear_user_page (page, vaddr, pg) clear_page(page)#define vma_alloc_zeroed_movable_folio (vma, vaddr) \ vma_alloc_folio(GFP_HIGHUSER_MOVABLE | __GFP_ZERO, 0, vma, vaddr)
arch/arc/include/asm/page.h +2 -0
@@ -32,6 +32,8 @@ struct page;void copy_user_highpage (struct page *to,struct page *from,unsigned long u_vaddr,struct vm_area_struct *vma); + +#define __HAVE_ARCH_CLEAR_USER_PAGEvoid clear_user_page (void *to,unsigned long u_vaddr,struct page *page); typedef struct {
arch/arm/include/asm/page-nommu.h +0 -1
@@ -11,7 +11,6 @@#define clear_page (page) memset((page), 0, PAGE_SIZE)#define copy_page (to,from) memcpy((to), (from), PAGE_SIZE) -#define clear_user_page (page, vaddr, pg) clear_page(page)#define copy_user_page (to, from, vaddr, pg) copy_page(to, from)/*
arch/arm64/include/asm/page.h +0 -1
@@ -36,7 +36,6 @@ struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma,void tag_clear_highpage (struct page *to);#define __HAVE_ARCH_TAG_CLEAR_HIGHPAGE -#define clear_user_page (page, vaddr, pg) clear_page(page)#define copy_user_page (to, from, vaddr, pg) copy_page(to, from)typedef struct page *pgtable_t ;
arch/csky/abiv1/inc/abi/page.h +1 -0
@@ -10,6 +10,7 @@ static inline unsigned long pages_do_alias(unsigned long addr1,return (addr1 ^ addr2) & (SHMLBA -1 ); } +#define __HAVE_ARCH_CLEAR_USER_PAGEstatic inline void clear_user_page (void *addr,unsigned long vaddr,struct page *page) {
arch/csky/abiv2/inc/abi/page.h +0 -7
@@ -1,11 +1,4 @@/* SPDX-License-Identifier: GPL-2.0 */ - -static inline void clear_user_page (void *addr,unsigned long vaddr, -struct page *page) -{ -clear_page (addr); -} -static inline void copy_user_page (void *to,void *from,unsigned long vaddr,struct page *page) {
arch/hexagon/include/asm/page.h +0 -1
@@ -113,7 +113,6 @@ static inline void clear_page(void *page)/* * Under assumption that kernel always "sees" user map... */ -#define clear_user_page (page, vaddr, pg) clear_page(page)#define copy_user_page (to, from, vaddr, pg) copy_page(to, from)static inline unsigned long virt_to_pfn (const void *kaddr)
arch/loongarch/include/asm/page.h +0 -1
@@ -30,7 +30,6 @@extern void clear_page (void *page);extern void copy_page (void *to,void *from); -#define clear_user_page (page, vaddr, pg) clear_page(page)#define copy_user_page (to, from, vaddr, pg) copy_page(to, from)extern unsigned long shm_align_mask;
arch/m68k/include/asm/page_mm.h +1 -0
@@ -52,6 +52,7 @@ static inline void clear_page(void *page)#define copy_page (to,from) memcpy((to), (from), PAGE_SIZE)#endif +#define __HAVE_ARCH_CLEAR_USER_PAGE#define clear_user_page (addr, vaddr, page) \ do { clear_page(addr); \ flush_dcache_page(page); \
arch/m68k/include/asm/page_no.h +0 -1
@@ -10,7 +10,6 @@ extern unsigned long memory_end;#define clear_page (page) memset((page), 0, PAGE_SIZE)#define copy_page (to,from) memcpy((to), (from), PAGE_SIZE) -#define clear_user_page (page, vaddr, pg) clear_page(page)#define copy_user_page (to, from, vaddr, pg) copy_page(to, from)#define vma_alloc_zeroed_movable_folio (vma, vaddr) \
arch/microblaze/include/asm/page.h +0 -1
@@ -45,7 +45,6 @@ typedef unsigned long pte_basic_t;# define copy_page (to, from) memcpy((to), (from), PAGE_SIZE)# define clear_page (pgaddr) memset((pgaddr), 0, PAGE_SIZE) -# define clear_user_page (pgaddr, vaddr, page) memset((pgaddr), 0, PAGE_SIZE)# define copy_user_page (vto, vfrom, vaddr, topg) \ memcpy((vto), (vfrom), PAGE_SIZE)
arch/mips/include/asm/page.h +1 -0
@@ -90,6 +90,7 @@ static inline void clear_user_page(void *addr, unsigned long vaddr,if (pages_do_alias ((unsigned long ) addr, vaddr &PAGE_MASK ))flush_data_cache_page ((unsigned long )addr); } +#define __HAVE_ARCH_CLEAR_USER_PAGEstruct vm_area_struct ; extern void copy_user_highpage(struct page *to, struct page *from,
arch/nios2/include/asm/page.h +1 -0
@@ -45,6 +45,7 @@struct page ; +#define __HAVE_ARCH_CLEAR_USER_PAGEextern void clear_user_page (void *addr,unsigned long vaddr,struct page *page);extern void copy_user_page (void *vto,void *vfrom,unsigned long vaddr,struct page *to);
arch/openrisc/include/asm/page.h +0 -1
@@ -30,7 +30,6 @@#define clear_page (page) memset((page), 0, PAGE_SIZE)#define copy_page (to, from) memcpy((to), (from), PAGE_SIZE) -#define clear_user_page (page, vaddr, pg) clear_page(page)#define copy_user_page (to, from, vaddr, pg) copy_page(to, from)/*
arch/parisc/include/asm/page.h +0 -1
@@ -21,7 +21,6 @@ struct vm_area_struct;void clear_page_asm (void *page);void copy_page_asm (void *to,void *from); -#define clear_user_page (vto, vaddr, page) clear_page_asm(vto)void copy_user_highpage (struct page *to,struct page *from,unsigned long vaddr,struct vm_area_struct *vma);#define __HAVE_ARCH_COPY_USER_HIGHPAGE
arch/powerpc/include/asm/page.h +1 -0
@@ -271,6 +271,7 @@ static inline const void *pfn_to_kaddr(unsigned long pfn)struct page ;extern void clear_user_page (void *page,unsigned long vaddr,struct page *pg); +#define __HAVE_ARCH_CLEAR_USER_PAGEextern void copy_user_page (void *to,void *from,unsigned long vaddr,struct page *p);extern int devmem_is_allowed (unsigned long pfn);
arch/riscv/include/asm/page.h +0 -1
@@ -50,7 +50,6 @@ void clear_page(void *page);#endif #define copy_page (to, from) memcpy((to), (from), PAGE_SIZE) -#define clear_user_page (pgaddr, vaddr, page) clear_page(pgaddr)#define copy_user_page (vto, vfrom, vaddr, topg) \ memcpy((vto), (vfrom), PAGE_SIZE)
arch/s390/include/asm/page.h +0 -1
@@ -65,7 +65,6 @@ static inline void copy_page(void *to, void *from) : : "memory", "cc"); } -#define clear_user_page (page, vaddr, pg) clear_page(page)#define copy_user_page (to, from, vaddr, pg) copy_page(to, from)#define vma_alloc_zeroed_movable_folio (vma, vaddr) \
arch/sparc/include/asm/page_32.h +2 -0
@@ -17,6 +17,8 @@#define clear_page (page) memset((void *)(page), 0, PAGE_SIZE)#define copy_page (to,from) memcpy((void *)(to), (void *)(from), PAGE_SIZE) + +#define __HAVE_ARCH_CLEAR_USER_PAGE#define clear_user_page (addr, vaddr, page) \ do { clear_page(addr); \ sparc_flush_page_to_ram(page); \
arch/sparc/include/asm/page_64.h +1 -0
@@ -43,6 +43,7 @@ void _clear_page(void *page);#define clear_page (X ) _clear_page((void *)(X))struct page ;void clear_user_page (void *addr,unsigned long vaddr,struct page *page); +#define __HAVE_ARCH_CLEAR_USER_PAGE#define copy_page (X ,Y ) memcpy((void *)(X), (void *)(Y), PAGE_SIZE)void copy_user_page (void *to,void *from,unsigned long vaddr,struct page *topage);#define __HAVE_ARCH_COPY_USER_HIGHPAGE
arch/um/include/asm/page.h +0 -1
@@ -26,7 +26,6 @@ struct page;#define clear_page (page) memset((void *)(page), 0, PAGE_SIZE)#define copy_page (to,from) memcpy((void *)(to), (void *)(from), PAGE_SIZE) -#define clear_user_page (page, vaddr, pg) clear_page(page)#define copy_user_page (to, from, vaddr, pg) copy_page(to, from)typedef struct {unsigned long pte; }pte_t ;
arch/x86/include/asm/page.h +0 -6
@@ -22,12 +22,6 @@ struct page;extern struct range pfn_mapped[];extern int nr_pfn_mapped; -static inline void clear_user_page (void *page,unsigned long vaddr, -struct page *pg) -{ -clear_page (page); -} -static inline void copy_user_page (void *to,void *from,unsigned long vaddr,struct page *topage) {
arch/xtensa/include/asm/page.h +0 -1
@@ -126,7 +126,6 @@ void clear_user_highpage(struct page *page, unsigned long vaddr);void copy_user_highpage (struct page *to,struct page *from,unsigned long vaddr,struct vm_area_struct *vma);#else -# define clear_user_page (page, vaddr, pg) clear_page(page)# define copy_user_page (to, from, vaddr, pg) copy_page(to, from)#endif
include/linux/mm.h +22 -0
@@ -3872,6 +3872,28 @@ static inline void clear_page_guard(struct zone *zone, struct page *page, unsigned int order) {}#endif /* CONFIG_DEBUG_PAGEALLOC */ +#ifndef __HAVE_ARCH_CLEAR_USER_PAGE +/** +* clear_user_page() - clear a page to be mapped to user space +* @addr: the address of the page +* @vaddr: the address of the user mapping +* @page: the page +*/ +static inline void clear_user_page (void *addr,unsigned long vaddr,struct page *page) +{ +#ifdef clear_user_highpage +/* +* If an architecture defines its own clear_user_highpage() variant, +* then we have to be a bit more careful here and cannot simply +* rely on clear_page(). +*/ +clear_user_highpage (page, vaddr); +#else +clear_page (addr); +#endif +} +#endif +#ifdef __HAVE_ARCH_GATE_AREAextern struct vm_area_struct *get_gate_vma (struct mm_struct *mm);extern int in_gate_area_no_mm (unsigned long addr);
Signature · 1 line
2.43.5
David Hildenbrand (Red Hat)
maintainerRe: [PATCH v8 1/7] treewide: provide a generic clear_user_page() variant
Quoted 65 lines · Ankur Arora
From: David Hildenbrand <[email protected]>
Let's drop all variants that effectively map to
clear_page()and provide it in a generic variant instead.We'll use
__HAVE_ARCH_CLEAR_USER_PAGE, similar to__HAVE_ARCH_COPY_USER_HIGHPAGE, to indicate whether an architecture provides it's own variant.We have to be a bit careful if an architecture provides a custom
clear_user_highpage(), because then it's very likely that some special flushing magic is happening behind the scenes.Maybe at some point these should be
CONFIG_options.Note that for parisc,
clear_page()andclear_user_page()map toclear_page_asm(), so we can just get rid of the customclear_user_page()implementation. There is aclear_user_page_asm()function on parisc, that seems to be unused. Not sure what's up with that.Signed-off-by David Hildenbrand <[email protected]>--- arch/alpha/include/asm/page.h | 1 - arch/arc/include/asm/page.h | 2 ++ arch/arm/include/asm/page-nommu.h | 1 - arch/arm64/include/asm/page.h | 1 - arch/csky/abiv1/inc/abi/page.h | 1 + arch/csky/abiv2/inc/abi/page.h | 7 ------- arch/hexagon/include/asm/page.h | 1 - arch/loongarch/include/asm/page.h | 1 - arch/m68k/include/asm/page_mm.h | 1 + arch/m68k/include/asm/page_no.h | 1 - arch/microblaze/include/asm/page.h | 1 - arch/mips/include/asm/page.h | 1 + arch/nios2/include/asm/page.h | 1 + arch/openrisc/include/asm/page.h | 1 - arch/parisc/include/asm/page.h | 1 - arch/powerpc/include/asm/page.h | 1 + arch/riscv/include/asm/page.h | 1 - arch/s390/include/asm/page.h | 1 - arch/sparc/include/asm/page_32.h | 2 ++ arch/sparc/include/asm/page_64.h | 1 + arch/um/include/asm/page.h | 1 - arch/x86/include/asm/page.h | 6 ------ arch/xtensa/include/asm/page.h | 1 - include/linux/mm.h | 22 ++++++++++++++++++++++ 24 files changed, 32 insertions(+), 26 deletions(-)arch/alpha/include/asm/page.h +0
-1@@ -11,7 +11,6 @@#define STRICT_MM_TYPECHECKS extern void clear_page (void *page); -#define clear_user_page (page, vaddr, pg) clear_page(page)#define vma_alloc_zeroed_movable_folio (vma, vaddr) \ vma_alloc_folio(GFP_HIGHUSER_MOVABLE | __GFP_ZERO, 0, vma, vaddr)arch/arc/include/asm/page.h +0
-0
arch/arc/include/asm/page.h struct page; · line 36
Quoted 5 lines
arch/arc/include/asm/page.h +2
-0@@ -32,6 +32,8 @@ struct page;void copy_user_highpage (struct page *to,struct page *from,unsigned long u_vaddr,struct vm_area_struct *vma); + +#define __HAVE_ARCH_CLEAR_USER_PAGE
After talking to Linus, the preferred way here is using
#define clear_user_page clear_user_page
Can you adjust the patch (+description) to avoid __HAVE_ARCH_CLEAR_USER_PAGE? If you want me to do it, just let me know.
Signature · 3 lines
Cheers David
PATCH v8 2/7 mm: introduce clear_pages() and clear_user_pages()
In reply to Ankur AroraIntroduce clear_pages(), to be overridden by architectures that support more efficient clearing of consecutive pages.
Also introduce clear_user_pages(), however, we will not expect this function to be overridden anytime soon.
We have to place the clear_user_pages() variant that uses clear_user_page() into mm/util.c for now to work around macro magic on sparc and m68k.
--- include/linux/mm.h | 41 +++++++++++++++++++++++++++++++++++++++++ mm/util.c | 13 +++++++++++++ 2 files changed, 54 insertions(+)
include/linux/mm.h +41 -0
@@ -3872,6 +3872,26 @@ static inline void clear_page_guard(struct zone *zone, struct page *page, unsigned int order) {}#endif /* CONFIG_DEBUG_PAGEALLOC */ +#ifndef __HAVE_ARCH_CLEAR_PAGES +/** +* clear_pages() - clear a page range for kernel-internal use. +* @addr: start address +* @npages: number of pages +* +* Use clear_user_pages() instead when clearing a page range to be +* mapped to user space. +* +* Does absolutely no exception handling. +*/ +static inline void clear_pages (void *addr,unsigned int npages) +{ +do { +clear_page (addr); + addr +=PAGE_SIZE ; + }while (--npages); +} +#endif +#ifndef __HAVE_ARCH_CLEAR_USER_PAGE/** * clear_user_page() - clear a page to be mapped to user space
@@ -3894,6 +3914,27 @@ static inline void clear_user_page(void *addr, unsigned long vaddr, struct page }#endif +/** +* clear_user_pages() - clear a page range to be mapped to user space +* @addr: start address +* @vaddr: start address of the user mapping +* @page: start page +* @npages: number of pages +* +* Assumes that the region (@addr, +@npages) has been validated +* already so this does no exception handling. +*/ +#ifdef __HAVE_ARCH_CLEAR_USER_PAGE +void clear_user_pages (void *addr,unsigned long vaddr, +struct page *page,unsigned int npages); +#else /* !__HAVE_ARCH_CLEAR_USER_PAGE */ +static inline void clear_user_pages (void *addr,unsigned long vaddr, +struct page *page,unsigned int npages) +{ +clear_pages (addr, npages); +} +#endif /* __HAVE_ARCH_CLEAR_USER_PAGE */ +#ifdef __HAVE_ARCH_GATE_AREAextern struct vm_area_struct *get_gate_vma (struct mm_struct *mm);extern int in_gate_area_no_mm (unsigned long addr);
mm/util.c +13 -0
@@ -1344,3 +1344,16 @@ bool page_range_contiguous(const struct page *page, unsigned long nr_pages) }EXPORT_SYMBOL (page_range_contiguous);#endif + +#ifdef __HAVE_ARCH_CLEAR_USER_PAGE +void clear_user_pages (void *addr,unsigned long vaddr, +struct page *page,unsigned int npages) +{ +do { +clear_user_page (addr, vaddr, page); + addr +=PAGE_SIZE ; + vaddr +=PAGE_SIZE ; + page++; + }while (--npages); +} +#endif /* __HAVE_ARCH_CLEAR_USER_PAGE */
Signature · 1 line
2.43.5
Re: [PATCH v8 2/7] mm: introduce clear_pages() and clear_user_pages()
Quoted 6 lines · Ankur Arora
Introduce
clear_pages(), to be overridden by architectures that support more efficient clearing of consecutive pages.Also introduce
clear_user_pages(), however, we will not expect this function to be overridden anytime soon.
Quoted 6 lines
We have to place the
clear_user_pages()variant that usesclear_user_page()intomm/util.cfor now to work around macro magic on sparc and m68k.Signed-off-by Ankur Arora <[email protected]>---
Signature · 3 lines
Cheers David
David Hildenbrand (Red Hat)
maintainerRe: [PATCH v8 2/7] mm: introduce clear_pages() and clear_user_pages()
In reply to Ankur AroraQuoted 21 lines · Ankur Arora
Introduce
clear_pages(), to be overridden by architectures that support more efficient clearing of consecutive pages.Also introduce
clear_user_pages(), however, we will not expect this function to be overridden anytime soon.We have to place the
clear_user_pages()variant that usesclear_user_page()intomm/util.cfor now to work around macro magic on sparc and m68k.Signed-off-by Ankur Arora <[email protected]>--- include/linux/mm.h | 41 +++++++++++++++++++++++++++++++++++++++++ mm/util.c | 13 +++++++++++++ 2 files changed, 54 insertions(+)include/linux/mm.h +0
-0
include/linux/mm.h static inline void clear_page_guard(struct zone *zone, struct page *page, · line 3875
Quoted 4 lines
include/linux/mm.h +1
-0@@ -3872,6 +3872,26 @@ static inline void clear_page_guard(struct zone *zone, struct page *page, unsigned int order) {}#endif /* CONFIG_DEBUG_PAGEALLOC */ +#ifndef __HAVE_ARCH_CLEAR_PAGES
Also here, let's base it on
#ifndef clear_pages
and similarly below for clear_user_pages.
(sorry for the back-and-forth, it's rather unclear what the best practice is)
Signature · 3 lines
Cheers David
Re: [PATCH v8 2/7] mm: introduce clear_pages() and clear_user_pages()
Quoted 24 lines · David Hildenbrand (Red Hat)
Quoted 17 lines · Ankur Arora
Introduce
clear_pages(), to be overridden by architectures that support more efficient clearing of consecutive pages. Also introduceclear_user_pages(), however, we will not expect this function to be overridden anytime soon.
We have to place theclear_user_pages()variant that usesclear_user_page()intomm/util.cfor now to work around macro magic on sparc and m68k.Signed-off-by Ankur Arora <[email protected]>--- include/linux/mm.h | 41 +++++++++++++++++++++++++++++++++++++++++ mm/util.c | 13 +++++++++++++ 2 files changed, 54 insertions(+)include/linux/mm.h +0
-0include/linux/mm.h static inline void clear_page_guard(struct zone *zone, struct page *page, · line 3874
Quoted 3 lines
include/linux/mm.h +0
-0@@ -3872,6 +3872,26 @@ static inline void clear_page_guard(struct zone *zone, struct page *page, unsigned int order) {}#endif /* CONFIG_DEBUG_PAGEALLOC */ +#ifndef __HAVE_ARCH_CLEAR_PAGESAlso here, let's base it on
Quoted 3 lines
#ifndef
clear_pagesand similarly below for
clear_user_pages.
Sounds good. For this one and the other.
Quoted 1 line
(sorry for the back-and-forth, it's rather unclear what the best practice is)
No worries.
--
ankur
PATCH v8 3/7 mm/highmem: introduce clear_user_highpages()
In reply to Ankur AroraDefine clear_user_highpages() which clears pages sequentially using the single page variant.
With !CONFIG_HIGHMEM, pages are contiguous so use the range clearing primitive clear_user_pages().
Note: fixed
--- include/linux/highmem.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+)
include/linux/highmem.h +29 -0
@@ -199,6 +199,11 @@ static inline void invalidate_kernel_vmap_range(void *vaddr, int size)/* when CONFIG_HIGHMEM is not set these will be plain clear/copy_page */ #ifndef clear_user_highpage +/** +* clear_user_highpage() - clear a page to be mapped to user space +* @page: start page +* @vaddr: start address of the user mapping +*/ static inline void clear_user_highpage (struct page *page,unsigned long vaddr) {void *addr =kmap_local_page (page);
@@ -207,6 +212,30 @@ static inline void clear_user_highpage(struct page *page, unsigned long vaddr) }#endif +/** +* clear_user_highpages() - clear a page range to be mapped to user space +* @page: start page +* @vaddr: start address of the user mapping +* @npages: number of pages +* +* Assumes that all the pages in the region (@page, +@npages) are valid +* so this does no exception handling. +*/ +static inline void clear_user_highpages (struct page *page,unsigned long vaddr, +unsigned int npages) +{ +if (!IS_ENABLED (CONFIG_HIGHMEM )) { +clear_user_pages (page_address (page), vaddr, page, npages); +return ; + } + +do { +clear_user_highpage (page, vaddr); + vaddr +=PAGE_SIZE ; + page++; + }while (--npages); +} +#ifndef vma_alloc_zeroed_movable_folio/** * vma_alloc_zeroed_movable_folio - Allocate a zeroed page for a VMA.
Signature · 1 line
2.43.5
Re: [PATCH v8 3/7] mm/highmem: introduce clear_user_highpages()
Quoted 9 lines · Ankur Arora
Define
clear_user_highpages()which clears pages sequentially using the single page variant.With !CONFIG_HIGHMEM, pages are contiguous so use the range clearing primitive
clear_user_pages().Signed-off-by Ankur Arora <[email protected]>Note: fixed
That should be dropped.
Quoted 41 lines
--- include/linux/highmem.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+)include/linux/highmem.h +22
-0@@ -199,6 +199,11 @@ static inline void invalidate_kernel_vmap_range(void *vaddr, int size)/* when CONFIG_HIGHMEM is not set these will be plain clear/copy_page */ #ifndef clear_user_highpage +/** +* clear_user_highpage() - clear a page to be mapped to user space +* @page: start page +* @vaddr: start address of the user mapping +*/ static inline void clear_user_highpage (struct page *page,unsigned long vaddr) {void *addr =kmap_local_page (page);@@ -207,6 +212,30 @@ static inline void clear_user_highpage(struct page *page, unsigned long vaddr) }#endif +/** +* clear_user_highpages() - clear a page range to be mapped to user space +* @page: start page +* @vaddr: start address of the user mapping +* @npages: number of pages +* +* Assumes that all the pages in the region (@page, +@npages) are valid +* so this does no exception handling. +*/ +static inline void clear_user_highpages (struct page *page,unsigned long vaddr, +unsigned int npages) +{ +if (!IS_ENABLED (CONFIG_HIGHMEM )) { +clear_user_pages (page_address (page), vaddr, page, npages); +return ; + } +
include/linux/highmem.h static inline void clear_user_highpage(struct page *page, unsigned long vaddr) · line 241
Quoted 10 lines
include/linux/highmem.h +7
-0+do { +clear_user_highpage (page, vaddr); + vaddr +=PAGE_SIZE ; + page++; + }while (--npages); +} +#ifndef vma_alloc_zeroed_movable_folio/** * vma_alloc_zeroed_movable_folio - Allocate a zeroed page for a VMA.
Signature · 3 lines
Cheers David
Re: [PATCH v8 3/7] mm/highmem: introduce clear_user_highpages()
Quoted 52 lines · David Hildenbrand (Red Hat)
Quoted 6 lines · Ankur Arora
Define
clear_user_highpages()which clears pages sequentially using the single page variant.
With !CONFIG_HIGHMEM, pages are contiguous so use the range clearing primitiveclear_user_pages().Signed-off-by Ankur Arora <[email protected]>Note: fixed
That should be dropped.
Quoted 42 lines
--- include/linux/highmem.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+)include/linux/highmem.h +25
-0@@ -199,6 +199,11 @@ static inline void invalidate_kernel_vmap_range(void *vaddr, int size)/* when CONFIG_HIGHMEM is not set these will be plain clear/copy_page */ #ifndef clear_user_highpage +/** +* clear_user_highpage() - clear a page to be mapped to user space +* @page: start page +* @vaddr: start address of the user mapping +*/ static inline void clear_user_highpage (struct page *page,unsigned long vaddr) {void *addr =kmap_local_page (page);@@ -207,6 +212,30 @@ static inline void clear_user_highpage(struct page *page, unsigned long vaddr) }#endif +/** +* clear_user_highpages() - clear a page range to be mapped to user space +* @page: start page +* @vaddr: start address of the user mapping +* @npages: number of pages +* +* Assumes that all the pages in the region (@page, +@npages) are valid +* so this does no exception handling. +*/ +static inline void clear_user_highpages(struct page *page, unsigned long vaddr, +unsigned int npages) +{ +if (!IS_ENABLED (CONFIG_HIGHMEM )) { +clear_user_pages (page_address (page), vaddr, page, npages); +return ; + } + + do { + clear_user_highpage(page, vaddr); + vaddr += PAGE_SIZE; + page++;
Quoted 8 lines
include/linux/highmem.h static inline void clear_user_highpage(struct page *page, unsigned long vaddr) · line 241
Quoted 6 lines
include/linux/highmem.h +3
-0+ }while (--npages); +} +#ifndef vma_alloc_zeroed_movable_folio/** * vma_alloc_zeroed_movable_folio - Allocate a zeroed page for a VMA.
Thanks (for this and the others.)
--
ankur
PATCH v8 4/7 x86/mm: Simplify clear_page_*
In reply to Ankur Aroraclear_page_rep() and clear_page_erms() are wrappers around "REP; STOS" variations. Inlining gets rid of an unnecessary CALL/RET (which isn't free when using RETHUNK speculative execution mitigations.) Fixup and rename clear_page_orig() to adapt to the changed calling convention.
Also add a comment from Dave Hansen detailing various clearing mechanisms used in clear_page().
--- arch/x86/include/asm/page_32.h | 6 +++++ arch/x86/include/asm/page_64.h | 46 +++++++++++++++++++++++++--------- arch/x86/lib/clear_page_64.S | 39 ++++++---------------------- 3 files changed, 48 insertions(+), 43 deletions(-)
arch/x86/include/asm/page_32.h +6 -0
@@ -17,6 +17,12 @@ extern unsigned long __phys_addr(unsigned long);#include <linux/string.h> +/** +* clear_page() - clear a page using a kernel virtual address. +* @page: address of kernel page +* +* Does absolutely no exception handling. +*/ static inline void clear_page (void *page) {memset (page,0 ,PAGE_SIZE );
arch/x86/include/asm/page_64.h +34 -12
57 lines not on this page · raw message
arch/x86/lib/clear_page_64.S +8 -31
@@ -6,30 +6,15 @@ #include <asm/asm.h> /* - * Most CPUs support enhanced REP MOVSB/STOSB instructions. It is - * recommended to use this when possible and we do use them by default. - * If enhanced REP MOVSB/STOSB is not available, try to use fast string. - * Otherwise, use original. + * Zero page aligned region. + * %rdi - dest + * %rcx - length */ - -/* - * Zero a page. - * %rdi - page - */ -SYM_TYPED_FUNC_START(clear_page_rep) - movl $4096/8,%ecx - xorl %eax,%eax - rep stosq - RET -SYM_FUNC_END(clear_page_rep) -EXPORT_SYMBOL_GPL(clear_page_rep) - -SYM_TYPED_FUNC_START(clear_page_orig) - xorl %eax,%eax - movl $4096/64,%ecx +SYM_TYPED_FUNC_START(memzero_page_aligned_unrolled) + shrq $6, %rcx .p2align 4 .Lloop: - decl %ecx + decq %rcx #define PUT(x) movq %rax,x*8(%rdi) movq %rax,(%rdi) PUT(1)
@@ -43,16 +28,8 @@ SYM_TYPED_FUNC_START(clear_page_orig) jnz .Lloop nop RET -SYM_FUNC_END(clear_page_orig) -EXPORT_SYMBOL_GPL(clear_page_orig) - -SYM_TYPED_FUNC_START(clear_page_erms) - movl $4096,%ecx - xorl %eax,%eax - rep stosb - RET -SYM_FUNC_END(clear_page_erms) -EXPORT_SYMBOL_GPL(clear_page_erms) +SYM_FUNC_END(memzero_page_aligned_unrolled) +EXPORT_SYMBOL_GPL(memzero_page_aligned_unrolled) /* * Default clear user-space.
Signature · 1 line
2.43.5
Borislav Petkov
maintainerRe: [PATCH v8 4/7] x86/mm: Simplify clear_page_*
Quoted 8 lines · Ankur Arora
+/**
+ *clear_page()- clear a page using a kernel virtual address.
+ * @addr: address of kernel page
+ *
+ * Switch between three implementations of page clearing based on CPU
+ * capabilities:
+ *
+ * -memzero_page_aligned_unrolled(): the oldest, slowest and universally
So I don't see how a memzero_<bla> name shows that it belongs to the clear_page "stack" of functions? clear_page_orig() kinda conveys better what this thing is. In any case, having "clear_page" somewhere there in the name should stay.
Quoted 2 lines
+ * supported method. Zeroes via 8-byte MOV instructions unrolled 8x
+ * to write a 64-byte cacheline in each loop iteration..
^
one fullstop is enough.
Quoted 2 lines
+ *
+ * - "rep stosq": really old CPUs had crummy REP implementations.
We spell all x86 insns in ALL CAPS. Like you've almost done.
Also, it is
REP; STOSQ
with a ;
Otherwise the idea for the cleanup makes sense.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v8 4/7] x86/mm: Simplify clear_page_*
Quoted 10 lines · Borislav Petkov
Quoted 8 lines · Ankur Arora
+/**
+ *clear_page()- clear a page using a kernel virtual address.
+ * @addr: address of kernel page
+ *
+ * Switch between three implementations of page clearing based on CPU
+ * capabilities:
+ *
+ * -memzero_page_aligned_unrolled(): the oldest, slowest and universally
Quoted 4 lines
So I don't see how a memzero_<bla> name shows that it belongs to the
clear_page"stack" of functions?clear_page_orig()kinda conveys better what this thing is. In any case, having "clear_page" somewhere there in the name should stay.
After this change the only remaining functions in x86/lib/clear_page_64.S are this one and rep_stos_alternative.
And so the _orig suffix seemed to be a little confusing. That's why the change to the more functional name.
Quoted 11 lines
Quoted 2 lines
+ * supported method. Zeroes via 8-byte MOV instructions unrolled 8x
+ * to write a 64-byte cacheline in each loop iteration..^one fullstop is enough.
Quoted 2 lines
+ *
+ * - "rep stosq": really old CPUs had crummy REP implementations.We spell all x86 insns in ALL CAPS. Like you've almost done.
Quoted 7 lines
Also, it is
REP; STOSQ
with a ;
Otherwise the idea for the cleanup makes sense.
Thanks. Will fix the above.
--
ankur
Borislav Petkov
maintainerRe: [PATCH v8 4/7] x86/mm: Simplify clear_page_*
Quoted 5 lines · Ankur Arora
After this change the only remaining functions in
x86/lib/clear_page_64.Sare this one andrep_stos_alternative.And so the
_origsuffix seemed to be a little confusing. That's why the change to the more functional name.
No, as said, having "clear_page" somewhere there in the name should stay. It can be "__clear_page" or something along those lines but not something completely different.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v8 4/7] x86/mm: Simplify clear_page_*
Quoted 10 lines · Borislav Petkov
Quoted 5 lines · Ankur Arora
After this change the only remaining functions in
x86/lib/clear_page_64.Sare this one andrep_stos_alternative.And so the
_origsuffix seemed to be a little confusing. That's why the change to the more functional name.No, as said, having "
clear_page" somewhere there in the name should stay. It can be "__clear_page" or something along those lines but not something completely different.
Sounds good. Will change.
--
ankur
PATCH v8 5/7 x86/clear_page: Introduce clear_pages()
In reply to Ankur AroraPerformance when clearing with string instructions (x86-64-stosq and similar) can vary significantly based on the chunk-size used.
$ perf bench mem memset -k 4KB -s 4GB -f x86-64-stosq # Running 'mem/memset' benchmark: # function 'x86-64-stosq' (movsq-based memset() in arch/x86/lib/memset_64.S) # Copying 4GB bytes ...
13.748208 GB/sec
$ perf bench mem memset -k 2MB -s 4GB -f x86-64-stosq # Running 'mem/memset' benchmark: # function 'x86-64-stosq' (movsq-based memset() in # arch/x86/lib/memset_64.S) # Copying 4GB bytes ...
15.067900 GB/sec
$ perf bench mem memset -k 1GB -s 4GB -f x86-64-stosq # Running 'mem/memset' benchmark: # function 'x86-64-stosq' (movsq-based memset() in arch/x86/lib/memset_64.S) # Copying 4GB bytes ...
38.104311 GB/sec
(Both on AMD Milan.)
With a change in chunk-size of 4KB to 1GB, we see the performance go from 13.7 GB/sec to 38.1 GB/sec. For a chunk-size of 2MB the change isn't quite as drastic but it is worth adding a clear_page() variant that can handle contiguous page-extents.
Also define ARCH_PAGE_CONTIG_NR to specify the maximum contiguous page range that can be zeroed when running under cooperative preemption models. This limits the worst case preemption latency.
--- arch/x86/include/asm/page_64.h | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)
arch/x86/include/asm/page_64.h +21 -5
@@ -43,8 +43,9 @@ extern unsigned long __phys_addr_symbol(unsigned long);void memzero_page_aligned_unrolled (void *addr,u64 len);/** -* clear_page() - clear a page using a kernel virtual address. -* @addr: address of kernel page +* clear_pages() - clear a page range using a kernel virtual address. +* @addr: start address of kernel page range +* @npages: number of pages * * Switch between three implementations of page clearing based on CPU * capabilities:
@@ -65,11 +66,11 @@ void memzero_page_aligned_unrolled(void *addr, u64 len);* * Does absolutely no exception handling. */ -static inline void clear_page (void *addr) +static inline void clear_pages (void *addr,unsigned int npages) { -u64 len =PAGE_SIZE ; +u64 len = npages *PAGE_SIZE ;/* -* Clean up KMSAN metadata for the page being cleared. The assembly call +* Clean up KMSAN metadata for the pages being cleared. The assembly call * below clobbers @addr, so we perform unpoisoning before it. */ kmsan_unpoison_memory (addr, len);
@@ -80,6 +81,21 @@ static inline void clear_page(void *addr) : "a" (0) : "cc", "memory"); } +#define __HAVE_ARCH_CLEAR_PAGES + +/* +* When running under cooperatively scheduled preemption models limit the +* maximum contiguous extent that can be cleared to pages worth 8MB. +* +* With a clearing BW of ~10GBps, this should result in worst case scheduling +* latency of ~1ms. +*/ +#define ARCH_PAGE_CONTIG_NR (8 << (20 - PAGE_SHIFT)) + +static inline void clear_page (void *addr) +{ +clear_pages (addr,1 ); +}void copy_page (void *to,void *from);KCFI_REFERENCE (copy_page);
Signature · 1 line
2.43.5
Borislav Petkov
maintainerRe: [PATCH v8 5/7] x86/clear_page: Introduce clear_pages()
Quoted 3 lines · Ankur Arora
Also define
ARCH_PAGE_CONTIG_NRto specify the maximum contiguous page range that can be zeroed when running under cooperative preemption models. This limits the worst case preemption latency.
Please do not explain what the patch does in the commit message - that should be clear from the diff itself. Rather, concentrate on why this patch exists.
Quoted 3 lines
+/*
+ * When running under cooperatively scheduled preemption models limit the
+ * maximum contiguous extent that can be cleared to pages worth 8MB.
Why?
Quoted 2 lines
+ *
+ * With a clearing BW of ~10GBps, this should result in worst case scheduling
This sounds like you have this bandwidth (please write it out - we have more than enough silly abbreviations) on *everything* x86 the kernel runs on. Which simply ain't true.
Quoted 3 lines
+ * latency of ~1ms.
+ */
+#defineARCH_PAGE_CONTIG_NR(8 << (20 -PAGE_SHIFT))
And so this looks like some magic number which makes sense only on some uarches but most likely it doesn't on others.
Why isn't this thing determined dynamically during boot or so, instead of hardcoding it this way and then having to change it again later when bandwidth increases?
Hmm, weird.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v8 5/7] x86/clear_page: Introduce clear_pages()
Quoted 7 lines · Borislav Petkov
Quoted 3 lines · Ankur Arora
Also define
ARCH_PAGE_CONTIG_NRto specify the maximum contiguous page range that can be zeroed when running under cooperative preemption models. This limits the worst case preemption latency.Please do not explain what the patch does in the commit message - that should be clear from the diff itself. Rather, concentrate on why this patch exists.
Ack.
Quoted 5 lines
Quoted 3 lines
+/*
+ * When running under cooperatively scheduled preemption models limit the
+ * maximum contiguous extent that can be cleared to pages worth 8MB.Why?
Will mention that the point is to minimize worst case preemption latency.
Quoted 7 lines
Quoted 2 lines
+ *
+ * With a clearing BW of ~10GBps, this should result in worst case schedulingThis sounds like you have this bandwidth (please write it out - we have more than enough silly abbreviations) on *everything* x86 the kernel runs on. Which simply ain't true.
Quoted 6 lines
Quoted 3 lines
+ * latency of ~1ms.
+ */
+#defineARCH_PAGE_CONTIG_NR(8 << (20 -PAGE_SHIFT))And so this looks like some magic number which makes sense only on some uarches but most likely it doesn't on others.
The intent was to use a large enough value that enables uarchs which do 'REP; STOS' optimizations, but not too large so we end up with high preemption latency.
Quoted 3 lines
Why isn't this thing determined dynamically during boot or so, instead of hardcoding it this way and then having to change it again later when bandwidth increases?
I thought of doing that but given that the precise value doesn't matter very much (and there's enough slack in it in either direction) it seemed unnecessary to do at this point.
Also, I'm not sure that a boot determined value would really help given that the 'REP; STOS' bandwidth could be high or low based on how saturated the bus is.
Clearly some of this detail should have been in my commit message.
Let me add it there.
Thanks
--
ankur
Borislav Petkov
maintainerRe: [PATCH v8 5/7] x86/clear_page: Introduce clear_pages()
Quoted 3 lines · Ankur Arora
The intent was to use a large enough value that enables uarchs which do 'REP; STOS' optimizations, but not too large so we end up with high preemption latency.
How is selecting that number tied to uarches which can do REP; STOSB? I assume you mean REP; STOSB where microcode magic glue aggregates larger moves than just u64 chunks but only under certain conditions and so on..., and not REP_GOOD where the microcode doesn't have problems with REP prefixes...
Quoted 8 lines
Quoted 3 lines
Why isn't this thing determined dynamically during boot or so, instead of hardcoding it this way and then having to change it again later when bandwidth increases?
I thought of doing that but given that the precise value doesn't matter very much (and there's enough slack in it in either direction) it seemed unnecessary to do at this point.
Quoted 5 lines
Also, I'm not sure that a boot determined value would really help given that the 'REP; STOS' bandwidth could be high or low based on how saturated the bus is.
Clearly some of this detail should have been in my commit message.
So you want to have, say, 8MB of contiguous range - if possible - and let the CPU do larger clears. And it depends on the scheduling model. And it depends on what the CPU can do wrt length aggregation. Close?
Well, I would like, please, for this to be properly documented why it was selected this way and what *all* the aspects were to select it this way so that we can know why it is there and we can change it in the future if needed.
It is very hard to do so if the reasoning behind it has disappeared in the bowels of lkml...
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v8 5/7] x86/clear_page: Introduce clear_pages()
Quoted 9 lines · Borislav Petkov
Quoted 3 lines · Ankur Arora
The intent was to use a large enough value that enables uarchs which do 'REP; STOS' optimizations, but not too large so we end up with high preemption latency.
How is selecting that number tied to uarches which can do REP; STOSB? I assume you mean REP; STOSB where microcode magic glue aggregates larger moves than just u64 chunks but only under certain conditions and so on..., and not
REP_GOODwhere the microcode doesn't have problems with REP prefixes...
Yes, to what you say below.
Quoted 12 lines
Quoted 12 lines
Quoted 3 lines
Why isn't this thing determined dynamically during boot or so, instead of hardcoding it this way and then having to change it again later when bandwidth increases?
I thought of doing that but given that the precise value doesn't matter very much (and there's enough slack in it in either direction) it seemed unnecessary to do at this point.
Also, I'm not sure that a boot determined value would really help given that the 'REP; STOS' bandwidth could be high or low based on how saturated the bus is.
Quoted 5 lines
Quoted 1 line
Clearly some of this detail should have been in my commit message.
So you want to have, say, 8MB of contiguous range - if possible - and let the CPU do larger clears. And it depends on the scheduling model. And it depends on what the CPU can do wrt length aggregation. Close?
Yeah pretty much that. Just to restate:
- be large enough so CPUs that can optimize, are able to optimize
- even in the bad cases (CPUs that don't optimize and/or are generally slow at this optimization): should be fast enough that we have reasonable preemption latency (which is an issue only for voluntary preemption etc)
Quoted 7 lines
Well, I would like, please, for this to be properly documented why it was selected this way and what *all* the aspects were to select it this way so that we can know why it is there and we can change it in the future if needed.
It is very hard to do so if the reasoning behind it has disappeared in the bowels of lkml...
Ack. Yeah I should have documented this way better.
Thanks
--
ankur
PATCH v8 6/7 mm, folio_zero_user: support clearing page ranges
In reply to Ankur AroraClear contiguous page ranges in folio_zero_user() instead of clearing a page-at-a-time. This enables CPU specific optimizations based on the length of the region.
Operating on arbitrarily large regions can lead to high preemption latency under cooperative preemption models. So, limit the worst case preemption latency via architecture specified PAGE_CONTIG_NR units.
The resultant performance depends on the kinds of optimizations available to the CPU for the region being cleared. Two classes of of optimizations:
- clearing iteration costs can be amortized over a range larger than a single page.
- cacheline allocation elision (seen on AMD Zen models).
Testing a demand fault workload shows an improved baseline from the first optimization and a larger improvement when the region being cleared is large enough for the second optimization.
AMD Milan (EPYC 7J13, boost=0, region=64GB on the local NUMA node):
$ perf bench mem map -p $pg-sz -f demand -s 64GB -l 5
page-at-a-time contiguous clearing change
(GB/s +- %stdev) (GB/s +- %stdev)
pg-sz=2MB 12.92 +- 2.55% 17.03 +- 0.70% + 31.8% preempt=*
pg-sz=1GB 17.14 +- 2.27% 18.04 +- 1.05% [#] + 5.2% preempt=none|voluntary pg-sz=1GB 17.26 +- 1.24% 42.17 +- 4.21% +144.3% preempt=full|lazy
[#] AMD Milan uses a threshold of LLC-size (~32MB) for eliding cacheline allocation, which is larger than ARCH_PAGE_CONTIG_NR, so preempt=none|voluntary see no improvement on the pg-sz=1GB.
Also as mentioned earlier, the baseline improvement is not specific to AMD Zen platforms. Intel Icelakex (pg-sz=2MB|1GB) sees a similar improvement as the Milan pg-sz=2MB workload above (~30%).
--- include/linux/mm.h | 6 ++++++ mm/memory.c | 42 +++++++++++++++++++++--------------------- 2 files changed, 27 insertions(+), 21 deletions(-)
include/linux/mm.h +6 -0
@@ -3872,6 +3872,12 @@ static inline void clear_page_guard(struct zone *zone, struct page *page, unsigned int order) {}#endif /* CONFIG_DEBUG_PAGEALLOC */ +#ifndef ARCH_PAGE_CONTIG_NR +#define PAGE_CONTIG_NR 1 +#else +#define PAGE_CONTIG_NR ARCH_PAGE_CONTIG_NR +#endif +#ifndef __HAVE_ARCH_CLEAR_PAGES/** * clear_pages() - clear a page range for kernel-internal use.
mm/memory.c +21 -21
61 lines not on this page · raw message
Signature · 1 line
2.43.5
David Hildenbrand (Red Hat)
maintainerRe: [PATCH v8 6/7] mm, folio_zero_user: support clearing page ranges
Quoted 55 lines · Ankur Arora
Clear contiguous page ranges in
folio_zero_user()instead of clearing a page-at-a-time. This enables CPU specific optimizations based on the length of the region.Operating on arbitrarily large regions can lead to high preemption latency under cooperative preemption models. So, limit the worst case preemption latency via architecture specified
PAGE_CONTIG_NRunits.The resultant performance depends on the kinds of optimizations available to the CPU for the region being cleared. Two classes of of optimizations:
- clearing iteration costs can be amortized over a range larger than a single page.
- cacheline allocation elision (seen on AMD Zen models).Testing a demand fault workload shows an improved baseline from the first optimization and a larger improvement when the region being cleared is large enough for the second optimization.
AMD Milan (EPYC 7J13, boost=0, region=64GB on the local NUMA node):
$ perf bench mem map -p $pg-sz -f demand -s 64GB -l 5page-at-a-time contiguous clearing change (GB/s +- %stdev) (GB/s +- %stdev) pg-sz=2MB 12.92 +- 2.55% 17.03 +- 0.70% + 31.8% preempt=* pg-sz=1GB 17.14 +- 2.27% 18.04 +- 1.05% [#] + 5.2% preempt=none|voluntary pg-sz=1GB 17.26 +- 1.24% 42.17 +- 4.21% +144.3% preempt=full|lazy[#] AMD Milan uses a threshold of LLC-size (~32MB) for eliding cacheline allocation, which is larger than
ARCH_PAGE_CONTIG_NR, so preempt=none|voluntary see no improvement on the pg-sz=1GB.Also as mentioned earlier, the baseline improvement is not specific to AMD Zen platforms. Intel Icelakex (pg-sz=2MB|1GB) sees a similar improvement as the Milan pg-sz=2MB workload above (~30%).
Signed-off-by Ankur Arora <[email protected]>Reviewed-by Raghavendra K T <[email protected]>Tested-by Raghavendra K T <[email protected]>--- include/linux/mm.h | 6 ++++++ mm/memory.c | 42 +++++++++++++++++++++--------------------- 2 files changed, 27 insertions(+), 21 deletions(-)include/linux/mm.h +0
-0
include/linux/mm.h static inline void clear_page_guard(struct zone *zone, struct page *page, · line 3879
Quoted 8 lines
include/linux/mm.h +5
-0@@ -3872,6 +3872,12 @@ static inline void clear_page_guard(struct zone *zone, struct page *page, unsigned int order) {}#endif /* CONFIG_DEBUG_PAGEALLOC */ +#ifndef ARCH_PAGE_CONTIG_NR +#define PAGE_CONTIG_NR 1 +#else +#define PAGE_CONTIG_NR ARCH_PAGE_CONTIG_NR +#endif
The name is a bit misleading. We need something that tells us that this is for patch-processing (clearing? maybe alter copying?) contig pages. Likely spelling out that this is for the non-preemptible case only.
I assume we can drop the "CONTIG", just like clear_pages() doesn't contain it etc.
CLEAR_PAGES_NON_PREEMPT_BATCH
PROCESS_PAGES_NON_PREEMPT_BATCH
Can you remind me again why this is arch specific, and why the default is 1 instead of, say 2,4,8 ... ?
Quoted 24 lines
include/linux/mm.h +1
-0+#ifndef __HAVE_ARCH_CLEAR_PAGES/** * clear_pages() - clear a page range for kernel-internal use. mm/memory.c +7
-4@@ -7144,40 +7144,40 @@ static inline int process_huge_page(return 0 ; } -static void clear_gigantic_page (struct folio *folio,unsigned long addr_hint, -unsigned int nr_pages) +/* +* Clear contiguous pages chunking them up when running under +* non-preemptible models. +*/ +static void clear_contig_highpages (struct page *page,unsigned long addr, +unsigned int npages) { -unsigned long addr =ALIGN_DOWN (addr_hint,folio_size (folio)); -int i; +unsigned int i, count, unit;
mm/memory.c static inline int process_huge_page( · line 7162
Quoted 10 lines
mm/memory.c +7
-2-might_sleep (); -for (i =0 ; i < nr_pages; i++) { + unit =preempt_model_preemptible () ? npages :PAGE_CONTIG_NR ; + +for (i =0 ; i < npages; ) { + count =min (unit, npages - i); +clear_user_highpages (page + i, + addr + i *PAGE_SIZE , count); + i += count;
Why not
for (i = 0; i < nr_pages; i += count) {
Also, I would leave the cond_resched(); where it was (before the invocation) to perform as little change as possible.
Quoted 36 lines
mm/memory.c +7
-15cond_resched (); -clear_user_highpage (folio_page (folio, i), addr + i *PAGE_SIZE ); } } -static int clear_subpage (unsigned long addr,int idx,void *arg) -{ -struct folio *folio = arg; - -clear_user_highpage (folio_page (folio, idx), addr); -return 0 ; -} -/** * folio_zero_user - Zero a folio which will be mapped to userspace. * @folio: The folio to zero. -* @addr_hint: The address will be accessed or the base address if uncelar. +* @addr_hint: The address accessed by the user or the base address. +* +* Uses architectural support for clear_pages() to zero page extents +* instead of clearing page-at-a-time. */ void folio_zero_user (struct folio *folio,unsigned long addr_hint) { -unsigned int nr_pages =folio_nr_pages (folio); +unsigned long base_addr =ALIGN_DOWN (addr_hint,folio_size (folio)); -if (unlikely (nr_pages >MAX_ORDER_NR_PAGES )) -clear_gigantic_page (folio, addr_hint, nr_pages); -else -process_huge_page (addr_hint, nr_pages, clear_subpage, folio); +clear_contig_highpages (folio_page (folio,0 ), + base_addr,folio_nr_pages (folio)); } static int copy_user_gigantic_page(struct folio *dst, struct folio *src,
Signature · 3 lines
Cheers David
Re: [PATCH v8 6/7] mm, folio_zero_user: support clearing page ranges
Quoted 56 lines · David Hildenbrand (Red Hat)
Quoted 43 lines · Ankur Arora
Clear contiguous page ranges in
folio_zero_user()instead of clearing a page-at-a-time. This enables CPU specific optimizations based on the length of the region.
Operating on arbitrarily large regions can lead to high preemption latency under cooperative preemption models. So, limit the worst case preemption latency via architecture specifiedPAGE_CONTIG_NRunits.
The resultant performance depends on the kinds of optimizations available to the CPU for the region being cleared. Two classes of of optimizations:
- clearing iteration costs can be amortized over a range larger than a single page.
- cacheline allocation elision (seen on AMD Zen models).
Testing a demand fault workload shows an improved baseline from the first optimization and a larger improvement when the region being cleared is large enough for the second optimization.
AMD Milan (EPYC 7J13, boost=0, region=64GB on the local NUMA node):$ perf bench mem map -p $pg-sz -f demand -s 64GB -l 5 page-at-a-time contiguous clearing change (GB/s +- %stdev) (GB/s +- %stdev) pg-sz=2MB 12.92 +- 2.55% 17.03 +- 0.70% + 31.8% preempt=* pg-sz=1GB 17.14 +- 2.27% 18.04 +- 1.05% [#] + 5.2% preempt=none|voluntary pg-sz=1GB 17.26 +- 1.24% 42.17 +- 4.21% +144.3% preempt=full|lazy [#] AMD Milan uses a threshold of LLC-size (~32MB) for eliding cacheline allocation, which is larger than ARCH_PAGE_CONTIG_NR, so preempt=none|voluntary see no improvement on the pg-sz=1GB. Also as mentioned earlier, the baseline improvement is not specific to AMD Zen platforms. Intel Icelakex (pg-sz=2MB|1GB) sees a similar improvement as the Milan pg-sz=2MB workload above (~30%). Signed-off-by: Ankur Arora <[email protected]> Reviewed-by: Raghavendra K T <[email protected]> Tested-by: Raghavendra K T <[email protected]> --- include/linux/mm.h | 6 ++++++ mm/memory.c | 42 +++++++++++++++++++++--------------------- 2 files changed, 27 insertions(+), 21 deletions(-)include/linux/mm.h +0
-0include/linux/mm.h static inline void clear_page_guard(struct zone *zone, struct page *page, · line 3878
Quoted 7 lines
include/linux/mm.h +4
-0@@ -3872,6 +3872,12 @@ static inline void clear_page_guard(struct zone *zone, struct page *page, unsigned int order) {}#endif /* CONFIG_DEBUG_PAGEALLOC */ +#ifndef ARCH_PAGE_CONTIG_NR +#define PAGE_CONTIG_NR 1 +#else +#define PAGE_CONTIG_NR ARCH_PAGE_CONTIG_NR +#endif The name is a bit misleading. We need something that tells us that this is for patch-processing (clearing? maybe alter copying?) contig pages. Likely spelling out that this is for the non-preemptible case only.
Quoted 6 lines
I assume we can drop the "CONTIG", just like
clear_pages()doesn't contain it etc.
CLEAR_PAGES_NON_PREEMPT_BATCH
PROCESS_PAGES_NON_PREEMPT_BATCH
I think this version is clearer. And would be viable for copying as well.
Quoted 2 lines
Can you remind me again why this is arch specific, and why the default is 1 instead of, say 2,4,8 ... ?
So, the only use for this value is to decide a reasonable frequency for calling cond_resched() when operating on hugepages.
And the idea was the arch was best placed to have a reasonably safe value based on the expected spread of bandwidths it might see across uarchs. And the default choice of 1 was to keep it close to what we have now.
Thinking about it now though, maybe it is better to instead do this in common code. We could have two sets of defines, PROCESS_PAGES_NON_PREEMPT_BATCH_{LARGE,SMALL}, the first for archs that define __HAVE_ARCH_CLEAR_PAGES and the second, without.
The first could be 8MB (so with a machine doing ~10GBps, scheduling latency of ~1ms), the second 512K (with ~512MBps, ~1ms)?
Quoted 36 lines
Quoted 23 lines
include/linux/mm.h +1
-0+#ifndef __HAVE_ARCH_CLEAR_PAGES/** * clear_pages() - clear a page range for kernel-internal use. mm/memory.c +6
-3@@ -7144,40 +7144,40 @@ static inline int process_huge_page(return 0 ; } -static void clear_gigantic_page(struct folio *folio, unsigned long addr_hint, -unsigned int nr_pages) +/* +* Clear contiguous pages chunking them up when running under +* non-preemptible models. +*/ +static void clear_contig_highpages (struct page *page,unsigned long addr, + unsigned int npages) { -unsigned long addr =ALIGN_DOWN (addr_hint,folio_size (folio)); -int i;mm/memory.c static inline int process_huge_page( · line 7163
Quoted 10 lines
mm/memory.c +8
-1+unsigned int i, count, unit; -might_sleep (); -for (i =0 ; i < nr_pages; i++) { + unit =preempt_model_preemptible () ? npages :PAGE_CONTIG_NR ; + +for (i =0 ; i < npages; ) { + count =min (unit, npages - i); +clear_user_highpages (page + i, + addr + i *PAGE_SIZE , count); + i += count;Why not
Quoted 4 lines
for (i = 0; i <
nr_pages; i += count) {Also, I would leave the
cond_resched(); where it was (before the invocation) to perform as little change as possible.
Both of those seem like good ideas.
Thanks
--
ankur
David Hildenbrand (Red Hat)
maintainerRe: [PATCH v8 6/7] mm, folio_zero_user: support clearing page ranges
Quoted 79 lines · Ankur Arora
Quoted 56 lines · David Hildenbrand (Red Hat)
Quoted 43 lines · Ankur Arora
Clear contiguous page ranges in
folio_zero_user()instead of clearing a page-at-a-time. This enables CPU specific optimizations based on the length of the region.
Operating on arbitrarily large regions can lead to high preemption latency under cooperative preemption models. So, limit the worst case preemption latency via architecture specifiedPAGE_CONTIG_NRunits.
The resultant performance depends on the kinds of optimizations available to the CPU for the region being cleared. Two classes of of optimizations:
- clearing iteration costs can be amortized over a range larger than a single page.
- cacheline allocation elision (seen on AMD Zen models).
Testing a demand fault workload shows an improved baseline from the first optimization and a larger improvement when the region being cleared is large enough for the second optimization.
AMD Milan (EPYC 7J13, boost=0, region=64GB on the local NUMA node):$ perf bench mem map -p $pg-sz -f demand -s 64GB -l 5 page-at-a-time contiguous clearing change (GB/s +- %stdev) (GB/s +- %stdev) pg-sz=2MB 12.92 +- 2.55% 17.03 +- 0.70% + 31.8% preempt=* pg-sz=1GB 17.14 +- 2.27% 18.04 +- 1.05% [#] + 5.2% preempt=none|voluntary pg-sz=1GB 17.26 +- 1.24% 42.17 +- 4.21% +144.3% preempt=full|lazy [#] AMD Milan uses a threshold of LLC-size (~32MB) for eliding cacheline allocation, which is larger than ARCH_PAGE_CONTIG_NR, so preempt=none|voluntary see no improvement on the pg-sz=1GB. Also as mentioned earlier, the baseline improvement is not specific to AMD Zen platforms. Intel Icelakex (pg-sz=2MB|1GB) sees a similar improvement as the Milan pg-sz=2MB workload above (~30%). Signed-off-by: Ankur Arora <[email protected]> Reviewed-by: Raghavendra K T <[email protected]> Tested-by: Raghavendra K T <[email protected]> --- include/linux/mm.h | 6 ++++++ mm/memory.c | 42 +++++++++++++++++++++--------------------- 2 files changed, 27 insertions(+), 21 deletions(-)include/linux/mm.h +0
-0include/linux/mm.h static inline void clear_page_guard(struct zone *zone, struct page *page, · line 3878
Quoted 7 lines
include/linux/mm.h +4
-0@@ -3872,6 +3872,12 @@ static inline void clear_page_guard(struct zone *zone, struct page *page, unsigned int order) {}#endif /* CONFIG_DEBUG_PAGEALLOC */ +#ifndef ARCH_PAGE_CONTIG_NR +#define PAGE_CONTIG_NR 1 +#else +#define PAGE_CONTIG_NR ARCH_PAGE_CONTIG_NR +#endif The name is a bit misleading. We need something that tells us that this is for patch-processing (clearing? maybe alter copying?) contig pages. Likely spelling out that this is for the non-preemptible case only.
Quoted 6 lines
I assume we can drop the "CONTIG", just like
clear_pages()doesn't contain it etc.
CLEAR_PAGES_NON_PREEMPT_BATCH
PROCESS_PAGES_NON_PREEMPT_BATCHI think this version is clearer. And would be viable for copying as well.
Quoted 2 lines
Can you remind me again why this is arch specific, and why the default is 1 instead of, say 2,4,8 ... ?
So, the only use for this value is to decide a reasonable frequency for calling
cond_resched()when operating on hugepages.And the idea was the arch was best placed to have a reasonably safe value based on the expected spread of bandwidths it might see across uarchs. And the default choice of 1 was to keep it close to what we have now.
Quoted 4 lines
Thinking about it now though, maybe it is better to instead do this in common code. We could have two sets of defines, PROCESS_PAGES_NON_PREEMPT_BATCH_{LARGE,SMALL}, the first for archs that define
__HAVE_ARCH_CLEAR_PAGESand the second, without.
Right, avoiding this dependency on arch code would be nice.
Also, it feels like something we can later optimize for archs without __HAVE_ARCH_CLEAR_PAGES in common code.
Signature · 3 lines
Cheers David
Re: [PATCH v8 6/7] mm, folio_zero_user: support clearing page ranges
Quoted 80 lines · David Hildenbrand (Red Hat)
Quoted 68 lines · Ankur Arora
Quoted 56 lines · David Hildenbrand (Red Hat)
Quoted 43 lines · Ankur Arora
Clear contiguous page ranges in
folio_zero_user()instead of clearing a page-at-a-time. This enables CPU specific optimizations based on the length of the region.
Operating on arbitrarily large regions can lead to high preemption latency under cooperative preemption models. So, limit the worst case preemption latency via architecture specifiedPAGE_CONTIG_NRunits.
The resultant performance depends on the kinds of optimizations available to the CPU for the region being cleared. Two classes of of optimizations:
- clearing iteration costs can be amortized over a range larger than a single page.
- cacheline allocation elision (seen on AMD Zen models).
Testing a demand fault workload shows an improved baseline from the first optimization and a larger improvement when the region being cleared is large enough for the second optimization.
AMD Milan (EPYC 7J13, boost=0, region=64GB on the local NUMA node):$ perf bench mem map -p $pg-sz -f demand -s 64GB -l 5 page-at-a-time contiguous clearing change (GB/s +- %stdev) (GB/s +- %stdev) pg-sz=2MB 12.92 +- 2.55% 17.03 +- 0.70% + 31.8% preempt=* pg-sz=1GB 17.14 +- 2.27% 18.04 +- 1.05% [#] + 5.2% preempt=none|voluntary pg-sz=1GB 17.26 +- 1.24% 42.17 +- 4.21% +144.3% preempt=full|lazy [#] AMD Milan uses a threshold of LLC-size (~32MB) for eliding cacheline allocation, which is larger than ARCH_PAGE_CONTIG_NR, so preempt=none|voluntary see no improvement on the pg-sz=1GB. Also as mentioned earlier, the baseline improvement is not specific to AMD Zen platforms. Intel Icelakex (pg-sz=2MB|1GB) sees a similar improvement as the Milan pg-sz=2MB workload above (~30%). Signed-off-by: Ankur Arora <[email protected]> Reviewed-by: Raghavendra K T <[email protected]> Tested-by: Raghavendra K T <[email protected]> --- include/linux/mm.h | 6 ++++++ mm/memory.c | 42 +++++++++++++++++++++--------------------- 2 files changed, 27 insertions(+), 21 deletions(-)include/linux/mm.h +0
-0include/linux/mm.h static inline void clear_page_guard(struct zone *zone, struct page *page, · line 3878
Quoted 7 lines
include/linux/mm.h +4
-0@@ -3872,6 +3872,12 @@ static inline void clear_page_guard(struct zone *zone, struct page *page, unsigned int order) {}#endif /* CONFIG_DEBUG_PAGEALLOC */ +#ifndef ARCH_PAGE_CONTIG_NR +#define PAGE_CONTIG_NR 1 +#else +#define PAGE_CONTIG_NR ARCH_PAGE_CONTIG_NR +#endif The name is a bit misleading. We need something that tells us that this is for patch-processing (clearing? maybe alter copying?) contig pages. Likely spelling out that this is for the non-preemptible case only.
Quoted 6 lines
I assume we can drop the "CONTIG", just like
clear_pages()doesn't contain it etc.
CLEAR_PAGES_NON_PREEMPT_BATCH
PROCESS_PAGES_NON_PREEMPT_BATCHI think this version is clearer. And would be viable for copying as well.
Quoted 2 lines
Can you remind me again why this is arch specific, and why the default is 1 instead of, say 2,4,8 ... ?
Quoted 10 lines
So, the only use for this value is to decide a reasonable frequency for calling
cond_resched()when operating on hugepages. And the idea was the arch was best placed to have a reasonably safe value based on the expected spread of bandwidths it might see across uarchs. And the default choice of 1 was to keep it close to what we have now.
Thinking about it now though, maybe it is better to instead do this in common code. We could have two sets of defines, PROCESS_PAGES_NON_PREEMPT_BATCH_{LARGE,SMALL}, the first for archs that define__HAVE_ARCH_CLEAR_PAGESand the second, without.
Quoted 4 lines
Right, avoiding this dependency on arch code would be nice.
Also, it feels like something we can later optimize for archs without
__HAVE_ARCH_CLEAR_PAGESin common code.
That makes sense. Will keep the default where it is (1) and just get rid of the arch dependency.
--
ankur
PATCH v8 7/7 mm: folio_zero_user: cache neighbouring pages
In reply to Ankur Arorafolio_zero_user() does straight zeroing without caring about temporal locality for caches.
This replaced commit c6ddfb6c5890 ("mm, clear_huge_page: move order algorithm into a separate function") where we cleared a page at a time converging to the faulting page from the left and the right.
To retain limited temporal locality, split the clearing in three
parts: the faulting page and its immediate neighbourhood, and, the remaining regions on the left and the right. The local neighbourhood will be cleared last.
Do this only when zeroing small folios (< MAX_ORDER_NR_PAGES) since there isn't much expectation of cache locality for large folios.
Performance
===
AMD Genoa (EPYC 9J14, cpus=2 sockets * 96 cores * 2 threads,
memory=2.2 TB, L1d= 16K/thread, L2=512K/thread, L3=2MB/thread)
anon-w-seq (vm-scalability):
stime utime
page-at-a-time 1654.63 ( +- 3.84% ) 811.00 ( +- 3.84% ) contiguous clearing 1602.86 ( +- 3.00% ) 970.75 ( +- 4.68% ) neighbourhood-last 1630.32 ( +- 2.73% ) 886.37 ( +- 5.19% )
Both stime and utime respond in expected ways. stime drops for both contiguous clearing (-3.14%) and neighbourhood-last (-1.46%) approaches. However, utime increases for both contiguous clearing (+19.7%) and neighbourhood-last (+9.28%).
In part this is because anon-w-seq runs with 384 processes zeroing anonymously mapped memory which they then access sequentially. As such this is a likely uncommon pattern where the memory bandwidth is saturated while also being cache limited because we access the entire region.
Kernel make workload (make -j 12 bzImage):
stime utime
page-at-a-time 138.16 ( +- 0.31% ) 1015.11 ( +- 0.05% ) contiguous clearing 133.42 ( +- 0.90% ) 1013.49 ( +- 0.05% ) neighbourhood-last 131.20 ( +- 0.76% ) 1011.36 ( +- 0.07% )
For make the utime stays relatively flat with an up to 4.9% improvement in the stime.
--- mm/memory.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-)
mm/memory.c +42 -2
@@ -7171,13 +7171,53 @@ static void clear_contig_highpages(struct page *page, unsigned long addr,* * Uses architectural support for clear_pages() to zero page extents * instead of clearing page-at-a-time. +* +* Clearing of small folios (< MAX_ORDER_NR_PAGES) is split in three parts: +* pages in the immediate locality of the faulting page, and its left, right +* regions; the local neighbourhood cleared last in order to keep cache +* lines of the target region hot. +* +* For larger folios we assume that there is no expectation of cache locality +* and just do a straight zero. */ void folio_zero_user (struct folio *folio,unsigned long addr_hint) {unsigned long base_addr =ALIGN_DOWN (addr_hint,folio_size (folio)); +const long fault_idx = (addr_hint - base_addr) /PAGE_SIZE ; +const struct range pg =DEFINE_RANGE (0 ,folio_nr_pages (folio) -1 ); +const int width =2 ;/* number of pages cleared last on either side */ +struct range r[3 ]; +int i; -clear_contig_highpages (folio_page (folio,0 ), - base_addr,folio_nr_pages (folio)); +if (folio_nr_pages (folio) >MAX_ORDER_NR_PAGES ) { +clear_contig_highpages (folio_page (folio,0 ), + base_addr,folio_nr_pages (folio)); +return ; + } + +/* +* Faulting page and its immediate neighbourhood. Cleared at the end to +* ensure it sticks around in the cache. +*/ + r[2 ] =DEFINE_RANGE (clamp_t (s64, fault_idx - width, pg.start, pg.end), +clamp_t (s64, fault_idx + width, pg.start, pg.end)); + +/* Region to the left of the fault */ + r[1 ] =DEFINE_RANGE (pg.start, +clamp_t (s64, r[2 ].start-1 , pg.start-1 , r[2 ].start)); + +/* Region to the right of the fault: always valid for the common fault_idx=0 case. */ + r[0 ] =DEFINE_RANGE (clamp_t (s64, r[2 ].end+1 , r[2 ].end, pg.end+1 ), + pg.end); + +for (i =0 ; i <=2 ; i++) { +unsigned int npages =range_len (&r[i]); +struct page *page =folio_page (folio, r[i].start); +unsigned long addr = base_addr +folio_page_idx (folio, page) *PAGE_SIZE ; + +if (npages >0 ) +clear_contig_highpages (page, addr, npages); + } } static int copy_user_gigantic_page(struct folio *dst, struct folio *src,
Signature · 1 line
2.43.5
Andrew Morton
maintainerQuoted 24 lines · On Mon, 27 Oct 2025 13:21:02 -0700 Ankur Arora <[email protected]> wrote:
This series adds clearing of contiguous page ranges for hugepages, improving on the current page-at-a-time approach in two ways:
- amortizes the per-page setup cost over a larger extent
- when using string instructions, exposes the real region size to the processor.A processor could use a knowledge of the extent to optimize the clearing. AMD Zen uarchs, as an example, elide allocation of cachelines for regions larger than L3-size.
Demand faulting a 64GB region shows performance improvements:
$ perf bench mem map -p $pg-sz -f demand -s 64GB -l 5baseline +series change
(GB/s +- %stdev) (GB/s +- %stdev)
pg-sz=2MB 12.92 +- 2.55% 17.03 +- 0.70% + 31.8% preempt=* pg-sz=1GB 17.14 +- 2.27% 18.04 +- 1.05% [#] + 5.2% preempt=none|voluntary pg-sz=1GB 17.26 +- 1.24% 42.17 +- 4.21% +144.3% preempt=full|lazy
Quoted 4 lines
[#] Milan uses a threshold of LLC-size (~32MB) for eliding cacheline allocation, which is higher than the maximum extent used on x86 (ARCH_CONTIG_PAGE_NR=8MB), so preempt=none|voluntary sees no improvement with pg-sz=1GB.
I wasn't understanding this preemption thing at all, but then I saw this in the v4 series changelogging:
: [#] Only with preempt=full|lazy because cooperatively preempted models : need regular invocations of cond_resched(). This limits the extent : sizes that can be cleared as a unit.
Please put this back in!!
It's possible that we're being excessively aggressive with those cond_resched()s. Have you investigating tuning their frequency so we can use larger extent sizes with these preemption models?
Quoted 8 lines
The anon-w-seq test in the vm-scalability benchmark, however, does show worse performance with utime increasing by ~9%:
stime utime
baseline 1654.63 ( +- 3.84% ) 811.00 ( +- 3.84% ) +series 1630.32 ( +- 2.73% ) 886.37 ( +- 5.19% )
Quoted 7 lines
In part this is because anon-w-seq runs with 384 processes zeroing anonymously mapped memory which they then access sequentially. As such this is a likely uncommon pattern where the memory bandwidth is saturated while also being cache limited because we access the entire region.
Raghavendra also tested previous version of the series on AMD Genoa [1].
I suggest you paste Raghavendra's results into this [0/N] - it's important material.
Quoted 26 lines
...
arch/alpha/include/asm/page.h | 1 - arch/arc/include/asm/page.h | 2 + arch/arm/include/asm/page-nommu.h | 1 - arch/arm64/include/asm/page.h | 1 - arch/csky/abiv1/inc/abi/page.h | 1 + arch/csky/abiv2/inc/abi/page.h | 7 --- arch/hexagon/include/asm/page.h | 1 - arch/loongarch/include/asm/page.h | 1 - arch/m68k/include/asm/page_mm.h | 1 + arch/m68k/include/asm/page_no.h | 1 - arch/microblaze/include/asm/page.h | 1 - arch/mips/include/asm/page.h | 1 + arch/nios2/include/asm/page.h | 1 + arch/openrisc/include/asm/page.h | 1 - arch/parisc/include/asm/page.h | 1 - arch/powerpc/include/asm/page.h | 1 + arch/riscv/include/asm/page.h | 1 - arch/s390/include/asm/page.h | 1 - arch/sparc/include/asm/page_32.h | 2 + arch/sparc/include/asm/page_64.h | 1 + arch/um/include/asm/page.h | 1 - arch/x86/include/asm/page.h | 6 --- arch/x86/include/asm/page_32.h | 6 +++
Quoted 8 lines
arch/x86/include/asm/page_64.h | 64 ++++++++++++++++++----- arch/x86/lib/clear_page_64.S | 39 +++----------- arch/xtensa/include/asm/page.h | 1 - include/linux/highmem.h | 29 +++++++++++ include/linux/mm.h | 69 +++++++++++++++++++++++++ mm/memory.c | 82 ++++++++++++++++++++++-------- mm/util.c | 13 +++++ 30 files changed, 247 insertions(+), 91 deletions(-)
I guess this is an mm.git thing, with x86 acks (please).
The documented review activity is rather thin at this time so I'll sit this out for a while. Please ping me next week and we can reassess,
Thanks.
[ My earlier reply to this ate up some of the headers and broke out of the thread. Resending. ]
Quoted 31 lines · Andrew Morton
Quoted 24 lines · On Mon, 27 Oct 2025 13:21:02 -0700 Ankur Arora <[email protected]> wrote:
This series adds clearing of contiguous page ranges for hugepages, improving on the current page-at-a-time approach in two ways:
- amortizes the per-page setup cost over a larger extent
- when using string instructions, exposes the real region size to the processor.A processor could use a knowledge of the extent to optimize the clearing. AMD Zen uarchs, as an example, elide allocation of cachelines for regions larger than L3-size.
Demand faulting a 64GB region shows performance improvements:
$ perf bench mem map -p $pg-sz -f demand -s 64GB -l 5baseline +series change
(GB/s +- %stdev) (GB/s +- %stdev)
pg-sz=2MB 12.92 +- 2.55% 17.03 +- 0.70% + 31.8% preempt=* pg-sz=1GB 17.14 +- 2.27% 18.04 +- 1.05% [#] + 5.2% preempt=none|voluntary pg-sz=1GB 17.26 +- 1.24% 42.17 +- 4.21% +144.3% preempt=full|lazyQuoted 4 lines
[#] Milan uses a threshold of LLC-size (~32MB) for eliding cacheline allocation, which is higher than the maximum extent used on x86 (ARCH_CONTIG_PAGE_NR=8MB), so preempt=none|voluntary sees no improvement with pg-sz=1GB.
Quoted 8 lines
I wasn't understanding this preemption thing at all, but then I saw this in the v4 series changelogging:
: [#] Only with preempt=full|lazy because cooperatively preempted models : need regular invocations of
cond_resched(). This limits the extent : sizes that can be cleared as a unit.Please put this back in!!
/me facepalms. Sorry you had to go that far back. Yeah, that doesn't make any kind of sense standalone. Will fix.
Quoted 3 lines
It's possible that we're being excessively aggressive with those cond_resched()s. Have you investigating tuning their frequency so we can use larger extent sizes with these preemption models?
folio_zero_user() does a small part of that: for 2MB pages the clearing is split in three parts with an intervening cond_resched() for each.
This is of course much simpler than the process_huge_page() approach where we do a left right dance around the faulting page.
I had implemented a version of process_huge_page() with larger extent sizes that narrowed as we got closer to the faulting page in [a] (x86 performance was similar to the current series. See [b]).
In hindsight however, that felt too elaborate and probably unnecessary on most modern systems where you have reasonably large caches. Where it might help, however, is on more cache constrained systems where the spatial locality really does matter.
So, my idea was to start with a simple version, get some testing and then fill in the gaps instead of starting with something like [a].
[a] https://lore.kernel.org/lkml/[email protected]/#r
[b] https://lore.kernel.org/lkml/[email protected]/
Quoted 14 lines
Quoted 14 lines
The anon-w-seq test in the vm-scalability benchmark, however, does show worse performance with utime increasing by ~9%:
stime utime
baseline 1654.63 ( +- 3.84% ) 811.00 ( +- 3.84% ) +series 1630.32 ( +- 2.73% ) 886.37 ( +- 5.19% )In part this is because anon-w-seq runs with 384 processes zeroing anonymously mapped memory which they then access sequentially. As such this is a likely uncommon pattern where the memory bandwidth is saturated while also being cache limited because we access the entire region.
Quoted 4 lines
Quoted 1 line
Raghavendra also tested previous version of the series on AMD Genoa [1].
I suggest you paste Raghavendra's results into this [0/N] - it's important material.
Thanks. Will do.
Quoted 35 lines
Quoted 26 lines
...
arch/alpha/include/asm/page.h | 1 - arch/arc/include/asm/page.h | 2 + arch/arm/include/asm/page-nommu.h | 1 - arch/arm64/include/asm/page.h | 1 - arch/csky/abiv1/inc/abi/page.h | 1 + arch/csky/abiv2/inc/abi/page.h | 7 --- arch/hexagon/include/asm/page.h | 1 - arch/loongarch/include/asm/page.h | 1 - arch/m68k/include/asm/page_mm.h | 1 + arch/m68k/include/asm/page_no.h | 1 - arch/microblaze/include/asm/page.h | 1 - arch/mips/include/asm/page.h | 1 + arch/nios2/include/asm/page.h | 1 + arch/openrisc/include/asm/page.h | 1 - arch/parisc/include/asm/page.h | 1 - arch/powerpc/include/asm/page.h | 1 + arch/riscv/include/asm/page.h | 1 - arch/s390/include/asm/page.h | 1 - arch/sparc/include/asm/page_32.h | 2 + arch/sparc/include/asm/page_64.h | 1 + arch/um/include/asm/page.h | 1 - arch/x86/include/asm/page.h | 6 --- arch/x86/include/asm/page_32.h | 6 +++Quoted 8 lines
arch/x86/include/asm/page_64.h | 64 ++++++++++++++++++----- arch/x86/lib/clear_page_64.S | 39 +++----------- arch/xtensa/include/asm/page.h | 1 - include/linux/highmem.h | 29 +++++++++++ include/linux/mm.h | 69 +++++++++++++++++++++++++ mm/memory.c | 82 ++++++++++++++++++++++-------- mm/util.c | 13 +++++ 30 files changed, 247 insertions(+), 91 deletions(-)
Quoted 1 line
I guess this is an mm.git thing, with x86 acks (please).
Ack that.
Quoted 2 lines
The documented review activity is rather thin at this time so I'll sit this out for a while. Please ping me next week and we can reassess,
Will do. And, thanks for the quick look!
--
ankur
Quoted 7 lines · Ankur Arora
[ My earlier reply to this ate up some of the headers and broke out of the thread. Resending. ]
Quoted 2 lines · Andrew Morton
On Mon, 27 Oct 2025 13:21:02 -0700 Ankur Arora <[email protected]> wrote:
[ ... ]
Quoted 84 lines
Quoted 4 lines
It's possible that we're being excessively aggressive with those cond_resched()s. Have you investigating tuning their frequency so we can use larger extent sizes with these preemption models?
folio_zero_user()does a small part of that: for 2MB pages the clearing is split in three parts with an interveningcond_resched()for each.This is of course much simpler than the
process_huge_page()approach where we do a left right dance around the faulting page.I had implemented a version of
process_huge_page()with larger extent sizes that narrowed as we got closer to the faulting page in [a] (x86 performance was similar to the current series. See [b]).In hindsight however, that felt too elaborate and probably unnecessary on most modern systems where you have reasonably large caches. Where it might help, however, is on more cache constrained systems where the spatial locality really does matter.
So, my idea was to start with a simple version, get some testing and then fill in the gaps instead of starting with something like [a].
[a] https://lore.kernel.org/lkml/[email protected]/#r
[b] https://lore.kernel.org/lkml/[email protected]/Quoted 14 lines
Quoted 14 lines
The anon-w-seq test in the vm-scalability benchmark, however, does show worse performance with utime increasing by ~9%:
stime utime
baseline 1654.63 ( +- 3.84% ) 811.00 ( +- 3.84% ) +series 1630.32 ( +- 2.73% ) 886.37 ( +- 5.19% )In part this is because anon-w-seq runs with 384 processes zeroing anonymously mapped memory which they then access sequentially. As such this is a likely uncommon pattern where the memory bandwidth is saturated while also being cache limited because we access the entire region.
Quoted 4 lines
Quoted 1 line
Raghavendra also tested previous version of the series on AMD Genoa [1].
I suggest you paste Raghavendra's results into this [0/N] - it's important material.
Thanks. Will do.
Quoted 35 lines
Quoted 34 lines
...
arch/alpha/include/asm/page.h | 1 - arch/arc/include/asm/page.h | 2 + arch/arm/include/asm/page-nommu.h | 1 - arch/arm64/include/asm/page.h | 1 - arch/csky/abiv1/inc/abi/page.h | 1 + arch/csky/abiv2/inc/abi/page.h | 7 --- arch/hexagon/include/asm/page.h | 1 - arch/loongarch/include/asm/page.h | 1 - arch/m68k/include/asm/page_mm.h | 1 + arch/m68k/include/asm/page_no.h | 1 - arch/microblaze/include/asm/page.h | 1 - arch/mips/include/asm/page.h | 1 + arch/nios2/include/asm/page.h | 1 + arch/openrisc/include/asm/page.h | 1 - arch/parisc/include/asm/page.h | 1 - arch/powerpc/include/asm/page.h | 1 + arch/riscv/include/asm/page.h | 1 - arch/s390/include/asm/page.h | 1 - arch/sparc/include/asm/page_32.h | 2 + arch/sparc/include/asm/page_64.h | 1 + arch/um/include/asm/page.h | 1 - arch/x86/include/asm/page.h | 6 --- arch/x86/include/asm/page_32.h | 6 +++ arch/x86/include/asm/page_64.h | 64 ++++++++++++++++++----- arch/x86/lib/clear_page_64.S | 39 +++----------- arch/xtensa/include/asm/page.h | 1 - include/linux/highmem.h | 29 +++++++++++ include/linux/mm.h | 69 +++++++++++++++++++++++++ mm/memory.c | 82 ++++++++++++++++++++++-------- mm/util.c | 13 +++++ 30 files changed, 247 insertions(+), 91 deletions(-)
Quoted 8 lines
Quoted 1 line
I guess this is an mm.git thing, with x86 acks (please).
Ack that.
Quoted 2 lines
The documented review activity is rather thin at this time so I'll sit this out for a while. Please ping me next week and we can reassess,
Will do. And, thanks for the quick look!
Hi Andrew
So, the comments I have so far are mostly about clarity around the connection with preempt model and some cleanups on the x86 patches.
Other than that, my major concern is wider testing (platforms and workloads) than mine has been.
Could you take another look at the series and see what else you think it needs.
Thanks
--
ankur
David Hildenbrand (Red Hat)
maintainerQuoted 108 lines · Ankur Arora
Quoted 7 lines · Ankur Arora
[ My earlier reply to this ate up some of the headers and broke out of the thread. Resending. ]
Quoted 2 lines · Andrew Morton
On Mon, 27 Oct 2025 13:21:02 -0700 Ankur Arora <[email protected]> wrote:
[ ... ]
Quoted 84 lines
Quoted 4 lines
It's possible that we're being excessively aggressive with those cond_resched()s. Have you investigating tuning their frequency so we can use larger extent sizes with these preemption models?
folio_zero_user()does a small part of that: for 2MB pages the clearing is split in three parts with an interveningcond_resched()for each.This is of course much simpler than the
process_huge_page()approach where we do a left right dance around the faulting page.I had implemented a version of
process_huge_page()with larger extent sizes that narrowed as we got closer to the faulting page in [a] (x86 performance was similar to the current series. See [b]).In hindsight however, that felt too elaborate and probably unnecessary on most modern systems where you have reasonably large caches. Where it might help, however, is on more cache constrained systems where the spatial locality really does matter.
So, my idea was to start with a simple version, get some testing and then fill in the gaps instead of starting with something like [a].
[a] https://lore.kernel.org/lkml/[email protected]/#r
[b] https://lore.kernel.org/lkml/[email protected]/Quoted 14 lines
Quoted 14 lines
The anon-w-seq test in the vm-scalability benchmark, however, does show worse performance with utime increasing by ~9%:
stime utime
baseline 1654.63 ( +- 3.84% ) 811.00 ( +- 3.84% ) +series 1630.32 ( +- 2.73% ) 886.37 ( +- 5.19% )In part this is because anon-w-seq runs with 384 processes zeroing anonymously mapped memory which they then access sequentially. As such this is a likely uncommon pattern where the memory bandwidth is saturated while also being cache limited because we access the entire region.
Quoted 4 lines
Quoted 1 line
Raghavendra also tested previous version of the series on AMD Genoa [1].
I suggest you paste Raghavendra's results into this [0/N] - it's important material.
Thanks. Will do.
Quoted 35 lines
Quoted 34 lines
...
arch/alpha/include/asm/page.h | 1 - arch/arc/include/asm/page.h | 2 + arch/arm/include/asm/page-nommu.h | 1 - arch/arm64/include/asm/page.h | 1 - arch/csky/abiv1/inc/abi/page.h | 1 + arch/csky/abiv2/inc/abi/page.h | 7 --- arch/hexagon/include/asm/page.h | 1 - arch/loongarch/include/asm/page.h | 1 - arch/m68k/include/asm/page_mm.h | 1 + arch/m68k/include/asm/page_no.h | 1 - arch/microblaze/include/asm/page.h | 1 - arch/mips/include/asm/page.h | 1 + arch/nios2/include/asm/page.h | 1 + arch/openrisc/include/asm/page.h | 1 - arch/parisc/include/asm/page.h | 1 - arch/powerpc/include/asm/page.h | 1 + arch/riscv/include/asm/page.h | 1 - arch/s390/include/asm/page.h | 1 - arch/sparc/include/asm/page_32.h | 2 + arch/sparc/include/asm/page_64.h | 1 + arch/um/include/asm/page.h | 1 - arch/x86/include/asm/page.h | 6 --- arch/x86/include/asm/page_32.h | 6 +++ arch/x86/include/asm/page_64.h | 64 ++++++++++++++++++----- arch/x86/lib/clear_page_64.S | 39 +++----------- arch/xtensa/include/asm/page.h | 1 - include/linux/highmem.h | 29 +++++++++++ include/linux/mm.h | 69 +++++++++++++++++++++++++ mm/memory.c | 82 ++++++++++++++++++++++-------- mm/util.c | 13 +++++ 30 files changed, 247 insertions(+), 91 deletions(-)Quoted 8 lines
Quoted 1 line
I guess this is an mm.git thing, with x86 acks (please).
Ack that.
Quoted 2 lines
The documented review activity is rather thin at this time so I'll sit this out for a while. Please ping me next week and we can reassess,
Will do. And, thanks for the quick look!
Hi Andrew
Quoted 8 lines
So, the comments I have so far are mostly about clarity around the connection with preempt model and some cleanups on the x86 patches.
Other than that, my major concern is wider testing (platforms and workloads) than mine has been.
Could you take another look at the series and see what else you think it needs.
Sorry for the delay from my side, I took another look at patches and had some smaller comments.
Signature · 3 lines
Cheers David