Skip to contentSkip to conversation

mm: folio_zero_user: clear contiguous pages

PATCH Superseded Ankur Arora 7 participants 24 messages Nov 21, 2025 – Nov 28, 2025

Subsystems (23)

v9 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v9 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 knowledge of the full extent to optimize the clearing better than if it sees only a single page sized extent at a time. AMD Zen uarchs, as an example, elide cacheline allocation for regions larger than LLC-size.

Demand faulting a 64GB region shows performance improvement:

 $ perf bench mem mmap -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

[#] Notice that we perform much better with preempt=full|lazy. That's
because preemptible models don't need explicit invocations of cond_resched() to ensure reasonable preemption latency, which, allows us to clear the full extent (1GB) in a single unit. In comparison the maximum extent used for preempt=none|voluntary is PROCESS_PAGES_NON_PREEMPT_BATCH (8MB).

The larger extent allows the processor to elide cacheline allocation (on Milan the threshold is LLC-size=32MB.)

(The hope is that eventually, in the fullness of time, the lazy preemption model will be able to do the same job that none or voluntary models are used for, allowing cond_resched() to go away.)

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 we are also cache limited because the workload accesses the entire region.

Raghavendra also tested previous version of the series on AMD Genoa also sees improvement [1] with preempt=lazy.
(The pg-sz=2MB improvement is much higher on Genoa than I see on Milan):

  $ perf bench mem map -p $page-size -f populate -s 64GB -l 10
                    base               patched              change
   pg-sz=2MB       12.731939 GB/sec    26.304263 GB/sec     106.6%
   pg-sz=1GB       26.232423 GB/sec    61.174836 GB/sec     133.2%

Changelog:

v9:
- Define PROCESS_PAGES_NON_PREEMPT_BATCH in common code (instead of inheriting ARCH_PAGE_CONTIG_NR.)
- Also document this in much greater detail as clearing pages needing a a constant dependent on the preemption model is facially quite odd.
(Suggested by David Hildenbrand, Andrew Morton, Borislav Petkov.)

- Switch architectural markers from __HAVE_ARCH_CLEAR_USER_PAGE (and similar) to clear_user_page etc. (Suggested by David Hildenbrand)

- s/memzero_page_aligned_unrolled/__clear_pages_unrolled/ (Suggested by Borislav Petkov.)
- style, comment fixes
(https://lore.kernel.org/lkml/[email protected]/)

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_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_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     | 59 +++++++++++++-----
 arch/x86/lib/clear_page_64.S       | 39 +++---------
 arch/xtensa/include/asm/page.h     |  1 -
 include/linux/highmem.h            | 29 +++++++++
 include/linux/mm.h                 | 98 ++++++++++++++++++++++++++++++
 mm/memory.c                        | 86 +++++++++++++++++++-------
 mm/util.c                          | 13 ++++
 28 files changed, 269 insertions(+), 94 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH v9 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 the macro clear_user_page 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.

---

Notes:

- For architectures which have a specialized version of clear_user_page(), define clear_user_page instead of __HAVE_ARCH_CLEAR_USER_PAGE.
- m68k/include/asm/page_mm.h, sparc/include/asm/page_32.h already define clear_user_page() as a macro so we just use that instead.

 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_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_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 ++++++++++++++++++++++
 22 files changed, 29 insertions(+), 26 deletions(-)
arch/alpha/include/asm/page.h +0 -1
index 5ec4c77e432e..d71ef845deca 100644
@@ -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
index 9720fe6b2c24..38214e126c6d 100644
@@ -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 clear_user_page clear_user_page
 void clear_user_page(void *to, unsigned long u_vaddr, struct page *page);
 
 typedef struct {
arch/arm/include/asm/page-nommu.h +0 -1
index 7c2c72323d17..e74415c959be 100644
@@ -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
index 258cca4b4873..0e8de245e283 100644
@@ -36,7 +36,6 @@ struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma,
 bool tag_clear_highpages(struct page *to, int numpages);
 #define __HAVE_ARCH_TAG_CLEAR_HIGHPAGES
 
-#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
index 2d2159933b76..58307254e7e5 100644
@@ -10,6 +10,7 @@ static inline unsigned long pages_do_alias(unsigned long addr1,
 	return (addr1 ^ addr2) & (SHMLBA-1);
 }
 
+#define clear_user_page clear_user_page
 static inline void clear_user_page(void *addr, unsigned long vaddr,
 				   struct page *page)
 {
arch/csky/abiv2/inc/abi/page.h +0 -7
index cf005f13cd15..a5a255013308 100644
@@ -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
index 137ba7c5de48..f0aed3ed812b 100644
@@ -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
index a3aaf34fba16..b83415fe4ffb 100644
@@ -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_no.h +0 -1
index 39db2026a4b4..d2532bc407ef 100644
@@ -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
index 90ac9f34b4b4..e1e396367ba7 100644
@@ -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
index bc3e3484c1bf..5ec428fcc887 100644
@@ -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 clear_user_page clear_user_page
 
 struct vm_area_struct;
 extern void copy_user_highpage(struct page *to, struct page *from,
arch/nios2/include/asm/page.h +1 -0
index 00a51623d38a..722956ac0bf8 100644
@@ -45,6 +45,7 @@
 
 struct page;
 
+#define clear_user_page clear_user_page
 extern 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
index 85797f94d1d7..d2cdbf3579bb 100644
@@ -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
index 8f4e51071ea1..3630b36d07da 100644
@@ -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
index b28fbb1d57eb..f2bb1f98eebe 100644
@@ -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 clear_user_page clear_user_page
 extern 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
index ffe213ad65a4..061b60b954ec 100644
@@ -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
index 9240a363c893..6635ba56d4b2 100644
@@ -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_64.h +1 -0
index d764d8a8586b..fd4dc85fb38b 100644
@@ -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 clear_user_page 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
index 6f54254aaf44..8cea97a9c8f9 100644
@@ -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
index 9265f2fca99a..416dc88e35c1 100644
@@ -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
index 20655174b111..059493256765 100644
@@ -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
index 7c79b3369b82..6fa6c188f99a 100644
@@ -3879,6 +3879,28 @@ static inline void clear_page_guard(struct zone *zone, struct page *page,
 				unsigned int order) {}
 #endif	/* CONFIG_DEBUG_PAGEALLOC */
 
+#ifndef 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_AREA
 extern 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.31.1
#2 lore raw

Re: [PATCH v9 1/7] treewide: provide a generic clear_user_page() variant

Le 21/11/2025 à 21:23, Ankur Arora a écrit :

Quoted 21 lines

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 the macro clear_user_page 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.

...

Quoted 23 lines
include/linux/mm.h +15 -0
index 7c79b3369b82..6fa6c188f99a 100644
@@ -3879,6 +3879,28 @@ static inline void clear_page_guard(struct zone *zone, struct page *page,
  				unsigned int order) {}
  #endif	/* CONFIG_DEBUG_PAGEALLOC */
 
+#ifndef 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().
+	 */

include/linux/mm.h static inline void clear_page_guard(struct zone *zone, struct page *page, · line 3906

Quoted 10 lines
include/linux/mm.h +7 -0
+	clear_user_highpage(page, vaddr);
+#else
+	clear_page(addr);
+#endif
+}
+#endif
+
  #ifdef __HAVE_ARCH_GATE_AREA
  extern struct vm_area_struct *get_gate_vma(struct mm_struct *mm);
  extern int in_gate_area_no_mm(unsigned long addr);

Isn't it chicken and egg with clear_user_highpage() in linux/highmem.h ? :

#ifndef clear_user_highpage
static inline void clear_user_highpage(struct page *page, unsigned long vaddr)

{
	void *addr = kmap_local_page(page);
	clear_user_page(addr, vaddr, page);
	kunmap_local(addr);
}

#endif

And at the end this function is the only caller of clear_user_page() so there is apparently no need for a generic clear_user_page(), at least not when clear_user_highpage() is defined.

I think is would be simpler and cleaner to instead add the following in linux/highmem.c:

#ifndef 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)

{
	clear_page(addr);
}

#endif

#3 lore raw

Re: [PATCH v9 1/7] treewide: provide a generic clear_user_page() variant

Quoted 55 lines · Christophe Leroy (CS GROUP)

Le 21/11/2025 à 21:23, Ankur Arora a écrit :

Quoted 21 lines

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 the macro clear_user_page 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.

...

Quoted 28 lines
include/linux/mm.h +20 -0
index 7c79b3369b82..6fa6c188f99a 100644
@@ -3879,6 +3879,28 @@ static inline void clear_page_guard(struct zone *zone, struct page *page,
   				unsigned int order) {}
   #endif	/* CONFIG_DEBUG_PAGEALLOC */
 
+#ifndef 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
+}
Quoted 8 lines

include/linux/mm.h static inline void clear_page_guard(struct zone *zone, struct page *page, · line 3906

Quoted 5 lines
include/linux/mm.h +2 -0
+#endif
+
   #ifdef __HAVE_ARCH_GATE_AREA
   extern struct vm_area_struct *get_gate_vma(struct mm_struct *mm);
   extern int in_gate_area_no_mm(unsigned long addr);

Isn't it chicken and egg with clear_user_highpage() in linux/highmem.h ? :

No really, because we make use of clear_user_highpage() only when the arch defines it, so the highmem.h variant is ignored?

Not that I particularly enjoy this way of handling it, so something cleaner would be nice :)

(in particular, relying on highmem.h defines in mm.h is a bit suboptimal)

Quoted 9 lines

#ifndef clear_user_highpage
static inline void clear_user_highpage(struct page *page, unsigned long vaddr)

{
	void *addr = kmap_local_page(page);
	clear_user_page(addr, vaddr, page);
	kunmap_local(addr);
}
Quoted 8 lines

#endif

And at the end this function is the only caller of clear_user_page() so there is apparently no need for a generic clear_user_page(), at least not when clear_user_highpage() is defined.

I think is would be simpler and cleaner to instead add the following in linux/highmem.c:

I assume you mean highmem.h

It's not really highmem.h material, but if it makes things cleaner, sure.

Might be that the compiler will not be happy about that.

@Ankur can you play with that and see if we can make compilers happy one way or the other?

Signature · 3 lines
Cheers

David
#4 lore raw

Re: [PATCH v9 1/7] treewide: provide a generic clear_user_page() variant

Quoted 65 lines · David Hildenbrand (Red Hat)
Quoted 55 lines · Christophe Leroy (CS GROUP)

Le 21/11/2025 à 21:23, Ankur Arora a écrit :

Quoted 21 lines

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 the macro clear_user_page 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.

...

Quoted 28 lines
include/linux/mm.h +20 -0
index 7c79b3369b82..6fa6c188f99a 100644
@@ -3879,6 +3879,28 @@ static inline void clear_page_guard(struct zone *zone, struct page *page,
    				unsigned int order) {}
    #endif	/* CONFIG_DEBUG_PAGEALLOC */
 
+#ifndef 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
+}
Quoted 8 lines

include/linux/mm.h static inline void clear_page_guard(struct zone *zone, struct page *page, · line 3906

Quoted 5 lines
include/linux/mm.h +2 -0
+#endif
+
    #ifdef __HAVE_ARCH_GATE_AREA
    extern struct vm_area_struct *get_gate_vma(struct mm_struct *mm);
    extern int in_gate_area_no_mm(unsigned long addr);

Isn't it chicken and egg with clear_user_highpage() in linux/highmem.h ? :

Quoted 7 lines

No really, because we make use of clear_user_highpage() only when the arch defines it, so the highmem.h variant is ignored?

Not that I particularly enjoy this way of handling it, so something cleaner would be nice :)

(in particular, relying on highmem.h defines in mm.h is a bit suboptimal)

Correction: there is no highmem.h dependency, as clear_user_highpage() comes directly from the architecture helpers in page.h

Signature · 3 lines
Cheers

David
#5 lore raw

Re: [PATCH v9 1/7] treewide: provide a generic clear_user_page() variant

In reply to David Hildenbrand (Red Hat)
Quoted 84 lines · David Hildenbrand (Red Hat)
Quoted 51 lines · Christophe Leroy (CS GROUP)
Quoted 21 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 the macro clear_user_page 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.

...

Quoted 26 lines
include/linux/mm.h +18 -0
index 7c79b3369b82..6fa6c188f99a 100644
@@ -3879,6 +3879,28 @@ static inline void clear_page_guard(struct zone *zone, struct page *page,
   				unsigned int order) {}
   #endif	/* CONFIG_DEBUG_PAGEALLOC */
   +#ifndef 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
Quoted 8 lines

include/linux/mm.h static inline void clear_page_guard(struct zone *zone, struct page *page, · line 3906

Quoted 6 lines
include/linux/mm.h +3 -0
+}
+#endif
+
   #ifdef __HAVE_ARCH_GATE_AREA
   extern struct vm_area_struct *get_gate_vma(struct mm_struct *mm);
   extern int in_gate_area_no_mm(unsigned long addr);

Isn't it chicken and egg with clear_user_highpage() in linux/highmem.h ? :

No really, because we make use of clear_user_highpage() only when the arch defines it, so the highmem.h variant is ignored?

Not that I particularly enjoy this way of handling it, so something cleaner would be nice :)

(in particular, relying on highmem.h defines in mm.h is a bit suboptimal)

Quoted 8 lines

#ifndef clear_user_highpage
static inline void clear_user_highpage(struct page *page, unsigned long vaddr)

{
	void *addr = kmap_local_page(page);
	clear_user_page(addr, vaddr, page);
	kunmap_local(addr);
}
Quoted 6 lines

#endif
And at the end this function is the only caller of clear_user_page() so there is apparently no need for a generic clear_user_page(), at least not when clear_user_highpage() is defined.
I think is would be simpler and cleaner to instead add the following in linux/highmem.c:

Quoted 8 lines

I assume you mean highmem.h

It's not really highmem.h material, but if it makes things cleaner, sure.

Might be that the compiler will not be happy about that.

@Ankur can you play with that and see if we can make compilers happy one way or the other?

This looks like a good change (and from my tests on a couple of configs seems to build fine.)

Though clear_user_pages() is also only called from clear_user_highpages(). Would be good to treat that similarly.

(I don't think it can be done because how the arch code treats both quite differently.)

Anyway I'll play with that a bit more.

--
ankur

#6 lore raw

Re: [PATCH v9 1/7] treewide: provide a generic clear_user_page() variant

Quoted 98 lines · Ankur Arora
Quoted 84 lines · David Hildenbrand (Red Hat)
Quoted 51 lines · Christophe Leroy (CS GROUP)
Quoted 21 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 the macro clear_user_page 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.

...

Quoted 26 lines
include/linux/mm.h +18 -0
index 7c79b3369b82..6fa6c188f99a 100644
@@ -3879,6 +3879,28 @@ static inline void clear_page_guard(struct zone *zone, struct page *page,
   				unsigned int order) {}
   #endif	/* CONFIG_DEBUG_PAGEALLOC */
   +#ifndef 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
Quoted 8 lines

include/linux/mm.h static inline void clear_page_guard(struct zone *zone, struct page *page, · line 3905

Quoted 6 lines
include/linux/mm.h +3 -0
+}
+#endif
+
   #ifdef __HAVE_ARCH_GATE_AREA
   extern struct vm_area_struct *get_gate_vma(struct mm_struct *mm);
   extern int in_gate_area_no_mm(unsigned long addr);

Isn't it chicken and egg with clear_user_highpage() in linux/highmem.h ? :

No really, because we make use of clear_user_highpage() only when the arch defines it, so the highmem.h variant is ignored?

Not that I particularly enjoy this way of handling it, so something cleaner would be nice :)

(in particular, relying on highmem.h defines in mm.h is a bit suboptimal)

Quoted 8 lines

#ifndef clear_user_highpage
static inline void clear_user_highpage(struct page *page, unsigned long vaddr)

{
	void *addr = kmap_local_page(page);
	clear_user_page(addr, vaddr, page);
	kunmap_local(addr);
}
Quoted 6 lines

#endif
And at the end this function is the only caller of clear_user_page() so there is apparently no need for a generic clear_user_page(), at least not when clear_user_highpage() is defined.
I think is would be simpler and cleaner to instead add the following in linux/highmem.c:

Quoted 8 lines

I assume you mean highmem.h

It's not really highmem.h material, but if it makes things cleaner, sure.

Might be that the compiler will not be happy about that.

@Ankur can you play with that and see if we can make compilers happy one way or the other?

This looks like a good change (and from my tests on a couple of configs seems to build fine.)

Quoted 7 lines

Though clear_user_pages() is also only called from clear_user_highpages(). Would be good to treat that similarly.

(I don't think it can be done because how the arch code treats both quite differently.)

Anyway I'll play with that a bit more.

How about something like this for clear_user_page() (though maybe I should be always defining clear_user_page() and not conditioning it on the existence of the generic clear_user_highpage()):

include/linux/highmem.h +16 -0
index abc20f9810fd..ca9d28aa29b2 100644
@@ -197,6 +197,22 @@ static inline void invalidate_kernel_vmap_range(void *vaddr, int size)
 }
 #endif
 
+#if !defined(clear_user_page) && !defined(clear_user_highpage)
+/**
+ * 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
+ *
+ * The sole user of clear_user_page() is clear_user_highpage().
+ * Define it if the arch does not and only if needed.
+ */
+static inline void clear_user_page(void *addr, unsigned long vaddr, struct page *page)
+{
+       clear_page(addr);
+}
+#endif
+
 /* when CONFIG_HIGHMEM is not set these will be plain clear/copy_page */
 #ifndef clear_user_highpage
 static inline void clear_user_highpage(struct page *page, unsigned long vaddr)

And for clear_user_pages():

include/linux/highmem.h +29 -0
index ca9d28aa29b2..b9b3cc76a91a 100644
@@ -223,6 +223,35 @@ static inline void clear_user_highpage(struct page *page, unsigned long vaddr)
 }
 #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.
+ */
+static inline void clear_user_pages(void *addr, unsigned long vaddr,
+               struct page *page, unsigned int npages)
+{
+#ifdef clear_user_page
+       do {
+               clear_user_page(addr, vaddr, page);
+               addr += PAGE_SIZE;
+               vaddr += PAGE_SIZE;
+               page++;
+       } while (--npages);
+#else
+       /*
+        * Prefer clear_pages() to allow for architectural optimizations
+        * when operations on contiguous page ranges.
+        */
+       clear_pages(addr, npages);
+#endif
+}
+
 

Tree with changes: github.com/terminus/linux clear-pages.v10-rc2

This seems to be fine on a variety of configurations.

David, could you take a quick look at it?

Thanks
--
ankur

#7 lore raw

Re: [PATCH v9 1/7] treewide: provide a generic clear_user_page() variant

Hi Ankur,

Le 28/11/2025 à 00:57, Ankur Arora a écrit :

Quoted 21 lines

Ankur Arora <[email protected]> writes:

How about something like this for clear_user_page() (though maybe I should be always defining clear_user_page() and not conditioning it on the existence of the generic clear_user_highpage()):

include/linux/highmem.h +5 -0
index abc20f9810fd..ca9d28aa29b2 100644
@@ -197,6 +197,22 @@ static inline void invalidate_kernel_vmap_range(void *vaddr, int size)
  }
  #endif
 
