Skip to contentSkip to conversation

mm/vmalloc: Speed up ioremap, vmalloc and vmap with contiguous memory

PATCH Superseded Wen Jiang 3 participants 28 messages Sep 17 – Sep 18

Subsystems (5)

v8 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Wen Jiang

author
#0 lore raw

v8 cover mm/vmalloc: Speed up ioremap, vmalloc and vmap with contiguous memory

From: Wen Jiang <[email protected]>

This patchset accelerates ioremap, vmalloc, and vmap when the memory is physically fully or partially contiguous. Two techniques are used:

1. Avoid page table rewalk when setting PTEs/PMDs for multiple memory segments
2. Use batched mappings wherever possible in both vmalloc and ARM64 layers

Besides accelerating the mapping path, this also enables large mappings (PMD and cont-PTE) for vmap, which are currently not supported.

Patches 1-4 decouple the PTE-level block mapping path from HugeTLB. Previously vmap_pte_range() installed cont-PTE mappings by reusing set_huge_pte_at() and huge_ptep_get_and_clear(), which are HugeTLB helpers gated by CONFIG_HUGETLB_PAGE. This made the feature silently unavailable on CONFIG_HUGETLB_PAGE=n kernels, and coupled mm/vmalloc.c to HugeTLB internals. Patches 1 and 2 add the arm64 and powerpc/8xx implementations of pte_set_huge()/pte_clear_huge(), which join the existing pmd/pud_set_huge() family. Patch 3 adds the generic fallbacks and converts mm/vmalloc.c over. Patch 4 then removes the now-dead init_mm special case from arm64's clear_flush().

Patch 5 extends ARM64 arch_vmap_pte_range_map_size() to batch multiple CONT_PTE blocks in one call instead of one at a time.

Patch 6 extracts a common helper vmap_set_ptes() that consolidates PTE mapping logic for the ioremap and vmalloc/vmap paths, handling both CONT_PTE and regular PTE mappings. This prepares for the next patch.

Patch 7 extends the page table walk path to support page shifts other than PAGE_SHIFT and eliminates the page table rewalk for huge vmalloc mappings. The function is renamed from vmap_small_pages_range_noflush() to vmap_pages_range_noflush_walk().

Patch 8 extracts vm_shift() to consolidate vmalloc mapping shift selection for reuse in the batching path.

Patches 9-10 add huge vmap support for contiguous pages, including support for non-compound pages with pfn alignment verification.

On the RK3588 8-core ARM64 SoC, with tasks pinned to a little core and the performance CPUfreq policy enabled, benchmark results:

* ioremap(1 MB): 1.35x faster (3407 ns -> 2526 ns)
* vmalloc(1 MB) mapping time (excluding allocation) with
VM_ALLOW_HUGE_VMAP: 1.42x faster (5.00 us -> 3.53 us)
* vmap(100MB) with order-8 pages: 8.3x faster (1235 us -> 149 us)

Many thanks to Xueyuan Chen for his testing efforts on RK3588 boards.

Large vmap() mappings were also tested by Leo Yan with ARM trace buffer units, including TRBE and SPE. These units use the CPU page tables for address translation when writing trace data to DRAM, so using larger vmap() mapping granules can reduce TLB pressure on the trace writer.

The TRBE test used a 1G CoreSight ETM AUX buffer. Across five runs on an isolated CPU, the average results were:

* dtlb_walk: 68.4 -> 59.4 (-13.16%)
* l1d_tlb_refill: 155.8 -> 119.6 (-23.23%)
* l2d_tlb_refill: 161435.8 -> 495.0 (-99.69%)

The SPE test used a 512M ARM SPE AUX buffer. Across five runs on an isolated CPU, the average results were:

* dtlb_walk: 1710.4 -> 1315.6 (-23.08%)
* l1d_tlb_refill: 16000.0 -> 15950.2 (-0.31%)
* l2d_tlb_refill: 4796.0 -> 2931.2 (-38.88%)

These results show that enabling larger vmap() mappings can materially reduce page table walks and TLB refills for large trace buffers.

Many thanks to Leo Yan for his testing efforts on ARM trace buffers.

Changes since v7:
- v7's patch 1 (which extended the hugetlb helpers in arch/arm64/mm/hugetlbpage.c) is replaced by pte_set_huge()/ pte_clear_huge(), split across patches 1-3 so that the arm64, powerpc/8xx and generic changes can be reviewed and acked
independently: patch 1 is arm64 only, patch 2 is powerpc/8xx only, patch 3 is the generic fallbacks plus the mm/vmalloc.c conversion. hugetlbpage.c no longer gains the CONT_PTE batching hooks, and patch 4 additionally removes its now-dead init_mm special case. mm/vmalloc.c no longer includes <linux/hugetlb.h>.
- The generic pte_set_huge()/pte_clear_huge() fallbacks WARN_ON_ONCE() instead of silently doing nothing. They only exist to keep the build working on architectures without PTE-level block mappings, where they are unreachable (patch 3).
- Patch 5 (v7 patch 2): use round_down(size, CONT_PTE_SIZE) instead of rounddown_pow_of_two(size), since pte_set_huge() takes the size directly without an ilog2() roundtrip. This lets a single call span several CONT_PTE blocks: a 768K (512K + 256K) ioremap is now 1.25x faster than v7.
- Patch 6 (v7 patch 3): vmap_set_ptes() now calls pte_set_huge() instead of the hugetlb path.
- Patch 9 (v7 patch 6): get_vmap_batch_order() takes pages+i instead of a separate idx argument, and applies pfn alignment limit to scan length rather than to the resulting order. Renamed idx/map_addr to batch_idx/batch_start. Dropped Dev's Reviewed-by.

Changes since v6:
- Add a clarifying comment about the reuse of hugetlb helpers by non-hugetlbfs(vmalloc) mm code (patch 1)
- Expand the arm64/vmalloc commit message and comment to clarify that multi-CONT_PTE_SIZE values are vmalloc mapping spans, not HugeTLB hstate sizes (patch 2)
- Move the local steps variable change in vmap_pte_range() into the vmap_set_ptes() extraction patch (patch 3)
- Propagate vmap_pages_pte_range() errors through the upper vmap_pages_*() levels instead of returning -ENOMEM for all failures (patch 4)
- Add a preparatory vm_shift() helper patch before the batching patch (patch 5)
- Guard the PFN alignment clamp in get_vmap_batch_order() against PFN 0 before calling __ffs() (patch 6)
- Fix kmsan_vmap_pages_range_noflush() indentation in the batching path (patch 6)

Changes since v5:
- No code changes.
- Pick up Reviewed-by and Tested-by tags from Dev, Leo and Uladzislau. Many thanks!
- Add TRBE/SPE large vmap() test results from Leo Yan to the cover letter.

Changes since v4:
- Move pgsize update before contig_ptes check (patch 1)
- Use rounddown_pow_of_two instead of __fls in arch_vmap_pte_range_map_size (patch 2)
- Reword comment to avoid mentioning cont_pte and remove if in vmap_set_ptes (patch 3)
- Rename vmap_batched() to vmap_pages_range_batched() (patch 5)
- Use batch_end as the batching cursor to avoid an unused start variable (patch 5)
- Check arch_vmap_pmd_supported before PMD mapping (patch 6)

Changes since v3:
- Squash vmap_pte_range() loop variable fix into patch 4 (patch 3, 4)
- Use shift >= PMD_SHIFT and fix *nr increment in vmap_pages_pmd_range() (patch 4)
- Pass page_shift directly without capping at PMD_SHIFT (patch 4, 5)
- Add vm_shift() helper and pass pgprot_t to get_vmap_batch_order() (patch 5)
- Use min(order, __ffs(pfn)) for graceful pfn alignment degradation, replacing IS_ALIGNED check (patch 5)
- Remove irrelevant ioremap_max_page_shift early-exit (patch 5)
- Add __get_vm_area_node_aligned_caller() wrapper, rename to vmap_get_aligned_vm_area() (patch 6)

Changes since v2:
- Use __fls instead of fls in arch_vmap_pte_range_map_size (patch 2)
- Add WARN_ON checks in vmap_pages_pmd_range (patch 4)
- Fix flush_cache_vmap to use saved start address instead of the already-advanced addr (patch 5)
- Rename __vmap_huge() to vmap_batched() (patch 5)
- Add caller parameter and unroll while(1) loop (patch 5)
- Squash patch 7 into patch 5 (stop scanning for compound pages after encountering small pages)

Changes since v1:
- Fix condition order and use PMD_SIZE instead of CONT_PMD_SIZE in patch 1 (Dev Jain)
- Squash patch 3+4 and patch 5+7 (Dev Jain)
- Replace "zigzag" with "page table rewalk" in commit messages (Dev Jain)
- Rename vmap_small_pages_range_noflush() to vmap_pages_range_noflush_walk() (Dev Jain)
- Extract vmap_set_ptes() as a new patch to consolidate PTE mapping logic between vmap_pte_range() and vmap_pages_pte_range(), handling both CONT_PTE and regular mappings (Mike Rapoport)
- Support non-compound pages in get_vmap_batch_order() by falling back to physical contiguity scanning with pfn alignment check (Dev Jain, Uladzislau Rezki)
- In get_vmap_batch_order(), filter out orders that the architecture cannot batch by checking arch_vmap_pte_supported_shift() directly. This avoids overhead for orders 1-3 on ARM64 CONT_PTE with 4K pages. (patch 5)

Barry Song (Xiaomi) (4):
arm64/vmalloc: allow arch_vmap_pte_range_map_size() to batch multiple
CONT_PTE
mm/vmalloc: extend page table walk to support larger page_shift sizes
and eliminate page table rewalk
mm/vmalloc: map contiguous pages in batches for vmap() if possible mm/vmalloc: align vm_area so vmap() can batch mappings

Wen Jiang (6):
arm64/mm: add pte_set_huge() and pte_clear_huge() powerpc/8xx: add pte_set_huge()
mm/vmalloc: use pte_set_huge()/pte_clear_huge() for PTE-level block
mappings
arm64/hugetlb: drop the init_mm special case in clear_flush() mm/vmalloc: extract vmap_set_ptes() to consolidate PTE mapping logic mm/vmalloc: extract vm_shift() to consolidate mapping shift selection

 arch/arm64/include/asm/pgtable.h             |   6 +
 arch/arm64/include/asm/vmalloc.h             |   8 +-
 arch/arm64/mm/hugetlbpage.c                  |   5 +-
 arch/arm64/mm/mmu.c                          |  19 ++
 arch/powerpc/include/asm/nohash/32/pte-8xx.h |   4 +
 arch/powerpc/mm/nohash/8xx.c                 |  29 ++
 include/linux/pgtable.h                      |  29 ++
 mm/vmalloc.c                                 | 268 ++++++++++++++-----
 8 files changed, 300 insertions(+), 68 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v8 1/10 arm64/mm: add pte_set_huge() and pte_clear_huge()

From: Wen Jiang <[email protected]>

vmap installs PTE-level block mappings (PTE_CONT on arm64) by reusing set_huge_pte_at() and huge_ptep_get_and_clear(), which are HugeTLB helpers gated by CONFIG_HUGETLB_PAGE. This makes the feature silently unavailable on CONFIG_HUGETLB_PAGE=n kernels and couples mm/vmalloc.c to HugeTLB internals it does not otherwise need.

Add pte_set_huge()/pte_clear_huge() to arm64, joining the existing pmd_set_huge()/pud_set_huge() family in mm/mmu.c. They build on __set_ptes() and __get_and_clear_full_ptes() directly, so they do not depend on CONFIG_HUGETLB_PAGE.

There is no caller yet: mm/vmalloc.c is converted later in this series, once the generic fallbacks are in place.

---
 arch/arm64/include/asm/pgtable.h |  6 ++++++
 arch/arm64/mm/mmu.c              | 19 +++++++++++++++++++
 2 files changed, 25 insertions(+)
arch/arm64/include/asm/pgtable.h +6 -0
index 4dfa42b7d0535..6ee1522fac78f 100644
@@ -1949,6 +1949,12 @@ static inline void clear_young_dirty_ptes(struct vm_area_struct *vma,
 
 #endif /* CONFIG_ARM64_CONTPTE */
 
+#define __HAVE_ARCH_PTE_SET_HUGE
+void pte_set_huge(pte_t *ptep, unsigned long addr, phys_addr_t phys,
+		  pgprot_t prot, unsigned long size);
+#define __HAVE_ARCH_PTE_CLEAR_HUGE
+pte_t pte_clear_huge(pte_t *ptep, unsigned long addr, unsigned long size);
+
 #endif /* !__ASSEMBLER__ */
 
 #endif /* __ASM_PGTABLE_H */
arch/arm64/mm/mmu.c +19 -0
index 92fedf4db9278..93b602a6c68a6 100644
@@ -1872,6 +1872,25 @@ int pmd_clear_huge(pmd_t *pmdp)
 	return 1;
 }
 
