mm: add huge pfnmap support for remap_pfn_range()
Subsystems (8)
v4 cover mm: add huge pfnmap support for remap_pfn_range()
This series is based on mm-unstable and depends on:
1. pgtable_has_pmd_leaves(), introduced by Luiz's series:
https://lore.kernel.org/linux-mm/[email protected]/ 2. mm/huge_memory: update file PMD counter before folio_put() https://lore.kernel.org/linux-mm/[email protected]/T/#u
v4:
- Following Matthew Wilcox's feedback that huge-page attribute handling should stay in architecture helpers:
https://lore.kernel.org/all/[email protected]/
Reworked the pgprot contract for architectures that enable CONFIG_ARCH_SUPPORTS_PMD_PFNMAP: pfn_pmd()/pfn_pud() construct PMD/PUD leaf entries from base-PTE pgprot_t, while pmd_pgprot()/pud_pgprot() return base-PTE pgprot_t. Added the required x86, arm64 and powerpc support; RISC-V already satisfies the required semantics.
- Refactored copy_huge_pmd() and __split_huge_pmd_locked() to first classify PMDs by pmd_present(), and then use vm_normal_folio_pmd() for present PMDs, and make move_huge_pmd() use has_deposited_pgtable().
- Introduced a restriction, following the discussion with Lorenzo and David, that remap_pfn_range() does not create PMD-sized mappings for VMAs that have a fault handler:
[https://lore.kernel.org/linux-mm/[email protected]/]
With this restriction, PMD PFNMAP entries in VMAs without fault handlers are known to have been installed by remap_pfn_range(), which deposits a page table when installing such mappings; PMD PFNMAP entries in VMAs with fault handlers are created through fault-time insertion paths such as vmf_insert_pfn_pmd().
v3: https://lore.kernel.org/all/[email protected]/
1. Architectural Type Safety (Matthew Wilcox):
Following the insightful architectural feedback from Matthew Wilcox in v2, the approach to clearing huge page attributes has been completely redesigned. Instead of spreading the pte_clrhuge() anti-pattern to ARM64 and RISC-V, this series enforces strict type safety at the lowest level: pfn_pte() must never natively return a PTE with huge page attributes set.
To achieve this without breaking the x86 core MM, the series is structured as:
- Fix historical type-casting abuses in x86 (vmemmap, vmalloc, CPA) where pfn_pte() was wrongly used to generate huge PMDs/PUDs.
- Update pfn_pte() on x86 and ARM64 to inherently filter out huge page attributes. (RISC-V leaf PMDs and PTEs share the exact same hardware format without a specific "huge" bit, so it is naturally compliant).
- Completely eradicate pte_clrhuge() from the x86 tree and clean up
the type-casting mess in `arch/x86/mm/init_64.c`.
2. Page Table Deposit fix during clone() (syzbot):
Previously, copy_huge_pmd() was unaware of special PMDs created by pfnmap, failing to deposit a page table for the child process during clone(). This led to crashes during process teardown or PMD splitting. The logic is now updated to properly allocate and deposit pgtables for pmd_special() entries.
v2: https://lore.kernel.org/linux-mm/[email protected]/#t
- remove "nohugepfnmap" boot option and "pfnmap_max_page_shift" variable.
- zap_deposited_table for non-special pmd.
- move set_pmd_at() inside pmd_lock.
- prevent PMD mapping creation when pgtable allocation fails.
- defer the refactor of pte_clrhuge() to a separate patch series. For now, add a TODO to track this.
v1: https://lore.kernel.org/linux-mm/[email protected]/
Overview
========
This patch series adds huge page support for remap_pfn_range(), automatically creating huge mappings when prerequisites are satisfied (size, alignment, architecture support, etc.) and falling back to normal page mappings otherwise.
This work builds on Peter Xu's previous efforts on huge pfnmap support [0].
TODO
====
- Add PUD-level huge page support. Currently, only PMD-level huge pages are supported.
Tests Done
==========
- Cross-build tests.
- Core MM Regression Tests
- Booted x86 kernel with debug_pagealloc=on to heavily stress the large page splitting logic in direct mapping. No panics observed.
- Ran make -C tools/testing/selftests/vm run_tests. Both THP and Hugetlbfs tests passed successfully, proving the pfn_pte() changes do not interfere with native huge page generation.
- Functional Tests (with a custom device driver & PTDUMP):
- Verified that remap_pfn_range() successfully creates 2MB mappings by observing /sys/kernel/debug/page_tables/current_user.
- Triggered PMD splits via 4K-granular mprotect() and partial munmap(), verifying correct fallback to 512 PTEs without corrupting permissions or causing kernel crashes.
- Triggered fork()/clone() on the mapped regions, validating the syzbot fix and ensuring safe pgtable deposit/withdraw lifecycle.
- Performance tests with custom device driver implementing mmap() with remap_pfn_range():
- lat_mem_rd benchmark modified to use mmap(device_fd) instead of
malloc() shows around 40% improvement in memory access latency with
huge page support compared to normal page mappings.
numactl -C 0 lat_mem_rd -t 4096M (stride=64)
Memory Size (MB) Without Huge Mapping With Huge Mapping Improvement
---------------- ----------------- -------------- -----------
64.00 148.858 ns 100.780 ns 32.3%
128.00 164.745 ns 103.537 ns 37.2%
256.00 169.907 ns 103.179 ns 39.3%
512.00 171.285 ns 103.072 ns 39.8%
1024.00 173.054 ns 103.055 ns 40.4%
2048.00 172.820 ns 103.091 ns 40.3%
4096.00 172.877 ns 103.115 ns 40.4%
- Custom memory copy operations on mmap(device_fd) show around 18% performance
improvement with huge page support compared to normal page mappings.
numactl -C 0 memcpy_test (memory copy performance test)
Memory Size (MB) Without Huge Mapping With Huge Mapping Improvement
---------------- ----------------- -------------- -----------
1024.00 95.76 ms 77.91 ms 18.6%
2048.00 190.87 ms 155.64 ms 18.5%
4096.00 380.84 ms 311.45 ms 18.2%[0] https://lore.kernel.org/all/[email protected]/T/#u
Yin Tirui (7):
x86/mm: use PTE-level pgprot for huge PFN helpers arm64/mm: use PTE-level pgprot for huge PFN helpers powerpc/mm: use PTE-level pgprot for huge PFN helpers mm/huge_memory: refactor copy_huge_pmd() mm/huge_memory: refactor __split_huge_pmd_locked() mm/huge_memory: make move_huge_pmd() use has_deposited_pgtable()
mm: add PMD-level PFNMAP support for remap_pfn_range()
arch/arm64/include/asm/pgtable.h | 48 +- arch/arm64/mm/mmu.c | 4 +- arch/powerpc/include/asm/book3s/64/pgtable.h | 5 +- arch/powerpc/include/asm/pgtable.h | 11 +- arch/powerpc/mm/book3s64/pgtable.c | 11 +- arch/x86/include/asm/pgtable.h | 68 ++- arch/x86/include/asm/pgtable_types.h | 12 +- arch/x86/mm/init_32.c | 8 +- arch/x86/mm/init_64.c | 30 +- arch/x86/mm/pat/set_memory.c | 51 +-- arch/x86/mm/pgtable.c | 8 +- arch/x86/power/hibernate_32.c | 6 +- mm/huge_memory.c | 440 +++++++++++-------- mm/internal.h | 21 + mm/memory.c | 87 +++- 15 files changed, 493 insertions(+), 317 deletions(-)
Signature · 1 line
2.43.0
RFC PATCH v4 1/7 x86/mm: use PTE-level pgprot for huge PFN helpers
Make the x86 PMD/PUD PFN helpers use PTE-level pgprot_t as the basic format.
pfn_pmd() and pfn_pud() now translate PTE-level attributes into large-page entries, including the x86 PAT/PSE encoding. pmd_pgprot() and pud_pgprot() translate large-page attributes back to PTE-level pgprot_t, hiding _PAGE_PSE and converting large-page PAT encoding back to the PTE PAT position.
Rework pmd_mkinvalid() and pud_mkinvalid() to use the same helpers:
extract a PTE-level pgprot_t with pmd_pgprot()/pud_pgprot(), clear PRESENT/PROTNONE, and rebuild the PMD/PUD entry with pfn_pmd()/pfn_pud().
The old explicit huge pgprot conversion helpers are no longer needed. Remove pte_clrhuge(), pgprot_large_2_4k(), pgprot_4k_2_large(), PAGE_KERNEL_LARGE and PAGE_KERNEL_LARGE_EXEC, and update x86 callers to construct PMD/PUD entries through the normal PFN helpers.
--- arch/x86/include/asm/pgtable.h | 68 +++++++++++++++++++--------- arch/x86/include/asm/pgtable_types.h | 12 +---- arch/x86/mm/init_32.c | 8 ++-- arch/x86/mm/init_64.c | 30 ++++-------- arch/x86/mm/pat/set_memory.c | 51 ++++++--------------- arch/x86/mm/pgtable.c | 8 +--- arch/x86/power/hibernate_32.c | 6 +-- 7 files changed, 77 insertions(+), 106 deletions(-)
arch/x86/include/asm/pgtable.h +46 -22
@@ -475,11 +475,6 @@ static inline pte_t pte_mkhuge(pte_t pte)return pte_set_flags (pte, _PAGE_PSE); } -static inline pte_t pte_clrhuge (pte_t pte) -{ -return pte_clear_flags (pte, _PAGE_PSE); -} -static inline pte_t pte_mkglobal (pte_t pte) {return pte_set_flags (pte, _PAGE_GLOBAL);
@@ -741,29 +736,31 @@ static inline pte_t pfn_pte(unsigned long page_nr, pgprot_t pgprot)static inline pmd_t pfn_pmd (unsigned long page_nr,pgprot_t pgprot) {phys_addr_t pfn = (phys_addr_t )page_nr <<PAGE_SHIFT ; - pfn ^=protnone_mask (pgprot_val (pgprot)); +pgprotval_t protval =protval_4k_2_large (pgprot_val (pgprot)); + + protval =check_pgprot (__pgprot (protval)); +if (protval) + protval |= _PAGE_PSE; + + pfn ^=protnone_mask (protval); pfn &=PHYSICAL_PMD_PAGE_MASK ; -return __pmd (pfn |check_pgprot (pgprot)); + +return __pmd (pfn | protval); }static inline pud_t pfn_pud (unsigned long page_nr,pgprot_t pgprot) {phys_addr_t pfn = (phys_addr_t )page_nr <<PAGE_SHIFT ; - pfn ^=protnone_mask (pgprot_val (pgprot)); - pfn &=PHYSICAL_PUD_PAGE_MASK ; -return __pud (pfn |check_pgprot (pgprot)); -} +pgprotval_t protval =protval_4k_2_large (pgprot_val (pgprot)); -static inline pmd_t pmd_mkinvalid (pmd_t pmd) -{ -return pfn_pmd (pmd_pfn (pmd), -__pgprot (pmd_flags (pmd) & ~(_PAGE_PRESENT|_PAGE_PROTNONE))); -} + protval =check_pgprot (__pgprot (protval)); +if (protval) + protval |= _PAGE_PSE; -static inline pud_t pud_mkinvalid (pud_t pud) -{ -return pfn_pud (pud_pfn (pud), -__pgprot (pud_flags (pud) & ~(_PAGE_PRESENT|_PAGE_PROTNONE))); + pfn ^=protnone_mask (protval); + pfn &=PHYSICAL_PUD_PAGE_MASK ; + +return __pud (pfn | protval); }static inline u64 flip_protnone_guard (u64 oldval,u64 val,u64 mask);
@@ -860,10 +857,37 @@ static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot) }#define pte_pgprot (x) __pgprot(pte_flags(x)) -#define pmd_pgprot (x) __pgprot(pmd_flags(x)) -#define pud_pgprot (x) __pgprot(pud_flags(x)) +static inline pgprot_t pmd_pgprot (pmd_t pmd) +{ +return __pgprot (protval_large_2_4k (pmd_flags (pmd))); +} + +#define pmd_pgprot pmd_pgprot + +static inline pgprot_t pud_pgprot (pud_t pud) +{ +return __pgprot (protval_large_2_4k (pud_flags (pud))); +} + +#define pud_pgprot pud_pgprot#define p4d_pgprot (x) __pgprot(p4d_flags(x)) +static inline pmd_t pmd_mkinvalid (pmd_t pmd) +{ +pgprot_t prot =pmd_pgprot (pmd); + +pgprot_val (prot) &= ~(_PAGE_PRESENT | _PAGE_PROTNONE); +return pfn_pmd (pmd_pfn (pmd), prot); +} + +static inline pud_t pud_mkinvalid (pud_t pud) +{ +pgprot_t prot =pud_pgprot (pud); + +pgprot_val (prot) &= ~(_PAGE_PRESENT | _PAGE_PROTNONE); +return pfn_pud (pud_pfn (pud), prot); +} +#define canon_pgprot (p) __pgprot(massage_pgprot(p)) static inline int is_new_memtype_allowed(u64 paddr, unsigned long size,
arch/x86/include/asm/pgtable_types.h +1 -11
@@ -251,8 +251,6 @@ enum page_cache_mode {#define PAGE_KERNEL_EXEC_NOENC __pgprot_mask(__PAGE_KERNEL_EXEC | 0)#define PAGE_KERNEL_ROX __pgprot_mask(__PAGE_KERNEL_ROX | _ENC)#define PAGE_KERNEL_NOCACHE __pgprot_mask(__PAGE_KERNEL_NOCACHE | _ENC) -#define PAGE_KERNEL_LARGE __pgprot_mask(__PAGE_KERNEL_LARGE | _ENC) -#define PAGE_KERNEL_LARGE_EXEC __pgprot_mask(__PAGE_KERNEL_LARGE_EXEC | _ENC)#define PAGE_KERNEL_VVAR __pgprot_mask(__PAGE_KERNEL_VVAR | _ENC)#define PAGE_KERNEL_IO __pgprot_mask(__PAGE_KERNEL_IO)
@@ -497,21 +495,13 @@ static inline pgprotval_t protval_4k_2_large(pgprotval_t val)return (val & ~(_PAGE_PAT | _PAGE_PAT_LARGE)) | ((val & _PAGE_PAT) << (_PAGE_BIT_PAT_LARGE - _PAGE_BIT_PAT)); } -static inline pgprot_t pgprot_4k_2_large (pgprot_t pgprot) -{ -return __pgprot (protval_4k_2_large (pgprot_val (pgprot))); -} +static inline pgprotval_t protval_large_2_4k (pgprotval_t val) {return (val & ~(_PAGE_PAT | _PAGE_PAT_LARGE)) | ((val & _PAGE_PAT_LARGE) >> (_PAGE_BIT_PAT_LARGE - _PAGE_BIT_PAT)); } -static inline pgprot_t pgprot_large_2_4k (pgprot_t pgprot) -{ -return __pgprot (protval_large_2_4k (pgprot_val (pgprot))); -} -typedef struct page *pgtable_t ;
arch/x86/mm/init_32.c +3 -5
@@ -311,14 +311,12 @@ kernel_physical_mapping_init(unsigned long start,*/ if (use_pse) {unsigned int addr2; -pgprot_t prot =PAGE_KERNEL_LARGE ; +pgprot_t prot =PAGE_KERNEL ;/* * first pass will use the same initial * identity mapping attribute + _PAGE_PSE. */ -pgprot_t init_prot = -__pgprot (PTE_IDENT_ATTR | - _PAGE_PSE); +pgprot_t init_prot =__pgprot (PTE_IDENT_ATTR ); pfn &=PMD_MASK >>PAGE_SHIFT ; addr2 = (pfn + PTRS_PER_PTE-1) * PAGE_SIZE +
@@ -326,7 +324,7 @@ kernel_physical_mapping_init(unsigned long start,if (is_x86_32_kernel_text (addr) ||is_x86_32_kernel_text (addr2)) - prot =PAGE_KERNEL_LARGE_EXEC ; + prot =PAGE_KERNEL_EXEC ; pages_2m++;if (mapping_iter ==1 )
arch/x86/mm/init_64.c +8 -22
@@ -90,13 +90,6 @@ DEFINE_ENTRY(pud, pud, init) DEFINE_ENTRY(pmd, pmd, init)DEFINE_ENTRY (pte, pte, init) -static inline pgprot_t prot_sethuge (pgprot_t prot) -{ -WARN_ON_ONCE (pgprot_val (prot) & _PAGE_PAT); - -return __pgprot (pgprot_val (prot) | _PAGE_PSE); -} -/* * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the * physical space so we can cache the place of the first one and move
@@ -390,8 +383,7 @@ static void __init __init_extra_mapping(unsigned long phys, unsigned long size,pmd_t *pmd;pgprot_t prot; -pgprot_val (prot) =pgprot_val (PAGE_KERNEL_LARGE ) | -protval_4k_2_large (cachemode2protval (cache)); +pgprot_val (prot) =pgprot_val (PAGE_KERNEL ) |cachemode2protval (cache);BUG_ON ((phys & ~PMD_MASK ) || (size & ~PMD_MASK ));for (; size; phys +=PMD_SIZE , size -=PMD_SIZE ) { pgd =pgd_offset_k ((unsigned long )__va (phys));
@@ -414,7 +406,7 @@ static void __init __init_extra_mapping(unsigned long phys, unsigned long size, } pmd =pmd_offset (pud, phys);BUG_ON (!pmd_none (*pmd)); -set_pmd (pmd,__pmd (phys |pgprot_val (prot))); +set_pmd (pmd,pfn_pmd (phys >>PAGE_SHIFT , prot)); } }
@@ -572,15 +564,13 @@ phys_pmd_init(pmd_t *pmd_page, unsigned long paddr, unsigned long paddr_end, paddr_last = paddr_next;continue ; } - new_prot =pte_pgprot (pte_clrhuge (*(pte_t *)pmd)); + new_prot =pmd_pgprot (*pmd); }if (page_size_mask & (1 <<PG_LEVEL_2M )) { pages++;spin_lock (&init_mm.page_table_lock); -set_pmd_init (pmd, -pfn_pmd (paddr >>PAGE_SHIFT ,prot_sethuge (prot)), - init); +set_pmd_init (pmd,pfn_pmd (paddr >>PAGE_SHIFT , prot), init);spin_unlock (&init_mm.page_table_lock); paddr_last = paddr_next;continue ;
@@ -658,15 +648,13 @@ phys_pud_init(pud_t *pud_page, unsigned long paddr, unsigned long paddr_end, paddr_last = paddr_next;continue ; } - prot =pte_pgprot (pte_clrhuge (*(pte_t *)pud)); + prot =pud_pgprot (*pud); }if (page_size_mask & (1 <<PG_LEVEL_1G )) { pages++;spin_lock (&init_mm.page_table_lock); -set_pud_init (pud, -pfn_pud (paddr >>PAGE_SHIFT ,prot_sethuge (prot)), - init); +set_pud_init (pud,pfn_pud (paddr >>PAGE_SHIFT , prot), init);spin_unlock (&init_mm.page_table_lock); paddr_last = paddr_next;continue ;
@@ -1519,11 +1507,9 @@ static int __meminitdata node_start;void __meminit vmemmap_set_pmd (pmd_t *pmd,void *p,int node,unsigned long addr,unsigned long next) { -pte_t entry; +pmd_t entry =pfn_pmd (__pa (p) >>PAGE_SHIFT ,PAGE_KERNEL ); - entry =pfn_pte (__pa (p) >>PAGE_SHIFT , -PAGE_KERNEL_LARGE ); -set_pmd (pmd,__pmd (pte_val (entry))); +set_pmd (pmd, entry);/* check to see if we have contiguous blocks */ if (p_end != p || node_start != node) {
arch/x86/mm/pat/set_memory.c +14 -37
129 lines not on this page · raw message
arch/x86/mm/pgtable.c +2 -6
@@ -644,9 +644,7 @@ int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot)if (pud_present (*pud) && !pud_leaf (*pud))return 0 ; -set_pte ((pte_t *)pud,pfn_pte ( - (u64 )addr >>PAGE_SHIFT , -__pgprot (protval_4k_2_large (pgprot_val (prot)) | _PAGE_PSE))); +set_pud (pud,pfn_pud ((u64 )addr >>PAGE_SHIFT , prot));return 1 ; }
@@ -676,9 +674,7 @@ int pmd_set_huge(pmd_t *pmd, phys_addr_t addr, pgprot_t prot)if (pmd_present (*pmd) && !pmd_leaf (*pmd))return 0 ; -set_pte ((pte_t *)pmd,pfn_pte ( - (u64 )addr >>PAGE_SHIFT , -__pgprot (protval_4k_2_large (pgprot_val (prot)) | _PAGE_PSE))); +set_pmd (pmd,pfn_pmd ((u64 )addr >>PAGE_SHIFT , prot));return 1 ; }
arch/x86/power/hibernate_32.c +3 -3
@@ -107,7 +107,7 @@ static int resume_physical_mapping_init(pgd_t *pgd_base)* NOTE: We can mark everything as executable here */ if (boot_cpu_has (X86_FEATURE_PSE )) { -set_pmd (pmd,pfn_pmd (pfn,PAGE_KERNEL_LARGE_EXEC )); +set_pmd (pmd,pfn_pmd (pfn,PAGE_KERNEL_EXEC )); pfn +=PTRS_PER_PTE ; }else {pte_t *max_pte;
@@ -156,13 +156,13 @@ static int set_up_temporary_text_mapping(pgd_t *pgd_base)if (boot_cpu_has (X86_FEATURE_PSE )) {set_pmd (pmd +pmd_index (restore_jump_address), -__pmd ((jump_address_phys &PMD_MASK ) |pgprot_val (PAGE_KERNEL_LARGE_EXEC ))); +pfn_pmd (jump_address_phys >>PAGE_SHIFT ,PAGE_KERNEL_EXEC )); }else { pte =resume_one_page_table_init (pmd);if (!pte)return -ENOMEM ;set_pte (pte +pte_index (restore_jump_address), -__pte ((jump_address_phys &PAGE_MASK ) |pgprot_val (PAGE_KERNEL_EXEC ))); +pfn_pte (jump_address_phys >>PAGE_SHIFT ,PAGE_KERNEL_EXEC )); }return 0 ;
Signature · 1 line
2.43.0
RFC PATCH v4 2/7 arm64/mm: use PTE-level pgprot for huge PFN helpers
In reply to Yin TiruiMake the arm64 PMD/PUD PFN helpers use PTE-level pgprot_t as the basic format.
pfn_pmd() and pfn_pud() now translate PTE-level attributes into block entries. pmd_pgprot() and pud_pgprot() translate block descriptor attributes back into PTE-level attributes.
Remove mk_pmd_sect_prot() and mk_pud_sect_prot().
--- arch/arm64/include/asm/pgtable.h | 48 ++++++++++++++++++++++---------- arch/arm64/mm/mmu.c | 4 +-- 2 files changed, 36 insertions(+), 16 deletions(-)
arch/arm64/include/asm/pgtable.h +34 -14
@@ -511,16 +511,6 @@ static inline pmd_t pte_pmd(pte_t pte)return __pmd (pte_val (pte)); } -static inline pgprot_t mk_pud_sect_prot (pgprot_t prot) -{ -return __pgprot ((pgprot_val (prot) & ~PUD_TYPE_MASK ) |PUD_TYPE_SECT ); -} - -static inline pgprot_t mk_pmd_sect_prot (pgprot_t prot) -{ -return __pgprot ((pgprot_val (prot) & ~PMD_TYPE_MASK ) |PMD_TYPE_SECT ); -} -static inline pte_t pte_swp_mkexclusive (pte_t pte) {return set_pte_bit (pte,__pgprot (PTE_SWP_EXCLUSIVE ));
@@ -628,7 +618,13 @@ static inline pmd_t pmd_mkspecial(pmd_t pmd)#define __pmd_to_phys (pmd) __pte_to_phys(pmd_pte(pmd))#define __phys_to_pmd_val (phys) __phys_to_pte_val(phys)#define pmd_pfn (pmd) ((__pmd_to_phys(pmd) & PMD_MASK) >> PAGE_SHIFT) -#define pfn_pmd (pfn,prot) __pmd(__phys_to_pmd_val((phys_addr_t)(pfn) << PAGE_SHIFT) | pgprot_val(prot)) +static inline pmd_t pfn_pmd (unsigned long pfn,pgprot_t prot) +{ +pmd_t pmd =__pmd (__phys_to_pmd_val ((phys_addr_t )pfn <<PAGE_SHIFT ) | +pgprot_val (prot)); + +return pmd_mkhuge (pmd); +}#define pud_young (pud) pte_young(pud_pte(pud))#define pud_mkyoung (pud) pte_pud(pte_mkyoung(pud_pte(pud)))
@@ -652,22 +648,46 @@ static inline pud_t pud_mkhuge(pud_t pud)#define __pud_to_phys (pud) __pte_to_phys(pud_pte(pud))#define __phys_to_pud_val (phys) __phys_to_pte_val(phys)#define pud_pfn (pud) ((__pud_to_phys(pud) & PUD_MASK) >> PAGE_SHIFT) -#define pfn_pud (pfn,prot) __pud(__phys_to_pud_val((phys_addr_t)(pfn) << PAGE_SHIFT) | pgprot_val(prot)) +static inline pud_t pfn_pud (unsigned long pfn,pgprot_t prot) +{ +pud_t pud =__pud (__phys_to_pud_val ((phys_addr_t )pfn <<PAGE_SHIFT ) | +pgprot_val (prot)); + +return pud_mkhuge (pud); +}#define pmd_pgprot pmd_pgprotstatic inline pgprot_t pmd_pgprot (pmd_t pmd) {unsigned long pfn =pmd_pfn (pmd); +pmdval_t protval =pmd_val (pmd) ^ +__phys_to_pmd_val ((phys_addr_t )pfn <<PAGE_SHIFT ); + +/* +* pgprot_t represents PTE-level attributes. Convert the PMD +* block descriptor type into a PTE page descriptor type. +*/ +pmdval_t mask =PMD_TYPE_MASK & ~PTE_VALID ; +pmdval_t val =PTE_TYPE_PAGE & ~PTE_VALID ; -return __pgprot (pmd_val (pfn_pmd (pfn,__pgprot (0 ))) ^pmd_val (pmd)); +return __pgprot ((protval & ~mask) | val); }#define pud_pgprot pud_pgprotstatic inline pgprot_t pud_pgprot (pud_t pud) {unsigned long pfn =pud_pfn (pud); +pudval_t protval =pud_val (pud) ^ +__phys_to_pud_val ((phys_addr_t )pfn <<PAGE_SHIFT ); + +/* +* pgprot_t represents PTE-level attributes. Convert the PUD +* block descriptor type into a PTE page descriptor type. +*/ +pudval_t mask =PUD_TYPE_MASK & ~PTE_VALID ; +pudval_t val =PTE_TYPE_PAGE & ~PTE_VALID ; -return __pgprot (pud_val (pfn_pud (pfn,__pgprot (0 ))) ^pud_val (pud)); +return __pgprot ((protval & ~mask) | val); } static inline void __set_ptes_anysz(struct mm_struct *mm, unsigned long addr,
arch/arm64/mm/mmu.c +2 -2
@@ -1816,7 +1816,7 @@ void vmemmap_free(unsigned long start, unsigned long end, int pud_set_huge(pud_t *pudp, phys_addr_t phys, pgprot_t prot) { - pud_t new_pud = pfn_pud(__phys_to_pfn(phys), mk_pud_sect_prot(prot)); + pud_t new_pud = pfn_pud(__phys_to_pfn(phys), prot); /* Only allow permission changes for now */ if (!pgattr_change_is_safe(READ_ONCE(pud_val(*pudp)),
@@ -1830,7 +1830,7 @@ int pud_set_huge(pud_t *pudp, phys_addr_t phys, pgprot_t prot) int pmd_set_huge(pmd_t *pmdp, phys_addr_t phys, pgprot_t prot) { - pmd_t new_pmd = pfn_pmd(__phys_to_pfn(phys), mk_pmd_sect_prot(prot)); + pmd_t new_pmd = pfn_pmd(__phys_to_pfn(phys), prot); /* Only allow permission changes for now */ if (!pgattr_change_is_safe(READ_ONCE(pmd_val(*pmdp)),
Signature · 1 line
2.43.0
Re: [PATCH mm-unstable RFC v4 2/7] arm64/mm: use PTE-level pgprot for huge PFN helpers
Quoted 38 lines · Yin Tirui
Make the arm64 PMD/PUD PFN helpers use PTE-level
pgprot_tas the basic format.
pfn_pmd()andpfn_pud()now translate PTE-level attributes into block entries.pmd_pgprot()andpud_pgprot()translate block descriptor attributes back into PTE-level attributes.Remove
mk_pmd_sect_prot()andmk_pud_sect_prot().Signed-off-by Yin Tirui <[email protected]>--- arch/arm64/include/asm/pgtable.h | 48 ++++++++++++++++++++++---------- arch/arm64/mm/mmu.c | 4 +-- 2 files changed, 36 insertions(+), 16 deletions(-)arch/arm64/include/asm/pgtable.h +0
-10@@ -511,16 +511,6 @@ static inline pmd_t pte_pmd(pte_t pte)return __pmd (pte_val (pte)); } -static inline pgprot_t mk_pud_sect_prot (pgprot_t prot) -{ -return __pgprot ((pgprot_val (prot) & ~PUD_TYPE_MASK ) |PUD_TYPE_SECT ); -} - -static inline pgprot_t mk_pmd_sect_prot (pgprot_t prot) -{ -return __pgprot ((pgprot_val (prot) & ~PMD_TYPE_MASK ) |PMD_TYPE_SECT ); -} -static inline pte_t pte_swp_mkexclusive (pte_t pte) {return set_pte_bit (pte,__pgprot (PTE_SWP_EXCLUSIVE ));@@ -628,7 +618,13 @@ static inline pmd_t pmd_mkspecial(pmd_t pmd)#define __pmd_to_phys (pmd) __pte_to_phys(pmd_pte(pmd))
arch/arm64/include/asm/pgtable.h static inline pmd_t pmd_mkspecial(pmd_t pmd) · line 627
Quoted 10 lines
arch/arm64/include/asm/pgtable.h +7
-1#define __phys_to_pmd_val (phys) __phys_to_pte_val(phys)#define pmd_pfn (pmd) ((__pmd_to_phys(pmd) & PMD_MASK) >> PAGE_SHIFT) -#define pfn_pmd (pfn,prot) __pmd(__phys_to_pmd_val((phys_addr_t)(pfn) << PAGE_SHIFT) | pgprot_val(prot)) +static inline pmd_t pfn_pmd (unsigned long pfn,pgprot_t prot) +{ +pmd_t pmd =__pmd (__phys_to_pmd_val ((phys_addr_t )pfn <<PAGE_SHIFT ) | +pgprot_val (prot)); + +return pmd_mkhuge (pmd); +}
There's a slight potential difference in behaviour here, in that the old mk_pXd_sect_prot() helpers would always return a pgprot_t with the valid bit set, whereas pXd_mkhuge() preserve it from the input prot.
I couldn't find any places that cared about this, though, so:
Will
RFC PATCH v4 3/7 powerpc/mm: use PTE-level pgprot for huge PFN helpers
In reply to Yin TiruiMake the powerpc PMD PFN helper use PTE-level pgprot_t as the basic format.
pmd_pgprot() currently derives pgprot_t from the PMD entry through pte_pgprot(). Some PMD leaf entries can carry H_PAGE_THP_HUGE, which is specific to huge PMDs and should not be propagated into PTE-level pgprot_t.
Mask H_PAGE_THP_HUGE out in pmd_pgprot().
--- arch/powerpc/include/asm/pgtable.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/pgtable.h +10 -1
@@ -67,7 +67,16 @@ static inline pgprot_t pte_pgprot(pte_t pte)#define pmd_pgprot pmd_pgprotstatic inline pgprot_t pmd_pgprot (pmd_t pmd) { -return pte_pgprot (pmd_pte (pmd)); +pgprot_t prot =pte_pgprot (pmd_pte (pmd)); + +/* +* pmd_pgprot() returns PTE-level pgprot_t. H_PAGE_THP_HUGE is specific +* to huge PMDs. +*/ +#ifdef H_PAGE_THP_HUGE + prot =__pgprot (pgprot_val (prot) & ~H_PAGE_THP_HUGE ); +#endif +return prot; }#define pud_pgprot pud_pgprot
Signature · 1 line
2.43.0
RFC PATCH v4 4/7 mm/huge_memory: refactor copy_huge_pmd()
In reply to Yin TiruiClassify the source PMD via pmd_present() and vm_normal_folio_pmd(), matching the way the PTE path uses pte_present() and vm_normal_page(). This moves the present-PMD decision from VMA identity checks to the actual PMD/folio state.
Drop the defensive "if (!pmd_trans_huge(pmd)) goto out_unlock" branch:
with mmap_write_lock held during fork, it should not occur.
Extract the present-PMD side of copy_huge_pmd() into copy_present_huge_pmd(). The helper owns the child pgtable passed by the
caller: it either deposits the pgtable when installing a copied PMD, or frees it on paths that do not install one.
The child pgtable is now allocated once up front and freed on every skip path. This makes file/shmem and PFNMAP/special skip paths take the PMD locks and free the preallocated pgtable before returning. These are not expected to be hot paths, and the PFNMAP case is reused by the follow-up PMD PFNMAP copy support.
--- mm/huge_memory.c | 175 +++++++++++++++++++++++++---------------------- 1 file changed, 95 insertions(+), 80 deletions(-)
mm/huge_memory.c +95 -80
@@ -1879,6 +1879,82 @@ bool touch_pmd(struct vm_area_struct *vma, unsigned long addr,return false; } +static int copy_present_huge_pmd ( +struct mm_struct *dst_mm,struct mm_struct *src_mm, +pmd_t *dst_pmd,pmd_t *src_pmd,unsigned long addr, +struct vm_area_struct *dst_vma,struct vm_area_struct *src_vma, +pmd_t pmd,pgtable_t pgtable,bool *need_split) +{ +struct folio *src_folio; +bool wrprotect = true; + + src_folio =vm_normal_folio_pmd (src_vma, addr, pmd); +if (!src_folio) { +/* +* When page table lock is held, the huge zero pmd should not be +* under splitting since we don't split the page itself, only pmd to +* a page table. +*/ +if (is_huge_zero_pmd (pmd)) { +/* +* mm_get_huge_zero_folio() will never allocate a new +* folio here, since we already have a zero page to +* copy. It just takes a reference. +*/ +mm_get_huge_zero_folio (dst_mm); + goto set_pmd; + } + +/* +* Making sure it's not a CoW VMA with writable +* mapping, otherwise it means either the anon page wrongly +* applied special bit, or we made the PRIVATE mapping be +* able to wrongly write to the backend MMIO. +*/ +VM_WARN_ON_ONCE (is_cow_mapping (src_vma->vm_flags) &&pmd_write (pmd)); +pte_free (dst_mm, pgtable); + pgtable =NULL ; + wrprotect = false; + goto set_pmd; + } + +/* File THPs are copied lazily by refaulting. */ +if (!folio_test_anon (src_folio)) { +pte_free (dst_mm, pgtable); +return 0 ; + } + +folio_get (src_folio); +if (unlikely (folio_try_dup_anon_rmap_pmd (src_folio, + &src_folio->page, + dst_vma, src_vma))) { +/* Page maybe pinned: split and retry the fault on PTEs. */ +folio_put (src_folio); +pte_free (dst_mm, pgtable); + *need_split = true; +return -EAGAIN ; + } +add_mm_counter (dst_mm,MM_ANONPAGES ,HPAGE_PMD_NR ); + +set_pmd: +if (pgtable) { +mm_inc_nr_ptes (dst_mm); +pgtable_trans_huge_deposit (dst_mm, dst_pmd, pgtable); + } + +if (wrprotect) { +pmdp_set_wrprotect (src_mm, addr, src_pmd); +if (!userfaultfd_wp (dst_vma)) + pmd =pmd_clear_uffd_wp (pmd); + pmd =pmd_wrprotect (pmd); + } + + pmd =pmd_mkold (pmd); +set_pmd_at (dst_mm, addr, dst_pmd, pmd); + +return 0 ; +} + static void copy_huge_non_present_pmd( struct mm_struct *dst_mm, struct mm_struct *src_mm, pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
@@ -1940,104 +2016,43 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) { spinlock_t *dst_ptl, *src_ptl; -struct page *src_page; -struct folio *src_folio; -pmd_t pmd;pgtable_t pgtable =NULL ; -int ret = -ENOMEM ; - - pmd =pmdp_get_lockless (src_pmd); -if (unlikely (pmd_present (pmd) &&pmd_special (pmd) && - !is_huge_zero_pmd (pmd))) { - dst_ptl =pmd_lock (dst_mm, dst_pmd); - src_ptl =pmd_lockptr (src_mm, src_pmd); -spin_lock_nested (src_ptl,SINGLE_DEPTH_NESTING ); -/* -* No need to recheck the pmd, it can't change with write -* mmap lock held here. -* -* Meanwhile, making sure it's not a CoW VMA with writable -* mapping, otherwise it means either the anon page wrongly -* applied special bit, or we made the PRIVATE mapping be -* able to wrongly write to the backend MMIO. -*/ -VM_WARN_ON_ONCE (is_cow_mapping (src_vma->vm_flags) &&pmd_write (pmd)); - goto set_pmd; - } - -/* Skip if can be re-fill on fault */ -if (!vma_is_anonymous (dst_vma)) -return 0 ; +bool need_split = false; +int ret =0 ; +pmd_t pmd; pgtable =pte_alloc_one (dst_mm);if (unlikely (!pgtable)) - goto out; +return -ENOMEM ; dst_ptl =pmd_lock (dst_mm, dst_pmd); src_ptl =pmd_lockptr (src_mm, src_pmd);spin_lock_nested (src_ptl,SINGLE_DEPTH_NESTING ); - ret = -EAGAIN ; pmd = *src_pmd; -if (unlikely (thp_migration_supported () && -pmd_is_valid_softleaf (pmd))) { -copy_huge_non_present_pmd (dst_mm, src_mm, dst_pmd, src_pmd, addr, +if (likely (pmd_present (pmd))) { + ret =copy_present_huge_pmd (dst_mm, src_mm, dst_pmd, src_pmd, addr, + dst_vma, src_vma, pmd, pgtable, &need_split); + }else if (unlikely (thp_migration_supported () &&pmd_is_valid_softleaf (pmd))) { +if (unlikely (!vma_is_anonymous (dst_vma))) +pte_free (dst_mm, pgtable); +else +copy_huge_non_present_pmd (dst_mm, src_mm, dst_pmd, src_pmd, addr, dst_vma, src_vma, pmd, pgtable); - ret =0 ; - goto out_unlock; - } - -if (unlikely (!pmd_trans_huge (pmd))) { + }else { +VM_WARN_ONCE (1 ,"unexpected non-present PMD %llx\n" , + (unsigned long long )pmd_val (pmd));pte_free (dst_mm, pgtable); - goto out_unlock; - } -/* -* When page table lock is held, the huge zero pmd should not be -* under splitting since we don't split the page itself, only pmd to -* a page table. -*/ -if (is_huge_zero_pmd (pmd)) { -/* -* mm_get_huge_zero_folio() will never allocate a new -* folio here, since we already have a zero page to -* copy. It just takes a reference. -*/ -mm_get_huge_zero_folio (dst_mm); - goto out_zero_page; + ret = -EAGAIN ; } - src_page =pmd_page (pmd); -VM_BUG_ON_PAGE (!PageHead (src_page), src_page); - src_folio =page_folio (src_page); +spin_unlock (src_ptl); +spin_unlock (dst_ptl); -folio_get (src_folio); -if (unlikely (folio_try_dup_anon_rmap_pmd (src_folio, src_page, dst_vma, src_vma))) { -/* Page maybe pinned: split and retry the fault on PTEs. */ -folio_put (src_folio); -pte_free (dst_mm, pgtable); -spin_unlock (src_ptl); -spin_unlock (dst_ptl); +if (unlikely (need_split))__split_huge_pmd (src_vma, src_pmd, addr, false); -return -EAGAIN ; - } -add_mm_counter (dst_mm,MM_ANONPAGES ,HPAGE_PMD_NR ); -out_zero_page: -mm_inc_nr_ptes (dst_mm); -pgtable_trans_huge_deposit (dst_mm, dst_pmd, pgtable); -pmdp_set_wrprotect (src_mm, addr, src_pmd); -if (!userfaultfd_wp (dst_vma)) - pmd =pmd_clear_uffd_wp (pmd); - pmd =pmd_wrprotect (pmd); -set_pmd: - pmd =pmd_mkold (pmd); -set_pmd_at (dst_mm, addr, dst_pmd, pmd); - ret =0 ; -out_unlock: -spin_unlock (src_ptl); -spin_unlock (dst_ptl); -out:return ret; }
Signature · 1 line
2.43.0
Dev Jain
reviewerRe: [PATCH mm-unstable RFC v4 4/7] mm/huge_memory: refactor copy_huge_pmd()
Quoted 7 lines · Yin Tirui
Classify the source PMD via
pmd_present()andvm_normal_folio_pmd(), matching the way the PTE path usespte_present()andvm_normal_page(). This moves the present-PMD decision from VMA identity checks to the actual PMD/folio state.Drop the defensive "if (!pmd_trans_huge(pmd)) goto
out_unlock" branch:
withmmap_write_lockheld during fork, it should not occur.
Split this from this patch? This is a functional change so will be better to review it separately.
Quoted 6 lines
Extract the present-PMD side of
copy_huge_pmd()intocopy_present_huge_pmd(). The helper owns the child pgtable passed by the
caller: it either deposits the pgtable when installing a copied PMD, or frees it on paths that do not install one.
Quoted 8 lines
The child pgtable is now allocated once up front and freed on every skip path. This makes file/shmem and PFNMAP/special skip paths take the PMD locks and free the preallocated pgtable before returning. These are not expected to be hot paths, and the PFNMAP case is reused by the follow-up PMD PFNMAP copy support.
Signed-off-by Yin Tirui <[email protected]>---
Since the series is still marked RFC, I think it would be better to send the refactor patches separately?
Quoted 45 lines
mm/huge_memory.c | 175 +++++++++++++++++++++++++---------------------- 1 file changed, 95 insertions(+), 80 deletions(-)mm/huge_memory.c +34
-0@@ -1879,6 +1879,82 @@ bool touch_pmd(struct vm_area_struct *vma, unsigned long addr,return false; } +static int copy_present_huge_pmd ( +struct mm_struct *dst_mm,struct mm_struct *src_mm, +pmd_t *dst_pmd,pmd_t *src_pmd,unsigned long addr, +struct vm_area_struct *dst_vma,struct vm_area_struct *src_vma, +pmd_t pmd,pgtable_t pgtable,bool *need_split) +{ +struct folio *src_folio; +bool wrprotect = true; + + src_folio =vm_normal_folio_pmd (src_vma, addr, pmd); +if (!src_folio) { +/* +* When page table lock is held, the huge zero pmd should not be +* under splitting since we don't split the page itself, only pmd to +* a page table. +*/ +if (is_huge_zero_pmd (pmd)) { +/* +* mm_get_huge_zero_folio() will never allocate a new +* folio here, since we already have a zero page to +* copy. It just takes a reference. +*/ +mm_get_huge_zero_folio (dst_mm); + goto set_pmd; + } + +/* +* Making sure it's not a CoW VMA with writable +* mapping, otherwise it means either the anon page wrongly +* applied special bit, or we made the PRIVATE mapping be +* able to wrongly write to the backend MMIO. +*/ +VM_WARN_ON_ONCE (is_cow_mapping (src_vma->vm_flags) &&pmd_write (pmd)); +pte_free (dst_mm, pgtable);
mm/huge_memory.c bool touch_pmd(struct vm_area_struct *vma, unsigned long addr, · line 1925
Quoted 10 lines
mm/huge_memory.c +10
-0+ pgtable =NULL ; + wrprotect = false; + goto set_pmd; + } + +/* File THPs are copied lazily by refaulting. */ +if (!folio_test_anon (src_folio)) { +pte_free (dst_mm, pgtable); +return 0 ; + }
You removed !vma_is_anonymous to condense it into !folio_test_anon. For private-file mappings that is not true, but okay since PMD mapping is not supported.
Quoted 159 lines
mm/huge_memory.c +51
-80+ +folio_get (src_folio); +if (unlikely (folio_try_dup_anon_rmap_pmd (src_folio, + &src_folio->page, + dst_vma, src_vma))) { +/* Page maybe pinned: split and retry the fault on PTEs. */ +folio_put (src_folio); +pte_free (dst_mm, pgtable); + *need_split = true; +return -EAGAIN ; + } +add_mm_counter (dst_mm,MM_ANONPAGES ,HPAGE_PMD_NR ); + +set_pmd: +if (pgtable) { +mm_inc_nr_ptes (dst_mm); +pgtable_trans_huge_deposit (dst_mm, dst_pmd, pgtable); + } + +if (wrprotect) { +pmdp_set_wrprotect (src_mm, addr, src_pmd); +if (!userfaultfd_wp (dst_vma)) + pmd =pmd_clear_uffd_wp (pmd); + pmd =pmd_wrprotect (pmd); + } + + pmd =pmd_mkold (pmd); +set_pmd_at (dst_mm, addr, dst_pmd, pmd); + +return 0 ; +} + static void copy_huge_non_present_pmd( struct mm_struct *dst_mm, struct mm_struct *src_mm, pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,@@ -1940,104 +2016,43 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) { spinlock_t *dst_ptl, *src_ptl; -struct page *src_page; -struct folio *src_folio; -pmd_t pmd;pgtable_t pgtable =NULL ; -int ret = -ENOMEM ; - - pmd =pmdp_get_lockless (src_pmd); -if (unlikely (pmd_present (pmd) &&pmd_special (pmd) && - !is_huge_zero_pmd (pmd))) { - dst_ptl =pmd_lock (dst_mm, dst_pmd); - src_ptl =pmd_lockptr (src_mm, src_pmd); -spin_lock_nested (src_ptl,SINGLE_DEPTH_NESTING ); -/* -* No need to recheck the pmd, it can't change with write -* mmap lock held here. -* -* Meanwhile, making sure it's not a CoW VMA with writable -* mapping, otherwise it means either the anon page wrongly -* applied special bit, or we made the PRIVATE mapping be -* able to wrongly write to the backend MMIO. -*/ -VM_WARN_ON_ONCE (is_cow_mapping (src_vma->vm_flags) &&pmd_write (pmd)); - goto set_pmd; - } - -/* Skip if can be re-fill on fault */ -if (!vma_is_anonymous (dst_vma)) -return 0 ; +bool need_split = false; +int ret =0 ; +pmd_t pmd; pgtable =pte_alloc_one (dst_mm);if (unlikely (!pgtable)) - goto out; +return -ENOMEM ; dst_ptl =pmd_lock (dst_mm, dst_pmd); src_ptl =pmd_lockptr (src_mm, src_pmd);spin_lock_nested (src_ptl,SINGLE_DEPTH_NESTING ); - ret = -EAGAIN ; pmd = *src_pmd; -if (unlikely (thp_migration_supported () && -pmd_is_valid_softleaf (pmd))) { -copy_huge_non_present_pmd (dst_mm, src_mm, dst_pmd, src_pmd, addr, +if (likely (pmd_present (pmd))) { + ret =copy_present_huge_pmd (dst_mm, src_mm, dst_pmd, src_pmd, addr, + dst_vma, src_vma, pmd, pgtable, &need_split); + }else if (unlikely (thp_migration_supported () &&pmd_is_valid_softleaf (pmd))) { +if (unlikely (!vma_is_anonymous (dst_vma))) +pte_free (dst_mm, pgtable); +else +copy_huge_non_present_pmd (dst_mm, src_mm, dst_pmd, src_pmd, addr, dst_vma, src_vma, pmd, pgtable); - ret =0 ; - goto out_unlock; - } - -if (unlikely (!pmd_trans_huge (pmd))) { + }else { +VM_WARN_ONCE (1 ,"unexpected non-present PMD %llx\n" , + (unsigned long long )pmd_val (pmd));pte_free (dst_mm, pgtable); - goto out_unlock; - } -/* -* When page table lock is held, the huge zero pmd should not be -* under splitting since we don't split the page itself, only pmd to -* a page table. -*/ -if (is_huge_zero_pmd (pmd)) { -/* -* mm_get_huge_zero_folio() will never allocate a new -* folio here, since we already have a zero page to -* copy. It just takes a reference. -*/ -mm_get_huge_zero_folio (dst_mm); - goto out_zero_page; + ret = -EAGAIN ; } - src_page =pmd_page (pmd); -VM_BUG_ON_PAGE (!PageHead (src_page), src_page); - src_folio =page_folio (src_page); +spin_unlock (src_ptl); +spin_unlock (dst_ptl); -folio_get (src_folio); -if (unlikely (folio_try_dup_anon_rmap_pmd (src_folio, src_page, dst_vma, src_vma))) { -/* Page maybe pinned: split and retry the fault on PTEs. */ -folio_put (src_folio); -pte_free (dst_mm, pgtable); -spin_unlock (src_ptl); -spin_unlock (dst_ptl); +if (unlikely (need_split))__split_huge_pmd (src_vma, src_pmd, addr, false); -return -EAGAIN ; - } -add_mm_counter (dst_mm,MM_ANONPAGES ,HPAGE_PMD_NR ); -out_zero_page: -mm_inc_nr_ptes (dst_mm); -pgtable_trans_huge_deposit (dst_mm, dst_pmd, pgtable); -pmdp_set_wrprotect (src_mm, addr, src_pmd); -if (!userfaultfd_wp (dst_vma)) - pmd =pmd_clear_uffd_wp (pmd); - pmd =pmd_wrprotect (pmd); -set_pmd: - pmd =pmd_mkold (pmd); -set_pmd_at (dst_mm, addr, dst_pmd, pmd); - ret =0 ; -out_unlock: -spin_unlock (src_ptl); -spin_unlock (dst_ptl); -out:return ret; }
RFC PATCH v4 5/7 mm/huge_memory: refactor __split_huge_pmd_locked()
In reply to Yin TiruiRework __split_huge_pmd_locked() to classify huge PMDs by the PMD entry itself instead of starting from vma_is_anonymous().
Present PMDs are classified with vm_normal_folio_pmd(): file/shmem THPs are dropped and refaulted later, anonymous THPs are split into PTEs, and PMDs without a normal folio are handled as huge zero or special PMDs.
Non-present PMDs are classified with pmd_to_softleaf_folio(): file/shmem migration entries are dropped, while anonymous migration/device-private entries are split into PTEs.
This also makes the anonymous decision folio-based. A private file mapping that has CoW'ed to an anonymous THP now follows the anonymous path even though the VMA is file-backed.
No intended behavioural change.
--- mm/huge_memory.c | 197 +++++++++++++++++++++++++++-------------------- 1 file changed, 114 insertions(+), 83 deletions(-)
mm/huge_memory.c +108 -83
@@ -3136,25 +3136,38 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,count_vm_event (THP_SPLIT_PMD ); -if (!vma_is_anonymous (vma)) { - old_pmd =pmdp_huge_clear_flush (vma, haddr, pmd); -/* -* We are going to unmap this huge page. So -* just go ahead and zap it -*/ -if (arch_needs_pgtable_deposit ()) -zap_deposited_table (mm, pmd); -if (vma_is_special_huge (vma)) -return ; -if (unlikely (pmd_is_migration_entry (old_pmd))) { -const softleaf_t old_entry =softleaf_from_pmd (old_pmd); +if (pmd_present (*pmd)) { + folio =vm_normal_folio_pmd (vma, haddr, *pmd); + +if (unlikely (!folio)) { +if (is_huge_zero_pmd (*pmd)) { +/* +* FIXME: Do we want to invalidate secondary mmu by calling +* mmu_notifier_arch_invalidate_secondary_tlbs() see comments below +* inside __split_huge_pmd() ? +* +* We are going from a zero huge page write protected to zero +* small page also write protected so it does not seems useful +* to invalidate secondary mmu at this time. +*/ +return __split_huge_zero_page_pmd (vma, haddr, pmd); + } - folio =softleaf_to_folio (old_entry); - }else if (is_huge_zero_pmd (old_pmd)) { +/* Present but not a normal folio: drop the PMD. */ + old_pmd =pmdp_huge_clear_flush (vma, haddr, pmd); +if (arch_needs_pgtable_deposit ()) +zap_deposited_table (mm, pmd);return ; - }else { + } + +if (unlikely (!folio_test_anon (folio))) { + old_pmd =pmdp_huge_clear_flush (vma, haddr, pmd); +if (arch_needs_pgtable_deposit ()) +zap_deposited_table (mm, pmd); +if (vma_is_special_huge (vma)) +return ; + page =pmd_page (old_pmd); - folio =page_folio (page);if (!folio_test_dirty (folio) &&pmd_dirty (old_pmd))folio_mark_dirty (folio);if (!folio_test_referenced (folio) &&pmd_young (old_pmd))
@@ -3164,72 +3177,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,folio_put (folio);return ; } -add_mm_counter (mm,mm_counter_file (folio), -HPAGE_PMD_NR ); -return ; - } - -if (is_huge_zero_pmd (*pmd)) { -/* -* FIXME: Do we want to invalidate secondary mmu by calling -* mmu_notifier_arch_invalidate_secondary_tlbs() see comments below -* inside __split_huge_pmd() ? -* -* We are going from a zero huge page write protected to zero -* small page also write protected so it does not seems useful -* to invalidate secondary mmu at this time. -*/ -return __split_huge_zero_page_pmd (vma, haddr, pmd); - } - -if (pmd_is_migration_entry (*pmd)) { -softleaf_t entry; - - old_pmd = *pmd; - entry =softleaf_from_pmd (old_pmd); - page =softleaf_to_page (entry); - folio =page_folio (page); - - soft_dirty =pmd_swp_soft_dirty (old_pmd); - uffd_wp =pmd_swp_uffd_wp (old_pmd); - - write =softleaf_is_migration_write (entry); -if (PageAnon (page)) - anon_exclusive =softleaf_is_migration_read_exclusive (entry); - young =softleaf_is_migration_young (entry); - dirty =softleaf_is_migration_dirty (entry); - }else if (pmd_is_device_private_entry (*pmd)) { -softleaf_t entry; - - old_pmd = *pmd; - entry =softleaf_from_pmd (old_pmd); - page =softleaf_to_page (entry); - folio =page_folio (page); - - soft_dirty =pmd_swp_soft_dirty (old_pmd); - uffd_wp =pmd_swp_uffd_wp (old_pmd); - - write =softleaf_is_device_private_write (entry); - anon_exclusive =PageAnonExclusive (page); - -/* -* Device private THP should be treated the same as regular -* folios w.r.t anon exclusive handling. See the comments for -* folio handling and anon_exclusive below. -*/ -if (freeze && anon_exclusive && -folio_try_share_anon_rmap_pmd (folio, page)) - freeze = false; -if (!freeze) { -rmap_t rmap_flags =RMAP_NONE ; - -folio_ref_add (folio,HPAGE_PMD_NR -1 ); -if (anon_exclusive) - rmap_flags |=RMAP_EXCLUSIVE ; -folio_add_anon_rmap_ptes (folio, page,HPAGE_PMD_NR , - vma, haddr, rmap_flags); - } - }else {/* * Up to this point the pmd is present and huge and userland has * the whole access to the hugepage during the split (which
@@ -3255,7 +3203,6 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,*/ old_pmd =pmdp_invalidate (vma, haddr, pmd); page =pmd_page (old_pmd); - folio =page_folio (page);if (pmd_dirty (old_pmd)) { dirty = true;folio_set_dirty (folio);
@@ -3266,7 +3213,6 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, uffd_wp =pmd_uffd_wp (old_pmd);VM_WARN_ON_FOLIO (!folio_ref_count (folio), folio); -VM_WARN_ON_FOLIO (!folio_test_anon (folio), folio);/* * Without "freeze", we'll simply split the PMD, propagating the
@@ -3296,6 +3242,85 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,folio_add_anon_rmap_ptes (folio, page,HPAGE_PMD_NR , vma, haddr, rmap_flags); } + }else { +/* +* Non-present PMD: a softleaf-encoded migration or +* device-private entry. pmd_to_softleaf_folio() warns and +* returns NULL for any other encoding. +*/ + folio =pmd_to_softleaf_folio (*pmd); +if (unlikely (!folio)) +return ; + +if (unlikely (!folio_test_anon (folio))) { +/* +* File/shmem migration entry: drop the PMD without +* splitting. Unlike the present case the entry holds +* neither a folio reference nor an rmap to release, +* so just adjust the RSS counter. +*/ +pmdp_huge_clear_flush (vma, haddr, pmd); +if (arch_needs_pgtable_deposit ()) +zap_deposited_table (mm, pmd); +if (unlikely (vma_is_special_huge (vma))) { +VM_WARN_ONCE (1 , +"unexpected special huge PMD migration entry\n" ); +return ; + } +add_mm_counter (mm,mm_counter_file (folio), -HPAGE_PMD_NR ); +return ; + } + +if (pmd_is_migration_entry (*pmd)) { +softleaf_t entry; + + old_pmd = *pmd; + entry =softleaf_from_pmd (old_pmd); + page =softleaf_to_page (entry); + + soft_dirty =pmd_swp_soft_dirty (old_pmd); + uffd_wp =pmd_swp_uffd_wp (old_pmd); + + write =softleaf_is_migration_write (entry); +if (PageAnon (page)) + anon_exclusive =softleaf_is_migration_read_exclusive (entry); + young =softleaf_is_migration_young (entry); + dirty =softleaf_is_migration_dirty (entry); + }else if (pmd_is_device_private_entry (*pmd)) { +softleaf_t entry; + + old_pmd = *pmd; + entry =softleaf_from_pmd (old_pmd); + page =softleaf_to_page (entry); + + soft_dirty =pmd_swp_soft_dirty (old_pmd); + uffd_wp =pmd_swp_uffd_wp (old_pmd); + + write =softleaf_is_device_private_write (entry); + anon_exclusive =PageAnonExclusive (page); + +/* +* Device-private THP should be treated the same as +* regular folios w.r.t. anon-exclusive handling. See +* the matching code for present anon folios above. +*/ +if (freeze && anon_exclusive && +folio_try_share_anon_rmap_pmd (folio, page)) + freeze = false; +if (!freeze) { +rmap_t rmap_flags =RMAP_NONE ; + +folio_ref_add (folio,HPAGE_PMD_NR -1 ); +if (anon_exclusive) + rmap_flags |=RMAP_EXCLUSIVE ; + +folio_add_anon_rmap_ptes (folio, page,HPAGE_PMD_NR , + vma, haddr, rmap_flags); + } + }else { +VM_WARN_ON_ONCE (1 ); +return ; + } }/*
Signature · 1 line
2.43.0
RFC PATCH v4 6/7 mm/huge_memory: make move_huge_pmd() use has_deposited_pgtable()
In reply to Yin TiruiUse has_deposited_pgtable() in move_huge_pmd() to decide whether pmd_move_must_withdraw() should move a deposited pgtable instead of using the VMA type.
PowerPC radix follows the generic rule. PowerPC hash keeps returning true.
--- arch/powerpc/include/asm/book3s/64/pgtable.h | 5 ++--- arch/powerpc/mm/book3s64/pgtable.c | 11 +++++------ mm/huge_memory.c | 20 ++++++++++++-------- 3 files changed, 19 insertions(+), 17 deletions(-)
arch/powerpc/include/asm/book3s/64/pgtable.h +2 -3
@@ -1424,9 +1424,8 @@ extern pud_t pudp_invalidate(struct vm_area_struct *vma, unsigned long address,#define pmd_move_must_withdraw pmd_move_must_withdrawstruct spinlock ; -extern int pmd_move_must_withdraw (struct spinlock *new_pmd_ptl, -struct spinlock *old_pmd_ptl, -struct vm_area_struct *vma); +extern bool pmd_move_must_withdraw (struct spinlock *new_pmd_ptl, +struct spinlock *old_pmd_ptl,bool has_deposit);/* * Hash translation mode use the deposited table to store hash pte * slot information.
arch/powerpc/mm/book3s64/pgtable.c +5 -6
@@ -548,15 +548,14 @@ void ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr,* pmd page. Hence if we have different pmd page we need to withdraw during pmd * move. * -* With hash we use deposited table always irrespective of anon or not. -* With radix we use deposited table only for anonymous mapping. +* With hash we use deposited table always irrespective of has_deposit or not. +* With radix we use the same rule as the generic implementation. */ -int pmd_move_must_withdraw (struct spinlock *new_pmd_ptl, -struct spinlock *old_pmd_ptl, -struct vm_area_struct *vma) +bool pmd_move_must_withdraw (struct spinlock *new_pmd_ptl, +struct spinlock *old_pmd_ptl,bool has_deposit) {if (radix_enabled ()) -return (new_pmd_ptl != old_pmd_ptl) &&vma_is_anonymous (vma); +return (new_pmd_ptl != old_pmd_ptl) && has_deposit;return true; }
mm/huge_memory.c +12 -8
@@ -2552,17 +2552,14 @@ bool zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, }#ifndef pmd_move_must_withdraw -static inline int pmd_move_must_withdraw (spinlock_t *new_pmd_ptl, -spinlock_t *old_pmd_ptl, -struct vm_area_struct *vma) +static inline bool pmd_move_must_withdraw (spinlock_t *new_pmd_ptl, +spinlock_t *old_pmd_ptl,bool has_deposit) {/* * With split pmd lock we also need to move preallocated * PTE page table if new_pmd is on different PMD page table. -* -* We also don't deposit and withdraw tables for file pages. */ -return (new_pmd_ptl != old_pmd_ptl) &&vma_is_anonymous (vma); +return (new_pmd_ptl != old_pmd_ptl) && has_deposit; }#endif
@@ -2595,8 +2592,11 @@ bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr, {spinlock_t *old_ptl, *new_ptl;pmd_t pmd; +struct folio *folio =NULL ;struct mm_struct *mm = vma->vm_mm;bool force_flush = false; +bool has_deposit; +bool is_present;/* * The destination pmd shouldn't be established, free_pgtables()
@@ -2618,11 +2618,15 @@ bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,if (new_ptl != old_ptl)spin_lock_nested (new_ptl,SINGLE_DEPTH_NESTING ); pmd =pmdp_huge_get_and_clear (mm, old_addr, old_pmd); -if (pmd_present (pmd)) + is_present =pmd_present (pmd); +if (is_present) force_flush = true;VM_BUG_ON (!pmd_none (*new_pmd)); -if (pmd_move_must_withdraw (new_ptl, old_ptl, vma)) { + folio =normal_or_softleaf_folio_pmd (vma, old_addr, pmd, is_present); + has_deposit =has_deposited_pgtable (vma, pmd, folio); + +if (pmd_move_must_withdraw (new_ptl, old_ptl, has_deposit)) {pgtable_t pgtable; pgtable =pgtable_trans_huge_withdraw (mm, old_pmd);pgtable_trans_huge_deposit (mm, new_pmd, pgtable);
Signature · 1 line
2.43.0
RFC PATCH v4 7/7 mm: add PMD-level PFNMAP support for remap_pfn_range()
In reply to Yin TiruiTeach remap_pfn_range() to install PMD-sized PFNMAP entries when the virtual range and PFN are PMD-aligned, the architecture exposes PMD PFNMAP support, and PMD leaves are available at runtime. The path only runs on VMAs without ->fault or ->huge_fault, so the resulting PMDs are known to be non-refaultable.
Non-refaultable PFNMAP PMDs cannot be rebuilt on demand and are therefore installed with a deposited pgtable. vma_pfnmap_has_deposited_pgtable() becomes the common predicate driving the deposit logic in copy_huge_pmd(), zap_huge_pmd() through has_deposited_pgtable(), and the new __split_huge_pfnmap_pmd().
The split path withdraws the pgtable and populates it with special PTEs derived from the original PMD using pmd_pfn() and pmd_pgprot(). With pmd_pgprot() returning PTE-level pgprot_t, this preserves protection and cache attributes without reintroducing pte_clrhuge().
--- mm/huge_memory.c | 60 ++++++++++++++++++++++++++++----- mm/internal.h | 21 ++++++++++++ mm/memory.c | 87 +++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 148 insertions(+), 20 deletions(-)
mm/huge_memory.c +49 -8
@@ -1879,6 +1879,8 @@ bool touch_pmd(struct vm_area_struct *vma, unsigned long addr,return false; } +static bool has_deposited_pgtable (struct vm_area_struct *vma,pmd_t pmdval, +struct folio *folio); static int copy_present_huge_pmd( struct mm_struct *dst_mm, struct mm_struct *src_mm, pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
@@ -1912,8 +1914,12 @@ static int copy_present_huge_pmd(* able to wrongly write to the backend MMIO. */ VM_WARN_ON_ONCE (is_cow_mapping (src_vma->vm_flags) &&pmd_write (pmd)); -pte_free (dst_mm, pgtable); - pgtable =NULL ; + +if (!has_deposited_pgtable (dst_vma, pmd,NULL )) { +pte_free (dst_mm, pgtable); + pgtable =NULL ; + } + wrprotect = false; goto set_pmd; }
@@ -2495,11 +2501,19 @@ static bool has_deposited_pgtable(struct vm_area_struct *vma, pmd_t pmdval,if (is_huge_zero_pmd (pmdval))return !vma_is_dax (vma); +/* +* PMD-sized PFNMAP mappings installed without fault handlers cannot be +* refaulted after the PMD is cleared, so they carry a deposited page +* table for later partial unmap/mprotect. +*/ +if (!folio) +return pmd_present (pmdval) &&vma_pfnmap_has_deposited_pgtable (vma); +/* * Otherwise, only anonymous folios are deposited, see * __do_huge_pmd_anonymous_page(). */ -return folio &&folio_test_anon (folio); +return folio_test_anon (folio); }/**
@@ -3118,6 +3132,32 @@ static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,pmd_populate (mm, pmd, pgtable); } +static void __split_huge_pfnmap_pmd (struct vm_area_struct *vma, +unsigned long haddr,pmd_t *pmd) +{ +struct mm_struct *mm = vma->vm_mm; +pgtable_t pgtable; +pmd_t old_pmd, _pmd; +pte_t *pte, entry; + + old_pmd =pmdp_huge_clear_flush (vma, haddr, pmd); +if (!has_deposited_pgtable (vma, old_pmd,NULL )) +return ; + + pgtable =pgtable_trans_huge_withdraw (mm, pmd); +pmd_populate (mm, &_pmd, pgtable); + + pte =pte_offset_map (&_pmd, haddr); +VM_BUG_ON (!pte); + + entry =pfn_pte (pmd_pfn (old_pmd),pmd_pgprot (old_pmd)); +set_ptes (mm, haddr, pte, entry,HPAGE_PMD_NR ); +pte_unmap (pte); + +smp_wmb ();/* make pte visible before pmd */ +pmd_populate (mm, pmd, pgtable); +} +static void __split_huge_pmd_locked (struct vm_area_struct *vma,pmd_t *pmd,unsigned long haddr,bool freeze) {
@@ -3157,11 +3200,12 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,return __split_huge_zero_page_pmd (vma, haddr, pmd); } -/* Present but not a normal folio: drop the PMD. */ - old_pmd =pmdp_huge_clear_flush (vma, haddr, pmd); -if (arch_needs_pgtable_deposit ()) -zap_deposited_table (mm, pmd); -return ; +/* +* Present PMDs without a normal folio are special mappings. Huge zero PMDs +* are handled above; the remaining PMD-level special mappings are PFNMAP +* mappings. +*/ +return __split_huge_pfnmap_pmd (vma, haddr, pmd); }if (unlikely (!folio_test_anon (folio))) {
mm/internal.h +21 -0
@@ -198,6 +198,27 @@ static inline void vma_close(struct vm_area_struct *vma) } } +static inline bool vma_has_fault_handler (const struct vm_area_struct *vma) +{ +const struct vm_operations_struct *vm_ops = vma->vm_ops; + +return vm_ops && (vm_ops->fault || vm_ops->huge_fault); +} + +/* +* PMD-sized PFNMAP mappings installed without fault handlers cannot be +* recreated after the PMD is cleared. Such mappings need a deposited page +* table so they can be split into PTEs for partial unmap/mprotect. +* +* Faultable PFNMAP VMAs can drop the PMD and refault it later, so they do +* not need a deposited page table. +*/ +static inline bool +vma_pfnmap_has_deposited_pgtable (const struct vm_area_struct *vma) +{ +return vma_test (vma,VMA_PFNMAP_BIT ) && !vma_has_fault_handler (vma); +} +/* unmap_vmas is in mm/memory.c */ void unmap_vmas (struct mmu_gather *tlb,struct unmap_desc *unmap);
mm/memory.c +75 -12
129 lines not on this page · raw message
Signature · 1 line
2.43.0
Lorenzo Stoakes
reviewerAllow me to be mildly pedantic (sorry :)
One thing I'd like to highlight here is that remap_pfn_range() is, in the long run, deprecated.
mmap_prepare callbacks will indicate a PFN remap mmap_action, which will do the heavy lifting (see [0]).
So perhaps worth referring to 'PFN remapping' or something?
(Since we already have mmap_prepare() in use, it's also kinda inaccurate to say remap_pfn_range() :)
[0]: https://www.kernel.org/doc/html/next/filesystems/mmap_prepare.html
Sorry, this is pretty nitpicky :)
Cheers, Lorenzo
Quoted 7 lines · Lorenzo Stoakes
Allow me to be mildly pedantic (sorry :)
One thing I'd like to highlight here is that
remap_pfn_range()is, in the long run, deprecated.
mmap_preparecallbacks will indicate a PFN remapmmap_action, which will do the heavy lifting (see [0]).
Thanks, I missed that mmap_action_remap() may also reach the same common PFN remap path.
Quoted 4 lines
So perhaps worth referring to 'PFN remapping' or something?
(Since we already have
mmap_prepare()in use, it's also kinda inaccurate to sayremap_pfn_range():)
Agreed, describing this as remap_pfn_range()support is indeed inaccurate. I'll reword it.
Quoted 4 lines
[0]: https://www.kernel.org/doc/html/next/filesystems/mmap_prepare.html
Sorry, this is pretty nitpicky :)
Not at all, this is really helpful.
Quoted 3 lines
Cheers, Lorenzo
Signature · 1 line
Yin Tirui