+#if !defined(clear_user_page) && !defined(clear_user_highpage)
+/**
+ * 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

include/linux/highmem.h static inline void invalidate_kernel_vmap_range(void *vaddr, int size) · line 214

Quoted 10 lines
include/linux/highmem.h +10 -0
+ * @page: the page
+ *
+ * The sole user of clear_user_page() is clear_user_highpage().
+ * Define it if the arch does not and only if needed.
+ */
+static inline void clear_user_page(void *addr, unsigned long vaddr, struct page *page)
+{
+       clear_page(addr);
+}
+#endif

WOuld be more obvious if you enclose that inside the same #ifdef as clear_user_highpage(), something like:

#ifndef clear_user_highpage

#ifndef clear_user_page
static inline void clear_user_page(void *addr, unsigned long vaddr, struct page *page)

{
	clear_page(addr);
}

#endif

static inline void clear_user_highpage(struct page *page, unsigned long vaddr)

{
	void *addr = kmap_local_page(page);
	clear_user_page(addr, vaddr, page);
	kunmap_local(addr);
}

#endif

Quoted 33 lines
include/linux/highmem.h +1 -0
+
  /* when CONFIG_HIGHMEM is not set these will be plain clear/copy_page */
  #ifndef clear_user_highpage
  static inline void clear_user_highpage(struct page *page, unsigned long vaddr)