+void pte_set_huge(pte_t *ptep, unsigned long addr, phys_addr_t phys,
+		  pgprot_t prot, unsigned long size)
+{
+	unsigned long pfn = __phys_to_pfn(phys);
+	pte_t new_pte = pte_mkcont(pfn_pte(pfn, prot));
+	unsigned int nr = size >> PAGE_SHIFT;
+
+	VM_WARN_ON(!IS_ALIGNED(pfn, CONT_PTES));
+	VM_WARN_ON(!IS_ALIGNED(size, CONT_PTE_SIZE));
+
+	__set_ptes(&init_mm, addr, ptep, new_pte, nr);
+}
+
+pte_t pte_clear_huge(pte_t *ptep, unsigned long addr, unsigned long size)
+{
+	return __get_and_clear_full_ptes(&init_mm, addr, ptep,
+					 size >> PAGE_SHIFT, 0);
+}
+
 static int __pmd_free_pte_page(pmd_t *pmdp, unsigned long addr,
 			       bool acquire_mmap_lock)
 {
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH v8 2/10 powerpc/8xx: add pte_set_huge()

In reply to Wen Jiang

From: Wen Jiang <[email protected]>

vmap installs PTE-level block mappings (SZ_16K/SZ_512K on powerpc/8xx) by reusing set_huge_pte_at(), a HugeTLB helper gated by CONFIG_HUGETLB_PAGE. This makes the feature silently unavailable on CONFIG_HUGETLB_PAGE=n kernels and couples mm/vmalloc.c to HugeTLB internals it does not otherwise need.

Add pte_set_huge() to powerpc/8xx, next to the existing pmd_clear_huge()/pud_clear_huge() in mm/nohash/8xx.c. It builds the huge PTE and writes it into the backing cells directly, without going through set_huge_pte_at(), so it does not depend on CONFIG_HUGETLB_PAGE.

No pte_clear_huge() is needed: 8xx does not implement arch_vmap_pte_range_unmap_size(), so the vmap unmap path never takes the block-mapping branch.

In practice PPC_8xx selects HUGETLBFS unconditionally, so CONFIG_HUGETLB_PAGE=n does not occur there today; this is a decoupling cleanup rather than a new configuration.

There is no caller yet: mm/vmalloc.c is converted later in this series, once the generic fallbacks are in place.

---
 arch/powerpc/include/asm/nohash/32/pte-8xx.h |  4 +++
 arch/powerpc/mm/nohash/8xx.c                 | 29 ++++++++++++++++++++
 2 files changed, 33 insertions(+)
arch/powerpc/include/asm/nohash/32/pte-8xx.h +4 -0
index e2ea8ba9f8cae..1ee3f8f0de974 100644
@@ -235,6 +235,10 @@ static inline pte_t ptep_get(pte_t *ptep)
 }
 #endif /* CONFIG_PPC_16K_PAGES */
 
+#define __HAVE_ARCH_PTE_SET_HUGE
+void pte_set_huge(pte_t *ptep, unsigned long addr, phys_addr_t phys,
+		  pgprot_t prot, unsigned long size);
+
 #endif
 
 #endif /* __KERNEL__ */
arch/powerpc/mm/nohash/8xx.c +29 -0
index a9d3f4729eada..2518d2a6bdb41 100644
@@ -220,3 +220,32 @@ int pmd_clear_huge(pmd_t *pmd)
 {
 	 return 0;
 }
+
+/*
+ * vmap PTE-level block mapping. Sets a present kernel mapping directly,
+ * so unlike set_huge_pte_at() it needs neither CONFIG_HUGETLB_PAGE nor
+ * set_pte_filter() (a no-op for non-exec kernel mappings).
+ */
+void pte_set_huge(pte_t *ptep, unsigned long addr, phys_addr_t phys,
+		  pgprot_t prot, unsigned long size)
+{
+	pmd_t *pmdp = pmd_off(&init_mm, addr);
+	pte_t pte = pfn_pte(PHYS_PFN(phys), prot);
+	pte_basic_t val;
+	pte_basic_t *entry = (pte_basic_t *)ptep;
+	int num, i;
+
+	pte = arch_make_huge_pte(pte, ilog2(size), 0);
+	val = pte_val(pte);
+
+	/*
+	 * Make sure hardware valid bit is not set. We don't do
+	 * tlb flush for this update.
+	 */
+	VM_WARN_ON(pte_hw_valid(*ptep) && !pte_protnone(*ptep));
+
+	num = number_of_cells_per_pte(pmdp, val, 1);
+
+	for (i = 0; i < num; i++, entry++, val += SZ_4K)
+		*entry = val;
+}
Signature · 1 line
2.34.1
author
#10 lore raw

PATCH v8 3/10 mm/vmalloc: use pte_set_huge()/pte_clear_huge() for PTE-level block mappings

In reply to Wen Jiang

From: Wen Jiang <[email protected]>

vmap installs PTE-level block mappings by reusing set_huge_pte_at() and huge_ptep_get_and_clear() under #ifdef CONFIG_HUGETLB_PAGE. This makes the feature silently unavailable on CONFIG_HUGETLB_PAGE=n kernels and couples mm/vmalloc.c to HugeTLB internals it does not otherwise need.

Now that arm64 and powerpc/8xx provide pte_set_huge() and pte_clear_huge(), add the generic fallbacks next to the existing pmd/pud_set_huge() family and convert vmap_pte_range() and vunmap_pte_range() to the new helpers. The CONFIG_HUGETLB_PAGE guards around the block-mapping paths are dropped, so PTE-level block mappings now also work on CONFIG_HUGETLB_PAGE=n kernels, and mm/vmalloc.c no longer includes <linux/hugetlb.h>.

The fallbacks exist only to keep the build working on architectures without PTE-level block mapping support. They are unreachable there:
the callers only run when arch_vmap_pte_range_map_size() or arch_vmap_pte_range_unmap_size() return a size other than PAGE_SIZE, which requires an arch implementation. WARN_ON_ONCE() makes that explicit rather than silently doing nothing.

---
 include/linux/pgtable.h | 29 +++++++++++++++++++++++++++++
 mm/vmalloc.c            | 19 ++++++-------------
 2 files changed, 35 insertions(+), 13 deletions(-)
include/linux/pgtable.h +29 -0
index cdd68ed3ae1a9..349ced999f959 100644
@@ -2134,6 +2134,35 @@ static inline int pmd_free_pte_page(pmd_t *pmd, unsigned long addr)
 }
 #endif	/* CONFIG_HAVE_ARCH_HUGE_VMAP */
 
+/*
+ * PTE-level block mappings for vmap.
+ *
+ * pte_set_huge() only has to be implemented by architectures whose
+ * arch_vmap_pte_range_map_size() can return a size other than PAGE_SIZE.
+ */
+#ifndef __HAVE_ARCH_PTE_SET_HUGE
+static inline void pte_set_huge(pte_t *ptep, unsigned long addr,
+				phys_addr_t phys, pgprot_t prot,
+				unsigned long size)
+{
+	WARN_ON_ONCE(1);
+}
+#endif
+
+/*
+ * Likewise, pte_clear_huge() only has to be implemented by architectures
+ * whose arch_vmap_pte_range_unmap_size() can return a size other than
+ * PAGE_SIZE.
+ */
+#ifndef __HAVE_ARCH_PTE_CLEAR_HUGE
+static inline pte_t pte_clear_huge(pte_t *ptep, unsigned long addr,
+				   unsigned long size)
+{
+	WARN_ON_ONCE(1);
+	return __pte(0);
+}
+#endif
+
 #ifndef __HAVE_ARCH_FLUSH_PMD_TLB_RANGE
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 /*
mm/vmalloc.c +6 -13
index bb6ae08d18f58..025b3a848d564 100644
@@ -38,7 +38,6 @@
 #include <linux/rbtree_augmented.h>
 #include <linux/overflow.h>
 #include <linux/pgtable.h>
-#include <linux/hugetlb.h>
 #include <linux/sched/mm.h>
 #include <asm/tlbflush.h>
 #include <asm/shmparam.h>
@@ -98,7 +97,7 @@ static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
 	pte_t *pte;
 	u64 pfn;
 	struct page *page;
-	unsigned long size = PAGE_SIZE;
+	unsigned long size;
 
 	if (WARN_ON_ONCE(!PAGE_ALIGNED(end - addr)))
 		return -EINVAL;
@@ -119,17 +118,12 @@ static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
 			BUG();
 		}
 
-#ifdef CONFIG_HUGETLB_PAGE
 		size = arch_vmap_pte_range_map_size(addr, end, pfn, max_page_shift);
 		if (size != PAGE_SIZE) {
-			pte_t entry = pfn_pte(pfn, prot);
-
-			entry = arch_make_huge_pte(entry, ilog2(size), 0);
-			set_huge_pte_at(&init_mm, addr, pte, entry, size);
+			pte_set_huge(pte, addr, PFN_PHYS(pfn), prot, size);
 			pfn += PFN_DOWN(size);
 			continue;
 		}
-#endif
 		set_pte_at(&init_mm, addr, pte, pfn_pte(pfn, prot));
 		pfn++;
 	} while (pte += PFN_DOWN(size), addr += size, addr != end);
@@ -368,25 +362,24 @@ static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
 {
 	pte_t *pte;
 	pte_t ptent;
-	unsigned long size = PAGE_SIZE;
+	unsigned long size;
 
 	pte = pte_offset_kernel(pmd, addr);
 	lazy_mmu_mode_enable();
 
 	do {
-#ifdef CONFIG_HUGETLB_PAGE
 		size = arch_vmap_pte_range_unmap_size(addr, pte);
 		if (size != PAGE_SIZE) {
 			if (WARN_ON(!IS_ALIGNED(addr, size))) {
 				addr = ALIGN_DOWN(addr, size);
 				pte = PTR_ALIGN_DOWN(pte, sizeof(*pte) * (size >> PAGE_SHIFT));
 			}
-			ptent = huge_ptep_get_and_clear(&init_mm, addr, pte, size);
+			ptent = pte_clear_huge(pte, addr, size);
 			if (WARN_ON(end - addr < size))
 				size = end - addr;
-		} else
-#endif
+		} else {
 			ptent = ptep_get_and_clear(&init_mm, addr, pte);
+		}
 		WARN_ON(!pte_none(ptent) && !pte_present(ptent));
 	} while (pte += (size >> PAGE_SHIFT), addr += size, addr != end);
 
Signature · 1 line
2.34.1
author
#18 lore raw

PATCH v8 4/10 arm64/hugetlb: drop the init_mm special case in clear_flush()

In reply to Wen Jiang

From: Wen Jiang <[email protected]>

clear_flush() calls flush_tlb_kernel_range() when mm is &init_mm. This was added by commit 06fc959fcff7 ("arm64/mm: Support huge pte-mapped pages in vmap") because vmap called set_huge_pte_at(&init_mm, ...).

Now that vmap uses pte_set_huge()/pte_clear_huge(), mm/vmalloc.c is no longer a caller of set_huge_pte_at() on arm64. The only remaining callers passing &init_mm are in arch/powerpc, which uses its own implementation. Remove the dead branch.

No functional change.

---
 arch/arm64/mm/hugetlbpage.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
arch/arm64/mm/hugetlbpage.c +1 -4
index 8e799c1fe0aa6..ce247b06fe03d 100644
@@ -206,10 +206,7 @@ static void clear_flush(struct mm_struct *mm,
 	for (i = 0; i < ncontig; i++, addr += pgsize, ptep++)
 		__ptep_get_and_clear_anysz(mm, addr, ptep, pgsize);
 
-	if (mm == &init_mm)
-		flush_tlb_kernel_range(saddr, addr);
-	else
-		__flush_hugetlb_tlb_range(&vma, saddr, addr, pgsize, TLBF_NOWALKCACHE);
+	__flush_hugetlb_tlb_range(&vma, saddr, addr, pgsize, TLBF_NOWALKCACHE);
 }
 
 void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
Signature · 1 line
2.34.1
author
#19 lore raw

PATCH v8 5/10 arm64/vmalloc: allow arch_vmap_pte_range_map_size() to batch multiple CONT_PTE

In reply to Wen Jiang

From: "Barry Song (Xiaomi)" <[email protected]>

Allow arch_vmap_pte_range_map_size to batch across multiple CONT_PTE blocks, reducing both PTE setup and TLB flush iterations.

For CONT_PTE_SIZE-aligned ranges, return a mapping size that may cover multiple CONT_PTE blocks, capped below PMD_SIZE. These sizes are vmalloc mapping spans, not HugeTLB hstate sizes.

---
 arch/arm64/include/asm/vmalloc.h | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
arch/arm64/include/asm/vmalloc.h +7 -1
index 4ec1acd3c1b34..4053b1ec1902e 100644
@@ -23,10 +23,14 @@ static inline unsigned long arch_vmap_pte_range_map_size(unsigned long addr,
 						unsigned long end, u64 pfn,
 						unsigned int max_page_shift)
 {
+	unsigned long size;
+
 	/*
 	 * If the block is at least CONT_PTE_SIZE in size, and is naturally
 	 * aligned in both virtual and physical space, then we can pte-map the
 	 * block using the PTE_CONT bit for more efficient use of the TLB.
+	 * The returned mapping size may cover multiple CONT_PTE_SIZE blocks,
+	 * capped below PMD_SIZE.
 	 */
 	if (max_page_shift < CONT_PTE_SHIFT)
 		return PAGE_SIZE;
@@ -40,7 +44,9 @@ static inline unsigned long arch_vmap_pte_range_map_size(unsigned long addr,
 	if (!IS_ALIGNED(PFN_PHYS(pfn), CONT_PTE_SIZE))
 		return PAGE_SIZE;
 
-	return CONT_PTE_SIZE;
+	size = min3(end - addr, 1UL << max_page_shift, PMD_SIZE >> 1);
+	size = round_down(size, CONT_PTE_SIZE);
+	return size;
 }
 
 #define arch_vmap_pte_range_unmap_size arch_vmap_pte_range_unmap_size
Signature · 1 line
2.34.1
author
#20 lore raw

PATCH v8 6/10 mm/vmalloc: extract vmap_set_ptes() to consolidate PTE mapping logic

In reply to Wen Jiang

From: Wen Jiang <[email protected]>

Extract the common PTE mapping logic from vmap_pte_range() into a shared helper vmap_set_ptes(). This handles both CONT_PTE and regular PTE mappings in a single function, preparing for the next patch which will extend vmap_pages_pte_range() to also use this helper.

No functional change.

---
 mm/vmalloc.c | 36 +++++++++++++++++++++++++++---------
 1 file changed, 27 insertions(+), 9 deletions(-)
