mm/vmalloc: Speed up ioremap, vmalloc and vmap with contiguous memory
Subsystems (3)
v3 cover mm/vmalloc: Speed up ioremap, vmalloc and vmap with contiguous memory
From: jiangwen6 <[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-2 extend ARM64 vmalloc CONT-PTE mapping to support multiple CONT-PTE regions instead of just one.
Patch 3 extracts a common helper vmap_set_ptes() that consolidates PTE mapping logic between the ioremap and vmalloc/vmap paths, handling both CONT_PTE and regular PTE mappings. This prepares for the next patch.
Patch 4 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().
Patches 5-6 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.53us)
* 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.
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) (5):
arm64/hugetlb: Extend batching of multiple CONT_PTE in a single PTE
setup
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 (1):
mm/vmalloc: Extract vmap_set_ptes() to consolidate PTE mapping logic
arch/arm64/include/asm/vmalloc.h | 6 +- arch/arm64/mm/hugetlbpage.c | 10 ++ mm/vmalloc.c | 235 ++++++++++++++++++++++++------- 3 files changed, 201 insertions(+), 50 deletions(-)
Signature · 1 line
2.34.1
PATCH v3 1/6 arm64/hugetlb: Extend batching of multiple CONT_PTE in a single PTE setup
From: "Barry Song (Xiaomi)" <[email protected]>
For sizes aligned to CONT_PTE_SIZE and smaller than PMD_SIZE, we can batch CONT_PTE settings instead of handling them individually.
--- arch/arm64/mm/hugetlbpage.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
arch/arm64/mm/hugetlbpage.c +10 -0
@@ -110,6 +110,12 @@ static inline int num_contig_ptes(unsigned long size, size_t *pgsize) contig_ptes =CONT_PTES ;break ;default : +if (size >0 && size <PMD_SIZE && +IS_ALIGNED (size,CONT_PTE_SIZE )) { + contig_ptes = size >>PAGE_SHIFT ; + *pgsize =PAGE_SIZE ; +break ; + }WARN_ON (!__hugetlb_valid_size (size)); }
@@ -359,6 +365,10 @@ pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags)case CONT_PTE_SIZE :return pte_mkcont (entry);default : +if (pagesize >0 && pagesize <PMD_SIZE && +IS_ALIGNED (pagesize,CONT_PTE_SIZE )) +return pte_mkcont (entry); +break ; } pr_warn("%s: unrecognized huge page size 0x%lx\n",
Signature · 1 line
2.34.1
Re: [PATCH v3 1/6] arm64/hugetlb: Extend batching of multiple CONT_PTE in a single PTE setup
Quoted 4 lines · Wen Jiang
From: "Barry Song (Xiaomi)" <[email protected]>
For sizes aligned to
CONT_PTE_SIZEand smaller thanPMD_SIZE, we can batchCONT_PTEsettings instead of handling them individually.
Better wording: "we can handle CONT_PTE_SIZE groups together"
Quoted 36 lines
Signed-off-by Barry Song (Xiaomi) <[email protected]>Signed-off-by Wen Jiang <[email protected]>Tested-by Xueyuan Chen <[email protected]>--- arch/arm64/mm/hugetlbpage.c | 10 ++++++++++ 1 file changed, 10 insertions(+)arch/arm64/mm/hugetlbpage.c +10
-0@@ -110,6 +110,12 @@ static inline int num_contig_ptes(unsigned long size, size_t *pgsize) contig_ptes =CONT_PTES ;break ;default : +if (size >0 && size <PMD_SIZE && +IS_ALIGNED (size,CONT_PTE_SIZE )) { + contig_ptes = size >>PAGE_SHIFT ; + *pgsize =PAGE_SIZE ; +break ; + }WARN_ON (!__hugetlb_valid_size (size)); }@@ -359,6 +365,10 @@ pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags)case CONT_PTE_SIZE :return pte_mkcont (entry);default : +if (pagesize >0 && pagesize <PMD_SIZE && +IS_ALIGNED (pagesize,CONT_PTE_SIZE )) +return pte_mkcont (entry); +break ; } pr_warn("%s: unrecognized huge page size 0x%lx\n",
PATCH v3 2/6 arm64/vmalloc: Allow arch_vmap_pte_range_map_size to batch multiple CONT_PTE
In reply to Wen JiangFrom: "Barry Song (Xiaomi)" <[email protected]>
Allow arch_vmap_pte_range_map_size to batch multiple CONT_PTE hugepages, reducing both PTE setup and TLB flush iterations.
--- arch/arm64/include/asm/vmalloc.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
arch/arm64/include/asm/vmalloc.h +5 -1
@@ -23,6 +23,8 @@ 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
@@ -40,7 +42,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 =1UL <<__fls (size); +return size; }#define arch_vmap_pte_range_unmap_size arch_vmap_pte_range_unmap_size
Signature · 1 line
2.34.1
Re: [PATCH v3 2/6] arm64/vmalloc: Allow arch_vmap_pte_range_map_size to batch multiple CONT_PTE
Quoted 3 lines · Wen Jiang
From: "Barry Song (Xiaomi)" <[email protected]>
Allow
arch_vmap_pte_range_map_sizeto batch multipleCONT_PTEhugepages,
..."to batch across multiple CONT_PTE blocks"
Quoted 33 lines
reducing both PTE setup and TLB flush iterations.
Signed-off-by Barry Song (Xiaomi) <[email protected]>Signed-off-by Wen Jiang <[email protected]>Tested-by Xueyuan Chen <[email protected]>--- arch/arm64/include/asm/vmalloc.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)arch/arm64/include/asm/vmalloc.h +5
-1@@ -23,6 +23,8 @@ 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 @@ -40,7 +42,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 =1UL <<__fls (size); +return size; }#define arch_vmap_pte_range_unmap_size arch_vmap_pte_range_unmap_size
PATCH v3 3/6 mm/vmalloc: Extract vmap_set_ptes() to consolidate PTE mapping logic
In reply to Wen JiangExtract 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.
The #ifdef CONFIG_HUGETLB_PAGE guard is moved inside vmap_set_ptes(), so callers no longer need to handle the conditional compilation.
--- mm/vmalloc.c | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-)
mm/vmalloc.c +34 -15
@@ -91,6 +91,35 @@ struct vfree_deferred { static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred);/*** Page table manipulation functions ***/ + +/* +* Set PTE mappings for the given PFN. Try CONT_PTE mappings first when +* supported, otherwise fall back to PAGE_SIZE mappings. +* +* Return: mapping size. +*/ +static __always_inline unsigned long vmap_set_ptes (pte_t *pte, +unsigned long addr,unsigned long end,u64 pfn, +pgprot_t prot,unsigned int max_page_shift) +{ +#ifdef CONFIG_HUGETLB_PAGE +if (max_page_shift >PAGE_SHIFT ) { +unsigned long size; + + 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); +return size; + } + } +#endif +set_pte_at (&init_mm, addr, pte,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,7 +127,8 @@ 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; +unsigned int steps;if (WARN_ON_ONCE (!PAGE_ALIGNED (end - addr)))return -EINVAL ;
@@ -119,20 +149,9 @@ 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); - 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); + 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
Uladzislau Rezki
maintainerRe: [PATCH v3 3/6] mm/vmalloc: Extract vmap_set_ptes() to consolidate PTE mapping logic
Quoted 91 lines · Wen Jiang
Extract the common PTE mapping logic from
vmap_pte_range()into a shared helpervmap_set_ptes(). This handles bothCONT_PTEand regular PTE mappings in a single function, preparing for the next patch which will extendvmap_pages_pte_range()to also use this helper.The #ifdef
CONFIG_HUGETLB_PAGEguard is moved insidevmap_set_ptes(), so callers no longer need to handle the conditional compilation.Signed-off-by Wen Jiang <[email protected]>Tested-by Xueyuan Chen <[email protected]>--- mm/vmalloc.c | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-)mm/vmalloc.c +34
-15@@ -91,6 +91,35 @@ struct vfree_deferred { static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred);/*** Page table manipulation functions ***/ + +/* +* Set PTE mappings for the given PFN. Try CONT_PTE mappings first when +* supported, otherwise fall back to PAGE_SIZE mappings. +* +* Return: mapping size. +*/ +static __always_inline unsigned long vmap_set_ptes (pte_t *pte, +unsigned long addr,unsigned long end,u64 pfn, +pgprot_t prot,unsigned int max_page_shift) +{ +#ifdef CONFIG_HUGETLB_PAGE +if (max_page_shift >PAGE_SHIFT ) { +unsigned long size; + + 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); +return size; + } + } +#endif +set_pte_at (&init_mm, addr, pte,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,7 +127,8 @@ 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; +unsigned int steps;if (WARN_ON_ONCE (!PAGE_ALIGNED (end - addr)))return -EINVAL ;@@ -119,20 +149,9 @@ 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); - 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); + 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
IMO, we should add just a helper with "no functional change" and second patch will extend it.
Otherwise you added a helper which has already been slightly modified.
--
Uladzislau Rezki
Re: [PATCH v3 3/6] mm/vmalloc: Extract vmap_set_ptes() to consolidate PTE mapping logic
Quoted 96 lines · Uladzislau Rezki
Quoted 89 lines · Wen Jiang
Extract the common PTE mapping logic from
vmap_pte_range()into a shared helpervmap_set_ptes(). This handles bothCONT_PTEand regular PTE mappings in a single function, preparing for the next patch which will extendvmap_pages_pte_range()to also use this helper.The #ifdef
CONFIG_HUGETLB_PAGEguard is moved insidevmap_set_ptes(), so callers no longer need to handle the conditional compilation.Signed-off-by Wen Jiang <[email protected]>Tested-by Xueyuan Chen <[email protected]>--- mm/vmalloc.c | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-)mm/vmalloc.c +34
-16@@ -91,6 +91,35 @@ struct vfree_deferred { static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred);/*** Page table manipulation functions ***/ + +/* +* Set PTE mappings for the given PFN. Try CONT_PTE mappings first when +* supported, otherwise fall back to PAGE_SIZE mappings. +* +* Return: mapping size. +*/ +static __always_inline unsigned long vmap_set_ptes (pte_t *pte, +unsigned long addr,unsigned long end,u64 pfn, +pgprot_t prot,unsigned int max_page_shift) +{ +#ifdef CONFIG_HUGETLB_PAGE +if (max_page_shift >PAGE_SHIFT ) { +unsigned long size; + + 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); +return size; + } + } +#endif +set_pte_at (&init_mm, addr, pte,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,7 +127,8 @@ 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; +unsigned int steps;if (WARN_ON_ONCE (!PAGE_ALIGNED (end - addr)))return -EINVAL ;@@ -119,20 +149,9 @@ 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); - 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); + 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 ; --Quoted 2 lines
2.34.1
IMO, we should add just a helper with "no functional change" and second patch will extend it.
Quoted 4 lines
Otherwise you added a helper which has already been slightly modified.
--
Uladzislau Rezki
Make sense, will split it, the steps/loops refactoring will move into patch 4.
Thanks,
Wen
PATCH v3 4/6 mm/vmalloc: Extend page table walk to support larger page_shift sizes and eliminate page table rewalk
In reply to Wen JiangFrom: "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 | 71 +++++++++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 31 deletions(-)
mm/vmalloc.c +40 -31
@@ -543,8 +543,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;
@@ -575,9 +577,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 ;
@@ -587,7 +590,7 @@ 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;
@@ -597,7 +600,27 @@ static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr,return -ENOMEM ;do { next =pmd_addr_end (addr, end); -if (vmap_pages_pte_range (pmd, addr, next, prot, pages, nr, mask)) + +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 << (shift -PAGE_SHIFT ); +continue ; + } + } + +if (vmap_pages_pte_range (pmd, addr, next, prot, pages, nr, mask, shift))return -ENOMEM ; }while (pmd++, addr = next, addr != end);return 0 ;
@@ -605,7 +628,7 @@ static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr,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;
@@ -615,7 +638,7 @@ static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,return -ENOMEM ;do { next =pud_addr_end (addr, end); -if (vmap_pages_pmd_range (pud, addr, next, prot, pages, nr, mask)) +if (vmap_pages_pmd_range (pud, addr, next, prot, pages, nr, mask, shift))return -ENOMEM ; }while (pud++, addr = next, addr != end);return 0 ;
@@ -623,7 +646,7 @@ static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,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;
@@ -633,14 +656,14 @@ static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,return -ENOMEM ;do { next =p4d_addr_end (addr, end); -if (vmap_pages_pud_range (p4d, addr, next, prot, pages, nr, mask)) +if (vmap_pages_pud_range (p4d, addr, next, prot, pages, nr, mask, shift))return -ENOMEM ; }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) +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;
@@ -655,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);
@@ -678,27 +701,13 @@ 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, +min (page_shift,PMD_SHIFT )); } int vmap_pages_range_noflush(unsigned long addr, unsigned long end,
Signature · 1 line
2.34.1
Re: [PATCH v3 4/6] mm/vmalloc: Extend page table walk to support larger page_shift sizes and eliminate page table rewalk
Quoted 66 lines · Wen Jiang
From: "Barry Song (Xiaomi)" <[email protected]>
vmap_pages_range_noflush_walk()(formerlyvmap_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 usingvmap_range_noflush()forpage_shiftvalues other thanPAGE_SHIFT.Extend it to support larger
page_shiftvalues, and add PMD- and contiguous-PTE mappings as well. Rename it tovmap_pages_range_noflush_walk()since it now handles more than just small pages.For
vmalloc()allocations withVM_ALLOW_HUGE_VMAP, we no longer need to iterate over pages one by one viavmap_range_noflush(), which would otherwise lead to page table rewalk. The code is now unified with thePAGE_SHIFTcase by simply callingvmap_pages_range_noflush_walk().Signed-off-by Barry Song (Xiaomi) <[email protected]>Signed-off-by Wen Jiang <[email protected]>Tested-by Xueyuan Chen <[email protected]>--- mm/vmalloc.c | 71 +++++++++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 31 deletions(-)mm/vmalloc.c +8
-5@@ -543,8 +543,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;@@ -575,9 +577,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 ;@@ -587,7 +590,7 @@ 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;@@ -597,7 +600,27 @@ static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr,return -ENOMEM ; do { next = pmd_addr_end(addr, end);
mm/vmalloc.c static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr, · line 611
Quoted 10 lines
mm/vmalloc.c +9
-1-if (vmap_pages_pte_range (pmd, addr, next, prot, pages, nr, mask)) + +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 ;
So I know these !page and !pfn_valid checks have been copied from vmap_pages_pte_range, but do they mean anything?
I think pfn_valid() makes sense in that someone may take a random VA/PA, convert it into a struct page and pass to vmap layer. But I don't see how anyone would pass page == NULL? At the very least, returning ENOMEM does not make sense because the pages are not being allocated by vmap() but have already been allocated.
Quoted 88 lines
mm/vmalloc.c +21
-17+ + phys_addr =page_to_phys (page); + +if (vmap_try_huge_pmd (pmd, addr, next, phys_addr, prot, + shift)) { + *mask |=PGTBL_PMD_MODIFIED ; + *nr +=1 << (shift -PAGE_SHIFT ); +continue ; + } + } + +if (vmap_pages_pte_range (pmd, addr, next, prot, pages, nr, mask, shift))return -ENOMEM ; }while (pmd++, addr = next, addr != end);return 0 ;@@ -605,7 +628,7 @@ static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr,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;@@ -615,7 +638,7 @@ static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,return -ENOMEM ;do { next =pud_addr_end (addr, end); -if (vmap_pages_pmd_range (pud, addr, next, prot, pages, nr, mask)) +if (vmap_pages_pmd_range (pud, addr, next, prot, pages, nr, mask, shift))return -ENOMEM ; }while (pud++, addr = next, addr != end);return 0 ;@@ -623,7 +646,7 @@ static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,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;@@ -633,14 +656,14 @@ static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,return -ENOMEM ;do { next =p4d_addr_end (addr, end); -if (vmap_pages_pud_range (p4d, addr, next, prot, pages, nr, mask)) +if (vmap_pages_pud_range (p4d, addr, next, prot, pages, nr, mask, shift))return -ENOMEM ; }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) +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;@@ -655,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);@@ -678,27 +701,13 @@ 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,
mm/vmalloc.c static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end, · line 710
Quoted 10 lines
mm/vmalloc.c +2
-8- page_shift); -if (err) -return err; - - addr +=1UL << page_shift; - } - -return 0 ; +return vmap_pages_range_noflush_walk (addr, end, prot, pages, +min (page_shift,PMD_SHIFT ));
We can easily extend to PUD huge mappings right? Not sure whether we should keep everything symmetric to how vmap_range_noflush() operates right now, since P4D mappings don't exist, but PUD looks worthwhile.
Quoted 3 lines
mm/vmalloc.c +0
-0} int vmap_pages_range_noflush(unsigned long addr, unsigned long end,
Re: [PATCH v3 4/6] mm/vmalloc: Extend page table walk to support larger page_shift sizes and eliminate page table rewalk
Quoted 85 lines · Dev Jain
On 22/05/26 11:01 am, Wen Jiang wrote:
From: "Barry Song (Xiaomi)" <[email protected]>
vmap_pages_range_noflush_walk()(formerlyvmap_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 usingvmap_range_noflush()forpage_shiftvalues other thanPAGE_SHIFT.Extend it to support larger
page_shiftvalues, and add PMD- and contiguous-PTE mappings as well. Rename it tovmap_pages_range_noflush_walk()since it now handles more than just small pages.For
vmalloc()allocations withVM_ALLOW_HUGE_VMAP, we no longer need to iterate over pages one by one viavmap_range_noflush(), which would otherwise lead to page table rewalk. The code is now unified with thePAGE_SHIFTcase by simply callingvmap_pages_range_noflush_walk().Signed-off-by Barry Song (Xiaomi) <[email protected]>Signed-off-by Wen Jiang <[email protected]>Tested-by Xueyuan Chen <[email protected]>--- mm/vmalloc.c | 71 +++++++++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 31 deletions(-)mm/vmalloc.c +8
-5@@ -543,8 +543,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;@@ -575,9 +577,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 ;@@ -587,7 +590,7 @@ 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;Quoted 4 lines
mm/vmalloc.c +0
-0@@ -597,7 +600,27 @@ static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr,return -ENOMEM ; do { next = pmd_addr_end(addr, end);mm/vmalloc.c static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr, · line 611
Quoted 10 lines
mm/vmalloc.c +9
-1-if (vmap_pages_pte_range (pmd, addr, next, prot, pages, nr, mask)) + +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 ;So I know these !page and !pfn_valid checks have been copied from
vmap_pages_pte_range, but do they mean anything?
Quoted 4 lines
I think
pfn_valid()makes sense in that someone may take a random VA/PA, convert it into a struct page and pass to vmap layer. But I don't see how anyone would pass page == NULL? At the very least, returning ENOMEM does not make sense because the pages are not being allocated byvmap()but have already been allocated.
Hi Dev,
vmap() is EXPORT_SYMBOL with many callers across drivers, each constructing the pages array differently. The !page check guards against malformed arrays at this API boundary.
The same -ENOMEM issue also exists in vmap_pages_pte_range(). Should I fix both in this patchset or leave it as a separate cleanup?
Quoted 96 lines
Quoted 96 lines
mm/vmalloc.c +21
-24+ + phys_addr =page_to_phys (page); + +if (vmap_try_huge_pmd (pmd, addr, next, phys_addr, prot, + shift)) { + *mask |=PGTBL_PMD_MODIFIED ; + *nr +=1 << (shift -PAGE_SHIFT ); +continue ; + } + } + +if (vmap_pages_pte_range (pmd, addr, next, prot, pages, nr, mask, shift))return -ENOMEM ; }while (pmd++, addr = next, addr != end);return 0 ;@@ -605,7 +628,7 @@ static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr,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;@@ -615,7 +638,7 @@ static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,return -ENOMEM ;do { next =pud_addr_end (addr, end); -if (vmap_pages_pmd_range (pud, addr, next, prot, pages, nr, mask)) +if (vmap_pages_pmd_range (pud, addr, next, prot, pages, nr, mask, shift))return -ENOMEM ; }while (pud++, addr = next, addr != end);return 0 ;@@ -623,7 +646,7 @@ static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,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;@@ -633,14 +656,14 @@ static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,return -ENOMEM ;do { next =p4d_addr_end (addr, end); -if (vmap_pages_pud_range (p4d, addr, next, prot, pages, nr, mask)) +if (vmap_pages_pud_range (p4d, addr, next, prot, pages, nr, mask, shift))return -ENOMEM ; }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) +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;@@ -655,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);@@ -678,27 +701,13 @@ 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; - } -
Quoted 9 lines
mm/vmalloc.c static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end, · line 710
Quoted 3 lines
mm/vmalloc.c +2
-1-return 0 ; +return vmap_pages_range_noflush_walk (addr, end, prot, pages, +min (page_shift,PMD_SHIFT ));We can easily extend to PUD huge mappings right? Not sure whether we should keep everything symmetric to how
vmap_range_noflush()operates right now, since P4D mappings don't exist, but PUD looks worthwhile.
PUD mapping requires 1GB of contiguous physical memory, but the buddy allocator's MAX_PAGE_ORDER is 10 (4MB on 4K pages). So page_shift passed to vmap_pages_range_noflush_walk() never exceeds PMD_SHIFT.
Thanks,
Wen
Quoted 4 lines
Quoted 3 lines
mm/vmalloc.c +0
-0} int vmap_pages_range_noflush(unsigned long addr, unsigned long end,
Re: [PATCH v3 4/6] mm/vmalloc: Extend page table walk to support larger page_shift sizes and eliminate page table rewalk
Quoted 91 lines · Wen Jiang
Quoted 85 lines · Dev Jain
On 22/05/26 11:01 am, Wen Jiang wrote:
From: "Barry Song (Xiaomi)" <[email protected]>
vmap_pages_range_noflush_walk()(formerlyvmap_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 usingvmap_range_noflush()forpage_shiftvalues other thanPAGE_SHIFT.Extend it to support larger
page_shiftvalues, and add PMD- and contiguous-PTE mappings as well. Rename it tovmap_pages_range_noflush_walk()since it now handles more than just small pages.For
vmalloc()allocations withVM_ALLOW_HUGE_VMAP, we no longer need to iterate over pages one by one viavmap_range_noflush(), which would otherwise lead to page table rewalk. The code is now unified with thePAGE_SHIFTcase by simply callingvmap_pages_range_noflush_walk().Signed-off-by Barry Song (Xiaomi) <[email protected]>Signed-off-by Wen Jiang <[email protected]>Tested-by Xueyuan Chen <[email protected]>--- mm/vmalloc.c | 71 +++++++++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 31 deletions(-)mm/vmalloc.c +8
-5@@ -543,8 +543,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;@@ -575,9 +577,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 ;@@ -587,7 +590,7 @@ 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;Quoted 4 lines
mm/vmalloc.c +0
-0@@ -597,7 +600,27 @@ static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr,return -ENOMEM ; do { next = pmd_addr_end(addr, end);mm/vmalloc.c static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr, · line 611
Quoted 10 lines
mm/vmalloc.c +9
-1-if (vmap_pages_pte_range (pmd, addr, next, prot, pages, nr, mask)) + +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 ;So I know these !page and !pfn_valid checks have been copied from
vmap_pages_pte_range, but do they mean anything?Quoted 4 lines
I think
pfn_valid()makes sense in that someone may take a random VA/PA, convert it into a struct page and pass to vmap layer. But I don't see how anyone would pass page == NULL? At the very least, returning ENOMEM does not make sense because the pages are not being allocated byvmap()but have already been allocated.
Quoted 8 lines
Hi Dev,
vmap()isEXPORT_SYMBOLwith many callers across drivers, each constructing the pages array differently. The !page check guards against malformed arrays at this API boundary.The same
-ENOMEMissue also exists invmap_pages_pte_range(). Should I fix both in this patchset or leave it as a separate cleanup?
Hmm - I think this is the issue of who validates what. I think the change should be done but this is not an urgent issue, so let us leave this for now.
Quoted 107 lines
Quoted 97 lines
Quoted 97 lines
mm/vmalloc.c +21
-24+ + phys_addr =page_to_phys (page); + +if (vmap_try_huge_pmd (pmd, addr, next, phys_addr, prot, + shift)) { + *mask |=PGTBL_PMD_MODIFIED ; + *nr +=1 << (shift -PAGE_SHIFT ); +continue ; + } + } + +if (vmap_pages_pte_range (pmd, addr, next, prot, pages, nr, mask, shift))return -ENOMEM ; }while (pmd++, addr = next, addr != end);return 0 ;@@ -605,7 +628,7 @@ static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr,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;@@ -615,7 +638,7 @@ static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,return -ENOMEM ;do { next =pud_addr_end (addr, end); -if (vmap_pages_pmd_range (pud, addr, next, prot, pages, nr, mask)) +if (vmap_pages_pmd_range (pud, addr, next, prot, pages, nr, mask, shift))return -ENOMEM ; }while (pud++, addr = next, addr != end);return 0 ;@@ -623,7 +646,7 @@ static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,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;@@ -633,14 +656,14 @@ static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,return -ENOMEM ;do { next =p4d_addr_end (addr, end); -if (vmap_pages_pud_range (p4d, addr, next, prot, pages, nr, mask)) +if (vmap_pages_pud_range (p4d, addr, next, prot, pages, nr, mask, shift))return -ENOMEM ; }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) +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;@@ -655,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);@@ -678,27 +701,13 @@ 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; - } -Quoted 9 lines
mm/vmalloc.c static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end, · line 710
Quoted 3 lines
mm/vmalloc.c +2
-1-return 0 ; +return vmap_pages_range_noflush_walk (addr, end, prot, pages, +min (page_shift,PMD_SHIFT ));We can easily extend to PUD huge mappings right? Not sure whether we should keep everything symmetric to how
vmap_range_noflush()operates right now, since P4D mappings don't exist, but PUD looks worthwhile.
Quoted 3 lines
PUD mapping requires 1GB of contiguous physical memory, but the buddy allocator's
MAX_PAGE_ORDERis 10 (4MB on 4K pages). Sopage_shiftpassed tovmap_pages_range_noflush_walk()never exceedsPMD_SHIFT.
Ah okay. It is really confusing to see PUD helpers everywhere when they are basically dead code ...
Quoted 7 lines
Thanks,
WenQuoted 4 lines
Quoted 3 lines
mm/vmalloc.c +0
-0} int vmap_pages_range_noflush(unsigned long addr, unsigned long end,
Re: [PATCH v3 4/6] mm/vmalloc: Extend page table walk to support larger page_shift sizes and eliminate page table rewalk
In reply to Wen JiangQuoted 206 lines · Wen Jiang
Quoted 85 lines · Dev Jain
On 22/05/26 11:01 am, Wen Jiang wrote:
From: "Barry Song (Xiaomi)" <[email protected]>
vmap_pages_range_noflush_walk()(formerlyvmap_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 usingvmap_range_noflush()forpage_shiftvalues other thanPAGE_SHIFT.Extend it to support larger
page_shiftvalues, and add PMD- and contiguous-PTE mappings as well. Rename it tovmap_pages_range_noflush_walk()since it now handles more than just small pages.For
vmalloc()allocations withVM_ALLOW_HUGE_VMAP, we no longer need to iterate over pages one by one viavmap_range_noflush(), which would otherwise lead to page table rewalk. The code is now unified with thePAGE_SHIFTcase by simply callingvmap_pages_range_noflush_walk().Signed-off-by Barry Song (Xiaomi) <[email protected]>Signed-off-by Wen Jiang <[email protected]>Tested-by Xueyuan Chen <[email protected]>--- mm/vmalloc.c | 71 +++++++++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 31 deletions(-)mm/vmalloc.c +8
-5@@ -543,8 +543,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;@@ -575,9 +577,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 ;@@ -587,7 +590,7 @@ 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;Quoted 4 lines
mm/vmalloc.c +0
-0@@ -597,7 +600,27 @@ static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr,return -ENOMEM ; do { next = pmd_addr_end(addr, end);mm/vmalloc.c static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr, · line 611
Quoted 10 lines
mm/vmalloc.c +9
-1-if (vmap_pages_pte_range (pmd, addr, next, prot, pages, nr, mask)) + +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 ;So I know these !page and !pfn_valid checks have been copied from
vmap_pages_pte_range, but do they mean anything?Quoted 4 lines
I think
pfn_valid()makes sense in that someone may take a random VA/PA, convert it into a struct page and pass to vmap layer. But I don't see how anyone would pass page == NULL? At the very least, returning ENOMEM does not make sense because the pages are not being allocated byvmap()but have already been allocated.Hi Dev,
vmap()isEXPORT_SYMBOLwith many callers across drivers, each constructing the pages array differently. The !page check guards against malformed arrays at this API boundary.The same
-ENOMEMissue also exists invmap_pages_pte_range(). Should I fix both in this patchset or leave it as a separate cleanup?Quoted 96 lines
Quoted 96 lines
mm/vmalloc.c +21
-24+ + phys_addr =page_to_phys (page); + +if (vmap_try_huge_pmd (pmd, addr, next, phys_addr, prot, + shift)) { + *mask |=PGTBL_PMD_MODIFIED ; + *nr +=1 << (shift -PAGE_SHIFT ); +continue ; + } + } + +if (vmap_pages_pte_range (pmd, addr, next, prot, pages, nr, mask, shift))return -ENOMEM ; }while (pmd++, addr = next, addr != end);return 0 ;@@ -605,7 +628,7 @@ static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr,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;@@ -615,7 +638,7 @@ static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,return -ENOMEM ;do { next =pud_addr_end (addr, end); -if (vmap_pages_pmd_range (pud, addr, next, prot, pages, nr, mask)) +if (vmap_pages_pmd_range (pud, addr, next, prot, pages, nr, mask, shift))return -ENOMEM ; }while (pud++, addr = next, addr != end);return 0 ;@@ -623,7 +646,7 @@ static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,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;@@ -633,14 +656,14 @@ static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,return -ENOMEM ;do { next =p4d_addr_end (addr, end); -if (vmap_pages_pud_range (p4d, addr, next, prot, pages, nr, mask)) +if (vmap_pages_pud_range (p4d, addr, next, prot, pages, nr, mask, shift))return -ENOMEM ; }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) +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;@@ -655,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);@@ -678,27 +701,13 @@ 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; - } -Quoted 9 lines
mm/vmalloc.c static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end, · line 710
Quoted 3 lines
mm/vmalloc.c +2
-1-return 0 ; +return vmap_pages_range_noflush_walk (addr, end, prot, pages, +min (page_shift,PMD_SHIFT ));We can easily extend to PUD huge mappings right? Not sure whether we should keep everything symmetric to how
vmap_range_noflush()operates right now, since P4D mappings don't exist, but PUD looks worthwhile.
Quoted 3 lines
PUD mapping requires 1GB of contiguous physical memory, but the buddy allocator's
MAX_PAGE_ORDERis 10 (4MB on 4K pages). Sopage_shiftpassed tovmap_pages_range_noflush_walk()never exceedsPMD_SHIFT.
Can we then just drop the min()? You can guard the try_huge_pmd with shift >= PMD_SHIFT - the walker has the necessary ingredients to work with a shift > PMD_SHIFT, so let us not confuse by this min() truncation.
Quoted 7 lines
Thanks,
WenQuoted 4 lines
Quoted 3 lines
mm/vmalloc.c +0
-0} int vmap_pages_range_noflush(unsigned long addr, unsigned long end,
Re: [PATCH v3 4/6] mm/vmalloc: Extend page table walk to support larger page_shift sizes and eliminate page table rewalk
Quoted 210 lines · Dev Jain
Quoted 206 lines · Wen Jiang
Quoted 85 lines · Dev Jain
On 22/05/26 11:01 am, Wen Jiang wrote:
From: "Barry Song (Xiaomi)" <[email protected]>
vmap_pages_range_noflush_walk()(formerlyvmap_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 usingvmap_range_noflush()forpage_shiftvalues other thanPAGE_SHIFT.Extend it to support larger
page_shiftvalues, and add PMD- and contiguous-PTE mappings as well. Rename it tovmap_pages_range_noflush_walk()since it now handles more than just small pages.For
vmalloc()allocations withVM_ALLOW_HUGE_VMAP, we no longer need to iterate over pages one by one viavmap_range_noflush(), which would otherwise lead to page table rewalk. The code is now unified with thePAGE_SHIFTcase by simply callingvmap_pages_range_noflush_walk().Signed-off-by Barry Song (Xiaomi) <[email protected]>Signed-off-by Wen Jiang <[email protected]>Tested-by Xueyuan Chen <[email protected]>--- mm/vmalloc.c | 71 +++++++++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 31 deletions(-)mm/vmalloc.c +8
-5@@ -543,8 +543,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;@@ -575,9 +577,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 ;@@ -587,7 +590,7 @@ 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;Quoted 4 lines
mm/vmalloc.c +0
-0@@ -597,7 +600,27 @@ static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr,return -ENOMEM ; do { next = pmd_addr_end(addr, end);mm/vmalloc.c static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr, · line 611
Quoted 10 lines
mm/vmalloc.c +9
-1-if (vmap_pages_pte_range (pmd, addr, next, prot, pages, nr, mask)) + +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 ;So I know these !page and !pfn_valid checks have been copied from
vmap_pages_pte_range, but do they mean anything?Quoted 4 lines
I think
pfn_valid()makes sense in that someone may take a random VA/PA, convert it into a struct page and pass to vmap layer. But I don't see how anyone would pass page == NULL? At the very least, returning ENOMEM does not make sense because the pages are not being allocated byvmap()but have already been allocated.Hi Dev,
vmap()isEXPORT_SYMBOLwith many callers across drivers, each constructing the pages array differently. The !page check guards against malformed arrays at this API boundary.The same
-ENOMEMissue also exists invmap_pages_pte_range(). Should I fix both in this patchset or leave it as a separate cleanup?Quoted 105 lines
Quoted 89 lines
mm/vmalloc.c +21
-17+ + phys_addr =page_to_phys (page); + +if (vmap_try_huge_pmd (pmd, addr, next, phys_addr, prot, + shift)) { + *mask |=PGTBL_PMD_MODIFIED ; + *nr +=1 << (shift -PAGE_SHIFT ); +continue ; + } + } + +if (vmap_pages_pte_range (pmd, addr, next, prot, pages, nr, mask, shift))return -ENOMEM ; }while (pmd++, addr = next, addr != end);return 0 ;@@ -605,7 +628,7 @@ static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr,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;@@ -615,7 +638,7 @@ static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,return -ENOMEM ;do { next =pud_addr_end (addr, end); -if (vmap_pages_pmd_range (pud, addr, next, prot, pages, nr, mask)) +if (vmap_pages_pmd_range (pud, addr, next, prot, pages, nr, mask, shift))return -ENOMEM ; }while (pud++, addr = next, addr != end);return 0 ;@@ -623,7 +646,7 @@ static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,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;@@ -633,14 +656,14 @@ static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,return -ENOMEM ;do { next =p4d_addr_end (addr, end); -if (vmap_pages_pud_range (p4d, addr, next, prot, pages, nr, mask)) +if (vmap_pages_pud_range (p4d, addr, next, prot, pages, nr, mask, shift))return -ENOMEM ; }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) +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;@@ -655,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);@@ -678,27 +701,13 @@ 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,mm/vmalloc.c static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end, · line 710
Quoted 10 lines
mm/vmalloc.c +2
-8- page_shift); -if (err) -return err; - - addr +=1UL << page_shift; - } - -return 0 ; +return vmap_pages_range_noflush_walk (addr, end, prot, pages, +min (page_shift,PMD_SHIFT ));We can easily extend to PUD huge mappings right? Not sure whether we should keep everything symmetric to how
vmap_range_noflush()operates right now, since P4D mappings don't exist, but PUD looks worthwhile.
Quoted 8 lines
Quoted 3 lines
PUD mapping requires 1GB of contiguous physical memory, but the buddy allocator's
MAX_PAGE_ORDERis 10 (4MB on 4K pages). Sopage_shiftpassed tovmap_pages_range_noflush_walk()never exceedsPMD_SHIFT.Can we then just drop the
min()? You can guard thetry_huge_pmdwith shift >=PMD_SHIFT- the walker has the necessary ingredients to work with a shift >PMD_SHIFT, so let us not confuse by thismin()truncation.
Will Drop the min() here.
Thanks.
Quoted 8 lines
Quoted 7 lines
Thanks,
WenQuoted 4 lines
Quoted 3 lines
mm/vmalloc.c +0
-0} int vmap_pages_range_noflush(unsigned long addr, unsigned long end,
PATCH v3 5/6 mm/vmalloc: map contiguous pages in batches for vmap() if possible
In reply to Wen JiangFrom: "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 first page's pfn must be aligned to the mapping order for the batched mapping to be used.
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
@@ -3542,6 +3542,84 @@ void vunmap(const void *addr) }EXPORT_SYMBOL (vunmap); +static inline int get_vmap_batch_order (struct page **pages, +unsigned int max_steps,unsigned int idx) +{ +unsigned int nr_contig; +int order; + +if (!IS_ENABLED (CONFIG_HAVE_ARCH_HUGE_VMAP ) || + ioremap_max_page_shift ==PAGE_SHIFT ) +return 0 ; + + nr_contig =num_pages_contiguous (&pages[idx], max_steps); +if (nr_contig <2 ) +return 0 ; + + order =fls (nr_contig) -1 ; + +if (arch_vmap_pte_supported_shift (PAGE_SIZE << order) ==PAGE_SHIFT ) +return 0 ; + +/* Ensure the first page's pfn is aligned to the order */ +if (!IS_ALIGNED (page_to_pfn (pages[idx]),1 << order)) +return 0 ; + +return order; +} + +static int vmap_batched (unsigned long addr,unsigned long end, +pgprot_t prot,struct page **pages) +{ +unsigned int count = (end - addr) >>PAGE_SHIFT ; +unsigned int prev_shift =0 , idx =0 ; +unsigned long start = addr, map_addr = 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 < count; ) { +unsigned int shift =PAGE_SHIFT + +get_vmap_batch_order (pages, count - i, i); + +if (!i) + prev_shift = shift; + +if (shift != prev_shift) { + err =vmap_pages_range_noflush_walk (map_addr, addr, + prot, pages + idx, +min (prev_shift,PMD_SHIFT )); +if (err) + goto out; + prev_shift = shift; + map_addr = addr; + idx = i; + } + +/* +* Once small pages are encountered, the remaining pages +* are likely small as well. +*/ +if (shift ==PAGE_SHIFT ) +break ; + + addr +=1UL << shift; + i +=1U << (shift -PAGE_SHIFT ); + } + +/* Remaining */ +if (map_addr < end) + err =vmap_pages_range_noflush_walk (map_addr, end, + prot, pages + idx,min (prev_shift,PMD_SHIFT )); + +out: +flush_cache_vmap (start, end); +return err; +} +/** * vmap - map an array of pages into virtually contiguous space * @pages: array of page pointers
@@ -3585,8 +3663,8 @@ 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_batched (addr, addr + size,pgprot_nx (prot), + pages) <0 ) {vunmap (area->addr);return NULL ; }
Signature · 1 line
2.34.1
Re: [PATCH v3 5/6] mm/vmalloc: map contiguous pages in batches for vmap() if possible
Quoted 36 lines · 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 first page's pfn must be aligned to the mapping order for the batched mapping to be used.Pages with the same
page_shiftare coalesced and mapped viavmap_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.
Signed-off-by Barry Song (Xiaomi) <[email protected]>Co-developed-by Dev Jain <[email protected]>Signed-off-by Dev Jain <[email protected]>Signed-off-by Wen Jiang <[email protected]>Tested-by Xueyuan Chen <[email protected]>--- mm/vmalloc.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 2 deletions(-)mm/vmalloc.c +0
-0@@ -3542,6 +3542,84 @@ void vunmap(const void *addr) }
mm/vmalloc.c void vunmap(const void *addr) · line 3552
Quoted 10 lines
mm/vmalloc.c +8
-0EXPORT_SYMBOL (vunmap); +static inline int get_vmap_batch_order (struct page **pages, +unsigned int max_steps,unsigned int idx) +{ +unsigned int nr_contig; +int order; + +if (!IS_ENABLED (CONFIG_HAVE_ARCH_HUGE_VMAP ) || + ioremap_max_page_shift ==PAGE_SHIFT )
Why bail out on ioremap_max_page_shift == PAGE_SHIFT? The code path for ioremap is different from vmap right?
Quoted 30 lines
mm/vmalloc.c +30
-0+return 0 ; + + nr_contig =num_pages_contiguous (&pages[idx], max_steps); +if (nr_contig <2 ) +return 0 ; + + order =fls (nr_contig) -1 ; + +if (arch_vmap_pte_supported_shift (PAGE_SIZE << order) ==PAGE_SHIFT ) +return 0 ; + +/* Ensure the first page's pfn is aligned to the order */ +if (!IS_ALIGNED (page_to_pfn (pages[idx]),1 << order)) +return 0 ; + +return order; +} + +static int vmap_batched (unsigned long addr,unsigned long end, +pgprot_t prot,struct page **pages) +{ +unsigned int count = (end - addr) >>PAGE_SHIFT ; +unsigned int prev_shift =0 , idx =0 ; +unsigned long start = addr, map_addr = addr; +int err; + + err =kmsan_vmap_pages_range_noflush (addr, end, prot, pages, +PAGE_SHIFT ,GFP_KERNEL ); +if (err) + goto out;
mm/vmalloc.c void vunmap(const void *addr) · line 3592
Quoted 10 lines
mm/vmalloc.c +10
-0+ +for (unsigned int i =0 ; i < count; ) { +unsigned int shift =PAGE_SHIFT + +get_vmap_batch_order (pages, count - i, i); + +if (!i) + prev_shift = shift; + +if (shift != prev_shift) { + err = vmap_pages_range_noflush_walk(map_addr, addr,
It would be worth documenting vmap_pages_range_noflush_walk() that it can take an array of pages which are not all contiguous, but it may have contiguous chunks, as hinted by page_shift.
Otherwise this looks good.
Quoted 44 lines
mm/vmalloc.c +32
-2+ prot, pages + idx, + min(prev_shift, PMD_SHIFT)); +if (err) + goto out; + prev_shift = shift; + map_addr = addr; + idx = i; + } + +/* +* Once small pages are encountered, the remaining pages +* are likely small as well. +*/ +if (shift ==PAGE_SHIFT ) +break ; + + addr +=1UL << shift; + i +=1U << (shift -PAGE_SHIFT ); + } + +/* Remaining */ +if (map_addr < end) + err =vmap_pages_range_noflush_walk (map_addr, end, + prot, pages + idx,min (prev_shift,PMD_SHIFT )); + +out: +flush_cache_vmap (start, end); +return err; +} +/** * vmap - map an array of pages into virtually contiguous space * @pages: array of page pointers @@ -3585,8 +3663,8 @@ 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_batched (addr, addr + size,pgprot_nx (prot), + pages) <0 ) {vunmap (area->addr);return NULL ; }
Re: [PATCH v3 5/6] mm/vmalloc: map contiguous pages in batches for vmap() if possible
Quoted 42 lines · Dev Jain
Quoted 38 lines · 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 first page's pfn must be aligned to the mapping order for the batched mapping to be used.Pages with the same
page_shiftare coalesced and mapped viavmap_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.
Signed-off-by Barry Song (Xiaomi) <[email protected]>Co-developed-by Dev Jain <[email protected]>Signed-off-by Dev Jain <[email protected]>Signed-off-by Wen Jiang <[email protected]>Tested-by Xueyuan Chen <[email protected]>--- mm/vmalloc.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 2 deletions(-)mm/vmalloc.c +0
-0@@ -3542,6 +3542,84 @@ void vunmap(const void *addr) }EXPORT_SYMBOL (vunmap);
Quoted 14 lines
mm/vmalloc.c void vunmap(const void *addr) · line 3552
Quoted 8 lines
mm/vmalloc.c +8
-0+static inline int get_vmap_batch_order (struct page **pages, +unsigned int max_steps,unsigned int idx) +{ +unsigned int nr_contig; +int order; + +if (!IS_ENABLED (CONFIG_HAVE_ARCH_HUGE_VMAP ) || + ioremap_max_page_shift ==PAGE_SHIFT )Why bail out on
ioremap_max_page_shift==PAGE_SHIFT? The code path for ioremap is different from vmap right?
ioremap_max_page_shift is under CONFIG_HAVE_ARCH_HUGE_VMAP which controls both ioremap and vmap huge mappings.
Quoted 92 lines
Quoted 30 lines
mm/vmalloc.c +30
-0+return 0 ; + + nr_contig =num_pages_contiguous (&pages[idx], max_steps); +if (nr_contig <2 ) +return 0 ; + + order =fls (nr_contig) -1 ; + +if (arch_vmap_pte_supported_shift (PAGE_SIZE << order) ==PAGE_SHIFT ) +return 0 ; + +/* Ensure the first page's pfn is aligned to the order */ +if (!IS_ALIGNED (page_to_pfn (pages[idx]),1 << order)) +return 0 ; + +return order; +} + +static int vmap_batched (unsigned long addr,unsigned long end, +pgprot_t prot,struct page **pages) +{ +unsigned int count = (end - addr) >>PAGE_SHIFT ; +unsigned int prev_shift =0 , idx =0 ; +unsigned long start = addr, map_addr = addr; +int err; + + err =kmsan_vmap_pages_range_noflush (addr, end, prot, pages, +PAGE_SHIFT ,GFP_KERNEL ); +if (err) + goto out;mm/vmalloc.c void vunmap(const void *addr) · line 3592
Quoted 10 lines
mm/vmalloc.c +10
-0+ +for (unsigned int i =0 ; i < count; ) { +unsigned int shift =PAGE_SHIFT + +get_vmap_batch_order (pages, count - i, i); + +if (!i) + prev_shift = shift; + +if (shift != prev_shift) { + err = vmap_pages_range_noflush_walk(map_addr, addr,It would be worth documenting
vmap_pages_range_noflush_walk()that it can take an array of pages which are not all contiguous, but it may have contiguous chunks, as hinted bypage_shift.Otherwise this looks good.
Quoted 44 lines
mm/vmalloc.c +32
-2+ prot, pages + idx, + min(prev_shift, PMD_SHIFT)); +if (err) + goto out; + prev_shift = shift; + map_addr = addr; + idx = i; + } + +/* +* Once small pages are encountered, the remaining pages +* are likely small as well. +*/ +if (shift ==PAGE_SHIFT ) +break ; + + addr +=1UL << shift; + i +=1U << (shift -PAGE_SHIFT ); + } + +/* Remaining */ +if (map_addr < end) + err =vmap_pages_range_noflush_walk (map_addr, end, + prot, pages + idx,min (prev_shift,PMD_SHIFT )); + +out: +flush_cache_vmap (start, end); +return err; +} +/** * vmap - map an array of pages into virtually contiguous space * @pages: array of page pointers @@ -3585,8 +3663,8 @@ 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_batched (addr, addr + size,pgprot_nx (prot), + pages) <0 ) {vunmap (area->addr);return NULL ; }
Re: [PATCH v3 5/6] mm/vmalloc: map contiguous pages in batches for vmap() if possible
Quoted 58 lines · Wen Jiang
Quoted 42 lines · Dev Jain
Quoted 38 lines · 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 first page's pfn must be aligned to the mapping order for the batched mapping to be used.Pages with the same
page_shiftare coalesced and mapped viavmap_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.
Signed-off-by Barry Song (Xiaomi) <[email protected]>Co-developed-by Dev Jain <[email protected]>Signed-off-by Dev Jain <[email protected]>Signed-off-by Wen Jiang <[email protected]>Tested-by Xueyuan Chen <[email protected]>--- mm/vmalloc.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 2 deletions(-)mm/vmalloc.c +0
-0@@ -3542,6 +3542,84 @@ void vunmap(const void *addr) }EXPORT_SYMBOL (vunmap);Quoted 14 lines
mm/vmalloc.c void vunmap(const void *addr) · line 3552
Quoted 8 lines
mm/vmalloc.c +8
-0+static inline int get_vmap_batch_order (struct page **pages, +unsigned int max_steps,unsigned int idx) +{ +unsigned int nr_contig; +int order; + +if (!IS_ENABLED (CONFIG_HAVE_ARCH_HUGE_VMAP ) || + ioremap_max_page_shift ==PAGE_SHIFT )Why bail out on
ioremap_max_page_shift==PAGE_SHIFT? The code path for ioremap is different from vmap right?
Quoted 2 lines
ioremap_max_page_shiftis underCONFIG_HAVE_ARCH_HUGE_VMAPwhich controls both ioremap and vmap huge mappings.
I don't get it. So with this patch if nohugeiomap is passed on kernel cmdline, then vmap-huge is also disabled. That does not sound correct. Currently ioremap_max_page_shift does not play at all with the normal vmap code path. It is only involved in ioremap_page_range().
Quoted 11 lines
Quoted 11 lines
mm/vmalloc.c void vunmap(const void *addr) · line 3562
Quoted 11 lines
mm/vmalloc.c +10
-0+return 0 ; + + nr_contig =num_pages_contiguous (&pages[idx], max_steps); +if (nr_contig <2 ) +return 0 ; + + order =fls (nr_contig) -1 ; + +if (arch_vmap_pte_supported_shift (PAGE_SIZE << order) ==PAGE_SHIFT ) +return 0 ;
Also, for arches where this function does not do anything special (i.e return PAGE_SHIFT), we will effectively not do any huge mappings for them.
Quoted 4 lines
Quoted 4 lines
mm/vmalloc.c void vunmap(const void *addr) · line 3566
Quoted 4 lines
mm/vmalloc.c +4
-0+ +/* Ensure the first page's pfn is aligned to the order */ +if (!IS_ALIGNED (page_to_pfn (pages[idx]),1 << order)) +return 0 ;
This condition is a bit fragile. It may happen that we have, say 2^8 contigous pages, but they are aligned to only 2^4. We are operating on a page array and have no idea if the caller has passed some random subrange of the array.
I think the purpose of these checks is this - to do an early bailout if arch does not support huge mappings, or the alignment is not correct, instead of finding this out very deep into vmap_pages_range_noflush_walk.
So you could do something like (completely untested and may miss some edge cases):
order = ilog2(nr_contig);
order = min(order, __ffs(page_to_pfn(pages[idx])));
order = vm_shift(PAGE_SIZE << order) - PAGE_SHIFT;
Where vm_shift() is the helper I had used in my patch.
Quoted 78 lines
Quoted 78 lines
Quoted 16 lines
mm/vmalloc.c +16
-0+ +return order; +} + +static int vmap_batched (unsigned long addr,unsigned long end, +pgprot_t prot,struct page **pages) +{ +unsigned int count = (end - addr) >>PAGE_SHIFT ; +unsigned int prev_shift =0 , idx =0 ; +unsigned long start = addr, map_addr = addr; +int err; + + err =kmsan_vmap_pages_range_noflush (addr, end, prot, pages, +PAGE_SHIFT ,GFP_KERNEL ); +if (err) + goto out;mm/vmalloc.c void vunmap(const void *addr) · line 3592
Quoted 10 lines
mm/vmalloc.c +10
-0+ +for (unsigned int i =0 ; i < count; ) { +unsigned int shift =PAGE_SHIFT + +get_vmap_batch_order (pages, count - i, i); + +if (!i) + prev_shift = shift; + +if (shift != prev_shift) { + err = vmap_pages_range_noflush_walk(map_addr, addr,It would be worth documenting
vmap_pages_range_noflush_walk()that it can take an array of pages which are not all contiguous, but it may have contiguous chunks, as hinted bypage_shift.Otherwise this looks good.
Quoted 44 lines
mm/vmalloc.c +32
-2+ prot, pages + idx, + min(prev_shift, PMD_SHIFT)); +if (err) + goto out; + prev_shift = shift; + map_addr = addr; + idx = i; + } + +/* +* Once small pages are encountered, the remaining pages +* are likely small as well. +*/ +if (shift ==PAGE_SHIFT ) +break ; + + addr +=1UL << shift; + i +=1U << (shift -PAGE_SHIFT ); + } + +/* Remaining */ +if (map_addr < end) + err =vmap_pages_range_noflush_walk (map_addr, end, + prot, pages + idx,min (prev_shift,PMD_SHIFT )); + +out: +flush_cache_vmap (start, end); +return err; +} +/** * vmap - map an array of pages into virtually contiguous space * @pages: array of page pointers @@ -3585,8 +3663,8 @@ 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_batched (addr, addr + size,pgprot_nx (prot), + pages) <0 ) {vunmap (area->addr);return NULL ; }
Re: [PATCH v3 5/6] mm/vmalloc: map contiguous pages in batches for vmap() if possible
Quoted 62 lines · Dev Jain
Quoted 58 lines · Wen Jiang
Quoted 56 lines · Dev Jain
Quoted 36 lines · 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 first page's pfn must be aligned to the mapping order for the batched mapping to be used.Pages with the same
page_shiftare coalesced and mapped viavmap_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.
Signed-off-by Barry Song (Xiaomi) <[email protected]>Co-developed-by Dev Jain <[email protected]>Signed-off-by Dev Jain <[email protected]>Signed-off-by Wen Jiang <[email protected]>Tested-by Xueyuan Chen <[email protected]>--- mm/vmalloc.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 2 deletions(-)mm/vmalloc.c +0
-0@@ -3542,6 +3542,84 @@ void vunmap(const void *addr) }mm/vmalloc.c void vunmap(const void *addr) · line 3552
Quoted 10 lines
mm/vmalloc.c +8
-0EXPORT_SYMBOL (vunmap); +static inline int get_vmap_batch_order (struct page **pages, +unsigned int max_steps,unsigned int idx) +{ +unsigned int nr_contig; +int order; + +if (!IS_ENABLED (CONFIG_HAVE_ARCH_HUGE_VMAP ) || + ioremap_max_page_shift ==PAGE_SHIFT )Why bail out on
ioremap_max_page_shift==PAGE_SHIFT? The code path for ioremap is different from vmap right?
Quoted 7 lines
Quoted 2 lines
ioremap_max_page_shiftis underCONFIG_HAVE_ARCH_HUGE_VMAPwhich controls both ioremap and vmap huge mappings.I don't get it. So with this patch if nohugeiomap is passed on kernel cmdline, then vmap-huge is also disabled. That does not sound correct. Currently
ioremap_max_page_shiftdoes not play at all with the normal vmap code path. It is only involved inioremap_page_range().
You're right, vmap path should not be affected by ioremap. I'll remove it.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
mm/vmalloc.c +6
-0+return 0 ; + + nr_contig =num_pages_contiguous (&pages[idx], max_steps); +if (nr_contig <2 ) +return 0 ; +
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
mm/vmalloc.c void vunmap(const void *addr) · line 3562
Quoted 4 lines
mm/vmalloc.c +4
-0+ order =fls (nr_contig) -1 ; + +if (arch_vmap_pte_supported_shift (PAGE_SIZE << order) ==PAGE_SHIFT ) +return 0 ;Also, for arches where this function does not do anything special (i.e return
PAGE_SHIFT), we will effectively not do any huge mappings for them.
Agreed, I'll add PMD-level handling here. Will reference your vm_shift() helper.
Quoted 16 lines
Quoted 4 lines
Quoted 4 lines
mm/vmalloc.c void vunmap(const void *addr) · line 3566
Quoted 4 lines
mm/vmalloc.c +4
-0+ +/* Ensure the first page's pfn is aligned to the order */ +if (!IS_ALIGNED (page_to_pfn (pages[idx]),1 << order)) +return 0 ;This condition is a bit fragile. It may happen that we have, say 2^8 contigous pages, but they are aligned to only 2^4. We are operating on a page array and have no idea if the caller has passed some random subrange of the array.
I think the purpose of these checks is this - to do an early bailout if arch does not support huge mappings, or the alignment is not correct, instead of finding this out very deep into
vmap_pages_range_noflush_walk.So you could do something like (completely untested and may miss some edge cases):
Quoted 8 lines
order =
ilog2(nr_contig);order =
min(order, __ffs(page_to_pfn(pages[idx])));order =
vm_shift(PAGE_SIZE << order)-PAGE_SHIFT;Where
vm_shift()is the helper I had used in my patch.
Will adopt this approach in v4.
Thanks for the through review!
Quoted 79 lines
Quoted 78 lines
Quoted 78 lines
Quoted 16 lines
mm/vmalloc.c +16
-0+ +return order; +} + +static int vmap_batched (unsigned long addr,unsigned long end, +pgprot_t prot,struct page **pages) +{ +unsigned int count = (end - addr) >>PAGE_SHIFT ; +unsigned int prev_shift =0 , idx =0 ; +unsigned long start = addr, map_addr = addr; +int err; + + err =kmsan_vmap_pages_range_noflush (addr, end, prot, pages, +PAGE_SHIFT ,GFP_KERNEL ); +if (err) + goto out;mm/vmalloc.c void vunmap(const void *addr) · line 3592
Quoted 10 lines
mm/vmalloc.c +10
-0+ +for (unsigned int i =0 ; i < count; ) { +unsigned int shift =PAGE_SHIFT + +get_vmap_batch_order (pages, count - i, i); + +if (!i) + prev_shift = shift; + +if (shift != prev_shift) { + err = vmap_pages_range_noflush_walk(map_addr, addr,It would be worth documenting
vmap_pages_range_noflush_walk()that it can take an array of pages which are not all contiguous, but it may have contiguous chunks, as hinted bypage_shift.Otherwise this looks good.
Quoted 44 lines
mm/vmalloc.c +32
-2+ prot, pages + idx, + min(prev_shift, PMD_SHIFT)); +if (err) + goto out; + prev_shift = shift; + map_addr = addr; + idx = i; + } + +/* +* Once small pages are encountered, the remaining pages +* are likely small as well. +*/ +if (shift ==PAGE_SHIFT ) +break ; + + addr +=1UL << shift; + i +=1U << (shift -PAGE_SHIFT ); + } + +/* Remaining */ +if (map_addr < end) + err =vmap_pages_range_noflush_walk (map_addr, end, + prot, pages + idx,min (prev_shift,PMD_SHIFT )); + +out: +flush_cache_vmap (start, end); +return err; +} +/** * vmap - map an array of pages into virtually contiguous space * @pages: array of page pointers @@ -3585,8 +3663,8 @@ 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_batched (addr, addr + size,pgprot_nx (prot), + pages) <0 ) {vunmap (area->addr);return NULL ; }
PATCH v3 6/6 mm/vmalloc: align vm_area so vmap() can batch mappings
In reply to Wen JiangFrom: "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.
--- mm/vmalloc.c | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-)
mm/vmalloc.c +32 -1
@@ -3620,6 +3620,37 @@ static int vmap_batched(unsigned long addr, unsigned long end,return err; } +static struct vm_struct *get_aligned_vm_area (unsigned long size, +unsigned long flags,const void *caller) +{ +struct vm_struct *vm_area; +unsigned int shift; + +/* Try PMD alignment for large sizes */ +if (size >=PMD_SIZE ) { + vm_area =__get_vm_area_node (size,PMD_SIZE ,PAGE_SHIFT , flags, +VMALLOC_START ,VMALLOC_END , +NUMA_NO_NODE ,GFP_KERNEL , caller); +if (vm_area) +return vm_area; + } + +/* Try CONT_PTE alignment */ + shift =arch_vmap_pte_supported_shift (size); +if (shift >PAGE_SHIFT ) { + vm_area =__get_vm_area_node (size,1UL << shift,PAGE_SHIFT , flags, +VMALLOC_START ,VMALLOC_END , +NUMA_NO_NODE ,GFP_KERNEL , caller); +if (vm_area) +return vm_area; + } + +/* Fall back to page alignment */ +return __get_vm_area_node (size,PAGE_SIZE ,PAGE_SHIFT , flags, +VMALLOC_START ,VMALLOC_END , +NUMA_NO_NODE ,GFP_KERNEL , caller); +} +/** * vmap - map an array of pages into virtually contiguous space * @pages: array of page pointers
@@ -3658,7 +3689,7 @@ 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 =get_aligned_vm_area (size, flags,__builtin_return_address (0 ));if (!area)return NULL ;
Signature · 1 line
2.34.1
Re: [PATCH v3 6/6] mm/vmalloc: align vm_area so vmap() can batch mappings
Quoted 68 lines · Wen Jiang
From: "Barry Song (Xiaomi)" <[email protected]>
Try to align the vmap virtual address to
PMD_SHIFTor a larger PTE mapping size hinted by the architecture, so contiguous pages can be batch-mapped when setting PMD or PTE entries.Signed-off-by Barry Song (Xiaomi) <[email protected]>Signed-off-by Wen Jiang <[email protected]>Tested-by Xueyuan Chen <[email protected]>--- mm/vmalloc.c | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-)mm/vmalloc.c +32
-1@@ -3620,6 +3620,37 @@ static int vmap_batched(unsigned long addr, unsigned long end,return err; } +static struct vm_struct *get_aligned_vm_area (unsigned long size, +unsigned long flags,const void *caller) +{ +struct vm_struct *vm_area; +unsigned int shift; + +/* Try PMD alignment for large sizes */ +if (size >=PMD_SIZE ) { + vm_area =__get_vm_area_node (size,PMD_SIZE ,PAGE_SHIFT , flags, +VMALLOC_START ,VMALLOC_END , +NUMA_NO_NODE ,GFP_KERNEL , caller); +if (vm_area) +return vm_area; + } + +/* Try CONT_PTE alignment */ + shift =arch_vmap_pte_supported_shift (size); +if (shift >PAGE_SHIFT ) { + vm_area =__get_vm_area_node (size,1UL << shift,PAGE_SHIFT , flags, +VMALLOC_START ,VMALLOC_END , +NUMA_NO_NODE ,GFP_KERNEL , caller); +if (vm_area) +return vm_area; + } + +/* Fall back to page alignment */ +return __get_vm_area_node (size,PAGE_SIZE ,PAGE_SHIFT , flags, +VMALLOC_START ,VMALLOC_END , +NUMA_NO_NODE ,GFP_KERNEL , caller); +} +/** * vmap - map an array of pages into virtually contiguous space * @pages: array of page pointers @@ -3658,7 +3689,7 @@ 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 =get_aligned_vm_area (size, flags,__builtin_return_address (0 ));if (!area)return NULL ;Signature · 1 line
2.34.1
This one LGTM:
--
Uladzislau Rezki
Re: [PATCH v3 6/6] mm/vmalloc: align vm_area so vmap() can batch mappings
In reply to Wen JiangQuoted 10 lines · Wen Jiang
From: "Barry Song (Xiaomi)" <[email protected]>
Try to align the vmap virtual address to
PMD_SHIFTor a larger PTE mapping size hinted by the architecture, so contiguous pages can be batch-mapped when setting PMD or PTE entries.Signed-off-by Barry Song (Xiaomi) <[email protected]>Signed-off-by Wen Jiang <[email protected]>Tested-by Xueyuan Chen <[email protected]>
Hmm okay I would have preferred to squash this in the previous, but the correctness of previous patch does not rely on this, so it's fine.
mm/vmalloc.c static int vmap_batched(unsigned long addr, unsigned long end, · line 3622
Quoted 12 lines
--- mm/vmalloc.c | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-)mm/vmalloc.c +0
-0@@ -3620,6 +3620,37 @@ static int vmap_batched(unsigned long addr, unsigned long end,return err; }
This is screaming for a helper :)
mm/vmalloc.c static int vmap_batched(unsigned long addr, unsigned long end, · line 3624
Quoted 2 lines
mm/vmalloc.c +2
-0+static struct vm_struct *get_aligned_vm_area (unsigned long size, +unsigned long flags,const void *caller)
Call this vmap_get_aligned_vm_area, then ...
mm/vmalloc.c static int vmap_batched(unsigned long addr, unsigned long end, · line 3633
Quoted 9 lines
mm/vmalloc.c +9
-0+{ +struct vm_struct *vm_area; +unsigned int shift; + +/* Try PMD alignment for large sizes */ +if (size >=PMD_SIZE ) { + vm_area =__get_vm_area_node (size,PMD_SIZE ,PAGE_SHIFT , flags, +VMALLOC_START ,VMALLOC_END , +NUMA_NO_NODE ,GFP_KERNEL , caller);
Add a wrapper over this called __get_vm_area_node_aligned_caller, which can call __get_vm_area_node() with all other arguments fixed, except "align".
Quoted 32 lines
mm/vmalloc.c +21
-1+if (vm_area) +return vm_area; + } + +/* Try CONT_PTE alignment */ + shift =arch_vmap_pte_supported_shift (size); +if (shift >PAGE_SHIFT ) { + vm_area =__get_vm_area_node (size,1UL << shift,PAGE_SHIFT , flags, +VMALLOC_START ,VMALLOC_END , +NUMA_NO_NODE ,GFP_KERNEL , caller); +if (vm_area) +return vm_area; + } + +/* Fall back to page alignment */ +return __get_vm_area_node (size,PAGE_SIZE ,PAGE_SHIFT , flags, +VMALLOC_START ,VMALLOC_END , +NUMA_NO_NODE ,GFP_KERNEL , caller); +} +/** * vmap - map an array of pages into virtually contiguous space * @pages: array of page pointers @@ -3658,7 +3689,7 @@ 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 =get_aligned_vm_area (size, flags,__builtin_return_address (0 ));if (!area)return NULL ;
Re: [PATCH v3 6/6] mm/vmalloc: align vm_area so vmap() can batch mappings
Quoted 34 lines · Dev Jain
Quoted 10 lines · Wen Jiang
From: "Barry Song (Xiaomi)" <[email protected]>
Try to align the vmap virtual address to
PMD_SHIFTor a larger PTE mapping size hinted by the architecture, so contiguous pages can be batch-mapped when setting PMD or PTE entries.Signed-off-by Barry Song (Xiaomi) <[email protected]>Signed-off-by Wen Jiang <[email protected]>Tested-by Xueyuan Chen <[email protected]>Hmm okay I would have preferred to squash this in the previous, but the correctness of previous patch does not rely on this, so it's fine.
mm/vmalloc.c static int vmap_batched(unsigned long addr, unsigned long end, · line 3622
Quoted 12 lines
--- mm/vmalloc.c | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-)mm/vmalloc.c +0
-0@@ -3620,6 +3620,37 @@ static int vmap_batched(unsigned long addr, unsigned long end,return err; }This is screaming for a helper :)
Quoted 7 lines
mm/vmalloc.c static int vmap_batched(unsigned long addr, unsigned long end, · line 3624
Quoted 2 lines
mm/vmalloc.c +2
-0+static struct vm_struct *get_aligned_vm_area (unsigned long size, +unsigned long flags,const void *caller)Call this
vmap_get_aligned_vm_area, then ...
Will rename get_aligned_vm_area to vmap_get_aligned_vm_area.
Quoted 4 lines
Quoted 4 lines
mm/vmalloc.c +4
-0+{ +struct vm_struct *vm_area; +unsigned int shift; +
Quoted 9 lines
mm/vmalloc.c static int vmap_batched(unsigned long addr, unsigned long end, · line 3633
Quoted 5 lines
mm/vmalloc.c +5
-0+/* Try PMD alignment for large sizes */ +if (size >=PMD_SIZE ) { + vm_area =__get_vm_area_node (size,PMD_SIZE ,PAGE_SHIFT , flags, +VMALLOC_START ,VMALLOC_END , +NUMA_NO_NODE ,GFP_KERNEL , caller);Add a wrapper over this called
__get_vm_area_node_aligned_caller, which can call__get_vm_area_node()with all other arguments fixed, except "align".
Will add a __get_vm_area_node_aligned_caller. Will send in v4.
Thanks,
Wen
Quoted 33 lines
Quoted 32 lines
mm/vmalloc.c +21
-1+if (vm_area) +return vm_area; + } + +/* Try CONT_PTE alignment */ + shift =arch_vmap_pte_supported_shift (size); +if (shift >PAGE_SHIFT ) { + vm_area =__get_vm_area_node (size,1UL << shift,PAGE_SHIFT , flags, +VMALLOC_START ,VMALLOC_END , +NUMA_NO_NODE ,GFP_KERNEL , caller); +if (vm_area) +return vm_area; + } + +/* Fall back to page alignment */ +return __get_vm_area_node (size,PAGE_SIZE ,PAGE_SHIFT , flags, +VMALLOC_START ,VMALLOC_END , +NUMA_NO_NODE ,GFP_KERNEL , caller); +} +/** * vmap - map an array of pages into virtually contiguous space * @pages: array of page pointers @@ -3658,7 +3689,7 @@ 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 =get_aligned_vm_area (size, flags,__builtin_return_address (0 ));if (!area)return NULL ;
Andrew Morton
maintainerQuoted 2 lines · On Fri, 22 May 2026 13:31:40 +0800 Wen Jiang <[email protected]> wrote:
This patchset accelerates ioremap, vmalloc, and vmap when the memory is physically fully or partially contiguous.
Thanks. AI review asked a few things and might have found an existing 32-bit bug in vmap():
https://sashiko.dev/#/patchset/[email protected]
Quoted 10 lines · Andrew Morton
Quoted 2 lines · On Fri, 22 May 2026 13:31:40 +0800 Wen Jiang <[email protected]> wrote:
This patchset accelerates ioremap, vmalloc, and vmap when the memory is physically fully or partially contiguous.
Thanks. AI review asked a few things and might have found an existing 32-bit bug in
vmap():https://sashiko.dev/#/patchset/[email protected]
Hi Andrew,
I've gone through the Sashiko findings:
- Patch 5 (arch_vmap_pte_supported_shift on x86): Over-interpretation. This targets ARM64 CONT_PTE. x86 falls through with PAGE_SHIFT same as before.
- Patch 5 (1 << order overflow at order=31): Over-interpretation. Reaching order=31 requires 8TB contiguous in a single vmap() not a realistic usage pattern.
- Patch 6 (GFP_KERNEL triggering purge): The purge only triggers when vmalloc space is already under pressure, and benefits the subsequent PAGE_SIZE fallback as well, not wasted work.
- Patch 6 (32-bit count << PAGE_SHIFT overflow): Pre-existing. Will send a separate fix.
- Patch 6 (unconditional alignment without checking contiguity):
The main vmap() users typically pass contiguous pages (e.g. system_heap order 8 -> 4 -> 0).
Thanks,
Wen
Andrew Morton
maintainerQuoted 7 lines · On Sat, 23 May 2026 16:26:36 +0800 Wen Jiang <[email protected]> wrote:
Quoted 6 lines · Andrew Morton
Quoted 2 lines · On Fri, 22 May 2026 13:31:40 +0800 Wen Jiang <[email protected]> wrote:
This patchset accelerates ioremap, vmalloc, and vmap when the memory is physically fully or partially contiguous.
Quoted 8 lines
Quoted 4 lines
Thanks. AI review asked a few things and might have found an existing 32-bit bug in
vmap():https://sashiko.dev/#/patchset/[email protected]Hi Andrew,
I've gone through the Sashiko findings:
Great, thanks. I won't take any action at this time - let's see what reviewers have to say.