And for clear_user_pages():

include/linux/highmem.h +18 -0
index ca9d28aa29b2..b9b3cc76a91a 100644
@@ -223,6 +223,35 @@ static inline void clear_user_highpage(struct page *page, unsigned long vaddr)
  }
  #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.
+ */
+static inline void clear_user_pages(void *addr, unsigned long vaddr,
+               struct page *page, unsigned int npages)
+{
+#ifdef clear_user_page
+       do {
+               clear_user_page(addr, vaddr, page);
+               addr += PAGE_SIZE;
+               vaddr += PAGE_SIZE;

include/linux/highmem.h static inline void clear_user_highpage(struct page *page, unsigned long vaddr) · line 253

Quoted 10 lines
include/linux/highmem.h +10 -0
+               page++;
+       } while (--npages);
+#else
+       /*
+        * Prefer clear_pages() to allow for architectural optimizations
+        * when operations on contiguous page ranges.
+        */
+       clear_pages(addr, npages);
+#endif
+}

Not sure to understand the logic. You say this is not expected to be overriden by architectures in the near future, then why do we need that ? Can't we do everything inside clear_user_highpages() for clarity ?

At the time being clear_user_page() is used exclusively by clear_user_highpage() so I expect clear_user_page() to only exist when CONFIG_HIGHMEM is enabled. And in that case clear_user_highpages() doesn't call clear_user_pages() so at the end only the else part remains, which is a simple call to clear_pages(). Why not just call clear_pages() directly from clear_user_highpages() and drop clear_user_pages() ?

Christophe

#8 lore raw

Re: [PATCH v9 1/7] treewide: provide a generic clear_user_page() variant

Quoted 25 lines · Christophe Leroy (CS GROUP)

Hi Ankur,

Quoted 22 lines · Ankur Arora

Ankur Arora <[email protected]> writes:

How about something like this for clear_user_page() (though maybe I should be always defining clear_user_page() and not conditioning it on the existence of the generic clear_user_highpage()):

include/linux/highmem.h +10 -0
index abc20f9810fd..ca9d28aa29b2 100644
@@ -197,6 +197,22 @@ static inline void invalidate_kernel_vmap_range(void *vaddr, int size)
  }
  #endif
+#if !defined(clear_user_page) && !defined(clear_user_highpage)
+/**
+ * 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
+ *
+ * The sole user of clear_user_page() is clear_user_highpage().
+ * Define it if the arch does not and only if needed.
+ */
Quoted 8 lines

include/linux/highmem.h static inline void invalidate_kernel_vmap_range(void *vaddr, int size) · line 214

Quoted 5 lines
include/linux/highmem.h +5 -0
+static inline void clear_user_page(void *addr, unsigned long vaddr, struct page *page)
+{
+       clear_page(addr);
+}
+#endif

WOuld be more obvious if you enclose that inside the same #ifdef as clear_user_highpage(), something like:

Yeah I was debating whether to do that or not.

Quoted 10 lines

#ifndef clear_user_highpage

#ifndef clear_user_page
static inline void clear_user_page(void *addr, unsigned long vaddr, struct page *page)