mm/vmalloc.c +27 -9
index 025b3a848d564..a0508bdb7d36f 100644
@@ -90,6 +90,29 @@ struct vfree_deferred {
 static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred);
 
 /*** Page table manipulation functions ***/
+
+/*
+ * Try contiguous mappings at the PTE level for arches which support them, and if
+ * requested by the caller. Fall back to PAGE_SIZE mappings otherwise.
+ *
+ * Return: mapping size.
+ */
+static __always_inline unsigned long vmap_set_ptes(pte_t *ptep,
+		unsigned long addr, unsigned long end, u64 pfn,
+		pgprot_t prot, unsigned int max_page_shift)
+{
+	unsigned long size;
+
+	size = arch_vmap_pte_range_map_size(addr, end, pfn, max_page_shift);
+	if (size != PAGE_SIZE) {
+		pte_set_huge(ptep, addr, PFN_PHYS(pfn), prot, size);
+		return size;
+	}
+
+	set_pte_at(&init_mm, addr, ptep, pfn_pte(pfn, prot));
+	return PAGE_SIZE;
+}
+
 static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
 			phys_addr_t phys_addr, pgprot_t prot,
 			unsigned int max_page_shift, pgtbl_mod_mask *mask)
@@ -98,6 +121,7 @@ static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
 	u64 pfn;
 	struct page *page;
 	unsigned long size;
+	unsigned int steps;
 
 	if (WARN_ON_ONCE(!PAGE_ALIGNED(end - addr)))
 		return -EINVAL;
@@ -118,15 +142,9 @@ static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
 			BUG();
 		}
 
-		size = arch_vmap_pte_range_map_size(addr, end, pfn, max_page_shift);
-		if (size != PAGE_SIZE) {
-			pte_set_huge(pte, addr, PFN_PHYS(pfn), prot, size);
-			pfn += PFN_DOWN(size);
-			continue;
-		}
-		set_pte_at(&init_mm, addr, pte, pfn_pte(pfn, prot));
-		pfn++;
-	} while (pte += PFN_DOWN(size), addr += size, addr != end);
+		size = vmap_set_ptes(pte, addr, end, pfn, prot, max_page_shift);
+		steps = PFN_DOWN(size);
+	} while (pte += steps, pfn += steps, addr += size, addr != end);
 
 	lazy_mmu_mode_disable();
 	*mask |= PGTBL_PTE_MODIFIED;
Signature · 1 line
2.34.1
author
#21 lore raw

PATCH v8 7/10 mm/vmalloc: extend page table walk to support larger page_shift sizes and eliminate page table rewalk

In reply to Wen Jiang

From: "Barry Song (Xiaomi)" <[email protected]>

vmap_pages_range_noflush_walk() (formerly vmap_small_pages_range_noflush()) provides a clean interface by taking struct page **pages and mapping them via direct PTE iteration. This avoids the page table rewalk seen when using vmap_range_noflush() for page_shift values other than PAGE_SHIFT.

Extend it to support larger page_shift values, and add PMD- and contiguous-PTE mappings as well.

Rename it to vmap_pages_range_noflush_walk() since it now handles more than just small pages.

For vmalloc() allocations with VM_ALLOW_HUGE_VMAP, we no longer need to iterate over pages one by one via vmap_range_noflush(), which would otherwise lead to page table rewalk. The code is now unified with the PAGE_SHIFT case by simply calling vmap_pages_range_noflush_walk().

---
 mm/vmalloc.c | 84 +++++++++++++++++++++++++++++++---------------------
 1 file changed, 50 insertions(+), 34 deletions(-)
mm/vmalloc.c +50 -34
index a0508bdb7d36f..cf1daf931a6f6 100644
@@ -535,8 +535,10 @@ void vunmap_range(unsigned long addr, unsigned long end)
 
 static int vmap_pages_pte_range(pmd_t *pmd, unsigned long addr,
 		unsigned long end, pgprot_t prot, struct page **pages, int *nr,
-		pgtbl_mod_mask *mask)
+		pgtbl_mod_mask *mask, unsigned int shift)
 {
+	unsigned long pfn, size;
+	unsigned int steps;
 	int err = 0;
 	pte_t *pte;
 
@@ -567,9 +569,10 @@ static int vmap_pages_pte_range(pmd_t *pmd, unsigned long addr,
 			break;
 		}
 
-		set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
-		(*nr)++;
-	} while (pte++, addr += PAGE_SIZE, addr != end);
+		pfn = page_to_pfn(page);
+		size = vmap_set_ptes(pte, addr, end, pfn, prot, shift);
+		steps = PFN_DOWN(size);
+	} while (pte += steps, *nr += steps, addr += size, addr != end);
 
 	lazy_mmu_mode_disable();
 	*mask |= PGTBL_PTE_MODIFIED;
@@ -579,60 +582,88 @@ static int vmap_pages_pte_range(pmd_t *pmd, unsigned long addr,
 
 static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr,
 		unsigned long end, pgprot_t prot, struct page **pages, int *nr,
-		pgtbl_mod_mask *mask)
+		pgtbl_mod_mask *mask, unsigned int shift)
 {
 	pmd_t *pmd;
 	unsigned long next;
+	int err;
 
 	pmd = pmd_alloc_track(&init_mm, pud, addr, mask);
 	if (!pmd)
 		return -ENOMEM;
 	do {
 		next = pmd_addr_end(addr, end);
-		if (vmap_pages_pte_range(pmd, addr, next, prot, pages, nr, mask))
-			return -ENOMEM;
+
+		if (shift >= PMD_SHIFT) {
+			struct page *page = pages[*nr];
+			phys_addr_t phys_addr;
+
+			if (WARN_ON(!page))
+				return -ENOMEM;
+			if (WARN_ON(!pfn_valid(page_to_pfn(page))))
+				return -EINVAL;
+
+			phys_addr = page_to_phys(page);
+
+			if (vmap_try_huge_pmd(pmd, addr, next, phys_addr, prot, shift)) {
+				*mask |= PGTBL_PMD_MODIFIED;
+				*nr += 1 << (PMD_SHIFT - PAGE_SHIFT);
+				continue;
+			}
+		}
+		err = vmap_pages_pte_range(pmd, addr, next, prot, pages, nr, mask, shift);
+		if (err)
+			return err;
 	} while (pmd++, addr = next, addr != end);
 	return 0;
 }
 
 static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,
 		unsigned long end, pgprot_t prot, struct page **pages, int *nr,
-		pgtbl_mod_mask *mask)
+		pgtbl_mod_mask *mask, unsigned int shift)
 {
 	pud_t *pud;
 	unsigned long next;
+	int err;
 
 	pud = pud_alloc_track(&init_mm, p4d, addr, mask);
 	if (!pud)
 		return -ENOMEM;
 	do {
 		next = pud_addr_end(addr, end);
-		if (vmap_pages_pmd_range(pud, addr, next, prot, pages, nr, mask))
-			return -ENOMEM;
+		err = vmap_pages_pmd_range(pud, addr, next, prot, pages, nr, mask, shift);
+		if (err)
+			return err;
 	} while (pud++, addr = next, addr != end);
 	return 0;
 }
 
 static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,
 		unsigned long end, pgprot_t prot, struct page **pages, int *nr,
-		pgtbl_mod_mask *mask)
+		pgtbl_mod_mask *mask, unsigned int shift)
 {
 	p4d_t *p4d;
 	unsigned long next;
+	int err;
 
 	p4d = p4d_alloc_track(&init_mm, pgd, addr, mask);
 	if (!p4d)
 		return -ENOMEM;
 	do {
 		next = p4d_addr_end(addr, end);
-		if (vmap_pages_pud_range(p4d, addr, next, prot, pages, nr, mask))
-			return -ENOMEM;
+		err = vmap_pages_pud_range(p4d, addr, next, prot, pages, nr, mask, shift);
+		if (err)
+			return err;
 	} while (p4d++, addr = next, addr != end);
 	return 0;
 }
 
-static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end,
-		pgprot_t prot, struct page **pages)
+/*
+ * It can take an array of pages which are not all contiguous, but it
+ * may have contiguous chunks, as hinted by @shift.
+ */
+static int vmap_pages_range_noflush_walk(unsigned long addr, unsigned long end,
+		pgprot_t prot, struct page **pages, unsigned int shift)
 {
 	unsigned long start = addr;
 	pgd_t *pgd;
@@ -647,7 +678,7 @@ static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end,
 		next = pgd_addr_end(addr, end);
 		if (pgd_bad(*pgd))
 			mask |= PGTBL_PGD_MODIFIED;
-		err = vmap_pages_p4d_range(pgd, addr, next, prot, pages, &nr, &mask);
+		err = vmap_pages_p4d_range(pgd, addr, next, prot, pages, &nr, &mask, shift);
 		if (err)
 			break;
 	} while (pgd++, addr = next, addr != end);
@@ -670,27 +701,12 @@ static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end,
 int __vmap_pages_range_noflush(unsigned long addr, unsigned long end,
 		pgprot_t prot, struct page **pages, unsigned int page_shift)
 {
-	unsigned int i, nr = (end - addr) >> PAGE_SHIFT;
-
 	WARN_ON(page_shift < PAGE_SHIFT);
 
-	if (!IS_ENABLED(CONFIG_HAVE_ARCH_HUGE_VMALLOC) ||
-			page_shift == PAGE_SHIFT)
-		return vmap_small_pages_range_noflush(addr, end, prot, pages);
+	if (!IS_ENABLED(CONFIG_HAVE_ARCH_HUGE_VMALLOC))
+		page_shift = PAGE_SHIFT;
 
-	for (i = 0; i < nr; i += 1U << (page_shift - PAGE_SHIFT)) {
-		int err;
-
-		err = vmap_range_noflush(addr, addr + (1UL << page_shift),
-					page_to_phys(pages[i]), prot,
-					page_shift);
-		if (err)
-			return err;
-
-		addr += 1UL << page_shift;
-	}
-
-	return 0;
+	return vmap_pages_range_noflush_walk(addr, end, prot, pages, page_shift);
 }
 
 int vmap_pages_range_noflush(unsigned long addr, unsigned long end,
Signature · 1 line
2.34.1
author
#22 lore raw

PATCH v8 8/10 mm/vmalloc: extract vm_shift() to consolidate mapping shift selection

In reply to Wen Jiang

From: Wen Jiang <[email protected]>

Extract the vmalloc mapping shift selection from __vmalloc_node_range_noprof() into vm_shift(), preparing for reuse by the vmap batching path.

No functional change.

---
 mm/vmalloc.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
mm/vmalloc.c +9 -5
index cf1daf931a6f6..ed868afd8194d 100644
@@ -3532,6 +3532,14 @@ void vunmap(const void *addr)
 }
 EXPORT_SYMBOL(vunmap);
 
+static inline unsigned int vm_shift(pgprot_t prot, unsigned long size)
+{
+	if (arch_vmap_pmd_supported(prot) && size >= PMD_SIZE)
+		return PMD_SHIFT;
+
+	return arch_vmap_pte_supported_shift(size);
+}
+
 /**
  * vmap - map an array of pages into virtually contiguous space
  * @pages: array of page pointers
@@ -4039,11 +4047,7 @@ void *__vmalloc_node_range_noprof(unsigned long size, unsigned long align,
 		 * supporting them.
 		 */
 
-		if (arch_vmap_pmd_supported(prot) && size >= PMD_SIZE)
-			shift = PMD_SHIFT;
-		else
-			shift = arch_vmap_pte_supported_shift(size);
-
+		shift = vm_shift(prot, size);
 		align = max(original_align, 1UL << shift);
 	}
 
Signature · 1 line
2.34.1
author
#23 lore raw

PATCH v8 9/10 mm/vmalloc: map contiguous pages in batches for vmap() if possible

In reply to Wen Jiang

From: "Barry Song (Xiaomi)" <[email protected]>

In many cases, the pages passed to vmap() may include high-order pages. For example, the systemheap often allocates pages in descending
order: order 8, then 4, then 0. Currently, vmap() iterates over every page individually—even pages inside a high-order block are handled one by one.

This patch detects physically contiguous pages (regardless of whether they are compound or non-compound) by scanning with num_pages_contiguous(), and maps them as a single contiguous block whenever possible. The mapping order is determined by taking the minimum of the contiguous page count and the pfn alignment, allowing graceful degradation when pfn alignment is less than the contiguous range.

Pages with the same page_shift are coalesced and mapped via vmap_pages_range_noflush_walk() to avoid page table rewalk.

As users typically allocate memory in descending orders (e.g. 8 → 4 → 0), once an order-0 page is encountered, we stop scanning for contiguous pages since subsequent pages are likely order-0 as well.

---
 mm/vmalloc.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 80 insertions(+), 2 deletions(-)
mm/vmalloc.c +80 -2
index ed868afd8194d..bce2d2c09f546 100644
@@ -3540,6 +3540,85 @@ static inline unsigned int vm_shift(pgprot_t prot, unsigned long size)
 	return arch_vmap_pte_supported_shift(size);
 }
 
