mm/vmalloc: Speed up ioremap, vmalloc and vmap with contiguous memory
Subsystems (3)
v7 cover mm/vmalloc: Speed up ioremap, vmalloc and vmap with contiguous memory
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 for 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().
Patch 5 extracts vm_shift() to consolidate vmalloc mapping shift selection for reuse in the batching path.
Patches 6-7 add huge vmap support for contiguous pages, including support for non-compound pages with pfn alignment verification.
On the RK3588 8-core ARM64 SoC, with tasks pinned to a little core and the performance CPUfreq policy enabled, benchmark results:
* ioremap(1 MB): 1.35x faster (3407 ns -> 2526 ns)
* vmalloc(1 MB) mapping time (excluding allocation) with
VM_ALLOW_HUGE_VMAP: 1.42x faster (5.00 us -> 3.53 us)
* vmap(100MB) with order-8 pages: 8.3x faster (1235 us -> 149 us)
Many thanks to Xueyuan Chen for his testing efforts on RK3588 boards.
Large vmap() mappings were also tested by Leo Yan with ARM trace buffer units, including TRBE and SPE. These units use the CPU page tables for address translation when writing trace data to DRAM, so using larger vmap() mapping granules can reduce TLB pressure on the trace writer.
The TRBE test used a 1G CoreSight ETM AUX buffer. Across five runs on an isolated CPU, the average results were:
* dtlb_walk: 68.4 -> 59.4 (-13.16%)
* l1d_tlb_refill: 155.8 -> 119.6 (-23.23%)
* l2d_tlb_refill: 161435.8 -> 495.0 (-99.69%)
The SPE test used a 512M ARM SPE AUX buffer. Across five runs on an isolated CPU, the average results were:
* dtlb_walk: 1710.4 -> 1315.6 (-23.08%)
* l1d_tlb_refill: 16000.0 -> 15950.2 (-0.31%)
* l2d_tlb_refill: 4796.0 -> 2931.2 (-38.88%)
These results show that enabling larger vmap() mappings can materially reduce page table walks and TLB refills for large trace buffers.
Many thanks to Leo Yan for his testing efforts on ARM trace buffers.
Changes since v6:
- Add a clarifying comment about the reuse of hugetlb helpers by non-hugetlbfs(vmalloc) mm code (patch 1)
- Expand the arm64/vmalloc commit message and comment to clarify that multi-CONT_PTE_SIZE values are vmalloc mapping spans, not HugeTLB hstate sizes (patch 2)
- Move the local steps variable change in vmap_pte_range() into the vmap_set_ptes() extraction patch (patch 3)
- Propagate vmap_pages_pte_range() errors through the upper vmap_pages_*() levels instead of returning -ENOMEM for all failures (patch 4)
- Add a preparatory vm_shift() helper patch before the batching patch (patch 5)
- Guard the PFN alignment clamp in get_vmap_batch_order() against PFN 0 before calling __ffs() (patch 6)
- Fix kmsan_vmap_pages_range_noflush() indentation in the batching path (patch 6)
Changes since v5:
- No code changes.
- Pick up Reviewed-by and Tested-by tags from Dev, Leo and Uladzislau. Many thanks!
- Add TRBE/SPE large vmap() test results from Leo Yan to the cover letter.
Changes since v4:
- Move pgsize update before contig_ptes check (patch 1)
- Use rounddown_pow_of_two instead of __fls in arch_vmap_pte_range_map_size (patch 2)
- Reword comment to avoid mentioning cont_pte and remove if in vmap_set_ptes (patch 3)
- Rename vmap_batched() to vmap_pages_range_batched() (patch 5)
- Use batch_end as the batching cursor to avoid an unused start variable (patch 5)
- Check arch_vmap_pmd_supported before PMD mapping (patch 6)
Changes since v3:
- Squash vmap_pte_range() loop variable fix into patch 4 (patch 3, 4)
- Use shift >= PMD_SHIFT and fix *nr increment in vmap_pages_pmd_range() (patch 4)
- Pass page_shift directly without capping at PMD_SHIFT (patch 4, 5)
- Add vm_shift() helper and pass pgprot_t to get_vmap_batch_order() (patch 5)
- Use min(order, __ffs(pfn)) for graceful pfn alignment degradation, replacing IS_ALIGNED check (patch 5)
- Remove irrelevant ioremap_max_page_shift early-exit (patch 5)
- Add __get_vm_area_node_aligned_caller() wrapper, rename to vmap_get_aligned_vm_area() (patch 6)
Changes since v2:
- Use __fls instead of fls in arch_vmap_pte_range_map_size (patch 2)
- Add WARN_ON checks in vmap_pages_pmd_range (patch 4)
- Fix flush_cache_vmap to use saved start address instead of the already-advanced addr (patch 5)
- Rename __vmap_huge() to vmap_batched() (patch 5)
- Add caller parameter and unroll while(1) loop (patch 5)
- Squash patch 7 into patch 5 (stop scanning for compound pages after encountering small pages)
Changes since v1:
- Fix condition order and use PMD_SIZE instead of CONT_PMD_SIZE in patch 1 (Dev Jain)
- Squash patch 3+4 and patch 5+7 (Dev Jain)
- Replace "zigzag" with "page table rewalk" in commit messages (Dev Jain)
- Rename vmap_small_pages_range_noflush() to vmap_pages_range_noflush_walk() (Dev Jain)
- Extract vmap_set_ptes() as a new patch to consolidate PTE mapping logic between vmap_pte_range() and vmap_pages_pte_range(), handling both CONT_PTE and regular mappings (Mike Rapoport)
- Support non-compound pages in get_vmap_batch_order() by falling back to physical contiguity scanning with pfn alignment check (Dev Jain, Uladzislau Rezki)
- In get_vmap_batch_order(), filter out orders that the architecture cannot batch by checking arch_vmap_pte_supported_shift() directly. This avoids overhead for orders 1-3 on ARM64 CONT_PTE with 4K pages. (patch 5)
Barry Song (Xiaomi) (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 (2):
mm/vmalloc: Extract vmap_set_ptes() to consolidate PTE mapping logic mm/vmalloc: Extract vm_shift() to consolidate mapping shift selection
arch/arm64/include/asm/vmalloc.h | 8 +- arch/arm64/mm/hugetlbpage.c | 15 ++ mm/vmalloc.c | 264 ++++++++++++++++++++++++------- 3 files changed, 229 insertions(+), 58 deletions(-)
Signature · 1 line
2.34.1
PATCH v7 1/7 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 handle CONT_PTE_SIZE groups together.
These additional sizes are mapping spans used by non-hugetlbfs(vmalloc) mm code, not new HugeTLB hstate sizes.
--- arch/arm64/mm/hugetlbpage.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
arch/arm64/mm/hugetlbpage.c +15 -0
@@ -94,6 +94,11 @@ static int find_num_contig(struct mm_struct *mm, unsigned long addr,return CONT_PTES ; } +/* +* num_contig_ptes(), set_huge_pte_at() and arch_make_huge_pte() can be +* used by non-hugetlbfs(vmalloc) mm code to set multiple huge mappings +* at the PTE level. +*/ static inline int num_contig_ptes (unsigned long size,size_t *pgsize) {int contig_ptes =1 ;
@@ -110,6 +115,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 )) { + *pgsize =PAGE_SIZE ; + contig_ptes = size >>PAGE_SHIFT ; +break ; + }WARN_ON (!__hugetlb_valid_size (size)); }
@@ -359,6 +370,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",
--
2.34.1
Re: [PATCH v7 1/7] arm64/hugetlb: Extend batching of multiple CONT_PTE in a single PTE setup
Quoted 8 lines · Wen Jiang
From: "Barry Song (Xiaomi)" <[email protected]>
For sizes aligned to
CONT_PTE_SIZEand smaller thanPMD_SIZE, we can handleCONT_PTE_SIZEgroups together.These additional sizes are mapping spans used by non-hugetlbfs(vmalloc) mm code, not new HugeTLB hstate sizes.
Quoted 7 lines
Signed-off-by Barry Song (Xiaomi) <[email protected]>Signed-off-by Wen Jiang <[email protected]>Tested-by Xueyuan Chen <[email protected]>Tested-by Leo Yan <[email protected]>Reviewed-by Dev Jain <[email protected]>--- arch/arm64/mm/hugetlbpage.c | 15 +++++++++++++++
Huh, why are we touching hugetlbpage.c, which is HUGETLB-only material:
$ cat arch/arm64/mm/Makefile | grep hugetlbpage obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o
Something is off here, if vmalloc ends up reusing some of these helpers, they should probably get moved out here into common arm64 code.
Signature · 3 lines
Cheers, David
Re: [PATCH v7 1/7] arm64/hugetlb: Extend batching of multiple CONT_PTE in a single PTE setup
Quoted 18 lines · David Hildenbrand (Arm)
Quoted 8 lines · Wen Jiang
From: "Barry Song (Xiaomi)" <[email protected]>
For sizes aligned to
CONT_PTE_SIZEand smaller thanPMD_SIZE, we can handleCONT_PTE_SIZEgroups together.These additional sizes are mapping spans used by non-hugetlbfs(vmalloc) mm code, not new HugeTLB hstate sizes.
Quoted 7 lines
Signed-off-by Barry Song (Xiaomi) <[email protected]>Signed-off-by Wen Jiang <[email protected]>Tested-by Xueyuan Chen <[email protected]>Tested-by Leo Yan <[email protected]>Reviewed-by Dev Jain <[email protected]>--- arch/arm64/mm/hugetlbpage.c | 15 +++++++++++++++
Quoted 7 lines
Huh, why are we touching
hugetlbpage.c, which is HUGETLB-only material:$ cat arch/arm64/mm/Makefile | grep hugetlbpage obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.oSomething is off here, if vmalloc ends up reusing some of these helpers, they should probably get moved out here into common arm64 code.
This has been an issue since the original commit f7ee1f13d606c1 ("mm/vmalloc: enable mapping of huge pages at pte level in vmap").
I agree that this likely needs some code reorganization. However, that would probably be better handled as a separate follow-up series rather than as part of this one, since it is not a small or trivial change.
The main problem is that the kernel lacks a set_ptes() helper for kernel space. As a result, hugetlb has been repurposed for this across multiple architectures since 2021 [1].
[1] https://lore.kernel.org/all/[email protected]/
Best Regards
Barry
Will Deacon
maintainerRe: [PATCH v7 1/7] arm64/hugetlb: Extend batching of multiple CONT_PTE in a single PTE setup
In reply to Wen JiangQuoted 34 lines · Wen Jiang
From: "Barry Song (Xiaomi)" <[email protected]>
For sizes aligned to
CONT_PTE_SIZEand smaller thanPMD_SIZE, we can handleCONT_PTE_SIZEgroups together.These additional sizes are mapping spans used by non-hugetlbfs(vmalloc) mm code, not new HugeTLB hstate sizes.
Signed-off-by Barry Song (Xiaomi) <[email protected]>Signed-off-by Wen Jiang <[email protected]>Tested-by Xueyuan Chen <[email protected]>Tested-by Leo Yan <[email protected]>Reviewed-by Dev Jain <[email protected]>--- arch/arm64/mm/hugetlbpage.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)arch/arm64/mm/hugetlbpage.c +5
-0@@ -94,6 +94,11 @@ static int find_num_contig(struct mm_struct *mm, unsigned long addr,return CONT_PTES ; } +/* +* num_contig_ptes(), set_huge_pte_at() and arch_make_huge_pte() can be +* used by non-hugetlbfs(vmalloc) mm code to set multiple huge mappings +* at the PTE level. +*/ static inline int num_contig_ptes (unsigned long size,size_t *pgsize) {int contig_ptes =1 ;
arch/arm64/mm/hugetlbpage.c static inline int num_contig_ptes(unsigned long size, size_t *pgsize) · line 123
Quoted 9 lines
arch/arm64/mm/hugetlbpage.c +6
-0@@ -110,6 +115,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 )) { + *pgsize =PAGE_SIZE ; + contig_ptes = size >>PAGE_SHIFT ; +break ; + }
Under which circumstances would you get a size of 0 here?
Given that you're relying on arch_vmap_pte_range_map_size() to give you a well-formed size, why isn't if sufficient to check only the alignment?
arch/arm64/mm/hugetlbpage.c static inline int num_contig_ptes(unsigned long size, size_t *pgsize) · line 124
Quoted 1 line
arch/arm64/mm/hugetlbpage.c +0
-0WARN_ON (!__hugetlb_valid_size (size));
I agree with David that it's messy having hugetlb tangled up in here. It means this validity check is now going to miss some genuinely bogus cases for the hugetlb path (as opposed to the vmalloc path).
Will
Re: [PATCH v7 1/7] arm64/hugetlb: Extend batching of multiple CONT_PTE in a single PTE setup
Quoted 48 lines · Will Deacon
Quoted 34 lines · Wen Jiang
From: "Barry Song (Xiaomi)" <[email protected]>
For sizes aligned to
CONT_PTE_SIZEand smaller thanPMD_SIZE, we can handleCONT_PTE_SIZEgroups together.These additional sizes are mapping spans used by non-hugetlbfs(vmalloc) mm code, not new HugeTLB hstate sizes.
Signed-off-by Barry Song (Xiaomi) <[email protected]>Signed-off-by Wen Jiang <[email protected]>Tested-by Xueyuan Chen <[email protected]>Tested-by Leo Yan <[email protected]>Reviewed-by Dev Jain <[email protected]>--- arch/arm64/mm/hugetlbpage.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)arch/arm64/mm/hugetlbpage.c +5
-0@@ -94,6 +94,11 @@ static int find_num_contig(struct mm_struct *mm, unsigned long addr,return CONT_PTES ; } +/* +* num_contig_ptes(), set_huge_pte_at() and arch_make_huge_pte() can be +* used by non-hugetlbfs(vmalloc) mm code to set multiple huge mappings +* at the PTE level. +*/ static inline int num_contig_ptes (unsigned long size,size_t *pgsize) {int contig_ptes =1 ;arch/arm64/mm/hugetlbpage.c static inline int num_contig_ptes(unsigned long size, size_t *pgsize) · line 123
Quoted 9 lines
arch/arm64/mm/hugetlbpage.c +6
-0@@ -110,6 +115,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 )) { + *pgsize =PAGE_SIZE ; + contig_ptes = size >>PAGE_SHIFT ; +break ; + }Under which circumstances would you get a size of 0 here?
Quoted 2 lines
Given that you're relying on
arch_vmap_pte_range_map_size()to give you a well-formed size, why isn't if sufficient to check only the alignment?
Hi Will,
Thanks very much for your review.
Are you suggesting the change below? If so, I'm fine with it. I guess the current code is just being overly cautious for defensive programming.
arch/arm64/mm/hugetlbpage.c +0 -0
@@ -115,8 +115,7 @@ 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)) {
+ if (IS_ALIGNED(size, CONT_PTE_SIZE)) {
*pgsize = PAGE_SIZE;
contig_ptes = size >> PAGE_SHIFT;
break;Quoted 6 lines
arch/arm64/mm/hugetlbpage.c static inline int num_contig_ptes(unsigned long size, size_t *pgsize) · line 124
Quoted 2 lines
arch/arm64/mm/hugetlbpage.c +0
-0WARN_ON (!__hugetlb_valid_size (size));I agree with David that it's messy having hugetlb tangled up in here. It means this validity check is now going to miss some genuinely bogus cases for the hugetlb path (as opposed to the vmalloc path).
I agree that coupling hugetlb with vmalloc is not ideal. As I explained to David, this has been an issue for a couple of years and affects multiple architectures since 2021:
where #ifdef CONFIG_HUGETLB_PAGE is required by vmalloc.
So I'd prefer to address it in a separate follow-up patch series.
For the WARN_ON(!__hugetlb_valid_size(size)) check, I don't see anything broken here. Hugetlb only uses sizes registered by hugetlbpage_init(), which are validated by arch_hugetlb_valid_size() implemented in arch/arm64/mm/hugetlbpage.c:
static int __init hugetlbpage_init(void)
{
BUILD_BUG_ON(HUGE_MAX_HSTATE < 4);
if (pud_sect_supported())
hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
hugetlb_add_hstate(CONT_PMD_SHIFT - PAGE_SHIFT);
hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
hugetlb_add_hstate(CONT_PTE_SHIFT - PAGE_SHIFT);
return 0;
}arch_initcall(hugetlbpage_init);
bool __init arch_hugetlb_valid_size(unsigned long size)
{
return __hugetlb_valid_size(size);
}Thanks
Barry
Re: [PATCH v7 1/7] arm64/hugetlb: Extend batching of multiple CONT_PTE in a single PTE setup
Quoted 11 lines
I agree that coupling hugetlb with vmalloc is not ideal. As I explained to David, this has been an issue for a couple of years and affects multiple architectures since 2021:
where
#ifdef CONFIG_HUGETLB_PAGEis required byvmalloc.So I'd prefer to address it in a separate follow-up patch series.
It's the perfect time to sort that out now :)
Signature · 3 lines
Cheers, David
Re: [PATCH v7 1/7] arm64/hugetlb: Extend batching of multiple CONT_PTE in a single PTE setup
Quoted 6 lines · David Hildenbrand (Arm)
Quoted 6 lines
I agree that coupling hugetlb with vmalloc is not ideal. As I explained to David, this has been an issue for a couple of years and affects multiple architectures since 2021:
Quoted 8 lines
Quoted 6 lines
where
#ifdef CONFIG_HUGETLB_PAGEis required byvmalloc.So I'd prefer to address it in a separate follow-up patch series.
It's the perfect time to sort that out now :)
OK, looks like I'll be working until at least 1 p.m. again today and tomorrow. :-)
Best Regards
Barry
Re: [PATCH v7 1/7] arm64/hugetlb: Extend batching of multiple CONT_PTE in a single PTE setup
Quoted 12 lines · Barry Song
Quoted 10 lines · David Hildenbrand (Arm)
Quoted 10 lines
I agree that coupling hugetlb with vmalloc is not ideal. As I explained to David, this has been an issue for a couple of years and affects multiple architectures since 2021:
where
#ifdef CONFIG_HUGETLB_PAGEis required byvmalloc.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
So I'd prefer to address it in a separate follow-up patch series.
It's the perfect time to sort that out now :)
OK, looks like I'll be working until at least 1 p.m. again today and tomorrow. :-)
Did you mean 1 am? I hope not :D
No need to rush, we're mostly done with new stuff for the upcoming merge window either way!
Signature · 3 lines
Cheers, David
Will Deacon
maintainerRe: [PATCH v7 1/7] arm64/hugetlb: Extend batching of multiple CONT_PTE in a single PTE setup
In reply to Barry SongQuoted 72 lines · Barry Song
Quoted 48 lines · Will Deacon
Quoted 34 lines · Wen Jiang
From: "Barry Song (Xiaomi)" <[email protected]>
For sizes aligned to
CONT_PTE_SIZEand smaller thanPMD_SIZE, we can handleCONT_PTE_SIZEgroups together.These additional sizes are mapping spans used by non-hugetlbfs(vmalloc) mm code, not new HugeTLB hstate sizes.
Signed-off-by Barry Song (Xiaomi) <[email protected]>Signed-off-by Wen Jiang <[email protected]>Tested-by Xueyuan Chen <[email protected]>Tested-by Leo Yan <[email protected]>Reviewed-by Dev Jain <[email protected]>--- arch/arm64/mm/hugetlbpage.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)arch/arm64/mm/hugetlbpage.c +5
-0@@ -94,6 +94,11 @@ static int find_num_contig(struct mm_struct *mm, unsigned long addr,return CONT_PTES ; } +/* +* num_contig_ptes(), set_huge_pte_at() and arch_make_huge_pte() can be +* used by non-hugetlbfs(vmalloc) mm code to set multiple huge mappings +* at the PTE level. +*/ static inline int num_contig_ptes (unsigned long size,size_t *pgsize) {int contig_ptes =1 ;arch/arm64/mm/hugetlbpage.c static inline int num_contig_ptes(unsigned long size, size_t *pgsize) · line 123
Quoted 9 lines
arch/arm64/mm/hugetlbpage.c +6
-0@@ -110,6 +115,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 )) { + *pgsize =PAGE_SIZE ; + contig_ptes = size >>PAGE_SHIFT ; +break ; + }Under which circumstances would you get a size of 0 here?
Quoted 2 lines
Given that you're relying on
arch_vmap_pte_range_map_size()to give you a well-formed size, why isn't if sufficient to check only the alignment?Thanks very much for your review.
Are you suggesting the change below? If so, I'm fine with it. I guess the current code is just being overly cautious for defensive programming.
arch/arm64/mm/hugetlbpage.c +1
-2@@ -115,8 +115,7 @@ 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 )) { +if (IS_ALIGNED (size,CONT_PTE_SIZE )) { *pgsize =PAGE_SIZE ; contig_ptes = size >>PAGE_SHIFT ;
Quoted 2 lines
break;
Yeah, that's what I had in mind.
Quoted 35 lines
Quoted 5 lines
arch/arm64/mm/hugetlbpage.c static inline int num_contig_ptes(unsigned long size, size_t *pgsize) · line 124
Quoted 1 line
arch/arm64/mm/hugetlbpage.c +0
-0WARN_ON (!__hugetlb_valid_size (size));I agree with David that it's messy having hugetlb tangled up in here. It means this validity check is now going to miss some genuinely bogus cases for the hugetlb path (as opposed to the vmalloc path).
I agree that coupling hugetlb with vmalloc is not ideal. As I explained to David, this has been an issue for a couple of years and affects multiple architectures since 2021:
where
#ifdef CONFIG_HUGETLB_PAGEis required byvmalloc.So I'd prefer to address it in a separate follow-up patch series.
For the
WARN_ON(!__hugetlb_valid_size(size))check, I don't see anything broken here. Hugetlb only uses sizes registered byhugetlbpage_init(), which are validated byarch_hugetlb_valid_size()implemented inarch/arm64/mm/hugetlbpage.c:static int
__inithugetlbpage_init(void){ BUILD_BUG_ON(HUGE_MAX_HSTATE < 4); if (pud_sect_supported()) hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT); hugetlb_add_hstate(CONT_PMD_SHIFT - PAGE_SHIFT); hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT); hugetlb_add_hstate(CONT_PTE_SHIFT - PAGE_SHIFT); return 0; }
Quoted 6 lines
arch_initcall(hugetlbpage_init);bool
__initarch_hugetlb_valid_size(unsigned long size){ return __hugetlb_valid_size(size); }
I'm just pointing out that the defensive checking in __hugetlb_valid_size(), which should really only warn if something has gone horribly wrong, will now not detect bogus sizes if the address is aligned to CONT_PTE_SIZE.
Keeping hugetlb and vmalloc separate would avoid this problem.
Will
PATCH v7 2/7 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 across multiple CONT_PTE blocks, reducing both PTE setup and TLB flush iterations.
For CONT_PTE_SIZE-aligned ranges, return a power-of-two mapping size that may cover multiple CONT_PTE blocks, capped below PMD_SIZE. These sizes are vmalloc mapping spans, not HugeTLB hstate sizes.
--- arch/arm64/include/asm/vmalloc.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
arch/arm64/include/asm/vmalloc.h +7 -1
@@ -23,10 +23,14 @@ static inline unsigned long arch_vmap_pte_range_map_size(unsigned long addr, unsigned long end, u64 pfn, unsigned int max_page_shift) { + unsigned long size; +/* * If the block is at least CONT_PTE_SIZE in size, and is naturally * aligned in both virtual and physical space, then we can pte-map the * block using the PTE_CONT bit for more efficient use of the TLB. +* The returned mapping size may cover multiple CONT_PTE_SIZE blocks, +* capped below PMD_SIZE. */ if (max_page_shift <CONT_PTE_SHIFT )return PAGE_SIZE ;
@@ -40,7 +44,9 @@ static inline unsigned long arch_vmap_pte_range_map_size(unsigned long addr,if (!IS_ALIGNED (PFN_PHYS (pfn),CONT_PTE_SIZE ))return PAGE_SIZE ; -return CONT_PTE_SIZE ; + size =min3 (end - addr,1UL << max_page_shift,PMD_SIZE >>1 ); + size =rounddown_pow_of_two (size); +return size; }#define arch_vmap_pte_range_unmap_size arch_vmap_pte_range_unmap_size
--
2.34.1
Re: [PATCH v7 2/7] arm64/vmalloc: Allow arch_vmap_pte_range_map_size to batch multiple CONT_PTE
Barry, Wen - the returned span is capped at PMD_SIZE >> 1 but only checked for CONT_PTE_SIZE alignment. In the batched vmap() path pages are only contiguous within a naturally aligned block, so when a PMD boundary splits a batch the next region starts mid block and the span runs past the contiguous run - set_huge_pte_at() maps the wrong pages, tail of the array left unmapped, no warning.
What do you think of clamping the span to the pfn physical alignment, like 8xx does:
while (!IS_ALIGNED(PFN_PHYS(pfn), size)) size >>= 1;
Cheers.
Re: [PATCH v7 2/7] arm64/vmalloc: Allow arch_vmap_pte_range_map_size to batch multiple CONT_PTE
Quoted 8 lines · David Carlier
Barry, Wen - the returned span is capped at
PMD_SIZE>> 1 but only checked forCONT_PTE_SIZEalignment. In the batchedvmap()path pages are only contiguous within a naturally aligned block, so when a PMD boundary splits a batch the next region starts mid block and the span runs past the contiguous run -set_huge_pte_at()maps the wrong pages, tail of the array left unmapped, no warning.
Quoted 5 lines
What do you think of clamping the span to the pfn physical alignment, like 8xx does:
while (!IS_ALIGNED(PFN_PHYS(pfn), size))
size >>= 1;
Thanks, David.
I noticed that 8xx has simplified this to a simple
if (!IS_ALIGNED(PFN_PHYS(pfn), size))
Could we do the same here?
arch/arm64/include/asm/vmalloc.h +0 -0
@@ -46,6 +46,8 @@ static inline unsigned long
arch_vmap_pte_range_map_size(unsigned long addr,
size = min3(end - addr, 1UL << max_page_shift, PMD_SIZE >> 1);
size = rounddown_pow_of_two(size);
+ if (!IS_ALIGNED(PFN_PHYS(pfn), size) || size < CONT_PTE_SIZE)
+ return PAGE_SIZE;
return size;}
Quoted 2 lines
Cheers.
Re: [PATCH v7 2/7] arm64/vmalloc: Allow arch_vmap_pte_range_map_size to batch multiple CONT_PTE
Quoted 29 lines · Barry Song
Quoted 8 lines · David Carlier
Barry, Wen - the returned span is capped at
PMD_SIZE>> 1 but only checked forCONT_PTE_SIZEalignment. In the batchedvmap()path pages are only contiguous within a naturally aligned block, so when a PMD boundary splits a batch the next region starts mid block and the span runs past the contiguous run -set_huge_pte_at()maps the wrong pages, tail of the array left unmapped, no warning.Quoted 5 lines
What do you think of clamping the span to the pfn physical alignment, like 8xx does:
while (!IS_ALIGNED(PFN_PHYS(pfn), size))
size >>= 1;Thanks, David.
I noticed that 8xx has simplified this to a simple
if (!IS_ALIGNED(PFN_PHYS(pfn), size))Could we do the same here?
arch/arm64/include/asm/vmalloc.h +0
-0
arch/arm64/include/asm/vmalloc.h static inline unsigned long · line 53
Quoted 8 lines
arch/arm64/include/asm/vmalloc.h +2
-0@@ -46,6 +46,8 @@ static inline unsigned long arch_vmap_pte_range_map_size(unsigned long addr, size =min3 (end - addr,1UL << max_page_shift,PMD_SIZE >>1 ); size =rounddown_pow_of_two (size); +if (!IS_ALIGNED (PFN_PHYS (pfn), size) || size <CONT_PTE_SIZE ) +return PAGE_SIZE ;return size; }
On second thought, the caller is walking the PUD, PMD, and PTE page tables, so it won't pass anything that crosses a PMD boundary here.
otherwise, the caller has been wrong. for example:
static int vmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
phys_addr_t phys_addr, pgprot_t prot, unsigned int max_page_shift, pgtbl_mod_mask *mask)
{
pmd_t *pmd;
unsigned long next;
int err = 0;
...
do {
next = pmd_addr_end(addr, end);
if (vmap_try_huge_pmd(pmd, addr, next, phys_addr, prot,
max_page_shift)) {
*mask |= PGTBL_PMD_MODIFIED;
continue;
}
err = vmap_pte_range(pmd, addr, next, phys_addr, prot,
max_page_shift, mask);
if (err)
break;
} while (pmd++, phys_addr += (next - addr), addr = next, addr != end);
return err;
}I guess we don't need to do anything here, David?
Barry
Re: [PATCH v7 2/7] arm64/vmalloc: Allow arch_vmap_pte_range_map_size to batch multiple CONT_PTE
In reply to Barry SongReview
Quoted 5 lines
Could we do the same here?
(quoted diff) +2
-0+ if (!IS_ALIGNED(PFN_PHYS(pfn), size) || size < CONT_PTE_SIZE) + return PAGE_SIZE;
Thanks for looking.
I don't think it carries over. 8xx tries two candidate sizes, so a failed alignment test falls back to the smaller one, whereas here we would return PAGE_SIZE and lose CONT_PTE over most of the span.
My concern was misplaced anyway, sorry. [addr, end) is always a single contiguous run here, and addr and PFN_PHYS(pfn) are already CONT_PTE_SIZE aligned, so every 16-entry group in the batch is naturally aligned. No extra check needed, the patch looks fine as is.
Cheers.
Will Deacon
maintainerRe: [PATCH v7 2/7] arm64/vmalloc: Allow arch_vmap_pte_range_map_size to batch multiple CONT_PTE
In reply to Wen JiangQuoted 38 lines · Wen Jiang
From: "Barry Song (Xiaomi)" <[email protected]>
Allow
arch_vmap_pte_range_map_sizeto batch across multipleCONT_PTEblocks, reducing both PTE setup and TLB flush iterations.For CONT_PTE_SIZE-aligned ranges, return a power-of-two mapping size that may cover multiple
CONT_PTEblocks, capped belowPMD_SIZE. These sizes are vmalloc mapping spans, not HugeTLB hstate sizes.Signed-off-by Barry Song (Xiaomi) <[email protected]>Signed-off-by Wen Jiang <[email protected]>Tested-by Xueyuan Chen <[email protected]>Tested-by Leo Yan <[email protected]>Reviewed-by Dev Jain <[email protected]>--- arch/arm64/include/asm/vmalloc.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)arch/arm64/include/asm/vmalloc.h +4
-0@@ -23,10 +23,14 @@ static inline unsigned long arch_vmap_pte_range_map_size(unsigned long addr, unsigned long end, u64 pfn, unsigned int max_page_shift) { + unsigned long size; +/* * If the block is at least CONT_PTE_SIZE in size, and is naturally * aligned in both virtual and physical space, then we can pte-map the * block using the PTE_CONT bit for more efficient use of the TLB. +* The returned mapping size may cover multiple CONT_PTE_SIZE blocks, +* capped below PMD_SIZE. */ if (max_page_shift <CONT_PTE_SHIFT )return PAGE_SIZE ;
arch/arm64/include/asm/vmalloc.h static inline unsigned long arch_vmap_pte_range_map_size(unsigned long addr, · line 49
Quoted 7 lines
arch/arm64/include/asm/vmalloc.h +3
-1@@ -40,7 +44,9 @@ static inline unsigned long arch_vmap_pte_range_map_size(unsigned long addr,if (!IS_ALIGNED (PFN_PHYS (pfn),CONT_PTE_SIZE ))return PAGE_SIZE ; -return CONT_PTE_SIZE ; + size =min3 (end - addr,1UL << max_page_shift,PMD_SIZE >>1 ); + size =rounddown_pow_of_two (size); +return size;
Why does this have to be a power of two? We should be able to work with regions where the start and end are suitably aligned. Is it because the hugetlb code works in terms of shifts?
Will
Re: [PATCH v7 2/7] arm64/vmalloc: Allow arch_vmap_pte_range_map_size to batch multiple CONT_PTE
Quoted 1 line · Will Deacon
[...]
Quoted 4 lines
Quoted 4 lines
arch/arm64/include/asm/vmalloc.h +0
-0@@ -40,7 +44,9 @@ static inline unsigned long arch_vmap_pte_range_map_size(unsigned long addr,if (!IS_ALIGNED (PFN_PHYS (pfn),CONT_PTE_SIZE ))return PAGE_SIZE ;
Quoted 8 lines
arch/arm64/include/asm/vmalloc.h static inline unsigned long arch_vmap_pte_range_map_size(unsigned long addr, · line 49
Quoted 4 lines
arch/arm64/include/asm/vmalloc.h +3
-1-return CONT_PTE_SIZE ; + size =min3 (end - addr,1UL << max_page_shift,PMD_SIZE >>1 ); + size =rounddown_pow_of_two (size); +return size;Why does this have to be a power of two? We should be able to work with regions where the start and end are suitably aligned. Is it because the hugetlb code works in terms of shifts?
Yep. For vmap, vmalloc, and hugetlb, this is all about the page size shift, since their memory is typically backed by the buddy allocator. However, after taking another look, I think ioremap() could benefit. For example, if someone calls ioremap() on a 576 KB region (64 KB * 9), we currently have to map it as 512 KB + 64 KB. Without our patch, it would require nine 64 KB mappings. If we allowed non-power-of-two sizes, it could be mapped with single time.
So supporting non-power-of-two sizes might provide a small performance benefit for ioremap(), although I suspect the gain would be limited. I can investigate this as a follow-up task, and if it turns out to provide a measurable improvement, I can send an incremental patch.
Thanks
Barry
PATCH v7 3/7 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.
No functional change.
--- mm/vmalloc.c | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-)
mm/vmalloc.c +32 -15
@@ -91,6 +91,33 @@ struct vfree_deferred { static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred);/*** Page table manipulation functions ***/ + +/* +* Try contiguous mappings at the PTE level for arches which support them, and if +* requested by the caller. Fall back to PAGE_SIZE mappings otherwise. +* +* Return: mapping size. +*/ +static __always_inline unsigned long vmap_set_ptes (pte_t *pte, +unsigned long addr,unsigned long end,u64 pfn, +pgprot_t prot,unsigned int max_page_shift) +{ +#ifdef CONFIG_HUGETLB_PAGE +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 +125,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 +147,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 ;
--
2.34.1
PATCH v7 4/7 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 | 86 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 34 deletions(-)
mm/vmalloc.c +52 -34
@@ -541,8 +541,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;
@@ -573,9 +575,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 ;
@@ -585,60 +588,90 @@ static int vmap_pages_pte_range(pmd_t *pmd, unsigned long addr,static int vmap_pages_pmd_range (pud_t *pud,unsigned long addr,unsigned long end,pgprot_t prot,struct page **pages,int *nr, -pgtbl_mod_mask *mask) +pgtbl_mod_mask *mask,unsigned int shift) {pmd_t *pmd;unsigned long next; +int err; pmd =pmd_alloc_track (&init_mm, pud, addr, mask);if (!pmd)return -ENOMEM ;do { next =pmd_addr_end (addr, end); -if (vmap_pages_pte_range (pmd, addr, next, prot, pages, nr, mask)) -return -ENOMEM ; + +if (shift >=PMD_SHIFT ) { +struct page *page = pages[*nr]; +phys_addr_t phys_addr; + +if (WARN_ON (!page)) +return -ENOMEM ; +if (WARN_ON (!pfn_valid (page_to_pfn (page)))) +return -EINVAL ; + + phys_addr =page_to_phys (page); + +if (vmap_try_huge_pmd (pmd, addr, next, phys_addr, prot, + shift)) { + *mask |=PGTBL_PMD_MODIFIED ; + *nr +=1 << (PMD_SHIFT -PAGE_SHIFT ); +continue ; + } + } + + err =vmap_pages_pte_range (pmd, addr, next, prot, pages, nr, mask, shift); +if (err) +return err; }while (pmd++, addr = next, addr != end);return 0 ; }static int vmap_pages_pud_range (p4d_t *p4d,unsigned long addr,unsigned long end,pgprot_t prot,struct page **pages,int *nr, -pgtbl_mod_mask *mask) +pgtbl_mod_mask *mask,unsigned int shift) {pud_t *pud;unsigned long next; +int err; pud =pud_alloc_track (&init_mm, p4d, addr, mask);if (!pud)return -ENOMEM ;do { next =pud_addr_end (addr, end); -if (vmap_pages_pmd_range (pud, addr, next, prot, pages, nr, mask)) -return -ENOMEM ; + err =vmap_pages_pmd_range (pud, addr, next, prot, pages, nr, mask, shift); +if (err) +return err; }while (pud++, addr = next, addr != end);return 0 ; }static int vmap_pages_p4d_range (pgd_t *pgd,unsigned long addr,unsigned long end,pgprot_t prot,struct page **pages,int *nr, -pgtbl_mod_mask *mask) +pgtbl_mod_mask *mask,unsigned int shift) {p4d_t *p4d;unsigned long next; +int err; p4d =p4d_alloc_track (&init_mm, pgd, addr, mask);if (!p4d)return -ENOMEM ;do { next =p4d_addr_end (addr, end); -if (vmap_pages_pud_range (p4d, addr, next, prot, pages, nr, mask)) -return -ENOMEM ; + err =vmap_pages_pud_range (p4d, addr, next, prot, pages, nr, mask, shift); +if (err) +return err; }while (p4d++, addr = next, addr != end);return 0 ; } -static int vmap_small_pages_range_noflush (unsigned long addr,unsigned long end, -pgprot_t prot,struct page **pages) +/* +* It can take an array of pages which are not all contiguous, but it +* may have contiguous chunks, as hinted by @shift. +*/ +static int vmap_pages_range_noflush_walk (unsigned long addr,unsigned long end, +pgprot_t prot,struct page **pages,unsigned int shift) {unsigned long start = addr;pgd_t *pgd;
@@ -653,7 +686,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);
@@ -676,27 +709,12 @@ static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end,int __vmap_pages_range_noflush (unsigned long addr,unsigned long end,pgprot_t prot,struct page **pages,unsigned int page_shift) { -unsigned int i, nr = (end - addr) >>PAGE_SHIFT ; -WARN_ON (page_shift <PAGE_SHIFT ); -if (!IS_ENABLED (CONFIG_HAVE_ARCH_HUGE_VMALLOC ) || - page_shift ==PAGE_SHIFT ) -return vmap_small_pages_range_noflush (addr, end, prot, pages); +if (!IS_ENABLED (CONFIG_HAVE_ARCH_HUGE_VMALLOC )) + page_shift =PAGE_SHIFT ; -for (i =0 ; i < nr; i +=1U << (page_shift -PAGE_SHIFT )) { -int err; - - err =vmap_range_noflush (addr, addr + (1UL << page_shift), -page_to_phys (pages[i]), prot, - page_shift); -if (err) -return err; - - addr +=1UL << page_shift; - } - -return 0 ; +return vmap_pages_range_noflush_walk (addr, end, prot, pages, page_shift); } int vmap_pages_range_noflush(unsigned long addr, unsigned long end,
--
2.34.1
PATCH v7 5/7 mm/vmalloc: Extract vm_shift() to consolidate mapping shift selection
In reply to Wen JiangExtract the vmalloc mapping shift selection from __vmalloc_node_range_noprof() into vm_shift(), preparing for reuse by the vmap batching path.
No functional change.
--- mm/vmalloc.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
mm/vmalloc.c +9 -5
@@ -3549,6 +3549,14 @@ void vunmap(const void *addr) }EXPORT_SYMBOL (vunmap); +static inline unsigned int vm_shift (pgprot_t prot,unsigned long size) +{ +if (arch_vmap_pmd_supported (prot) && size >=PMD_SIZE ) +return PMD_SHIFT ; + +return arch_vmap_pte_supported_shift (size); +} +/** * vmap - map an array of pages into virtually contiguous space * @pages: array of page pointers
@@ -4047,11 +4055,7 @@ void *__vmalloc_node_range_noprof(unsigned long size, unsigned long align,* supporting them. */ -if (arch_vmap_pmd_supported (prot) && size >=PMD_SIZE ) - shift =PMD_SHIFT ; -else - shift =arch_vmap_pte_supported_shift (size); - + shift =vm_shift (prot, size); align =max (original_align,1UL << shift); }
--
2.34.1
Re: [PATCH v7 5/7] mm/vmalloc: Extract vm_shift() to consolidate mapping shift selection
Quoted 11 lines · Wen Jiang
Extract the vmalloc mapping shift selection from
__vmalloc_node_range_noprof()intovm_shift(), preparing for reuse by the vmap batching path.No functional change.
Suggested-by Dev Jain <[email protected]>Signed-off-by Wen Jiang <[email protected]>Tested-by Xueyuan Chen <[email protected]>Tested-by Leo Yan <[email protected]>---
Signature · 3 lines
Cheers, David
Re: [PATCH v7 5/7] mm/vmalloc: Extract vm_shift() to consolidate mapping shift selection
In reply to Wen JiangQuoted 11 lines · Wen Jiang
Extract the vmalloc mapping shift selection from
__vmalloc_node_range_noprof()intovm_shift(), preparing for reuse by the vmap batching path.No functional change.
Suggested-by Dev Jain <[email protected]>Signed-off-by Wen Jiang <[email protected]>Tested-by Xueyuan Chen <[email protected]>Tested-by Leo Yan <[email protected]>---
Quoted 38 lines
mm/vmalloc.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)mm/vmalloc.c +9
-6@@ -3549,6 +3549,14 @@ void vunmap(const void *addr) }EXPORT_SYMBOL (vunmap); +static inline unsigned int vm_shift (pgprot_t prot,unsigned long size) +{ +if (arch_vmap_pmd_supported (prot) && size >=PMD_SIZE ) +return PMD_SHIFT ; + +return arch_vmap_pte_supported_shift (size); +} +/** * vmap - map an array of pages into virtually contiguous space * @pages: array of page pointers @@ -4047,11 +4055,7 @@ void *__vmalloc_node_range_noprof(unsigned long size, unsigned long align,* supporting them. */ -if (arch_vmap_pmd_supported (prot) && size >=PMD_SIZE ) - shift =PMD_SHIFT ; -else - shift =arch_vmap_pte_supported_shift (size); - + shift =vm_shift (prot, size); align =max (original_align,1UL << shift); } --2.34.1
PATCH v7 6/7 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 mapping order is determined by taking the minimum of the contiguous page count and the pfn alignment, allowing graceful degradation when pfn alignment is less than the contiguous range.
Pages with the same page_shift are coalesced and mapped via vmap_pages_range_noflush_walk() to avoid page table rewalk.
As users typically allocate memory in descending orders (e.g. 8 → 4 → 0), once an order-0 page is encountered, we stop scanning for contiguous pages since subsequent pages are likely order-0 as well.
--- mm/vmalloc.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 2 deletions(-)
mm/vmalloc.c +80 -2
@@ -3557,6 +3557,84 @@ static inline unsigned int vm_shift(pgprot_t prot, unsigned long size)return arch_vmap_pte_supported_shift (size); } +static inline int get_vmap_batch_order (struct page **pages, +pgprot_t prot,unsigned int max_steps,unsigned int idx) +{ +unsigned long pfn; +unsigned int nr_contig; +int order; + +if (!IS_ENABLED (CONFIG_HAVE_ARCH_HUGE_VMAP )) +return 0 ; + + nr_contig =num_pages_contiguous (&pages[idx], max_steps); +if (nr_contig <2 ) +return 0 ; + + order =ilog2 (nr_contig); + pfn =page_to_pfn (pages[idx]); + +/* Limit order by pfn alignment */ +if (pfn >0 ) + order =min_t (int, order,__ffs (pfn)); + +if (vm_shift (prot,PAGE_SIZE << order) ==PAGE_SHIFT ) +return 0 ; + +return order; +} + +static int vmap_pages_range_batched (unsigned long addr,unsigned long end, +pgprot_t prot,struct page **pages) +{ +unsigned int count = (end - addr) >>PAGE_SHIFT ; +unsigned int prev_shift =0 , idx =0 ; +unsigned long map_addr = addr, batch_end = addr; +int err; + + err =kmsan_vmap_pages_range_noflush (addr, end, prot, pages, +PAGE_SHIFT ,GFP_KERNEL ); +if (err) + goto out; + +for (unsigned int i =0 ; i < count; ) { +unsigned int shift =PAGE_SHIFT + +get_vmap_batch_order (pages, prot, count - i, i); + +if (!i) + prev_shift = shift; + +if (shift != prev_shift) { + err =vmap_pages_range_noflush_walk (map_addr, batch_end, + prot, pages + idx, prev_shift); +if (err) + goto out; + prev_shift = shift; + map_addr = batch_end; + idx = i; + } + +/* +* Once small pages are encountered, the remaining pages +* are likely small as well. +*/ +if (shift ==PAGE_SHIFT ) +break ; + + batch_end +=1UL << shift; + i +=1U << (shift -PAGE_SHIFT ); + } + +/* Remaining */ +if (map_addr < end) + err =vmap_pages_range_noflush_walk (map_addr, end, + prot, pages + idx, prev_shift); + +out: +flush_cache_vmap (addr, end); +return err; +} +/** * vmap - map an array of pages into virtually contiguous space * @pages: array of page pointers
@@ -3600,8 +3678,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_pages_range_batched (addr, addr + size,pgprot_nx (prot), + pages) <0 ) {vunmap (area->addr);return NULL ; }
--
2.34.1
Re: [PATCH v7 6/7] mm/vmalloc: map contiguous pages in batches for vmap() if possible
Quoted 39 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 mapping order is determined by taking the minimum of the contiguous page count and the pfn alignment, allowing graceful degradation when pfn alignment is less than the contiguous range.Pages with the same
page_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]>Tested-by Leo Yan <[email protected]>Reviewed-by Dev Jain <[email protected]>--- mm/vmalloc.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 2 deletions(-)mm/vmalloc.c +0
-0
mm/vmalloc.c static inline unsigned int vm_shift(pgprot_t prot, unsigned long size) · line 3561
Quoted 5 lines
mm/vmalloc.c +2
-0@@ -3557,6 +3557,84 @@ static inline unsigned int vm_shift(pgprot_t prot, unsigned long size)return arch_vmap_pte_supported_shift (size); } +static inline int get_vmap_batch_order (struct page **pages, +pgprot_t prot,unsigned int max_steps,unsigned int idx)
Why pass in pages, idx when you can really just pass pages+idx?
And just call it "nr_pages" instead of "max_steps".
Quoted 8 lines
mm/vmalloc.c +8
-0+{ +unsigned long pfn; +unsigned int nr_contig; +int order; + +if (!IS_ENABLED (CONFIG_HAVE_ARCH_HUGE_VMAP )) +return 0 ; +
mm/vmalloc.c static inline unsigned int vm_shift(pgprot_t prot, unsigned long size) · line 3579
Quoted 10 lines
mm/vmalloc.c +10
-0+ nr_contig =num_pages_contiguous (&pages[idx], max_steps); +if (nr_contig <2 ) +return 0 ; + + order =ilog2 (nr_contig); + pfn =page_to_pfn (pages[idx]); + +/* Limit order by pfn alignment */ +if (pfn >0 ) + order =min_t (int, order,__ffs (pfn));
Wouldn't it make sense to determine that before you call num_pages_contiguous? Because then, you can just scan to that maximum instead of the given nr_pages.
mm/vmalloc.c static inline unsigned int vm_shift(pgprot_t prot, unsigned long size) · line 3590
Quoted 11 lines
mm/vmalloc.c +11
-0+ +if (vm_shift (prot,PAGE_SIZE << order) ==PAGE_SHIFT ) +return 0 ; + +return order; +} + +static int vmap_pages_range_batched (unsigned long addr,unsigned long end, +pgprot_t prot,struct page **pages) +{ +unsigned int count = (end - addr) >>PAGE_SHIFT ;
"nr_pages" ? Also, can be const.
mm/vmalloc.c static inline unsigned int vm_shift(pgprot_t prot, unsigned long size) · line 3596
Quoted 6 lines
mm/vmalloc.c +6
-0+unsigned int prev_shift =0 , idx =0 ; +unsigned long map_addr = addr, batch_end = addr; +int err; + + err =kmsan_vmap_pages_range_noflush (addr, end, prot, pages, +PAGE_SHIFT ,GFP_KERNEL );
Is the indentation on the second parameter line off?
mm/vmalloc.c static inline unsigned int vm_shift(pgprot_t prot, unsigned long size) · line 3602
Quoted 6 lines
mm/vmalloc.c +6
-0+if (err) + goto out; + +for (unsigned int i =0 ; i < count; ) { +unsigned int shift =PAGE_SHIFT + +get_vmap_batch_order (pages, prot, count - i, i);
Having two indices, i and idx, is just confusing.
The whole function is a bit overly complicated. Does it really buy us much to batch over vmap_pages_range_noflush_walk() calling with the same shift?
Quoted 7 lines
mm/vmalloc.c +7
-0+ +if (!i) + prev_shift = shift; + +if (shift != prev_shift) { + err =vmap_pages_range_noflush_walk (map_addr, batch_end, + prot, pages + idx, prev_shift);
mm/vmalloc.c static inline unsigned int vm_shift(pgprot_t prot, unsigned long size) · line 3619
Quoted 10 lines
mm/vmalloc.c +10
-0+if (err) + goto out; + prev_shift = shift; + map_addr = batch_end; + idx = i; + } + +/* +* Once small pages are encountered, the remaining pages +* are likely small as well.
"small pages" is odd. Maybe
"Once we fail to batch pages, we expect to fail batching for all remaining pages, so just give up."
Quoted 22 lines
mm/vmalloc.c +18
-0+*/ +if (shift ==PAGE_SHIFT ) +break ; + + batch_end +=1UL << shift; + i +=1U << (shift -PAGE_SHIFT ); + } + +/* Remaining */ +if (map_addr < end) + err =vmap_pages_range_noflush_walk (map_addr, end, + prot, pages + idx, prev_shift); + +out: +flush_cache_vmap (addr, end); +return err; +} +/** * vmap - map an array of pages into virtually contiguous space * @pages: array of page pointers
mm/vmalloc.c void *vmap(struct page **pages, unsigned int count, · line 3682
Quoted 7 lines
mm/vmalloc.c +2
-2@@ -3600,8 +3678,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_pages_range_batched (addr, addr + size,pgprot_nx (prot), + pages) <0 ) {
Nit: single line would make that nicer to read.
Signature · 3 lines
Cheers, David
Re: [PATCH v7 6/7] mm/vmalloc: map contiguous pages in batches for vmap() if possible
[------]
Quoted 6 lines
Quoted 6 lines
mm/vmalloc.c +6
-0+ + nr_contig =num_pages_contiguous (&pages[idx], max_steps); +if (nr_contig <2 ) +return 0 ; + + order =ilog2 (nr_contig);
Quoted 8 lines
mm/vmalloc.c static inline unsigned int vm_shift(pgprot_t prot, unsigned long size) · line 3579
Quoted 5 lines
mm/vmalloc.c +5
-0+ pfn =page_to_pfn (pages[idx]); + +/* Limit order by pfn alignment */ +if (pfn >0 ) + order =min_t (int, order,__ffs (pfn));Wouldn't it make sense to determine that before you call
num_pages_contiguous? Because then, you can just scan to that maximum instead of the givennr_pages.
Right!
Quoted 33 lines
mm/vmalloc.c static inline unsigned int vm_shift(pgprot_t prot, unsigned long size) · line 3590
Quoted 12 lines
mm/vmalloc.c +11
-0+ +if (vm_shift (prot,PAGE_SIZE << order) ==PAGE_SHIFT ) +return 0 ; + +return order; +} + +static int vmap_pages_range_batched (unsigned long addr,unsigned long end, +pgprot_t prot,struct page **pages) +{ +unsigned int count = (end - addr) >>PAGE_SHIFT ;"
nr_pages" ? Also, can be const.mm/vmalloc.c static inline unsigned int vm_shift(pgprot_t prot, unsigned long size) · line 3596
Quoted 6 lines
mm/vmalloc.c +6
-0+unsigned int prev_shift =0 , idx =0 ; +unsigned long map_addr = addr, batch_end = addr; +int err; + + err =kmsan_vmap_pages_range_noflush (addr, end, prot, pages, +PAGE_SHIFT ,GFP_KERNEL );Is the indentation on the second parameter line off?
mm/vmalloc.c static inline unsigned int vm_shift(pgprot_t prot, unsigned long size) · line 3602
Quoted 6 lines
mm/vmalloc.c +6
-0+if (err) + goto out; + +for (unsigned int i =0 ; i < count; ) { +unsigned int shift =PAGE_SHIFT + +get_vmap_batch_order (pages, prot, count - i, i);Having two indices, i and idx, is just confusing.
Quoted 2 lines
The whole function is a bit overly complicated. Does it really buy us much to batch over
vmap_pages_range_noflush_walk()calling with the same shift?
It will buy us more in the sense that, vmap() speed is sensitive to the number of pagetable walks. I think that is reflected by the ioremap(1MB) 1.35x speedup mentioned in the cover letter.
In terms of callers, not sure. I would expect a caller to get the highest power of 2 from the total needed nr_pages, then get the next highest power of 2 from the remaining, and so on.
For the arm64 TRBE usecase, the relevant code is the rb_alloc_aux_page() caller in kernel/events/ring_buffer.c. We may get multiple same order requests if can't get the first requested higher order.
Since on the cover letter I see Wen and team have tested on boards, I lean towards saying it is reasonable to assume a usecase where such order fallback can happen.
But yeah the code for this currently is not so nice I agree.
Quoted 56 lines
Quoted 8 lines
mm/vmalloc.c +7
-0+ +if (!i) + prev_shift = shift; + +if (shift != prev_shift) { + err =vmap_pages_range_noflush_walk (map_addr, batch_end, + prot, pages + idx, prev_shift);mm/vmalloc.c static inline unsigned int vm_shift(pgprot_t prot, unsigned long size) · line 3619
Quoted 10 lines
mm/vmalloc.c +10
-0+if (err) + goto out; + prev_shift = shift; + map_addr = batch_end; + idx = i; + } + +/* +* Once small pages are encountered, the remaining pages +* are likely small as well. "small pages" is odd. Maybe
"Once we fail to batch pages, we expect to fail batching for all remaining pages, so just give up."
Quoted 22 lines
mm/vmalloc.c +18
-0+*/ +if (shift ==PAGE_SHIFT ) +break ; + + batch_end +=1UL << shift; + i +=1U << (shift -PAGE_SHIFT ); + } + +/* Remaining */ +if (map_addr < end) + err =vmap_pages_range_noflush_walk (map_addr, end, + prot, pages + idx, prev_shift); + +out: +flush_cache_vmap (addr, end); +return err; +} +/** * vmap - map an array of pages into virtually contiguous space * @pages: array of page pointers mm/vmalloc.c void *vmap(struct page **pages, unsigned int count, · line 3682
Quoted 7 lines
mm/vmalloc.c +2
-2@@ -3600,8 +3678,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_pages_range_batched (addr, addr + size,pgprot_nx (prot), + pages) <0 ) {Nit: single line would make that nicer to read.
Re: [PATCH v7 6/7] mm/vmalloc: map contiguous pages in batches for vmap() if possible
Quoted 18 lines · Dev Jain
[------]
Quoted 6 lines
Quoted 6 lines
mm/vmalloc.c +6
-0+ + nr_contig =num_pages_contiguous (&pages[idx], max_steps); +if (nr_contig <2 ) +return 0 ; + + order =ilog2 (nr_contig);Quoted 8 lines
mm/vmalloc.c static inline unsigned int vm_shift(pgprot_t prot, unsigned long size) · line 3579
Quoted 5 lines
mm/vmalloc.c +5
-0+ pfn =page_to_pfn (pages[idx]); + +/* Limit order by pfn alignment */ +if (pfn >0 ) + order =min_t (int, order,__ffs (pfn));Wouldn't it make sense to determine that before you call
num_pages_contiguous? Because then, you can just scan to that maximum instead of the givennr_pages.
Quoted 3 lines
Right!
Agreed. I will check the PFN alignment limit first before calling num_pages_contiguous() to avoid unnecessary scanning.
Quoted 9 lines
Quoted 9 lines
mm/vmalloc.c static inline unsigned int vm_shift(pgprot_t prot, unsigned long size) · line 3596
Quoted 6 lines
mm/vmalloc.c +6
-0+unsigned int prev_shift =0 , idx =0 ; +unsigned long map_addr = addr, batch_end = addr; +int err; + + err =kmsan_vmap_pages_range_noflush (addr, end, prot, pages, +PAGE_SHIFT ,GFP_KERNEL );Is the indentation on the second parameter line off?
Will check this, thanks.
Quoted 9 lines
Quoted 9 lines
mm/vmalloc.c static inline unsigned int vm_shift(pgprot_t prot, unsigned long size) · line 3602
Quoted 6 lines
mm/vmalloc.c +6
-0+if (err) + goto out; + +for (unsigned int i =0 ; i < count; ) { +unsigned int shift =PAGE_SHIFT + +get_vmap_batch_order (pages, prot, count - i, i);Having two indices, i and idx, is just confusing.
How about renaming idx to batch_start? I think using i and batch_start will make the logic much clearer.
Quoted 11 lines
Quoted 2 lines
The whole function is a bit overly complicated. Does it really buy us much to batch over
vmap_pages_range_noflush_walk()calling with the same shift?It will buy us more in the sense that,
vmap()speed is sensitive to the number of pagetable walks. I think that is reflected by the ioremap(1MB) 1.35x speedup mentioned in the cover letter.In terms of callers, not sure. I would expect a caller to get the highest power of 2 from the total needed
nr_pages, then get the next highest power of 2 from the remaining, and so on.
Quoted 8 lines
For the arm64 TRBE usecase, the relevant code is the
rb_alloc_aux_page()caller inkernel/events/ring_buffer.c. We may get multiple same order requests if can't get the first requested higher order.Since on the cover letter I see Wen and team have tested on boards, I lean towards saying it is reasonable to assume a usecase where such order fallback can happen.
Thanks. The batching is necessary because it significantly reduces page table walks and effectively handles fallback scenarios (such as the arm64 TRBE use case).
Quoted 26 lines
But yeah the code for this currently is not so nice I agree.
Quoted 24 lines
Quoted 8 lines
mm/vmalloc.c +7
-0+ +if (!i) + prev_shift = shift; + +if (shift != prev_shift) { + err =vmap_pages_range_noflush_walk (map_addr, batch_end, + prot, pages + idx, prev_shift);mm/vmalloc.c static inline unsigned int vm_shift(pgprot_t prot, unsigned long size) · line 3619
Quoted 10 lines
mm/vmalloc.c +10
-0+if (err) + goto out; + prev_shift = shift; + map_addr = batch_end; + idx = i; + } + +/* +* Once small pages are encountered, the remaining pages +* are likely small as well. "small pages" is odd. Maybe
"Once we fail to batch pages, we expect to fail batching for all remaining pages, so just give up."
Will update the comment.
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
mm/vmalloc.c +18
-0+*/ +if (shift ==PAGE_SHIFT ) +break ; + + batch_end +=1UL << shift; + i +=1U << (shift -PAGE_SHIFT ); + } + +/* Remaining */ +if (map_addr < end) + err =vmap_pages_range_noflush_walk (map_addr, end, + prot, pages + idx, prev_shift); + +out: +flush_cache_vmap (addr, end); +return err; +} +/** * vmap - map an array of pages into virtually contiguous space * @pages: array of page pointers @@ -3600,8 +3678,8 @@ void *vmap(struct page **pages, unsigned int count,return NULL ;
Quoted 9 lines
Quoted 8 lines
mm/vmalloc.c void *vmap(struct page **pages, unsigned int count, · line 3682
Quoted 5 lines
mm/vmalloc.c +2
-2addr = (unsigned long )area->addr; -if (vmap_pages_range (addr, addr + size,pgprot_nx (prot), - pages,PAGE_SHIFT ) <0 ) { +if (vmap_pages_range_batched (addr, addr + size,pgprot_nx (prot), + pages) <0 ) {Nit: single line would make that nicer to read.
Once you confirm, I will send out a diff to address all the points mentioned above. Thanks again for the review!
Best regards,
Wen
Re: [PATCH v7 6/7] mm/vmalloc: map contiguous pages in batches for vmap() if possible
Hi David and Dev,
The updated diff is blow.
Subject: [PATCH] mm/vmalloc: simplify vmap batching helper and limit scan by PFN alignment
Pass the current page pointer directly to the batching helper, and limit the contiguous scan by PFN alignment before checking for a batch size. This keeps the helper interface simpler and avoids scanning more pages than can actually be mapped together.
---
mm/vmalloc.c +17 -17
@@ -3558,7 +3558,7 @@ static inline unsigned int vm_shift(pgprot_t prot, unsigned long size) }static inline int get_vmap_batch_order (struct page **pages, -pgprot_t prot,unsigned int max_steps,unsigned int idx) +pgprot_t prot,unsigned int nr_pages) {unsigned long pfn;unsigned int nr_contig;
@@ -3567,16 +3567,16 @@ static inline int get_vmap_batch_order(struct page **pages,if (!IS_ENABLED (CONFIG_HAVE_ARCH_HUGE_VMAP ))return 0 ; - nr_contig =num_pages_contiguous (&pages[idx], max_steps); +/* Limit nr_pages by pfn alignment */ + pfn =page_to_pfn (*pages); +if (pfn >0 ) + nr_pages = min_t(unsigned int, nr_pages, 1UL << __ffs(pfn)); + + nr_contig =num_pages_contiguous (pages, nr_pages);if (nr_contig <2 )return 0 ; order =ilog2 (nr_contig); - pfn =page_to_pfn (pages[idx]); - -/* Limit order by pfn alignment */ -if (pfn >0 ) - order =min_t (int, order,__ffs (pfn));if (vm_shift (prot,PAGE_SIZE << order) ==PAGE_SHIFT )return 0 ;
@@ -3587,8 +3587,8 @@ static inline int get_vmap_batch_order(struct page **pages,static int vmap_pages_range_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 ; +const unsigned int nr_pages = (end - addr) >>PAGE_SHIFT ; +unsigned int prev_shift =0 , batch_start =0 ;unsigned long map_addr = addr, batch_end = addr;int err;
@@ -3597,26 +3597,26 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end,if (err) goto out; -for (unsigned int i =0 ; i < count; ) { +for (unsigned int i =0 ; i < nr_pages; ) {unsigned int shift =PAGE_SHIFT + -get_vmap_batch_order (pages, prot, count - i, i); +get_vmap_batch_order (pages + i, prot, nr_pages - i);if (!i) prev_shift = shift;if (shift != prev_shift) { err =vmap_pages_range_noflush_walk (map_addr, batch_end, - prot, pages + idx, prev_shift); + prot, pages + batch_start, prev_shift);if (err) goto out; prev_shift = shift; map_addr = batch_end; - idx = i; + batch_start = i; }/* -* Once small pages are encountered, the remaining pages -* are likely small as well. +* Once we fail to batch pages, we expect to fail batching +* for all remaining pages, so just give up. */ if (shift ==PAGE_SHIFT )break ;
@@ -3627,8 +3627,8 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end,/* Remaining */ if (map_addr < end) - err =vmap_pages_range_noflush_walk (map_addr, end, - prot, pages + idx, prev_shift); + err =vmap_pages_range_noflush_walk (map_addr, end, prot, + pages + batch_start, prev_shift); out:flush_cache_vmap (addr, end);
PATCH v7 7/7 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. We do not expect any significant overhead for this.
Add __get_vm_area_node_aligned_caller() as a wrapper over __get_vm_area_node() to simplify repeated calls with fixed arguments.
--- mm/vmalloc.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-)
mm/vmalloc.c +34 -1
@@ -3320,6 +3320,14 @@ struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags, NUMA_NO_NODE, GFP_KERNEL, caller); } +static struct vm_struct *__get_vm_area_node_aligned_caller (unsigned long size, +unsigned long align,unsigned long flags,const void *caller) +{ +return __get_vm_area_node (size, align,PAGE_SHIFT , flags, +VMALLOC_START ,VMALLOC_END , +NUMA_NO_NODE ,GFP_KERNEL , caller); +} +/** * find_vm_area - find a continuous kernel virtual area * @addr: base address
@@ -3635,6 +3643,30 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end,return err; } +static struct vm_struct *vmap_get_aligned_vm_area (unsigned long size, +unsigned long flags,pgprot_t prot,const void *caller) +{ +struct vm_struct *vm_area; +unsigned int shift; + +if (arch_vmap_pmd_supported (prot) && size >=PMD_SIZE ) { + vm_area =__get_vm_area_node_aligned_caller (size,PMD_SIZE , + flags, caller); +if (vm_area) +return vm_area; + } + + shift =arch_vmap_pte_supported_shift (size); +if (shift >PAGE_SHIFT ) { + vm_area =__get_vm_area_node_aligned_caller (size,1UL << shift, + flags, caller); +if (vm_area) +return vm_area; + } + +return __get_vm_area_node_aligned_caller (size,PAGE_SIZE , flags, caller); +} +/** * vmap - map an array of pages into virtually contiguous space * @pages: array of page pointers
@@ -3673,7 +3705,8 @@ void *vmap(struct page **pages, unsigned int count,return NULL ; size = (unsigned long )count <<PAGE_SHIFT ; - area =get_vm_area_caller (size, flags,__builtin_return_address (0 )); + area =vmap_get_aligned_vm_area (size, flags, prot, +__builtin_return_address (0 ));if (!area)return NULL ;
--
2.34.1
Andrew Morton
maintainerQuoted 2 lines · On Wed, 15 Jul 2026 20:08:06 +0800 Wen Jiang <[email protected]> wrote:
This patchset accelerates ioremap, vmalloc, and vmap when the memory is physically fully or partially contiguous. Two techniques are used:
Thanks, I've updated mm.git's mm-unstable branch to this version.
Quoted 9 lines
Changes since v6:
- Add a clarifying comment about the reuse of hugetlb helpers by non-hugetlbfs(vmalloc) mm code (patch 1)
- Expand the arm64/vmalloc commit message and comment to clarify that multi-CONT_PTE_SIZE values are vmalloc mapping spans, not HugeTLB hstate sizes (patch 2)
- Move the local steps variable change invmap_pte_range()into thevmap_set_ptes()extraction patch (patch 3)
- Propagatevmap_pages_pte_range()errors through the upper
Quoted 8 lines
vmap_pages_*() levels instead of returning
-ENOMEMfor all failures (patch 4)
- Add a preparatoryvm_shift()helper patch before the batching patch (patch 5)
- Guard the PFN alignment clamp inget_vmap_batch_order()against PFN 0 before calling__ffs()(patch 6)
- Fixkmsan_vmap_pages_range_noflush()indentation in the batching path (patch 6)
Here's how v7 altered mm.git:
arch/arm64/include/asm/vmalloc.h | 2 + arch/arm64/mm/hugetlbpage.c | 5 ++++ mm/vmalloc.c | 31 ++++++++++++++++------------- 3 files changed, 25 insertions(+), 13 deletions(-)
arch/arm64/include/asm/vmalloc.h renamed from arch/arm64/include/asm/vmalloc.h~b +2 -0
@@ -29,6 +29,8 @@ static inline unsigned long arch_vmap_pt* If the block is at least CONT_PTE_SIZE in size, and is naturally * aligned in both virtual and physical space, then we can pte-map the * block using the PTE_CONT bit for more efficient use of the TLB. +* The returned mapping size may cover multiple CONT_PTE_SIZE blocks, +* capped below PMD_SIZE. */ if (max_page_shift <CONT_PTE_SHIFT )return PAGE_SIZE ;
arch/arm64/mm/hugetlbpage.c renamed from arch/arm64/mm/hugetlbpage.c~b +5 -0
@@ -94,6 +94,11 @@ static int find_num_contig(struct mm_strreturn CONT_PTES ; } +/* +* num_contig_ptes(), set_huge_pte_at() and arch_make_huge_pte() can be +* used by non-hugetlbfs(vmalloc) mm code to set multiple huge mappings +* at the PTE level. +*/ static inline int num_contig_ptes (unsigned long size,size_t *pgsize) {int contig_ptes =1 ;
mm/vmalloc.c renamed from mm/vmalloc.c~b +18 -13
@@ -616,6 +616,7 @@ static int vmap_pages_pmd_range(pud_t *p {pmd_t *pmd;unsigned long next; +int err; pmd =pmd_alloc_track (&init_mm, pud, addr, mask);if (!pmd)
@@ -642,8 +643,9 @@ static int vmap_pages_pmd_range(pud_t *p } } -if (vmap_pages_pte_range (pmd, addr, next, prot, pages, nr, mask, shift)) -return -ENOMEM ; + err =vmap_pages_pte_range (pmd, addr, next, prot, pages, nr, mask, shift); +if (err) +return err; }while (pmd++, addr = next, addr != end);return 0 ; }
@@ -654,14 +656,16 @@ static int vmap_pages_pud_range(p4d_t *p {pud_t *pud;unsigned long next; +int err; pud =pud_alloc_track (&init_mm, p4d, addr, mask);if (!pud)return -ENOMEM ;do { next =pud_addr_end (addr, end); -if (vmap_pages_pmd_range (pud, addr, next, prot, pages, nr, mask, shift)) -return -ENOMEM ; + err =vmap_pages_pmd_range (pud, addr, next, prot, pages, nr, mask, shift); +if (err) +return err; }while (pud++, addr = next, addr != end);return 0 ; }
@@ -672,14 +676,16 @@ static int vmap_pages_p4d_range(pgd_t *p {p4d_t *p4d;unsigned long next; +int err; p4d =p4d_alloc_track (&init_mm, pgd, addr, mask);if (!p4d)return -ENOMEM ;do { next =p4d_addr_end (addr, end); -if (vmap_pages_pud_range (p4d, addr, next, prot, pages, nr, mask, shift)) -return -ENOMEM ; + err =vmap_pages_pud_range (p4d, addr, next, prot, pages, nr, mask, shift); +if (err) +return err; }while (p4d++, addr = next, addr != end);return 0 ; }
@@ -3591,6 +3597,7 @@ static inline unsigned int vm_shift(pgprstatic inline int get_vmap_batch_order (struct page **pages,pgprot_t prot,unsigned int max_steps,unsigned int idx) { +unsigned long pfn;unsigned int nr_contig;int order;
@@ -3602,9 +3609,11 @@ static inline int get_vmap_batch_order(sreturn 0 ; order =ilog2 (nr_contig); + pfn =page_to_pfn (pages[idx]);/* Limit order by pfn alignment */ - order =min_t (int, order,__ffs (page_to_pfn (pages[idx]))); +if (pfn >0 ) + order =min_t (int, order,__ffs (pfn));if (vm_shift (prot,PAGE_SIZE << order) ==PAGE_SHIFT )return 0 ;
@@ -3621,7 +3630,7 @@ static int vmap_pages_range_batched(unsiint err; err =kmsan_vmap_pages_range_noflush (addr, end, prot, pages, -PAGE_SHIFT ,GFP_KERNEL ); +PAGE_SHIFT ,GFP_KERNEL );if (err) goto out;
@@ -4199,11 +4208,7 @@ void *__vmalloc_node_range_noprof(unsign* supporting them. */ -if (arch_vmap_pmd_supported (prot) && size >=PMD_SIZE ) - shift =PMD_SHIFT ; -else - shift =arch_vmap_pte_supported_shift (size); - + shift =vm_shift (prot, size); align =max (original_align,1UL << shift); }
_