{
	clear_page(addr);
}

#endif

Quoted 7 lines

static inline void clear_user_highpage(struct page *page, unsigned long vaddr)

{
	void *addr = kmap_local_page(page);
	clear_user_page(addr, vaddr, page);
	kunmap_local(addr);
}

#endif

On second thoughts after the discussion below it's less confusing if I didn't tie these two together:

#ifndef clear_user_page
static inline void clear_user_page(void *addr, unsigned long vaddr, struct page *page) {
clear_page(addr);
}
#endif

#ifndef clear_user_highpage
static inline void clear_user_highpage(struct page *page, unsigned long vaddr) {
void *addr = kmap_local_page(page);
clear_user_page(addr, vaddr, page);
kunmap_local(addr);
}
#endif

Quoted 36 lines
Quoted 36 lines
include/linux/highmem.h +1 -0
+
  /* when CONFIG_HIGHMEM is not set these will be plain clear/copy_page */
  #ifndef clear_user_highpage
  static inline void clear_user_highpage(struct page *page, unsigned long vaddr)

And for clear_user_pages():

include/linux/highmem.h +24 -0
index ca9d28aa29b2..b9b3cc76a91a 100644
@@ -223,6 +223,35 @@ static inline void clear_user_highpage(struct page *page, unsigned long vaddr)
  }
  #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.
+ */
+static inline void clear_user_pages(void *addr, unsigned long vaddr,
+               struct page *page, unsigned int npages)
+{
+#ifdef clear_user_page
+       do {
+               clear_user_page(addr, vaddr, page);
+               addr += PAGE_SIZE;
+               vaddr += PAGE_SIZE;
+               page++;
+       } while (--npages);
+#else
+       /*
+        * Prefer clear_pages() to allow for architectural optimizations
+        * when operations on contiguous page ranges.
Quoted 8 lines

include/linux/highmem.h static inline void clear_user_highpage(struct page *page, unsigned long vaddr) · line 253

Quoted 4 lines
include/linux/highmem.h +4 -0
+        */
+       clear_pages(addr, npages);
+#endif
+}

Not sure to understand the logic. You say this is not expected to be overriden by architectures in the near future, then why do we need that ? Can't we do everything inside clear_user_highpages() for clarity ?

clear_user_page[s]() is semantically different enough from clear_user_highpages() that I don't think it makes sense to fuse the two.

For one thing, we would have to disentangle them again if/once CONFIG_HIGHMEM goes away.

Quoted 2 lines

At the time being clear_user_page() is used exclusively by clear_user_highpage() so I expect clear_user_page() to only exist when CONFIG_HIGHMEM is enabled. And

The generic clear_user_page() only exists if the generic clear_user_highpage() is defined. The arch might provide a clear_user_page() of its own (ex. arm/sparc etc).

Quoted 4 lines

in that case clear_user_highpages() doesn't call clear_user_pages() so at the end only the else part remains, which is a simple call to clear_pages(). Why not just call clear_pages() directly from clear_user_highpages() and drop clear_user_pages() ?

For !HIGHMEM, the clear_user_pages() might be just clear_pages() or a loop around an architecture specific clear_user_page().

--
ankur

#9 lore raw

PATCH v9 2/7 mm: introduce clear_pages() and clear_user_pages()

In reply to 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 uses clear_user_page() into mm/util.c for now to work around macro magic on sparc and m68k.

---

Notes:
- Use macros clear_pages, clear_user_page, instead of __HAVE_ARCH_CLEAR_PAGES, __HAVE_ARCH_CLEAR_USER_PAGE.

 include/linux/mm.h | 41 +++++++++++++++++++++++++++++++++++++++++
 mm/util.c          | 13 +++++++++++++
 2 files changed, 54 insertions(+)
include/linux/mm.h +41 -0
index 6fa6c188f99a..c397ee2f6dd5 100644
@@ -3879,6 +3879,26 @@ static inline void clear_page_guard(struct zone *zone, struct page *page,
 				unsigned int order) {}
 #endif	/* CONFIG_DEBUG_PAGEALLOC */
 
+#ifndef 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 clear_user_page
 /**
  * clear_user_page() - clear a page to be mapped to user space
@@ -3901,6 +3921,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 clear_user_pages
+void clear_user_pages(void *addr, unsigned long vaddr,
+		struct page *page, unsigned int npages);
+#else
+static inline void clear_user_pages(void *addr, unsigned long vaddr,
+		struct page *page, unsigned int npages)
+{
+	clear_pages(addr, npages);
+}
+#endif
+
 #ifdef __HAVE_ARCH_GATE_AREA
 extern 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
index 8989d5767528..3c6cd44db1bd 100644
@@ -1344,3 +1344,16 @@ bool page_range_contiguous(const struct page *page, unsigned long nr_pages)
 }
 EXPORT_SYMBOL(page_range_contiguous);
 #endif
+
+#ifdef 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
Signature · 1 line
2.31.1
#10 lore raw

Re: [PATCH v9 2/7] mm: introduce clear_pages() and clear_user_pages()

kernel test robot noticed the following build errors:

Hi Ankur,

kernel test robot noticed the following build errors:

[auto build test ERROR on linus/master]
[also build test ERROR on v6.18-rc6 next-20251121]
[cannot apply to akpm-mm/mm-everything tip/x86/core arm64/for-next/core gerg-m68knommu/for-next deller-parisc/for-next powerpc/next powerpc/fixes s390/features uml/next vgupta-arc/for-next vgupta-arc/for-curr openrisc/for-next] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Ankur-Arora/treewide-provide-a-generic-clear_user_page-variant/20251122-042758
base:   linus/master
patch link:    https://lore.kernel.org/r/20251121202352.494700-3-ankur.a.arora%40oracle.com
patch subject: [PATCH v9 2/7] mm: introduce clear_pages() and clear_user_pages()
config: arc-allnoconfig (https://download.01.org/0day-ci/archive/20251122/[email protected]/config)
compiler: arc-linux-gcc (GCC) 15.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251122/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> mm/util.c:1349:6: error: redefinition of 'clear_user_pages'

1349 | void clear_user_pages(void *addr, unsigned long vaddr,
| ^~~~~~~~~~~~~~~~
In file included from mm/util.c:2:
include/linux/mm.h:3938:20: note: previous definition of 'clear_user_pages' with type 'void(void *, long unsigned int, struct page *, unsigned int)'
3938 | static inline void clear_user_pages(void *addr, unsigned long vaddr,
| ^~~~~~~~~~~~~~~~

vim +/clear_user_pages +1349 mm/util.c

1347
1348 #ifdef clear_user_page

> 1349	void clear_user_pages(void *addr, unsigned long vaddr,
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#11 lore raw

Re: [PATCH v9 2/7] mm: introduce clear_pages() and clear_user_pages()

In reply to Ankur Arora

Le 21/11/2025 à 21:23, Ankur Arora a écrit :

Quoted 37 lines

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 uses clear_user_page() into mm/util.c for now to work around macro magic on sparc and m68k.

---

Notes:
- Use macros clear_pages, clear_user_page, instead of __HAVE_ARCH_CLEAR_PAGES, __HAVE_ARCH_CLEAR_USER_PAGE.

  include/linux/mm.h | 41 +++++++++++++++++++++++++++++++++++++++++
  mm/util.c          | 13 +++++++++++++
  2 files changed, 54 insertions(+)
include/linux/mm.h +7 -0
index 6fa6c188f99a..c397ee2f6dd5 100644
@@ -3879,6 +3879,26 @@ static inline void clear_page_guard(struct zone *zone, struct page *page,
  				unsigned int order) {}
  #endif	/* CONFIG_DEBUG_PAGEALLOC */
 
+#ifndef 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

include/linux/mm.h static inline void clear_page_guard(struct zone *zone, struct page *page, · line 3898

Quoted 10 lines
include/linux/mm.h +10 -0
+ * 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);

Why a 'do while' instead of a 'while' ?