+static inline int get_vmap_batch_order(struct page **pages,
+		pgprot_t prot, unsigned int nr_pages)
+{
+	unsigned long pfn;
+	unsigned int nr_contig;
+	int order;
+
+	if (!IS_ENABLED(CONFIG_HAVE_ARCH_HUGE_VMAP))
+		return 0;
+
+	/* Limit nr_pages by pfn alignment */
+	pfn = page_to_pfn(*pages);
+	if (pfn > 0)
+		nr_pages = min_t(size_t, nr_pages, 1UL << __ffs(pfn));
+
+	nr_contig = num_pages_contiguous(pages, nr_pages);
+	if (nr_contig < 2)
+		return 0;
+
+	order = ilog2(nr_contig);
+
+	if (vm_shift(prot, PAGE_SIZE << order) == PAGE_SHIFT)
+		return 0;
+
+	return order;
+}
+
+static int vmap_pages_range_batched(unsigned long addr, unsigned long end,
+		pgprot_t prot, struct page **pages)
+{
+	const unsigned int nr_pages = (end - addr) >> PAGE_SHIFT;
+	unsigned int prev_shift = 0, batch_idx = 0;
+	unsigned long batch_start = addr, batch_end = addr;
+	int err;
+
+	err = kmsan_vmap_pages_range_noflush(addr, end, prot, pages,
+					     PAGE_SHIFT, GFP_KERNEL);
+
+	if (err)
+		goto out;
+
+	for (unsigned int i = 0; i < nr_pages; ) {
+		unsigned int shift = PAGE_SHIFT +
+			get_vmap_batch_order(pages + i, prot, nr_pages - i);
+
+		if (!i)
+			prev_shift = shift;
+
+		if (shift != prev_shift) {
+			err = vmap_pages_range_noflush_walk(batch_start, batch_end,
+					prot, pages + batch_idx, prev_shift);
+			if (err)
+				goto out;
+			prev_shift = shift;
+			batch_start = batch_end;
+			batch_idx = i;
+		}
+
+		/*
+		 * Once we fail to batch pages, we expect to fail batching
+		 * for all remaining pages, so just give up.
+		 */
+		if (shift == PAGE_SHIFT)
+			break;
+
+		batch_end += 1UL << shift;
+		i += 1U << (shift - PAGE_SHIFT);
+	}
+
+	/* Remaining */
+	if (batch_start < end)
+		err = vmap_pages_range_noflush_walk(batch_start, end, prot,
+				pages + batch_idx, prev_shift);
+
+out:
+	flush_cache_vmap(addr, end);
+	return err;
+}
+
 /**
  * vmap - map an array of pages into virtually contiguous space
  * @pages: array of page pointers
@@ -3583,8 +3662,7 @@ void *vmap(struct page **pages, unsigned int count,
 		return NULL;
 
 	addr = (unsigned long)area->addr;
-	if (vmap_pages_range(addr, addr + size, pgprot_nx(prot),
-				pages, PAGE_SHIFT) < 0) {
+	if (vmap_pages_range_batched(addr, addr + size, pgprot_nx(prot), pages) < 0) {
 		vunmap(area->addr);
 		return NULL;
 	}
Signature · 1 line
2.34.1
author
#24 lore raw

PATCH v8 10/10 mm/vmalloc: align vm_area so vmap() can batch mappings

In reply to Wen Jiang

From: "Barry Song (Xiaomi)" <[email protected]>

Try to align the vmap virtual address to PMD_SHIFT or a larger PTE mapping size hinted by the architecture, so contiguous pages can be batch-mapped when setting PMD or PTE entries. We do not expect any significant overhead for this.

Add __get_vm_area_node_aligned_caller() as a wrapper over __get_vm_area_node() to simplify repeated calls with fixed arguments.

---
 mm/vmalloc.c | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)
mm/vmalloc.c +34 -1
index bce2d2c09f546..84413def1d771 100644
@@ -3306,6 +3306,14 @@ struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
 				  NUMA_NO_NODE, GFP_KERNEL, caller);
 }
 
+static struct vm_struct *__get_vm_area_node_aligned_caller(unsigned long size,
+		unsigned long align, unsigned long flags, const void *caller)
+{
+	return __get_vm_area_node(size, align, PAGE_SHIFT, flags,
+				  VMALLOC_START, VMALLOC_END,
+				  NUMA_NO_NODE, GFP_KERNEL, caller);
+}
+
 /**
  * find_vm_area - find a continuous kernel virtual area
  * @addr:	  base address
@@ -3619,6 +3627,30 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end,
 	return err;
 }
 
+static struct vm_struct *vmap_get_aligned_vm_area(unsigned long size,
+		unsigned long flags, pgprot_t prot, const void *caller)
+{
+	struct vm_struct *vm_area;
+	unsigned int shift;
+
+	if (arch_vmap_pmd_supported(prot) && size >= PMD_SIZE) {
+		vm_area = __get_vm_area_node_aligned_caller(size, PMD_SIZE,
+				flags, caller);
+		if (vm_area)
+			return vm_area;
+	}
+
+	shift = arch_vmap_pte_supported_shift(size);
+	if (shift > PAGE_SHIFT) {
+		vm_area = __get_vm_area_node_aligned_caller(size, 1UL << shift,
+				flags, caller);
+		if (vm_area)
+			return vm_area;
+	}
+
+	return __get_vm_area_node_aligned_caller(size, PAGE_SIZE, flags, caller);
+}
+
 /**
  * vmap - map an array of pages into virtually contiguous space
  * @pages: array of page pointers
@@ -3657,7 +3689,8 @@ void *vmap(struct page **pages, unsigned int count,
 		return NULL;
 
 	size = (unsigned long)count << PAGE_SHIFT;
-	area = get_vm_area_caller(size, flags, __builtin_return_address(0));
+	area = vmap_get_aligned_vm_area(size, flags, prot,
+				__builtin_return_address(0));
 	if (!area)
 		return NULL;
 
Signature · 1 line
2.34.1
#25 lore raw
In reply to Wen Jiang

Hi,

Le 17/09/2026 à 07:29, Wen Jiang a écrit :

Quoted 5 lines

From: Wen Jiang <[email protected]>

This patchset accelerates ioremap, vmalloc, and vmap when the memory is physically fully or partially contiguous. Two techniques are used:

Quoted 8 lines

1. Avoid page table rewalk when setting PTEs/PMDs for multiple memory segments
2. Use batched mappings wherever possible in both vmalloc and ARM64 layers

Besides accelerating the mapping path, this also enables large mappings (PMD and cont-PTE) for vmap, which are currently not supported.

This series doesn't apply. I tried to apply it on top of v7.2, v7.3-rc3 and next-20260916

Can you tell how to apply it ?

$ LANG= b4 shazam -l [email protected]
Grabbing thread from lore.kernel.org/all/[email protected]/t.mbox.gz
Checking for newer revisions
Grabbing search results from lore.kernel.org
Analyzing 11 messages in the thread
Analyzing 41 code-review messages
Checking attestation on all messages, may take a moment...
---
  ✓ [PATCH v8 1/10] arm64/mm: add pte_set_huge() and pte_clear_huge()
    + Link: 
https://lore.kernel.org/r/[email protected]
  ✓ [PATCH v8 2/10] powerpc/8xx: add pte_set_huge()
    + Link: 
https://lore.kernel.org/r/[email protected]
  ✓ [PATCH v8 3/10] mm/vmalloc: use pte_set_huge()/pte_clear_huge() for 
PTE-level block mappings
    + Link: 
https://lore.kernel.org/r/[email protected]
  ✓ [PATCH v8 4/10] arm64/hugetlb: drop the init_mm special case in 
clear_flush()
    + Link: 
https://lore.kernel.org/r/[email protected]
  ✓ [PATCH v8 5/10] arm64/vmalloc: allow arch_vmap_pte_range_map_size() 
to batch multiple CONT_PTE
    + Link: 
https://lore.kernel.org/r/[email protected]
  ✓ [PATCH v8 6/10] mm/vmalloc: extract vmap_set_ptes() to consolidate 
PTE mapping logic
    + Link: 
https://lore.kernel.org/r/[email protected]
  ✓ [PATCH v8 7/10] mm/vmalloc: extend page table walk to support 
larger page_shift sizes and eliminate page table rewalk
    + Link: 
https://lore.kernel.org/r/[email protected]
  ✓ [PATCH v8 8/10] mm/vmalloc: extract vm_shift() to consolidate 
mapping shift selection
    + Link: 
https://lore.kernel.org/r/[email protected]
  ✓ [PATCH v8 9/10] mm/vmalloc: map contiguous pages in batches for 
vmap() if possible
    + Link: 
https://lore.kernel.org/r/[email protected]
  ✓ [PATCH v8 10/10] mm/vmalloc: align vm_area so vmap() can batch mappings
    + Link: 
https://lore.kernel.org/r/[email protected]
  ---
  ✓ Signed: DKIM/gmail.com
---
Total patches: 10
---
NOTE: some trailers ignored due to from/email mismatches:
    ! Trailer: Reviewed-by: Uladzislau Rezki (Sony) <[email protected]>
     Msg From: Barry Song <[email protected]>
NOTE: Rerun with -S to apply them anyway
---
Applying: arm64/mm: add pte_set_huge() and pte_clear_huge()
Patch failed at 0001 arm64/mm: add pte_set_huge() and pte_clear_huge()
error: patch failed: arch/arm64/mm/mmu.c:1872
error: arch/arm64/mm/mmu.c: patch does not apply
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"

Thanks
Christophe

Quoted 195 lines

Patches 1-4 decouple the PTE-level block mapping path from HugeTLB. Previously vmap_pte_range() installed cont-PTE mappings by reusing set_huge_pte_at() and huge_ptep_get_and_clear(), which are HugeTLB helpers gated by CONFIG_HUGETLB_PAGE. This made the feature silently unavailable on CONFIG_HUGETLB_PAGE=n kernels, and coupled mm/vmalloc.c to HugeTLB internals. Patches 1 and 2 add the arm64 and powerpc/8xx implementations of pte_set_huge()/pte_clear_huge(), which join the existing pmd/pud_set_huge() family. Patch 3 adds the generic fallbacks and converts mm/vmalloc.c over. Patch 4 then removes the now-dead init_mm special case from arm64's clear_flush().

Patch 5 extends ARM64 arch_vmap_pte_range_map_size() to batch multiple CONT_PTE blocks in one call instead of one at a time.

Patch 6 extracts a common helper vmap_set_ptes() that consolidates PTE mapping logic for the ioremap and vmalloc/vmap paths, handling both CONT_PTE and regular PTE mappings. This prepares for the next patch.

Patch 7 extends the page table walk path to support page shifts other than PAGE_SHIFT and eliminates the page table rewalk for huge vmalloc mappings. The function is renamed from vmap_small_pages_range_noflush() to vmap_pages_range_noflush_walk().

Patch 8 extracts vm_shift() to consolidate vmalloc mapping shift selection for reuse in the batching path.

Patches 9-10 add huge vmap support for contiguous pages, including support for non-compound pages with pfn alignment verification.

On the RK3588 8-core ARM64 SoC, with tasks pinned to a little core and the performance CPUfreq policy enabled, benchmark results:

* ioremap(1 MB): 1.35x faster (3407 ns -> 2526 ns)
* vmalloc(1 MB) mapping time (excluding allocation) with
VM_ALLOW_HUGE_VMAP: 1.42x faster (5.00 us -> 3.53 us)
* vmap(100MB) with order-8 pages: 8.3x faster (1235 us -> 149 us)

Many thanks to Xueyuan Chen for his testing efforts on RK3588 boards.

Large vmap() mappings were also tested by Leo Yan with ARM trace buffer units, including TRBE and SPE. These units use the CPU page tables for address translation when writing trace data to DRAM, so using larger vmap() mapping granules can reduce TLB pressure on the trace writer.

The TRBE test used a 1G CoreSight ETM AUX buffer. Across five runs on an isolated CPU, the average results were:

* dtlb_walk: 68.4 -> 59.4 (-13.16%)
* l1d_tlb_refill: 155.8 -> 119.6 (-23.23%)
* l2d_tlb_refill: 161435.8 -> 495.0 (-99.69%)

The SPE test used a 512M ARM SPE AUX buffer. Across five runs on an isolated CPU, the average results were:

* dtlb_walk: 1710.4 -> 1315.6 (-23.08%)
* l1d_tlb_refill: 16000.0 -> 15950.2 (-0.31%)
* l2d_tlb_refill: 4796.0 -> 2931.2 (-38.88%)

These results show that enabling larger vmap() mappings can materially reduce page table walks and TLB refills for large trace buffers.

Many thanks to Leo Yan for his testing efforts on ARM trace buffers.

Changes since v7:
- v7's patch 1 (which extended the hugetlb helpers in arch/arm64/mm/hugetlbpage.c) is replaced by pte_set_huge()/ pte_clear_huge(), split across patches 1-3 so that the arm64, powerpc/8xx and generic changes can be reviewed and acked
independently: patch 1 is arm64 only, patch 2 is powerpc/8xx only, patch 3 is the generic fallbacks plus the mm/vmalloc.c conversion. hugetlbpage.c no longer gains the CONT_PTE batching hooks, and patch 4 additionally removes its now-dead init_mm special case. mm/vmalloc.c no longer includes <linux/hugetlb.h>.
- The generic pte_set_huge()/pte_clear_huge() fallbacks WARN_ON_ONCE() instead of silently doing nothing. They only exist to keep the build working on architectures without PTE-level block mappings, where they are unreachable (patch 3).
- Patch 5 (v7 patch 2): use round_down(size, CONT_PTE_SIZE) instead of rounddown_pow_of_two(size), since pte_set_huge() takes the size directly without an ilog2() roundtrip. This lets a single call span several CONT_PTE blocks: a 768K (512K + 256K) ioremap is now 1.25x faster than v7.
- Patch 6 (v7 patch 3): vmap_set_ptes() now calls pte_set_huge() instead of the hugetlb path.
- Patch 9 (v7 patch 6): get_vmap_batch_order() takes pages+i instead of a separate idx argument, and applies pfn alignment limit to scan length rather than to the resulting order. Renamed idx/map_addr to batch_idx/batch_start. Dropped Dev's Reviewed-by.

Changes since v6:
- Add a clarifying comment about the reuse of hugetlb helpers by non-hugetlbfs(vmalloc) mm code (patch 1)
- Expand the arm64/vmalloc commit message and comment to clarify that multi-CONT_PTE_SIZE values are vmalloc mapping spans, not HugeTLB hstate sizes (patch 2)
- Move the local steps variable change in vmap_pte_range() into the vmap_set_ptes() extraction patch (patch 3)
- Propagate vmap_pages_pte_range() errors through the upper vmap_pages_*() levels instead of returning -ENOMEM for all failures (patch 4)
- Add a preparatory vm_shift() helper patch before the batching patch (patch 5)
- Guard the PFN alignment clamp in get_vmap_batch_order() against PFN 0 before calling __ffs() (patch 6)
- Fix kmsan_vmap_pages_range_noflush() indentation in the batching path (patch 6)

Changes since v5:
- No code changes.
- Pick up Reviewed-by and Tested-by tags from Dev, Leo and Uladzislau. Many thanks!
- Add TRBE/SPE large vmap() test results from Leo Yan to the cover letter.

Changes since v4:
- Move pgsize update before contig_ptes check (patch 1)
- Use rounddown_pow_of_two instead of __fls in arch_vmap_pte_range_map_size (patch 2)
- Reword comment to avoid mentioning cont_pte and remove if in vmap_set_ptes (patch 3)
- Rename vmap_batched() to vmap_pages_range_batched() (patch 5)
- Use batch_end as the batching cursor to avoid an unused start variable (patch 5)
- Check arch_vmap_pmd_supported before PMD mapping (patch 6)

Changes since v3:
- Squash vmap_pte_range() loop variable fix into patch 4 (patch 3, 4)
- Use shift >= PMD_SHIFT and fix *nr increment in vmap_pages_pmd_range() (patch 4)
- Pass page_shift directly without capping at PMD_SHIFT (patch 4, 5)
- Add vm_shift() helper and pass pgprot_t to get_vmap_batch_order() (patch 5)
- Use min(order, __ffs(pfn)) for graceful pfn alignment degradation, replacing IS_ALIGNED check (patch 5)
- Remove irrelevant ioremap_max_page_shift early-exit (patch 5)
- Add __get_vm_area_node_aligned_caller() wrapper, rename to vmap_get_aligned_vm_area() (patch 6)

Changes since v2:
- Use __fls instead of fls in arch_vmap_pte_range_map_size (patch 2)
- Add WARN_ON checks in vmap_pages_pmd_range (patch 4)
- Fix flush_cache_vmap to use saved start address instead of the already-advanced addr (patch 5)
- Rename __vmap_huge() to vmap_batched() (patch 5)
- Add caller parameter and unroll while(1) loop (patch 5)
- Squash patch 7 into patch 5 (stop scanning for compound pages after encountering small pages)

Changes since v1:
- Fix condition order and use PMD_SIZE instead of CONT_PMD_SIZE in patch 1 (Dev Jain)
- Squash patch 3+4 and patch 5+7 (Dev Jain)
- Replace "zigzag" with "page table rewalk" in commit messages (Dev Jain)
- Rename vmap_small_pages_range_noflush() to vmap_pages_range_noflush_walk() (Dev Jain)
- Extract vmap_set_ptes() as a new patch to consolidate PTE mapping logic between vmap_pte_range() and vmap_pages_pte_range(), handling both CONT_PTE and regular mappings (Mike Rapoport)
- Support non-compound pages in get_vmap_batch_order() by falling back to physical contiguity scanning with pfn alignment check (Dev Jain, Uladzislau Rezki)
- In get_vmap_batch_order(), filter out orders that the architecture cannot batch by checking arch_vmap_pte_supported_shift() directly. This avoids overhead for orders 1-3 on ARM64 CONT_PTE with 4K pages. (patch 5)

Barry Song (Xiaomi) (4):
arm64/vmalloc: allow arch_vmap_pte_range_map_size() to batch multiple
CONT_PTE
mm/vmalloc: extend page table walk to support larger page_shift sizes
and eliminate page table rewalk
mm/vmalloc: map contiguous pages in batches for vmap() if possible mm/vmalloc: align vm_area so vmap() can batch mappings

Wen Jiang (6):
arm64/mm: add pte_set_huge() and pte_clear_huge() powerpc/8xx: add pte_set_huge()
mm/vmalloc: use pte_set_huge()/pte_clear_huge() for PTE-level block
mappings
arm64/hugetlb: drop the init_mm special case in clear_flush() mm/vmalloc: extract vmap_set_ptes() to consolidate PTE mapping logic mm/vmalloc: extract vm_shift() to consolidate mapping shift selection

  arch/arm64/include/asm/pgtable.h             |   6 +
  arch/arm64/include/asm/vmalloc.h             |   8 +-
  arch/arm64/mm/hugetlbpage.c                  |   5 +-
  arch/arm64/mm/mmu.c                          |  19 ++
  arch/powerpc/include/asm/nohash/32/pte-8xx.h |   4 +
  arch/powerpc/mm/nohash/8xx.c                 |  29 ++
  include/linux/pgtable.h                      |  29 ++
  mm/vmalloc.c                                 | 268 ++++++++++++++-----
  8 files changed, 300 insertions(+), 68 deletions(-)
#26 lore raw

Le 17/09/2026 à 14:17, Christophe Leroy (CS GROUP) a écrit :

Quoted 13 lines

Hi,

Le 17/09/2026 à 07:29, Wen Jiang a écrit :

Quoted 10 lines

From: Wen Jiang <[email protected]>

This patchset accelerates ioremap, vmalloc, and vmap when the memory is physically fully or partially contiguous. Two techniques are used:

1. Avoid page table rewalk when setting PTEs/PMDs for multiple memory segments
2. Use batched mappings wherever possible in both vmalloc and ARM64 layers

Quoted 7 lines
Quoted 3 lines

Besides accelerating the mapping path, this also enables large mappings (PMD and cont-PTE) for vmap, which are currently not supported.

This series doesn't apply. I tried to apply it on top of v7.2, v7.3-rc3 and next-20260916

Can you tell how to apply it ?

Finaly I was able to apply it on top of v7.1

Christophe

#11 lore raw

Re: [PATCH v8 03/10] mm/vmalloc: use pte_set_huge()/pte_clear_huge() for PTE-level block mappings

In reply to Wen Jiang

Le 17/09/2026 à 07:29, Wen Jiang a écrit :

Quoted 38 lines

From: Wen Jiang <[email protected]>

vmap installs PTE-level block mappings by reusing set_huge_pte_at() and huge_ptep_get_and_clear() under #ifdef CONFIG_HUGETLB_PAGE. This makes the feature silently unavailable on CONFIG_HUGETLB_PAGE=n kernels and couples mm/vmalloc.c to HugeTLB internals it does not otherwise need.

Now that arm64 and powerpc/8xx provide pte_set_huge() and pte_clear_huge(), add the generic fallbacks next to the existing pmd/pud_set_huge() family and convert vmap_pte_range() and vunmap_pte_range() to the new helpers. The CONFIG_HUGETLB_PAGE guards around the block-mapping paths are dropped, so PTE-level block mappings now also work on CONFIG_HUGETLB_PAGE=n kernels, and mm/vmalloc.c no longer includes <linux/hugetlb.h>.

The fallbacks exist only to keep the build working on architectures without PTE-level block mapping support. They are unreachable there:
the callers only run when arch_vmap_pte_range_map_size() or arch_vmap_pte_range_unmap_size() return a size other than PAGE_SIZE, which requires an arch implementation. WARN_ON_ONCE() makes that explicit rather than silently doing nothing.

---
  include/linux/pgtable.h | 29 +++++++++++++++++++++++++++++
  mm/vmalloc.c            | 19 ++++++-------------
  2 files changed, 35 insertions(+), 13 deletions(-)
include/linux/pgtable.h +2 -0
index cdd68ed3ae1a9..349ced999f959 100644
@@ -2134,6 +2134,35 @@ static inline int pmd_free_pte_page(pmd_t *pmd, unsigned long addr)
  }
  #endif	/* CONFIG_HAVE_ARCH_HUGE_VMAP */
 
+/*
+ * PTE-level block mappings for vmap.

include/linux/pgtable.h static inline int pmd_free_pte_page(pmd_t *pmd, unsigned long addr) · line 2148

Quoted 10 lines
include/linux/pgtable.h +10 -0
+ *
+ * pte_set_huge() only has to be implemented by architectures whose
+ * arch_vmap_pte_range_map_size() can return a size other than PAGE_SIZE.
+ */
+#ifndef __HAVE_ARCH_PTE_SET_HUGE
+static inline void pte_set_huge(pte_t *ptep, unsigned long addr,
+				phys_addr_t phys, pgprot_t prot,
+				unsigned long size)
+{
+	WARN_ON_ONCE(1);

BUILD_BUG_ON() would be better here.

It should be possible because fallback arch_vmap_pte_range_map_size() will constant-fold PAGE_SIZE so pte_set_huge() will never be called.

Quoted 4 lines
include/linux/pgtable.h +4 -0
+}
+#endif
+
+/*

include/linux/pgtable.h static inline int pmd_free_pte_page(pmd_t *pmd, unsigned long addr) · line 2162

Quoted 10 lines
include/linux/pgtable.h +10 -0
+ * Likewise, pte_clear_huge() only has to be implemented by architectures
+ * whose arch_vmap_pte_range_unmap_size() can return a size other than
+ * PAGE_SIZE.
+ */
+#ifndef __HAVE_ARCH_PTE_CLEAR_HUGE
+static inline pte_t pte_clear_huge(pte_t *ptep, unsigned long addr,
+				   unsigned long size)
+{
+	WARN_ON_ONCE(1);
+	return __pte(0);

Same I guess.

Quoted 6 lines
include/linux/pgtable.h +3 -0
+}
+#endif
+
  #ifndef __HAVE_ARCH_FLUSH_PMD_TLB_RANGE
  #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  /*
author
#27 lore raw
In reply to Christophe Leroy (CS GROUP)
Quoted 6 lines · Christophe Leroy (CS GROUP)
Quoted 2 lines · Christophe Leroy (CS GROUP)

Hi,

Hi Christophe,

Thanks for reviewing,

Quoted 15 lines
Quoted 15 lines · Wen Jiang
Quoted 13 lines

From: Wen Jiang <[email protected]>

This patchset accelerates ioremap, vmalloc, and vmap when the memory is physically fully or partially contiguous. Two techniques are used:

1. Avoid page table rewalk when setting PTEs/PMDs for multiple memory segments
2. Use batched mappings wherever possible in both vmalloc and ARM64 layers

Besides accelerating the mapping path, this also enables large mappings (PMD and cont-PTE) for vmap, which are currently not supported.

Quoted 8 lines
Quoted 4 lines

This series doesn't apply. I tried to apply it on top of v7.2, v7.3-rc3 and next-20260916

Can you tell how to apply it ?

Finaly I was able to apply it on top of v7.1

Yes. This series was based on v7.1. I'll rebase it onto the mainline in the next version.

Quoted 1 line

Christophe

author
#12 lore raw

Re: [PATCH v8 03/10] mm/vmalloc: use pte_set_huge()/pte_clear_huge() for PTE-level block mappings

In reply to Christophe Leroy (CS GROUP)
Quoted 55 lines · Christophe Leroy (CS GROUP)
Quoted 38 lines · Wen Jiang

From: Wen Jiang <[email protected]>

vmap installs PTE-level block mappings by reusing set_huge_pte_at() and huge_ptep_get_and_clear() under #ifdef CONFIG_HUGETLB_PAGE. This makes the feature silently unavailable on CONFIG_HUGETLB_PAGE=n kernels and couples mm/vmalloc.c to HugeTLB internals it does not otherwise need.

Now that arm64 and powerpc/8xx provide pte_set_huge() and pte_clear_huge(), add the generic fallbacks next to the existing pmd/pud_set_huge() family and convert vmap_pte_range() and vunmap_pte_range() to the new helpers. The CONFIG_HUGETLB_PAGE guards around the block-mapping paths are dropped, so PTE-level block mappings now also work on CONFIG_HUGETLB_PAGE=n kernels, and mm/vmalloc.c no longer includes <linux/hugetlb.h>.

The fallbacks exist only to keep the build working on architectures without PTE-level block mapping support. They are unreachable there:
the callers only run when arch_vmap_pte_range_map_size() or arch_vmap_pte_range_unmap_size() return a size other than PAGE_SIZE, which requires an arch implementation. WARN_ON_ONCE() makes that explicit rather than silently doing nothing.

---
  include/linux/pgtable.h | 29 +++++++++++++++++++++++++++++
  mm/vmalloc.c            | 19 ++++++-------------
  2 files changed, 35 insertions(+), 13 deletions(-)
include/linux/pgtable.h +2 -0
index cdd68ed3ae1a9..349ced999f959 100644
@@ -2134,6 +2134,35 @@ static inline int pmd_free_pte_page(pmd_t *pmd, unsigned long addr)
  }
  #endif      /* CONFIG_HAVE_ARCH_HUGE_VMAP */
 