Are you certain that this function will never ever be called with a nul npages ?

Quoted 13 lines
include/linux/mm.h +6 -0
+}
+#endif
+
  #ifndef clear_user_page
  /**
   * clear_user_page() - clear a page to be mapped to user space
@@ -3901,6 +3921,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

include/linux/mm.h static inline void clear_user_page(void *addr, unsigned long vaddr, struct page · line 3936

Quoted 10 lines
include/linux/mm.h +10 -0
+ * @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 clear_user_pages
+void clear_user_pages(void *addr, unsigned long vaddr,
+		struct page *page, unsigned int npages);

By doing this you forbid architectures to define it as a static inline, is that wanted ?

Quoted 16 lines
include/linux/mm.h +8 -0
+#else
+static inline void clear_user_pages(void *addr, unsigned long vaddr,
+		struct page *page, unsigned int npages)
+{
+	clear_pages(addr, npages);
+}
+#endif
+
  #ifdef __HAVE_ARCH_GATE_AREA
  extern struct vm_area_struct *get_gate_vma(struct mm_struct *mm);
  extern int in_gate_area_no_mm(unsigned long addr);
mm/util.c +0 -0
index 8989d5767528..3c6cd44db1bd 100644

mm/util.c bool page_range_contiguous(const struct page *page, unsigned long nr_pages) · line 1349

Quoted 6 lines
mm/util.c +3 -0
@@ -1344,3 +1344,16 @@ bool page_range_contiguous(const struct page *page, unsigned long nr_pages)
  }
  EXPORT_SYMBOL(page_range_contiguous);
  #endif
+
+#ifdef clear_user_page
+void clear_user_pages(void *addr, 

What happens if clear_user_page is defined but not clear_user_pages ? In that case it seems like the definition in linux/mm.h will conflict.

unsigned long vaddr,

mm/util.c bool page_range_contiguous(const struct page *page, unsigned long nr_pages) · line 1357

Quoted 8 lines
mm/util.c +8 -0
+		struct page *page, unsigned int npages)
+{
+	do {
+		clear_user_page(addr, vaddr, page);
+		addr += PAGE_SIZE;
+		vaddr += PAGE_SIZE;
+		page++;
+	} while (--npages);

Same, are you sure npages will never be nul ?

Quoted 2 lines
mm/util.c +2 -0
+}
+#endif
#12 lore raw

Re: [PATCH v9 2/7] mm: introduce clear_pages() and clear_user_pages()

Replying here while I am already at it.

Quoted 11 lines
Quoted 11 lines
include/linux/mm.h +11 -0
+#ifndef 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.
+ */
Quoted 8 lines