+/*
+ * PTE-level block mappings for vmap.

include/linux/pgtable.h static inline int pmd_free_pte_page(pmd_t *pmd, unsigned long addr) · line 2148

Quoted 10 lines
include/linux/pgtable.h +10 -0
+ *
+ * pte_set_huge() only has to be implemented by architectures whose
+ * arch_vmap_pte_range_map_size() can return a size other than PAGE_SIZE.
+ */
+#ifndef __HAVE_ARCH_PTE_SET_HUGE
+static inline void pte_set_huge(pte_t *ptep, unsigned long addr,
+                             phys_addr_t phys, pgprot_t prot,
+                             unsigned long size)
+{
+     WARN_ON_ONCE(1);

BUILD_BUG_ON() would be better here.

Quoted 3 lines

It should be possible because fallback arch_vmap_pte_range_map_size() will constant-fold PAGE_SIZE so pte_set_huge() will never be called.

Agreed. These fallbacks exist only to keep the build working on architectures with PTE-level block mappings and should never actually be reached, so BUILD_BUG_ON() is right. I'll make that change in v9.

Thanks,
Wen

Quoted 23 lines
Quoted 4 lines
include/linux/pgtable.h +4 -0
+}
+#endif
+
+/*

include/linux/pgtable.h static inline int pmd_free_pte_page(pmd_t *pmd, unsigned long addr) · line 2162

Quoted 10 lines
include/linux/pgtable.h +10 -0
+ * Likewise, pte_clear_huge() only has to be implemented by architectures
+ * whose arch_vmap_pte_range_unmap_size() can return a size other than
+ * PAGE_SIZE.
+ */
+#ifndef __HAVE_ARCH_PTE_CLEAR_HUGE
+static inline pte_t pte_clear_huge(pte_t *ptep, unsigned long addr,
+                                unsigned long size)
+{
+     WARN_ON_ONCE(1);
+     return __pte(0);

Same I guess.

Quoted 6 lines
include/linux/pgtable.h +3 -0
+}
+#endif
+
  #ifndef __HAVE_ARCH_FLUSH_PMD_TLB_RANGE
  #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  /*
#13 lore raw

Re: [PATCH v8 03/10] mm/vmalloc: use pte_set_huge()/pte_clear_huge() for PTE-level block mappings

Quoted 58 lines · Wen Jiang
Quoted 55 lines · Christophe Leroy (CS GROUP)
Quoted 38 lines · Wen Jiang

From: Wen Jiang <[email protected]>

vmap installs PTE-level block mappings by reusing set_huge_pte_at() and huge_ptep_get_and_clear() under #ifdef CONFIG_HUGETLB_PAGE. This makes the feature silently unavailable on CONFIG_HUGETLB_PAGE=n kernels and couples mm/vmalloc.c to HugeTLB internals it does not otherwise need.

Now that arm64 and powerpc/8xx provide pte_set_huge() and pte_clear_huge(), add the generic fallbacks next to the existing pmd/pud_set_huge() family and convert vmap_pte_range() and vunmap_pte_range() to the new helpers. The CONFIG_HUGETLB_PAGE guards around the block-mapping paths are dropped, so PTE-level block mappings now also work on CONFIG_HUGETLB_PAGE=n kernels, and mm/vmalloc.c no longer includes <linux/hugetlb.h>.

The fallbacks exist only to keep the build working on architectures without PTE-level block mapping support. They are unreachable there:
the callers only run when arch_vmap_pte_range_map_size() or arch_vmap_pte_range_unmap_size() return a size other than PAGE_SIZE, which requires an arch implementation. WARN_ON_ONCE() makes that explicit rather than silently doing nothing.

---
  include/linux/pgtable.h | 29 +++++++++++++++++++++++++++++
  mm/vmalloc.c            | 19 ++++++-------------
  2 files changed, 35 insertions(+), 13 deletions(-)
include/linux/pgtable.h +2 -0
index cdd68ed3ae1a9..349ced999f959 100644
@@ -2134,6 +2134,35 @@ static inline int pmd_free_pte_page(pmd_t *pmd, unsigned long addr)
  }
  #endif      /* CONFIG_HAVE_ARCH_HUGE_VMAP */
 