include/linux/mm.h static inline void clear_page_guard(struct zone *zone, struct page *page, · line 3898

Quoted 6 lines
include/linux/mm.h +6 -0
+static inline void clear_pages(void *addr, unsigned int npages)
+{
+	do {
+		clear_page(addr);
+		addr += PAGE_SIZE;
+	} while (--npages);

Why a 'do while' instead of a 'while' ?

More efficient when we know that npages > 0.

Quoted 3 lines

Are you certain that this function will never ever be called with a nul npages ?

That is the expectation here, yes. We should probably document that expectation.

Quoted 11 lines
Quoted 11 lines
include/linux/mm.h +3 -0
+}
+#endif
+
   #ifndef clear_user_page
   /**
    * clear_user_page() - clear a page to be mapped to user space
@@ -3901,6 +3921,27 @@ static inline void clear_user_page(void *addr, unsigned long vaddr, struct page
   }
   #endif
 
Quoted 16 lines

include/linux/mm.h static inline void clear_user_page(void *addr, unsigned long vaddr, struct page · line 3936

Quoted 13 lines
include/linux/mm.h +13 -0
+/**
+ * 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 clear_user_pages
+void clear_user_pages(void *addr, unsigned long vaddr,
+		struct page *page, unsigned int npages);

By doing this you forbid architectures to define it as a static inline, is that wanted ?

Note that this is not the intention. The intention is to either use a direct mapping to clear_pages(), or fallback to the variant in mm/util.c.

The architecture is currently never expected to provide clear_user_pages().

Wondering if we can make that cleaner.

I'm wondering if the dependency on highmem.h here in mm.h is rather the problem.

How I hate this macro crap with arch overrides.

Quoted 18 lines
Quoted 18 lines
include/linux/mm.h +8 -0
+#else
+static inline void clear_user_pages(void *addr, unsigned long vaddr,
+		struct page *page, unsigned int npages)
+{
+	clear_pages(addr, npages);
+}
+#endif
+
   #ifdef __HAVE_ARCH_GATE_AREA
   extern struct vm_area_struct *get_gate_vma(struct mm_struct *mm);
   extern int in_gate_area_no_mm(unsigned long addr);
mm/util.c +0 -0
index 8989d5767528..3c6cd44db1bd 100644
@@ -1344,3 +1344,16 @@ bool page_range_contiguous(const struct page *page, unsigned long nr_pages)
   }
Quoted 8 lines

mm/util.c bool page_range_contiguous(const struct page *page, unsigned long nr_pages)

Quoted 5 lines
mm/util.c +3 -0
   EXPORT_SYMBOL(page_range_contiguous);
   #endif
+
+#ifdef clear_user_page
+void clear_user_pages(void *addr,

What happens if clear_user_page is defined but not clear_user_pages ? In that case it seems like the definition in linux/mm.h will conflict.

The generic mm.h variant will not set clear_user_page() and consequently we map directly to clear_pages().

Signature · 3 lines
Cheers

David
#13 lore raw

Re: [PATCH v9 2/7] mm: introduce clear_pages() and clear_user_pages()

From: Lance Yang <[email protected]>

Quoted 98 lines · David Hildenbrand (Red Hat)

Replying here while I am already at it.

Quoted 11 lines
Quoted 11 lines
include/linux/mm.h +11 -0
+#ifndef 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.
+ */
Quoted 8 lines

include/linux/mm.h static inline void clear_page_guard(struct zone *zone, struct page *page, · line 3898

Quoted 6 lines
include/linux/mm.h +6 -0
+static inline void clear_pages(void *addr, unsigned int npages)
+{
+	do {
+		clear_page(addr);
+		addr += PAGE_SIZE;
+	} while (--npages);

Why a 'do while' instead of a 'while' ?

More efficient when we know that npages > 0.

Quoted 3 lines

Are you certain that this function will never ever be called with a nul npages ?

That is the expectation here, yes. We should probably document that expectation.

Quoted 11 lines
Quoted 11 lines
include/linux/mm.h +3 -0
+}
+#endif
+
   #ifndef clear_user_page
   /**
    * clear_user_page() - clear a page to be mapped to user space
@@ -3901,6 +3921,27 @@ static inline void clear_user_page(void *addr, unsigned long vaddr, struct page
   }
   #endif
 
Quoted 16 lines

include/linux/mm.h static inline void clear_user_page(void *addr, unsigned long vaddr, struct page · line 3936

Quoted 13 lines
include/linux/mm.h +13 -0
+/**
+ * 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 clear_user_pages
+void clear_user_pages(void *addr, unsigned long vaddr,
+		struct page *page, unsigned int npages);

By doing this you forbid architectures to define it as a static inline, is that wanted ?

Note that this is not the intention. The intention is to either use a direct mapping to clear_pages(), or fallback to the variant in mm/util.c.

The architecture is currently never expected to provide clear_user_pages().

Wondering if we can make that cleaner.

I'm wondering if the dependency on highmem.h here in mm.h is rather the problem.

How I hate this macro crap with arch overrides.

Quoted 18 lines
Quoted 18 lines
include/linux/mm.h +8 -0
+#else
+static inline void clear_user_pages(void *addr, unsigned long vaddr,
+		struct page *page, unsigned int npages)
+{
+	clear_pages(addr, npages);
+}
+#endif
+
   #ifdef __HAVE_ARCH_GATE_AREA
   extern struct vm_area_struct *get_gate_vma(struct mm_struct *mm);
   extern int in_gate_area_no_mm(unsigned long addr);
mm/util.c +0 -0
index 8989d5767528..3c6cd44db1bd 100644
@@ -1344,3 +1344,16 @@ bool page_range_contiguous(const struct page *page, unsigned long nr_pages)
   }
Quoted 8 lines

mm/util.c bool page_range_contiguous(const struct page *page, unsigned long nr_pages)

Quoted 5 lines
mm/util.c +3 -0
   EXPORT_SYMBOL(page_range_contiguous);
   #endif
+
+#ifdef clear_user_page
+void clear_user_pages(void *addr,

What happens if clear_user_page is defined but not clear_user_pages ? In that case it seems like the definition in linux/mm.h will conflict.

Quoted 2 lines

The generic mm.h variant will not set clear_user_page() and consequently we map directly to clear_pages().

Hmm, I suspect there might be a subtle issue with the build flow on SPARC ...

Inside include/linux/mm.h, the guard checks for clear_user_pages (plural). Since SPARC doesn't define that, the header provides the static inline fallback.

However, mm/util.c includes that header. And since SPARC does define clear_user_page (singular), the .c file proceeds to compile the non-static definition as well.

Wouldn't that result in the compiler seeing both a static inline and a non-static definition in the same translation unit? It seems like this would trigger a redefinition error ...

Thanks,
Lance

#14 lore raw

Re: [PATCH v9 2/7] mm: introduce clear_pages() and clear_user_pages()

Quoted 111 lines · Lance Yang

From: Lance Yang <[email protected]>

Quoted 98 lines · David Hildenbrand (Red Hat)

Replying here while I am already at it.

Quoted 11 lines
Quoted 11 lines
include/linux/mm.h +11 -0
+#ifndef 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.
+ */
Quoted 8 lines

include/linux/mm.h static inline void clear_page_guard(struct zone *zone, struct page *page, · line 3898

Quoted 6 lines
include/linux/mm.h +6 -0
+static inline void clear_pages(void *addr, unsigned int npages)
+{
+	do {
+		clear_page(addr);
+		addr += PAGE_SIZE;
+	} while (--npages);

Why a 'do while' instead of a 'while' ?

More efficient when we know that npages > 0.

Quoted 3 lines

Are you certain that this function will never ever be called with a nul npages ?

That is the expectation here, yes. We should probably document that expectation.

Quoted 11 lines
Quoted 11 lines
include/linux/mm.h +3 -0
+}
+#endif
+
   #ifndef clear_user_page
   /**
    * clear_user_page() - clear a page to be mapped to user space
@@ -3901,6 +3921,27 @@ static inline void clear_user_page(void *addr, unsigned long vaddr, struct page
   }
   #endif
 
Quoted 16 lines

include/linux/mm.h static inline void clear_user_page(void *addr, unsigned long vaddr, struct page · line 3936

Quoted 13 lines
include/linux/mm.h +13 -0
+/**
+ * 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 clear_user_pages
+void clear_user_pages(void *addr, unsigned long vaddr,
+		struct page *page, unsigned int npages);

By doing this you forbid architectures to define it as a static inline, is that wanted ?

Note that this is not the intention. The intention is to either use a direct mapping to clear_pages(), or fallback to the variant in mm/util.c.

The architecture is currently never expected to provide clear_user_pages().

Wondering if we can make that cleaner.

I'm wondering if the dependency on highmem.h here in mm.h is rather the problem.

How I hate this macro crap with arch overrides.

Quoted 18 lines
Quoted 18 lines
include/linux/mm.h +8 -0
+#else
+static inline void clear_user_pages(void *addr, unsigned long vaddr,
+		struct page *page, unsigned int npages)
+{
+	clear_pages(addr, npages);
+}
+#endif
+
   #ifdef __HAVE_ARCH_GATE_AREA
   extern struct vm_area_struct *get_gate_vma(struct mm_struct *mm);
   extern int in_gate_area_no_mm(unsigned long addr);
mm/util.c +0 -0
index 8989d5767528..3c6cd44db1bd 100644
@@ -1344,3 +1344,16 @@ bool page_range_contiguous(const struct page *page, unsigned long nr_pages)
   }
Quoted 8 lines

mm/util.c bool page_range_contiguous(const struct page *page, unsigned long nr_pages)

Quoted 5 lines
mm/util.c +3 -0
   EXPORT_SYMBOL(page_range_contiguous);
   #endif
+
+#ifdef clear_user_page
+void clear_user_pages(void *addr,

What happens if clear_user_page is defined but not clear_user_pages ? In that case it seems like the definition in linux/mm.h will conflict.

Quoted 2 lines

The generic mm.h variant will not set clear_user_page() and consequently we map directly to clear_pages().

Hmm, I suspect there might be a subtle issue with the build flow on SPARC ...

Inside include/linux/mm.h, the guard checks for clear_user_pages (plural). Since SPARC doesn't define that, the header provides the static inline fallback.

Quoted 7 lines

However, mm/util.c includes that header. And since SPARC does define clear_user_page (singular), the .c file proceeds to compile the non-static definition as well.

Wouldn't that result in the compiler seeing both a static inline and a non-static definition in the same translation unit? It seems like this would trigger a redefinition error ...

Yeah it would.

I had only posted the linux/highmem.h header bits for brevity but the full patch removes the mm/util.c bits.

Sorry about the confusion.

--
ankur

#15 lore raw

PATCH v9 3/7 mm/highmem: introduce clear_user_highpages()

In reply to 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().

---
 include/linux/highmem.h | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
include/linux/highmem.h +29 -0
index abc20f9810fd..c7ca88ce9937 100644
@@ -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.31.1
#16 lore raw

PATCH v9 4/7 x86/mm: Simplify clear_page_*

In reply to Ankur Arora

clear_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().

---

Notes:
- s/memzero_page_aligned_unrolled/__clear_pages_unrolled/
- fixup comment to specify x86 insns in the standard ALL CAPS style.

 arch/x86/include/asm/page_32.h |  6 ++++
 arch/x86/include/asm/page_64.h | 50 ++++++++++++++++++++++++----------
 arch/x86/lib/clear_page_64.S   | 39 ++++++--------------------
 3 files changed, 49 insertions(+), 46 deletions(-)
arch/x86/include/asm/page_32.h +6 -0
index 0c623706cb7e..19fddb002cc9 100644
@@ -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 +35 -15

61 lines not on this page · raw message

arch/x86/lib/clear_page_64.S +8 -31
index a508e4a8c66a..c245b7fc01cd 100644
@@ -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(__clear_pages_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(__clear_pages_unrolled)
+EXPORT_SYMBOL_GPL(__clear_pages_unrolled)
 
 /*
  * Default clear user-space.
Signature · 1 line
2.31.1
maintainer Reviewed-by on v9 4/7
#17 lore raw

Re: [PATCH v9 4/7] x86/mm: Simplify clear_page_*

Quoted 12 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:
+ *
+ * - __clear_pages_unrolled(): the oldest, slowest and universally
+ * supported method. Zeroes via 8-byte MOV instructions unrolled 8x
+ * to write a 64-byte cacheline in each loop iteration.
+ *
+ * - "REP; STOSQ": really old CPUs had crummy REP implementations.

Quoted 8 lines
+ *    Vendor CPU setup code sets 'REP_GOOD' on CPUs where REP can be
+ *    trusted. The instruction writes 8-byte per REP iteration but
+ *    CPUs can internally batch these together and do larger writes.
+ *
+ *  - "REP; STOSB": CPUs that enumerate 'ERMS' have an improved STOS
+ *    implementation that is less picky about alignment and where
+ *    STOSB (1-byte at a time) is actually faster than STOSQ (8-bytes
+ *    at a time.)

Please put here in BIG RED LETTERS something along the lines of:

"The inline asm has a CALL instruction and usually that is a no-no due to the compiler not knowing that there's a CALL inside the asm and thus won't track callee-clobbered registers but in this case, all the callee clobbereds by __clear_pages_unrolled() are part of the inline asm register specification so that is fine.

Just don't assume that you can call *any* function from inside asm due to the above."

arch/x86/include/asm/page_64.h extern unsigned long __phys_addr_symbol(unsigned long); · line 74

Quoted 10 lines

+ *
+ * Does absolutely no exception handling.
+ */

arch/x86/include/asm/page_64.h +3 -1
+static inline void clear_page(void *addr)
 {
+	u64 len = PAGE_SIZE;
 	/*
 	 * Clean up KMSAN metadata for the page being cleared. The assembly call
-	 * below clobbers @page, so we perform unpoisoning before it.
+	 * below clobbers @addr, so we perform unpoisoning before it.

s/we //

Quoted 6 lines
arch/x86/include/asm/page_64.h +0 -5
 	 */
-	kmsan_unpoison_memory(page, PAGE_SIZE);
-	alternative_call_2(clear_page_orig,
-			   clear_page_rep, X86_FEATURE_REP_GOOD,
-			   clear_page_erms, X86_FEATURE_ERMS,
-			   "=D" (page),

arch/x86/include/asm/page_64.h extern unsigned long __phys_addr_symbol(unsigned long); · line 83

Quoted 10 lines
arch/x86/include/asm/page_64.h +7 -2
-			   "D" (page),
-			   "cc", "memory", "rax", "rcx");
+	kmsan_unpoison_memory(addr, len);
+	asm volatile(ALTERNATIVE_2("call __clear_pages_unrolled",
+				   "shrq $3, %%rcx; rep stosq", X86_FEATURE_REP_GOOD,
+				   "rep stosb", X86_FEATURE_ERMS)
+			: "+c" (len), "+D" (addr), ASM_CALL_CONSTRAINT
+			: "a" (0)
+			: "cc", "memory");
 }

With that:

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#18 lore raw

Re: [PATCH v9 4/7] x86/mm: Simplify clear_page_*

Quoted 24 lines · Borislav Petkov
Quoted 12 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:
+ *
+ * - __clear_pages_unrolled(): the oldest, slowest and universally
+ * supported method. Zeroes via 8-byte MOV instructions unrolled 8x
+ * to write a 64-byte cacheline in each loop iteration.
+ *
+ * - "REP; STOSQ": really old CPUs had crummy REP implementations.

Quoted 8 lines
+ *    Vendor CPU setup code sets 'REP_GOOD' on CPUs where REP can be
+ *    trusted. The instruction writes 8-byte per REP iteration but
+ *    CPUs can internally batch these together and do larger writes.
+ *
+ *  - "REP; STOSB": CPUs that enumerate 'ERMS' have an improved STOS
+ *    implementation that is less picky about alignment and where
+ *    STOSB (1-byte at a time) is actually faster than STOSQ (8-bytes
+ *    at a time.)

Please put here in BIG RED LETTERS something along the lines of:

Quoted 8 lines

"The inline asm has a CALL instruction and usually that is a no-no due to the compiler not knowing that there's a CALL inside the asm and thus won't track callee-clobbered registers but in this case, all the callee clobbereds by __clear_pages_unrolled() are part of the inline asm register specification so that is fine.

Just don't assume that you can call *any* function from inside asm due to the above."

Will add something clarifying this.

Quoted 32 lines

arch/x86/include/asm/page_64.h extern unsigned long __phys_addr_symbol(unsigned long); · line 74

Quoted 10 lines

+ *
+ * Does absolutely no exception handling.
+ */

arch/x86/include/asm/page_64.h +3 -1
+static inline void clear_page(void *addr)
 {
+	u64 len = PAGE_SIZE;
 	/*
 	 * Clean up KMSAN metadata for the page being cleared. The assembly call
-	 * below clobbers @page, so we perform unpoisoning before it.
+	 * below clobbers @addr, so we perform unpoisoning before it.

s/we //

Quoted 6 lines
arch/x86/include/asm/page_64.h +0 -5
 	 */
-	kmsan_unpoison_memory(page, PAGE_SIZE);
-	alternative_call_2(clear_page_orig,
-			   clear_page_rep, X86_FEATURE_REP_GOOD,
-			   clear_page_erms, X86_FEATURE_ERMS,
-			   "=D" (page),

arch/x86/include/asm/page_64.h extern unsigned long __phys_addr_symbol(unsigned long); · line 83

Quoted 10 lines
arch/x86/include/asm/page_64.h +7 -2
-			   "D" (page),
-			   "cc", "memory", "rax", "rcx");
+	kmsan_unpoison_memory(addr, len);
+	asm volatile(ALTERNATIVE_2("call __clear_pages_unrolled",
+				   "shrq $3, %%rcx; rep stosq", X86_FEATURE_REP_GOOD,
+				   "rep stosb", X86_FEATURE_ERMS)
+			: "+c" (len), "+D" (addr), ASM_CALL_CONSTRAINT
+			: "a" (0)
+			: "cc", "memory");
 }

With that:

Quoted 1 line

Thanks!

--
ankur

#19 lore raw

Re: [PATCH v9 4/7] x86/mm: Simplify clear_page_*

In reply to Ankur Arora
Quoted 16 lines · Ankur Arora

+ * Switch between three implementations of page clearing based on CPU
+ * capabilities:
+ *
+ * - __clear_pages_unrolled(): the oldest, slowest and universally
+ * supported method. Zeroes via 8-byte MOV instructions unrolled 8x
+ * to write a 64-byte cacheline in each loop iteration.
+ *
+ * - "REP; STOSQ": really old CPUs had crummy REP implementations.

+ *    Vendor CPU setup code sets 'REP_GOOD' on CPUs where REP can be
+ *    trusted. The instruction writes 8-byte per REP iteration but
+ *    CPUs can internally batch these together and do larger writes.
+ *
+ *  - "REP; STOSB": CPUs that enumerate 'ERMS' have an improved STOS
+ *    implementation that is less picky about alignment and where
+ *    STOSB (1-byte at a time) is actually faster than STOSQ (8-bytes
+ *    at a time.)
Quoted 1 line

+ *

I think this is somewhat odd commentary in this context.

Note about "crummy REP implementations" should be in description of __clear_pages_unrolled as it justifies its existence (I think the routine would be best whacked btw, but I'm not going to argue about it in this thread).
Description of STOSQ notes the CPU can do more than 8 bytes at a time, while description of STOSB claim does not make such a clarification. At the same time the note about less picky about alignment makes no significance in the context of page clearing as they are, well, page aligned.

There is a fucky real-world problem with ERMS worth noting: there are hypervisor setups out there which *hide* the bit by default (no really, see Proxmox for example -- you get a bare bones pre-ERMS cpuid)

With all this in mind, modulo poor grammar on my end, I would suggest something like this:

<quote>
There are 3 variants implemented:
- REP; STOSB: used if the CPU supports "Enhanced REP MOVSB/STOSB" (aka ERMS), which is true for majority of microarchitectures today
- REP; STOSQ: fallback if the ERMS bit is not present
- __clear_pages_unrolled: code for CPUs which are determined to have poor REP support, only concerns long obsolete uarchs.

Warnings: some hypervisors are configured to expose a very limited set of capabilites in the guest, fitering out ERMS even if present. As such the STOSQ variant is still in active use on some setups even when hardware does not need it.
</quote>

#20 lore raw

Re: [PATCH v9 4/7] x86/mm: Simplify clear_page_*

Quoted 21 lines · Mateusz Guzik
Quoted 16 lines · Ankur Arora

+ * Switch between three implementations of page clearing based on CPU
+ * capabilities:
+ *
+ * - __clear_pages_unrolled(): the oldest, slowest and universally
+ * supported method. Zeroes via 8-byte MOV instructions unrolled 8x
+ * to write a 64-byte cacheline in each loop iteration.
+ *
+ * - "REP; STOSQ": really old CPUs had crummy REP implementations.

+ *    Vendor CPU setup code sets 'REP_GOOD' on CPUs where REP can be
+ *    trusted. The instruction writes 8-byte per REP iteration but
+ *    CPUs can internally batch these together and do larger writes.
+ *
+ *  - "REP; STOSB": CPUs that enumerate 'ERMS' have an improved STOS
+ *    implementation that is less picky about alignment and where
+ *    STOSB (1-byte at a time) is actually faster than STOSQ (8-bytes
+ *    at a time.)
Quoted 1 line

+ *

I think this is somewhat odd commentary in this context.

Quoted 9 lines

Note about "crummy REP implementations" should be in description of __clear_pages_unrolled as it justifies its existence (I think the routine would be best whacked btw, but I'm not going to argue about it in this thread).
Description of STOSQ notes the CPU can do more than 8 bytes at a time, while description of STOSB claim does not make such a clarification. At the same time the note about less picky about alignment makes no significance in the context of page clearing as they are, well, page aligned.

Good point. I'll rework the comment a little bit to align things better (maybe reusing some of what you suggest below).

Quoted 16 lines

There is a fucky real-world problem with ERMS worth noting: there are hypervisor setups out there which *hide* the bit by default (no really, see Proxmox for example -- you get a bare bones pre-ERMS cpuid)

With all this in mind, modulo poor grammar on my end, I would suggest something like this:

<quote>
There are 3 variants implemented:
- REP; STOSB: used if the CPU supports "Enhanced REP MOVSB/STOSB" (aka ERMS), which is true for majority of microarchitectures today
- REP; STOSQ: fallback if the ERMS bit is not present
- __clear_pages_unrolled: code for CPUs which are determined to have poor REP support, only concerns long obsolete uarchs.

Quoted 5 lines

Warnings: some hypervisors are configured to expose a very limited set of capabilites in the guest, fitering out ERMS even if present. As such the STOSQ variant is still in active use on some setups even when hardware does not need it.
</quote>

The last bit is useful context though maybe some of it fits better in the commit message.

Thanks
ankur

#21 lore raw

PATCH v9 5/7 x86/clear_page: Introduce clear_pages()

In reply to Ankur Arora

Performance 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 from 4KB to 1GB, we see the performance go from 13.7 GB/sec to 38.1 GB/sec. For the 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.

---

Notes:
- get rid of ARCH_PAGE_CONTIG_NR. Subsequent patches add this constant (which was relevant for cooperative preemption models) to common code.

 arch/x86/include/asm/page_64.h | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
arch/x86/include/asm/page_64.h +12 -5
index 6157bf46590e..043e57c4ad12 100644
@@ -44,8 +44,9 @@ void __clear_pages_unrolled(void *page);
 KCFI_REFERENCE(__clear_pages_unrolled);
 
 /**
- * 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:
@@ -66,11 +67,11 @@ KCFI_REFERENCE(__clear_pages_unrolled);
  *
  * 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);
@@ -81,6 +82,12 @@ static inline void clear_page(void *addr)
 			: "a" (0)
 			: "cc", "memory");
 }
+#define clear_pages clear_pages
+
+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.31.1
#22 lore raw

PATCH v9 6/7 mm, folio_zero_user: support clearing page ranges

In reply to Ankur Arora

Clear contiguous page ranges in folio_zero_user() instead of clearing a single page at a time. Exposing larger ranges enables extent based processor optimizations.

However, because the underlying clearing primitives do not, or might not be able to check to call cond_resched() to check if preemption is required, limit the worst case preemption latency by doing the clearing in no more than PROCESS_PAGES_NON_PREEMPT_BATCH units.

For architectures that define clear_pages(), we assume that the clearing is fast and define PROCESS_PAGES_NON_PREEMPT_BATCH as 8MB worth of pages. This should be large enough to allow the processor to optimize the operation and yet small enough that we see reasonable preemption latency for when this optimization is not possible (ex. slow microarchitectures, memory bandwidth saturation.)

Architectures that don't define clear_pages() will continue to use the base value (single page). And, preemptible models don't need invocations of cond_resched() so don't care about the batch size.

The resultant performance depends on the kinds of optimizations available to the CPU for the region size being cleared. Two classes of optimizations:

- clearing iteration costs are 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 mmap -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

[#] Notice that we perform much better with preempt=full|lazy. As
mentioned above, preemptible models not needing explicit invocations of cond_resched() allows us to clear the full extent (1GB) in a single unit.
In comparison the maximum extent used for preempt=none|voluntary is PROCESS_PAGES_NON_PREEMPT_BATCH (8MB).

The larger extent allows the processor to elide cacheline allocation (on Milan the threshold is LLC-size=32MB.)

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%).

---

Notes:
- Define PROCESS_PAGES_NON_PREEMPT_BATCH in common code (instead of inheriting ARCH_PAGE_CONTIG_NR.)
- Also document this in much greater detail as clearing pages needing a a constant dependent on the preemption model is facially quite odd.
- Minor loop reorganization in clear_contig_highpages().

 include/linux/mm.h | 35 +++++++++++++++++++++++++++++++++++
 mm/memory.c        | 46 +++++++++++++++++++++++++---------------------
 2 files changed, 60 insertions(+), 21 deletions(-)
include/linux/mm.h +35 -0
index c397ee2f6dd5..ac20d2022cdf 100644
@@ -3889,6 +3889,15 @@ static inline void clear_page_guard(struct zone *zone, struct page *page,
  * mapped to user space.
  *
  * Does absolutely no exception handling.
+ *
+ * Note that even though the clearing operation is preemptible, clear_pages()
+ * does not (and on architectures where it reduces to a few long-running
+ * but preemptible instructions, might not be able to) call cond_resched()
+ * to check if rescheduling is required.
+ *
+ * Running under preemptible models this is not a problem. Under cooperatively
+ * scheduled preemption models, however, the caller is expected to limit
+ * @npages to no more than PROCESS_PAGES_NON_PREEMPT_BATCH.
  */
 static inline void clear_pages(void *addr, unsigned int npages)
 {
@@ -3899,6 +3908,32 @@ static inline void clear_pages(void *addr, unsigned int npages)
 }
 #endif
 
+#ifndef PROCESS_PAGES_NON_PREEMPT_BATCH
+#ifdef clear_pages
+/*
+ * The architecture defines clear_pages(), and we assume that it is
+ * generally "fast". So choose a batch size large enough to allow the processor
+ * headroom for optimizing the operation and yet small enough that we see
+ * reasonable preemption latency for when this optimization is not possible
+ * (ex. slow microarchitectures, memory bandwidth saturation.)
+ *
+ * With a value of 8MB and assuming a memory bandwidth of ~10GBps, this should
+ * result in worst case preemption latency of around 1ms when clearing pages.
+ *
+ * (See comment above clear_pages() for why preemption latency is a concern
+ * here.)
+ */
+#define PROCESS_PAGES_NON_PREEMPT_BATCH		(8 << (20 - PAGE_SHIFT))
+#else /* !clear_pages */
+/*
+ * The architecture does not provide a clear_pages() implementation. Assume
+ * that clear_page() -- which clear_pages() will fallback to -- is relatively
+ * slow and choose a small value for PROCESS_PAGES_NON_PREEMPT_BATCH.
+ */
+#define PROCESS_PAGES_NON_PREEMPT_BATCH		1
+#endif
+#endif
+
 #ifndef clear_user_page
 /**
  * clear_user_page() - clear a page to be mapped to user space
mm/memory.c +25 -21

65 lines not on this page · raw message

Signature · 1 line
2.31.1
#23 lore raw

PATCH v9 7/7 mm: folio_zero_user: cache neighbouring pages

In reply to Ankur Arora

folio_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 likely an 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
index 5e78af316647..05bc9fc7289d 100644
@@ -7193,13 +7193,53 @@ static void clear_contig_highpages(struct page *page, unsigned long addr,
  * @addr_hint: The address accessed by the user or the base address.
  *
  * Uses architectural support to clear page ranges.
+ *
+ * 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 is cleared last in order to keep cache
+ * lines of the faulting 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.31.1