+/*
+ * PTE-level block mappings for vmap.

include/linux/pgtable.h static inline int pmd_free_pte_page(pmd_t *pmd, unsigned long addr) · line 2148

Quoted 10 lines
include/linux/pgtable.h +10 -0
+ *
+ * pte_set_huge() only has to be implemented by architectures whose
+ * arch_vmap_pte_range_map_size() can return a size other than PAGE_SIZE.
+ */
+#ifndef __HAVE_ARCH_PTE_SET_HUGE
+static inline void pte_set_huge(pte_t *ptep, unsigned long addr,
+                             phys_addr_t phys, pgprot_t prot,
+                             unsigned long size)
+{
+     WARN_ON_ONCE(1);

BUILD_BUG_ON() would be better here.

Quoted 8 lines
Quoted 3 lines

It should be possible because fallback arch_vmap_pte_range_map_size() will constant-fold PAGE_SIZE so pte_set_huge() will never be called.

Agreed. These fallbacks exist only to keep the build working on architectures with PTE-level block mappings and should never actually be reached, so BUILD_BUG_ON() is right. I'll make that change in v9.

I am not quite sure. It won't be called at runtime because vmap size/unmap size return PAGE_SIZE, so the code won't reach this branch. But it will still be built.

So, would a BUILD_BUG_ON() trigger a build failure here?

Best Regards
Barry

#2 lore raw

Re: [PATCH v8 01/10] arm64/mm: add pte_set_huge() and pte_clear_huge()

In reply to Wen Jiang
Quoted 14 lines · Wen Jiang

From: Wen Jiang <[email protected]>

vmap installs PTE-level block mappings (PTE_CONT on arm64) by reusing set_huge_pte_at() and huge_ptep_get_and_clear(), which are HugeTLB helpers gated by CONFIG_HUGETLB_PAGE. This makes the feature silently unavailable on CONFIG_HUGETLB_PAGE=n kernels and couples mm/vmalloc.c to HugeTLB internals it does not otherwise need.

Add pte_set_huge()/pte_clear_huge() to arm64, joining the existing pmd_set_huge()/pud_set_huge() family in mm/mmu.c. They build on __set_ptes() and __get_and_clear_full_ptes() directly, so they do not depend on CONFIG_HUGETLB_PAGE.

Quoted 4 lines

There is no caller yet: mm/vmalloc.c is converted later in this series, once the generic fallbacks are in place.

I tried to apply the patch to mm-new, but it doesn't apply.

% git am raw
Applying: arm64/mm: add pte_set_huge() and pte_clear_huge()
error: patch failed: arch/arm64/mm/mmu.c:1872
error: arch/arm64/mm/mmu.c: patch does not apply
Patch failed at 0001 arm64/mm: add pte_set_huge() and pte_clear_huge()
hint: Use 'git am --show-current-patch=diff' to see the failed patch When you have resolved this problem, run "git am --continue". If you prefer to skip this patch, run "git am --skip" instead. To restore the original branch and stop patching, run "git am --abort".

% patch -p1 < raw
patching file 'arch/arm64/include/asm/pgtable.h'
patching file 'arch/arm64/mm/mmu.c'
1 out of 1 hunks failed--saving rejects to 'arch/arm64/mm/mmu.c.rej'

[...]

arch/arm64/mm/mmu.c int pmd_clear_huge(pmd_t *pmdp) · line 1879

Quoted 5 lines
arch/arm64/mm/mmu.c +5 -0
+void pte_set_huge(pte_t *ptep, unsigned long addr, phys_addr_t phys,
+                 pgprot_t prot, unsigned long size)
+{
+       unsigned long pfn = __phys_to_pfn(phys);
+       pte_t new_pte = pte_mkcont(pfn_pte(pfn, prot));

Maybe we can drop the new_ prefix.

arch/arm64/mm/mmu.c int pmd_clear_huge(pmd_t *pmdp) · line 1883

Quoted 4 lines
arch/arm64/mm/mmu.c +4 -0
+       unsigned int nr = size >> PAGE_SHIFT;
+
+       VM_WARN_ON(!IS_ALIGNED(pfn, CONT_PTES));
+       VM_WARN_ON(!IS_ALIGNED(size, CONT_PTE_SIZE));

Do we also need to check that addr is aligned?

Best Regards
Barry

#5 lore raw

Re: [PATCH v8 02/10] powerpc/8xx: add pte_set_huge()

In reply to Wen Jiang
Quoted 9 lines · Wen Jiang

From: Wen Jiang <[email protected]>

vmap installs PTE-level block mappings (SZ_16K/SZ_512K on powerpc/8xx) by reusing set_huge_pte_at(), a HugeTLB helper gated by CONFIG_HUGETLB_PAGE. This makes the feature silently unavailable on CONFIG_HUGETLB_PAGE=n kernels and couples mm/vmalloc.c to HugeTLB internals it does not otherwise need.

Quoted 8 lines

Add pte_set_huge() to powerpc/8xx, next to the existing pmd_clear_huge()/pud_clear_huge() in mm/nohash/8xx.c. It builds the huge PTE and writes it into the backing cells directly, without going through set_huge_pte_at(), so it does not depend on CONFIG_HUGETLB_PAGE.

No pte_clear_huge() is needed: 8xx does not implement arch_vmap_pte_range_unmap_size(), so the vmap unmap path never takes the block-mapping branch.

I assume you mean the block-unmapping (or PTE-clearing) branch. Please make this clear in the changelog.

Quoted 10 lines

In practice PPC_8xx selects HUGETLBFS unconditionally, so CONFIG_HUGETLB_PAGE=n does not occur there today; this is a decoupling cleanup rather than a new configuration.

There is no caller yet: mm/vmalloc.c is converted later in this series, once the generic fallbacks are in place.

---

BTW, this patch applies cleanly to the mm-new branch.

Best Regards
Barry

#14 lore raw

Re: [PATCH v8 03/10] mm/vmalloc: use pte_set_huge()/pte_clear_huge() for PTE-level block mappings

In reply to Barry Song

Le 17/09/2026 à 23:44, Barry Song a écrit :

Quoted 63 lines · Wen Jiang
Quoted 62 lines
Quoted 58 lines · Christophe Leroy (CS GROUP)
Quoted 38 lines · Wen Jiang

From: Wen Jiang <[email protected]>

vmap installs PTE-level block mappings by reusing set_huge_pte_at() and huge_ptep_get_and_clear() under #ifdef CONFIG_HUGETLB_PAGE. This makes the feature silently unavailable on CONFIG_HUGETLB_PAGE=n kernels and couples mm/vmalloc.c to HugeTLB internals it does not otherwise need.

Now that arm64 and powerpc/8xx provide pte_set_huge() and pte_clear_huge(), add the generic fallbacks next to the existing pmd/pud_set_huge() family and convert vmap_pte_range() and vunmap_pte_range() to the new helpers. The CONFIG_HUGETLB_PAGE guards around the block-mapping paths are dropped, so PTE-level block mappings now also work on CONFIG_HUGETLB_PAGE=n kernels, and mm/vmalloc.c no longer includes <linux/hugetlb.h>.

The fallbacks exist only to keep the build working on architectures without PTE-level block mapping support. They are unreachable there:
the callers only run when arch_vmap_pte_range_map_size() or arch_vmap_pte_range_unmap_size() return a size other than PAGE_SIZE, which requires an arch implementation. WARN_ON_ONCE() makes that explicit rather than silently doing nothing.

---
   include/linux/pgtable.h | 29 +++++++++++++++++++++++++++++
   mm/vmalloc.c            | 19 ++++++-------------
   2 files changed, 35 insertions(+), 13 deletions(-)
include/linux/pgtable.h +2 -0
index cdd68ed3ae1a9..349ced999f959 100644
@@ -2134,6 +2134,35 @@ static inline int pmd_free_pte_page(pmd_t *pmd, unsigned long addr)
   }
   #endif      /* CONFIG_HAVE_ARCH_HUGE_VMAP */
 
+/*
+ * PTE-level block mappings for vmap.

include/linux/pgtable.h static inline int pmd_free_pte_page(pmd_t *pmd, unsigned long addr) · line 2148

Quoted 10 lines
include/linux/pgtable.h +10 -0
+ *
+ * pte_set_huge() only has to be implemented by architectures whose
+ * arch_vmap_pte_range_map_size() can return a size other than PAGE_SIZE.
+ */
+#ifndef __HAVE_ARCH_PTE_SET_HUGE
+static inline void pte_set_huge(pte_t *ptep, unsigned long addr,
+                             phys_addr_t phys, pgprot_t prot,
+                             unsigned long size)
+{
+     WARN_ON_ONCE(1);

BUILD_BUG_ON() would be better here.

It should be possible because fallback arch_vmap_pte_range_map_size() will constant-fold PAGE_SIZE so pte_set_huge() will never be called.

Quoted 8 lines
Quoted 4 lines

Agreed. These fallbacks exist only to keep the build working on architectures with PTE-level block mappings and should never actually be reached, so BUILD_BUG_ON() is right. I'll make that change in v9.

I am not quite sure. It won't be called at runtime because vmap size/unmap size return PAGE_SIZE, so the code won't reach this branch. But it will still be built.

The fallbacks are defined as:

#ifndef arch_vmap_pte_range_map_size
static inline unsigned long arch_vmap_pte_range_map_size(unsigned long addr, unsigned long end,
u64 pfn, unsigned int max_page_shift)

{
	return PAGE_SIZE;
}

#endif

#ifndef arch_vmap_pte_range_unmap_size
static inline unsigned long arch_vmap_pte_range_unmap_size(unsigned long addr,
pte_t *ptep)

{
	return PAGE_SIZE;
}

#endif

Therefore in:

                size = arch_vmap_pte_range_unmap_size(addr, pte);
                if (size != PAGE_SIZE) {

GCC knows 'size' is const and its value is PAGE_SIZE, so it won't emit the branch at all.

It is call constant folding, some explanation here: https://en.wikipedia.org/wiki/Constant_folding

Quoted 2 lines

So, would a BUILD_BUG_ON() trigger a build failure here?

It shouldn't, if it does it is a compiled bug or this is because someone has redefined arch_vmap_pte_range_map_size() and not pte_set_huge() which we'd better know at build time rather than at runtime.

Quoted 4 lines

Best Regards
Barry

reviewer Reviewed-by on v8 2/10
#7 lore raw

Re: [PATCH v8 02/10] powerpc/8xx: add pte_set_huge()

In reply to Wen Jiang

Le 17/09/2026 à 07:29, Wen Jiang a écrit :

Quoted 13 lines

From: Wen Jiang <[email protected]>

vmap installs PTE-level block mappings (SZ_16K/SZ_512K on powerpc/8xx) by reusing set_huge_pte_at(), a HugeTLB helper gated by CONFIG_HUGETLB_PAGE. This makes the feature silently unavailable on CONFIG_HUGETLB_PAGE=n kernels and couples mm/vmalloc.c to HugeTLB internals it does not otherwise need.

Add pte_set_huge() to powerpc/8xx, next to the existing pmd_clear_huge()/pud_clear_huge() in mm/nohash/8xx.c. It builds the huge PTE and writes it into the backing cells directly, without going through set_huge_pte_at(), so it does not depend on CONFIG_HUGETLB_PAGE.

Quoted 7 lines

No pte_clear_huge() is needed: 8xx does not implement arch_vmap_pte_range_unmap_size(), so the vmap unmap path never takes the block-mapping branch.

In practice PPC_8xx selects HUGETLBFS unconditionally, so CONFIG_HUGETLB_PAGE=n does not occur there today; this is a decoupling cleanup rather than a new configuration.

8xx selects CONFIG_HUGETLB_PAGE because it uses set_huge_pte_at() to map kernel text with large pages. That was added by commit 34536d780683 ("powerpc/8xx: Add a function to early map kernel via huge pages")

But now pte_set_huge() could be used instead in __early_map_kernel_hugepage() and then CONFIG_HUGETLB_PAGE could be removed.

Quoted 5 lines

There is no caller yet: mm/vmalloc.c is converted later in this series, once the generic fallbacks are in place.

Quoted 57 lines
---
  arch/powerpc/include/asm/nohash/32/pte-8xx.h |  4 +++
  arch/powerpc/mm/nohash/8xx.c                 | 29 ++++++++++++++++++++
  2 files changed, 33 insertions(+)
arch/powerpc/include/asm/nohash/32/pte-8xx.h +4 -0
index e2ea8ba9f8cae..1ee3f8f0de974 100644
@@ -235,6 +235,10 @@ static inline pte_t ptep_get(pte_t *ptep)
  }
  #endif /* CONFIG_PPC_16K_PAGES */
 
+#define __HAVE_ARCH_PTE_SET_HUGE
+void pte_set_huge(pte_t *ptep, unsigned long addr, phys_addr_t phys,
+		  pgprot_t prot, unsigned long size);
+
  #endif
 
  #endif /* __KERNEL__ */
arch/powerpc/mm/nohash/8xx.c +29 -0
index a9d3f4729eada..2518d2a6bdb41 100644
@@ -220,3 +220,32 @@ int pmd_clear_huge(pmd_t *pmd)
  {
  	 return 0;
  }
+
+/*
+ * vmap PTE-level block mapping. Sets a present kernel mapping directly,
+ * so unlike set_huge_pte_at() it needs neither CONFIG_HUGETLB_PAGE nor
+ * set_pte_filter() (a no-op for non-exec kernel mappings).
+ */
+void pte_set_huge(pte_t *ptep, unsigned long addr, phys_addr_t phys,
+		  pgprot_t prot, unsigned long size)
+{
+	pmd_t *pmdp = pmd_off(&init_mm, addr);
+	pte_t pte = pfn_pte(PHYS_PFN(phys), prot);
+	pte_basic_t val;
+	pte_basic_t *entry = (pte_basic_t *)ptep;
+	int num, i;
+
+	pte = arch_make_huge_pte(pte, ilog2(size), 0);
+	val = pte_val(pte);
+
+	/*
+	 * Make sure hardware valid bit is not set. We don't do
+	 * tlb flush for this update.
+	 */
+	VM_WARN_ON(pte_hw_valid(*ptep) && !pte_protnone(*ptep));
+
+	num = number_of_cells_per_pte(pmdp, val, 1);
+
+	for (i = 0; i < num; i++, entry++, val += SZ_4K)
+		*entry = val;
+}
#15 lore raw

Re: [PATCH v8 03/10] mm/vmalloc: use pte_set_huge()/pte_clear_huge() for PTE-level block mappings

In reply to Christophe Leroy (CS GROUP)
Quoted 5 lines · Christophe Leroy (CS GROUP)
Quoted 1 line · Barry Song

On Thu, Sep 17, 2026 at 10:41 PM Wen Jiang <[email protected]> wrote:

[...]

Quoted 69 lines
Quoted 28 lines
Quoted 28 lines
Quoted 27 lines
Quoted 11 lines
include/linux/pgtable.h +2 -0
index cdd68ed3ae1a9..349ced999f959 100644
@@ -2134,6 +2134,35 @@ static inline int pmd_free_pte_page(pmd_t *pmd, unsigned long addr)
   }
   #endif      /* CONFIG_HAVE_ARCH_HUGE_VMAP */
 
+/*
+ * PTE-level block mappings for vmap.

include/linux/pgtable.h static inline int pmd_free_pte_page(pmd_t *pmd, unsigned long addr) · line 2148

Quoted 10 lines
include/linux/pgtable.h +10 -0
+ *
+ * pte_set_huge() only has to be implemented by architectures whose
+ * arch_vmap_pte_range_map_size() can return a size other than PAGE_SIZE.
+ */
+#ifndef __HAVE_ARCH_PTE_SET_HUGE
+static inline void pte_set_huge(pte_t *ptep, unsigned long addr,
+                             phys_addr_t phys, pgprot_t prot,
+                             unsigned long size)
+{
+     WARN_ON_ONCE(1);

BUILD_BUG_ON() would be better here.

It should be possible because fallback arch_vmap_pte_range_map_size() will constant-fold PAGE_SIZE so pte_set_huge() will never be called.

Quoted 8 lines
Quoted 4 lines

Agreed. These fallbacks exist only to keep the build working on architectures with PTE-level block mappings and should never actually be reached, so BUILD_BUG_ON() is right. I'll make that change in v9.

I am not quite sure. It won't be called at runtime because vmap size/unmap size return PAGE_SIZE, so the code won't reach this branch. But it will still be built.

The fallbacks are defined as:

#ifndef arch_vmap_pte_range_map_size
static inline unsigned long arch_vmap_pte_range_map_size(unsigned long
addr, unsigned long end,
u64 pfn, unsigned int max_page_shift)

{
        return PAGE_SIZE;
}

#endif

#ifndef arch_vmap_pte_range_unmap_size
static inline unsigned long arch_vmap_pte_range_unmap_size(unsigned long addr,
pte_t *ptep)

{
        return PAGE_SIZE;
}

#endif

Therefore in:

                 size = arch_vmap_pte_range_unmap_size(addr, pte);
                 if (size != PAGE_SIZE) {

GCC knows 'size' is const and its value is PAGE_SIZE, so it won't emit the branch at all.

It is call constant folding, some explanation here:
https://en.wikipedia.org/wiki/Constant_folding

Quoted 1 line
Quoted 5 lines
Quoted 1 line

So, would a BUILD_BUG_ON() trigger a build failure here?

It shouldn't, if it does it is a compiled bug or this is because someone has redefined arch_vmap_pte_range_map_size() and not pte_set_huge() which we'd better know at build time rather than at runtime.

Thanks, Christophe. I was also thinking about compiler optimization. I was just a bit worried that we're touching the common MM code, which affects almost all architectures, so I'm not quite sure whether this is supported by all GCC versions used by those architectures.

If it is supported by all of them, I agree that BUILD_BUG_ON() is a perfect approach.

Best Regards
Barry

#16 lore raw

Re: [PATCH v8 03/10] mm/vmalloc: use pte_set_huge()/pte_clear_huge() for PTE-level block mappings

Le 18/09/2026 à 08:14, Barry Song a écrit :

Quoted 84 lines
Quoted 5 lines · Christophe Leroy (CS GROUP)
Quoted 1 line · Barry Song

On Thu, Sep 17, 2026 at 10:41 PM Wen Jiang <[email protected]> wrote:

[...]

Quoted 69 lines
Quoted 28 lines
Quoted 28 lines
Quoted 27 lines
Quoted 11 lines
include/linux/pgtable.h +2 -0
index cdd68ed3ae1a9..349ced999f959 100644
@@ -2134,6 +2134,35 @@ static inline int pmd_free_pte_page(pmd_t *pmd, unsigned long addr)
    }
    #endif      /* CONFIG_HAVE_ARCH_HUGE_VMAP */
 
+/*
+ * PTE-level block mappings for vmap.

include/linux/pgtable.h static inline int pmd_free_pte_page(pmd_t *pmd, unsigned long addr) · line 2148

Quoted 10 lines
include/linux/pgtable.h +10 -0
+ *
+ * pte_set_huge() only has to be implemented by architectures whose
+ * arch_vmap_pte_range_map_size() can return a size other than PAGE_SIZE.
+ */
+#ifndef __HAVE_ARCH_PTE_SET_HUGE
+static inline void pte_set_huge(pte_t *ptep, unsigned long addr,
+                             phys_addr_t phys, pgprot_t prot,
+                             unsigned long size)
+{
+     WARN_ON_ONCE(1);

BUILD_BUG_ON() would be better here.

It should be possible because fallback arch_vmap_pte_range_map_size() will constant-fold PAGE_SIZE so pte_set_huge() will never be called.

Quoted 8 lines
Quoted 4 lines

Agreed. These fallbacks exist only to keep the build working on architectures with PTE-level block mappings and should never actually be reached, so BUILD_BUG_ON() is right. I'll make that change in v9.

I am not quite sure. It won't be called at runtime because vmap size/unmap size return PAGE_SIZE, so the code won't reach this branch. But it will still be built.

The fallbacks are defined as:

#ifndef arch_vmap_pte_range_map_size
static inline unsigned long arch_vmap_pte_range_map_size(unsigned long
addr, unsigned long end,
u64 pfn, unsigned int max_page_shift)

{
         return PAGE_SIZE;
}

#endif

#ifndef arch_vmap_pte_range_unmap_size
static inline unsigned long arch_vmap_pte_range_unmap_size(unsigned long addr,
pte_t *ptep)

{
         return PAGE_SIZE;
}

#endif

Therefore in:

                  size = arch_vmap_pte_range_unmap_size(addr, pte);
                  if (size != PAGE_SIZE) {

GCC knows 'size' is const and its value is PAGE_SIZE, so it won't emit the branch at all.

It is call constant folding, some explanation here:
https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FConstant_folding&data=05%7C02%7Cchristophe.leroy%40csgroup.eu%7C869dac21f28d45988a3d08df154c225a%7C8b87af7d86474dc78df45f69a2011bb5%7C0%7C0%7C639253088878897551%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=Dvtz1%2FAHvAbVYO%2BRaHTOizcD%2FclJ9FqTcWXKMhIFVyw%3D&reserved=0

Quoted 1 line
Quoted 5 lines
Quoted 1 line

So, would a BUILD_BUG_ON() trigger a build failure here?

It shouldn't, if it does it is a compiled bug or this is because someone has redefined arch_vmap_pte_range_map_size() and not pte_set_huge() which we'd better know at build time rather than at runtime.

Quoted 7 lines

Thanks, Christophe. I was also thinking about compiler optimization. I was just a bit worried that we're touching the common MM code, which affects almost all architectures, so I'm not quite sure whether this is supported by all GCC versions used by those architectures.

If it is supported by all of them, I agree that BUILD_BUG_ON() is a perfect approach.

AFAIU this is the assumption made by the kernel, see https://docs.kernel.org/process/coding-style.html#conditional-compilation

This is the same compiler, I see no reason why ability to constant-fold would be dependant on architecture.

Christophe

author
#3 lore raw

Re: [PATCH v8 01/10] arm64/mm: add pte_set_huge() and pte_clear_huge()

In reply to Barry Song
Quoted 41 lines · Barry Song
Quoted 14 lines · Wen Jiang

From: Wen Jiang <[email protected]>

vmap installs PTE-level block mappings (PTE_CONT on arm64) by reusing set_huge_pte_at() and huge_ptep_get_and_clear(), which are HugeTLB helpers gated by CONFIG_HUGETLB_PAGE. This makes the feature silently unavailable on CONFIG_HUGETLB_PAGE=n kernels and couples mm/vmalloc.c to HugeTLB internals it does not otherwise need.

Add pte_set_huge()/pte_clear_huge() to arm64, joining the existing pmd_set_huge()/pud_set_huge() family in mm/mmu.c. They build on __set_ptes() and __get_and_clear_full_ptes() directly, so they do not depend on CONFIG_HUGETLB_PAGE.

Quoted 4 lines

There is no caller yet: mm/vmalloc.c is converted later in this series, once the generic fallbacks are in place.

I tried to apply the patch to mm-new, but it doesn't apply.

% git am raw
Applying: arm64/mm: add pte_set_huge() and pte_clear_huge()
error: patch failed: arch/arm64/mm/mmu.c:1872
error: arch/arm64/mm/mmu.c: patch does not apply
Patch failed at 0001 arm64/mm: add pte_set_huge() and pte_clear_huge()
hint: Use 'git am --show-current-patch=diff' to see the failed patch When you have resolved this problem, run "git am --continue". If you prefer to skip this patch, run "git am --skip" instead. To restore the original branch and stop patching, run "git am --abort".

% patch -p1 < raw
patching file 'arch/arm64/include/asm/pgtable.h'
patching file 'arch/arm64/mm/mmu.c'
1 out of 1 hunks failed--saving rejects to 'arch/arm64/mm/mmu.c.rej'

[...]

Quoted 8 lines

arch/arm64/mm/mmu.c int pmd_clear_huge(pmd_t *pmdp) · line 1879

Quoted 5 lines
arch/arm64/mm/mmu.c +5 -0
+void pte_set_huge(pte_t *ptep, unsigned long addr, phys_addr_t phys,
+                 pgprot_t prot, unsigned long size)
+{
+       unsigned long pfn = __phys_to_pfn(phys);
+       pte_t new_pte = pte_mkcont(pfn_pte(pfn, prot));

Maybe we can drop the new_ prefix.

Will do.

Quoted 7 lines

arch/arm64/mm/mmu.c int pmd_clear_huge(pmd_t *pmdp) · line 1883

Quoted 4 lines
arch/arm64/mm/mmu.c +4 -0
+       unsigned int nr = size >> PAGE_SHIFT;
+
+       VM_WARN_ON(!IS_ALIGNED(pfn, CONT_PTES));
+       VM_WARN_ON(!IS_ALIGNED(size, CONT_PTE_SIZE));

Do we also need to check that addr is aligned?

Yes. arch_vmap_pte_range_map_size() already guarantees all three, since we are already checking two of them here, we should complete the set. I'll add it in v9.

Thanks,
Wen

author
#6 lore raw

Re: [PATCH v8 02/10] powerpc/8xx: add pte_set_huge()

In reply to Barry Song
Quoted 16 lines · Barry Song
Quoted 14 lines · Wen Jiang

From: Wen Jiang <[email protected]>

vmap installs PTE-level block mappings (SZ_16K/SZ_512K on powerpc/8xx) by reusing set_huge_pte_at(), a HugeTLB helper gated by CONFIG_HUGETLB_PAGE. This makes the feature silently unavailable on CONFIG_HUGETLB_PAGE=n kernels and couples mm/vmalloc.c to HugeTLB internals it does not otherwise need.

Add pte_set_huge() to powerpc/8xx, next to the existing pmd_clear_huge()/pud_clear_huge() in mm/nohash/8xx.c. It builds the huge PTE and writes it into the backing cells directly, without going through set_huge_pte_at(), so it does not depend on CONFIG_HUGETLB_PAGE.

Quoted 7 lines
Quoted 3 lines

No pte_clear_huge() is needed: 8xx does not implement arch_vmap_pte_range_unmap_size(), so the vmap unmap path never takes the block-mapping branch.

I assume you mean the block-unmapping (or PTE-clearing) branch. Please make this clear in the changelog.

Sure. Will fix it.

Thanks,
Wen

Quoted 15 lines
Quoted 10 lines

In practice PPC_8xx selects HUGETLBFS unconditionally, so CONFIG_HUGETLB_PAGE=n does not occur there today; this is a decoupling cleanup rather than a new configuration.

There is no caller yet: mm/vmalloc.c is converted later in this series, once the generic fallbacks are in place.

---

BTW, this patch applies cleanly to the mm-new branch.

Best Regards
Barry

author
#17 lore raw

Re: [PATCH v8 03/10] mm/vmalloc: use pte_set_huge()/pte_clear_huge() for PTE-level block mappings

In reply to Christophe Leroy (CS GROUP)
Quoted 96 lines · Christophe Leroy (CS GROUP)
Quoted 84 lines · Barry Song
Quoted 5 lines · Christophe Leroy (CS GROUP)
Quoted 1 line · Barry Song

On Thu, Sep 17, 2026 at 10:41 PM Wen Jiang <[email protected]> wrote:

[...]

Quoted 69 lines
Quoted 28 lines
Quoted 28 lines
Quoted 27 lines
Quoted 11 lines
include/linux/pgtable.h +2 -0
index cdd68ed3ae1a9..349ced999f959 100644
@@ -2134,6 +2134,35 @@ static inline int pmd_free_pte_page(pmd_t *pmd, unsigned long addr)
    }
    #endif      /* CONFIG_HAVE_ARCH_HUGE_VMAP */
 
+/*
+ * PTE-level block mappings for vmap.

include/linux/pgtable.h static inline int pmd_free_pte_page(pmd_t *pmd, unsigned long addr) · line 2148

Quoted 10 lines
include/linux/pgtable.h +10 -0
+ *
+ * pte_set_huge() only has to be implemented by architectures whose
+ * arch_vmap_pte_range_map_size() can return a size other than PAGE_SIZE.
+ */
+#ifndef __HAVE_ARCH_PTE_SET_HUGE
+static inline void pte_set_huge(pte_t *ptep, unsigned long addr,
+                             phys_addr_t phys, pgprot_t prot,
+                             unsigned long size)
+{
+     WARN_ON_ONCE(1);

BUILD_BUG_ON() would be better here.

It should be possible because fallback arch_vmap_pte_range_map_size() will constant-fold PAGE_SIZE so pte_set_huge() will never be called.

Quoted 8 lines
Quoted 4 lines

Agreed. These fallbacks exist only to keep the build working on architectures with PTE-level block mappings and should never actually be reached, so BUILD_BUG_ON() is right. I'll make that change in v9.

I am not quite sure. It won't be called at runtime because vmap size/unmap size return PAGE_SIZE, so the code won't reach this branch. But it will still be built.

The fallbacks are defined as:

#ifndef arch_vmap_pte_range_map_size
static inline unsigned long arch_vmap_pte_range_map_size(unsigned long
addr, unsigned long end,
u64 pfn, unsigned int max_page_shift)

{
         return PAGE_SIZE;
}

#endif

#ifndef arch_vmap_pte_range_unmap_size
static inline unsigned long arch_vmap_pte_range_unmap_size(unsigned long addr,
pte_t *ptep)

{
         return PAGE_SIZE;
}

#endif

Therefore in:

                  size = arch_vmap_pte_range_unmap_size(addr, pte);
                  if (size != PAGE_SIZE) {

GCC knows 'size' is const and its value is PAGE_SIZE, so it won't emit the branch at all.

It is call constant folding, some explanation here:
https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FConstant_folding&data=05%7C02%7Cchristophe.leroy%40csgroup.eu%7C869dac21f28d45988a3d08df154c225a%7C8b87af7d86474dc78df45f69a2011bb5%7C0%7C0%7C639253088878897551%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=Dvtz1%2FAHvAbVYO%2BRaHTOizcD%2FclJ9FqTcWXKMhIFVyw%3D&reserved=0

Quoted 1 line
Quoted 5 lines
Quoted 1 line

So, would a BUILD_BUG_ON() trigger a build failure here?

It shouldn't, if it does it is a compiled bug or this is because someone has redefined arch_vmap_pte_range_map_size() and not pte_set_huge() which we'd better know at build time rather than at runtime.

Quoted 7 lines

Thanks, Christophe. I was also thinking about compiler optimization. I was just a bit worried that we're touching the common MM code, which affects almost all architectures, so I'm not quite sure whether this is supported by all GCC versions used by those architectures.

If it is supported by all of them, I agree that BUILD_BUG_ON() is a perfect approach.

Quoted 7 lines

AFAIU this is the assumption made by the kernel, see
https://docs.kernel.org/process/coding-style.html#conditional-compilation

This is the same compiler, I see no reason why ability to constant-fold would be dependant on architecture.

Christophe

Hi Barry and Christophe,

pgtable.h already does this in a few places, the !THP fallback of pmdp_clear_flush_young() is just BUILD_BUG(), and its caller in mm/rmap.c, a file that is built unconditionally, is guarded by IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) rather than #ifdef. So on every THP=n build, on every architecture, that call has to be eliminated by the compiler or the build breaks.

I built it with BUILD_BUG() on x86_64, arm64 and powerpc (8xx) to be sure, and all three are clean.

It will be BUILD_BUG() rather than BUILD_BUG_ON(1): BUILD_BUG_ON() expects a condition the compiler should know is false, while BUILD_BUG() is documented as the way to flag code that is expected to be eliminated at build time.

Thanks,
Wen

#8 lore raw

Re: [PATCH v8 02/10] powerpc/8xx: add pte_set_huge()

In reply to Christophe Leroy (CS GROUP)
Quoted 25 lines · Christophe Leroy (CS GROUP)
Quoted 13 lines · Wen Jiang

From: Wen Jiang <[email protected]>

vmap installs PTE-level block mappings (SZ_16K/SZ_512K on powerpc/8xx) by reusing set_huge_pte_at(), a HugeTLB helper gated by CONFIG_HUGETLB_PAGE. This makes the feature silently unavailable on CONFIG_HUGETLB_PAGE=n kernels and couples mm/vmalloc.c to HugeTLB internals it does not otherwise need.

Add pte_set_huge() to powerpc/8xx, next to the existing pmd_clear_huge()/pud_clear_huge() in mm/nohash/8xx.c. It builds the huge PTE and writes it into the backing cells directly, without going through set_huge_pte_at(), so it does not depend on CONFIG_HUGETLB_PAGE.

Quoted 7 lines

No pte_clear_huge() is needed: 8xx does not implement arch_vmap_pte_range_unmap_size(), so the vmap unmap path never takes the block-mapping branch.

In practice PPC_8xx selects HUGETLBFS unconditionally, so CONFIG_HUGETLB_PAGE=n does not occur there today; this is a decoupling cleanup rather than a new configuration.

Quoted 6 lines

8xx selects CONFIG_HUGETLB_PAGE because it uses set_huge_pte_at() to map kernel text with large pages. That was added by commit 34536d780683 ("powerpc/8xx: Add a function to early map kernel via huge pages")

But now pte_set_huge() could be used instead in __early_map_kernel_hugepage() and then CONFIG_HUGETLB_PAGE could be removed.

Hi Christophe,

Do you mean the code below? If so, it sounds like a good place to clean this up for powerpc as a separate cleanup patch.

arch/powerpc/mm/nohash/8xx.c +0 -0
index 2518d2a6bdb4..830cdb391161 100644
@@ -87,9 +87,7 @@ static int __ref

__early_map_kernel_hugepage(unsigned long va, phys_addr_t pa,

        if (WARN_ON(!ptep))
                return -ENOMEM;

- set_huge_pte_at(&init_mm, va, ptep,
- arch_make_huge_pte(pfn_pte(pa >> PAGE_SHIFT,
prot), shift, 0),
- 1UL << shift);
+ pte_set_huge(ptep, va, pa, prot, 1UL << shift);

        return 0;

}

(quoted diff) +0 -0

b/arch/powerpc/platforms/Kconfig.cputype
index f0adbf7ecb7f..efba4d960083 100644

arch/powerpc/platforms/Kconfig.cputype +0 -1
@@ -41,7 +41,6 @@ config PPC_8xx
        select FSL_SOC
        select PPC_KUEP
        select HAVE_ARCH_VMAP_STACK
-       select HUGETLBFS
 
 config 44x
        bool "AMCC 44x, 46x or 47x"
Quoted 8 lines
Quoted 6 lines

There is no caller yet: mm/vmalloc.c is converted later in this series, once the generic fallbacks are in place.

Best Regards
Barry

#9 lore raw

Re: [PATCH v8 02/10] powerpc/8xx: add pte_set_huge()

Le 18/09/2026 à 10:44, Barry Song a écrit :

Quoted 31 lines
Quoted 29 lines · Christophe Leroy (CS GROUP)
Quoted 13 lines · Wen Jiang

From: Wen Jiang <[email protected]>

vmap installs PTE-level block mappings (SZ_16K/SZ_512K on powerpc/8xx) by reusing set_huge_pte_at(), a HugeTLB helper gated by CONFIG_HUGETLB_PAGE. This makes the feature silently unavailable on CONFIG_HUGETLB_PAGE=n kernels and couples mm/vmalloc.c to HugeTLB internals it does not otherwise need.

Add pte_set_huge() to powerpc/8xx, next to the existing pmd_clear_huge()/pud_clear_huge() in mm/nohash/8xx.c. It builds the huge PTE and writes it into the backing cells directly, without going through set_huge_pte_at(), so it does not depend on CONFIG_HUGETLB_PAGE.

Quoted 7 lines

No pte_clear_huge() is needed: 8xx does not implement arch_vmap_pte_range_unmap_size(), so the vmap unmap path never takes the block-mapping branch.

In practice PPC_8xx selects HUGETLBFS unconditionally, so CONFIG_HUGETLB_PAGE=n does not occur there today; this is a decoupling cleanup rather than a new configuration.

8xx selects CONFIG_HUGETLB_PAGE because it uses set_huge_pte_at() to map kernel text with large pages. That was added by commit 34536d780683 ("powerpc/8xx: Add a function to early map kernel via huge pages")

Quoted 7 lines
Quoted 2 lines

But now pte_set_huge() could be used instead in __early_map_kernel_hugepage() and then CONFIG_HUGETLB_PAGE could be removed.

Hi Christophe,

Do you mean the code below? If so, it sounds like a good place to clean this up for powerpc as a separate cleanup patch.

Yes this is what I have in mind allthough I have not studied it in details.

Quoted 43 lines
arch/powerpc/mm/nohash/8xx.c +1 -3
index 2518d2a6bdb4..830cdb391161 100644
@@ -87,9 +87,7 @@ static int __ref
 __early_map_kernel_hugepage(unsigned long va, phys_addr_t pa,
         if (WARN_ON(!ptep))
                 return -ENOMEM;
 
-       set_huge_pte_at(&init_mm, va, ptep,
-                       arch_make_huge_pte(pfn_pte(pa >> PAGE_SHIFT,
 prot), shift, 0),
-                       1UL << shift);
+       pte_set_huge(ptep, va, pa, prot, 1UL << shift);
 
         return 0;

}

arch/powerpc/platforms/Kconfig.cputype +0 -1
index f0adbf7ecb7f..efba4d960083 100644
@@ -41,7 +41,6 @@ config PPC_8xx
         select FSL_SOC
         select PPC_KUEP
         select HAVE_ARCH_VMAP_STACK
-       select HUGETLBFS
 
  config 44x
         bool "AMCC 44x, 46x or 47x"
Quoted 9 lines
Quoted 7 lines

There is no caller yet: mm/vmalloc.c is converted later in this series, once the generic fallbacks are in place.

Best Regards
Barry