arm64: Add PUD THP support for DAX and pfnmap
Subsystems (8)
v2 cover arm64: Add PUD THP support for DAX and pfnmap
This series adds PUD THP support for arm64, enabling PUD THP for DAX and pfnmap.
Patch 1 moves the identical pudp_invalidate() implementations from x86 and riscv to a generic location so arm64 can reuse it.
Patch 2 is a minor cleanup that simplifies pmd_special/pmd_mkspecial.
Patch 3 adds the necessary spurious PUD fault fix and PUD page table helpers and selects HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD.
Changes since v1:
* fix issues reported by sashiko
Jinjiang Tu (4):
mm: define generic pudp_invalidate() implementation
arm64: simplify pmd_special and pmd_mkspecial
mm: add spurious fault fixing support for huge pud
arm64: Add PUD THP support for DAX and pfnmap
arch/arm64/Kconfig | 1 + arch/arm64/include/asm/pgtable.h | 104 +++++++++++++++---- arch/powerpc/include/asm/book3s/64/pgtable.h | 1 + arch/riscv/include/asm/pgtable.h | 3 - arch/riscv/mm/pgtable.c | 9 -- arch/x86/include/asm/pgtable.h | 3 - arch/x86/mm/pgtable.c | 12 --- include/linux/huge_mm.h | 5 +- include/linux/pgtable.h | 11 ++ mm/huge_memory.c | 17 +-- mm/internal.h | 2 +- mm/memory.c | 14 ++- mm/pgtable-generic.c | 14 +++ 13 files changed, 131 insertions(+), 65 deletions(-)
Signature · 1 line
2.54.0
PATCH v2 1/4 mm: define generic pudp_invalidate() implementation
x86 and riscv have identical pudp_invalidate() implementations. Move it to mm/pgtable-generic.c with the __HAVE_ARCH_PUDP_INVALIDATE override guard. PowerPC keeps its own implementation. In the later patch, arm64 will use the generic implementation too.
No functional change intended.
--- arch/powerpc/include/asm/book3s/64/pgtable.h | 1 + arch/riscv/include/asm/pgtable.h | 3 --- arch/riscv/mm/pgtable.c | 9 --------- arch/x86/include/asm/pgtable.h | 3 --- arch/x86/mm/pgtable.c | 12 ------------ include/linux/pgtable.h | 7 +++++++ mm/pgtable-generic.c | 14 ++++++++++++++ 7 files changed, 22 insertions(+), 27 deletions(-)
arch/powerpc/include/asm/book3s/64/pgtable.h +1 -0
@@ -1412,6 +1412,7 @@ static inline pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm,#define __HAVE_ARCH_PMDP_INVALIDATEextern pmd_t pmdp_invalidate (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp); +#define __HAVE_ARCH_PUDP_INVALIDATEextern pud_t pudp_invalidate (struct vm_area_struct *vma,unsigned long address,pud_t *pudp);
arch/riscv/include/asm/pgtable.h +0 -3
@@ -1162,9 +1162,6 @@ static inline pud_t pud_mkinvalid(pud_t pud)return __pud (pud_val (pud) & ~(_PAGE_PRESENT | _PAGE_PROT_NONE)); } -extern pud_t pudp_invalidate (struct vm_area_struct *vma,unsigned long address, -pud_t *pudp); -static inline pud_t pud_modify (pud_t pud,pgprot_t newprot) {return pte_pud (pte_modify (pud_pte (pud), newprot));
arch/riscv/mm/pgtable.c +0 -9
@@ -154,15 +154,6 @@ pmd_t pmdp_collapse_flush(struct vm_area_struct *vma, return pmd; } -pud_t pudp_invalidate (struct vm_area_struct *vma,unsigned long address, -pud_t *pudp) -{ -VM_WARN_ON_ONCE (!pud_present (*pudp)); -pud_t old =pudp_establish (vma, address, pudp,pud_mkinvalid (*pudp)); - -flush_pud_tlb_range (vma, address, address +HPAGE_PUD_SIZE ); -return old; -}#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ pte_t pte_mkwrite (pte_t pte,struct vm_area_struct *vma)
arch/x86/include/asm/pgtable.h +0 -3
@@ -1391,9 +1391,6 @@ static inline pud_t pudp_establish(struct vm_area_struct *vma,extern pmd_t pmdp_invalidate_ad (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp); -pud_t pudp_invalidate (struct vm_area_struct *vma,unsigned long address, -pud_t *pudp); -/* * Page table pages are page-aligned. The lower half of the top * level is used for userspace and the top half for the kernel.
arch/x86/mm/pgtable.c +0 -12
@@ -519,18 +519,6 @@ pmd_t pmdp_invalidate_ad(struct vm_area_struct *vma, unsigned long address, }#endif -#if defined(CONFIG_TRANSPARENT_HUGEPAGE ) && \ - defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD ) -pud_t pudp_invalidate (struct vm_area_struct *vma,unsigned long address, -pud_t *pudp) -{ -VM_WARN_ON_ONCE (!pud_present (*pudp)); -pud_t old =pudp_establish (vma, address, pudp,pud_mkinvalid (*pudp)); -flush_pud_tlb_range (vma, address, address +HPAGE_PUD_SIZE ); -return old; -} -#endif -/** * reserve_top_address - Reserve a hole in the top of the kernel address space * @reserve: Size of hole to reserve
include/linux/pgtable.h +7 -0
@@ -1318,6 +1318,13 @@ extern pmd_t pmdp_invalidate_ad(struct vm_area_struct *vma, unsigned long address, pmd_t *pmdp);#endif +#ifndef __HAVE_ARCH_PUDP_INVALIDATE +#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD +pud_t pudp_invalidate (struct vm_area_struct *vma,unsigned long address, +pud_t *pudp); +#endif +#endif +#ifndef __HAVE_ARCH_PTE_SAMEstatic inline int pte_same (pte_t pte_a,pte_t pte_b) {
mm/pgtable-generic.c +14 -0
@@ -229,6 +229,20 @@ pmd_t pmdp_invalidate_ad(struct vm_area_struct *vma, unsigned long address, }#endif +#ifndef __HAVE_ARCH_PUDP_INVALIDATE +#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD +pud_t pudp_invalidate (struct vm_area_struct *vma,unsigned long address, +pud_t *pudp) +{ +VM_WARN_ON_ONCE (!pud_present (*pudp)); +pud_t old =pudp_establish (vma, address, pudp,pud_mkinvalid (*pudp)); + +flush_pud_tlb_range (vma, address, address +HPAGE_PUD_SIZE ); +return old; +} +#endif +#endif +#ifndef pmdp_collapse_flushpmd_t pmdp_collapse_flush (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp)
Signature · 1 line
2.54.0
Re: [PATCH v2 1/4] mm: define generic pudp_invalidate() implementation
Quoted 9 lines · Jinjiang Tu
x86 and riscv have identical
pudp_invalidate()implementations. Move it tomm/pgtable-generic.cwith the__HAVE_ARCH_PUDP_INVALIDATEoverride guard. PowerPC keeps its own implementation. In the later patch, arm64 will use the generic implementation too.No functional change intended.
Signed-off-by Jinjiang Tu <[email protected]>---
Signature · 3 lines
Cheers, David
PATCH v2 2/4 arm64: simplify pmd_special and pmd_mkspecial
In reply to Jinjiang TuUse pte_special() and pte_mkspecial() helper to simplify pmd_special() and pmd_mkspecial(), and kill unused set_pmd_bit() and clear_pmd_bit() helper.
--- arch/arm64/include/asm/pgtable.h | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-)
arch/arm64/include/asm/pgtable.h +2 -17
@@ -240,18 +240,6 @@ static inline pte_t set_pte_bit(pte_t pte, pgprot_t prot)return pte; } -static inline pmd_t clear_pmd_bit (pmd_t pmd,pgprot_t prot) -{ -pmd_val (pmd) &= ~pgprot_val (prot); -return pmd; -} - -static inline pmd_t set_pmd_bit (pmd_t pmd,pgprot_t prot) -{ -pmd_val (pmd) |=pgprot_val (prot); -return pmd; -} -static inline pte_t pte_mkwrite_novma (pte_t pte) { pte =set_pte_bit (pte,__pgprot (PTE_WRITE ));
@@ -577,11 +565,8 @@ static inline pmd_t pmd_mkhuge(pmd_t pmd) }#ifdef CONFIG_ARCH_SUPPORTS_PMD_PFNMAP -#define pmd_special (pte) (!!((pmd_val(pte) & PTE_SPECIAL))) -static inline pmd_t pmd_mkspecial (pmd_t pmd) -{ -return set_pmd_bit (pmd,__pgprot (PTE_SPECIAL )); -} +#define pmd_special (pmd) pte_special(pmd_pte(pmd)) +#define pmd_mkspecial (pmd) pte_pmd(pte_mkspecial(pmd_pte(pmd)))#endif #define __pmd_to_phys (pmd) __pte_to_phys(pmd_pte(pmd))
Signature · 1 line
2.54.0
PATCH v2 3/4 mm: add spurious fault fixing support for huge pud
In reply to Jinjiang TuSpurious fault fixing is supported for PTE and huge PMD mappings, but not for huge PUD mappings.
The next patch in the series adds PUD THP support for DAX and pfnmap on arm64. arm64 write protection faults only flush the local TLB and leave stale read-only entries to be flushed by the faulting CPU, so these mappings need the huge PUD variant of the same fix.
--- arch/arm64/include/asm/pgtable.h | 4 ++++ include/linux/huge_mm.h | 5 +++-- include/linux/pgtable.h | 4 ++++ mm/huge_memory.c | 17 +++++++++-------- mm/internal.h | 2 +- mm/memory.c | 14 ++++++++++---- 6 files changed, 31 insertions(+), 15 deletions(-)
arch/arm64/include/asm/pgtable.h +4 -0
@@ -107,6 +107,10 @@ static inline void arch_leave_lazy_mmu_mode(void)__flush_tlb_range (vma, address, address +PMD_SIZE ,PMD_SIZE ,2 , \ TLBF_NOBROADCAST | TLBF_NONOTIFY | TLBF_NOWALKCACHE) +#define flush_tlb_fix_spurious_fault_pud (vma, address, pudp) \ + __flush_tlb_range(vma, address, address + PUD_SIZE, PUD_SIZE, 1, \ + TLBF_NOBROADCAST | TLBF_NONOTIFY | TLBF_NOWALKCACHE) +#ifdef CONFIG_ARM64_PA_BITS_52 static inline phys_addr_t __pte_to_phys (pte_t pte) {
include/linux/huge_mm.h +3 -2
@@ -16,10 +16,11 @@ int copy_huge_pud(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma pud_t *dst_pud, pud_t *src_pud, unsigned long addr);#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD -void huge_pud_set_accessed (struct vm_fault *vmf,pud_t orig_pud); +bool huge_pud_set_accessed (struct vm_fault *vmf,pud_t orig_pud);#else -static inline void huge_pud_set_accessed (struct vm_fault *vmf,pud_t orig_pud) +static inline bool huge_pud_set_accessed (struct vm_fault *vmf,pud_t orig_pud) { +return true; }#endif
include/linux/pgtable.h +4 -0
@@ -1493,6 +1493,10 @@ static inline void arch_swap_restore(swp_entry_t entry, struct folio *folio)#define flush_tlb_fix_spurious_fault_pmd (vma, address, pmdp) do { } while (0)#endif +#ifndef flush_tlb_fix_spurious_fault_pud +#define flush_tlb_fix_spurious_fault_pud (vma, address, pudp) do { } while (0) +#endif +/* * When walking page tables, get the address of the next boundary, * or the end address of the range if that comes earlier. Although no
mm/huge_memory.c +9 -8
@@ -2059,7 +2059,7 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, }#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD -void touch_pud (struct vm_area_struct *vma,unsigned long addr, +bool touch_pud (struct vm_area_struct *vma,unsigned long addr,pud_t *pud,bool write) {pud_t _pud;
@@ -2068,8 +2068,12 @@ void touch_pud(struct vm_area_struct *vma, unsigned long addr,if (write) _pud =pud_mkdirty (_pud);if (pudp_set_access_flags (vma, addr &HPAGE_PUD_MASK , - pud, _pud, write)) + pud, _pud, write)) {update_mmu_cache_pud (vma, addr, pud); +return true; + } + +return false; } int copy_huge_pud(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
@@ -2118,17 +2122,14 @@ int copy_huge_pud(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma return ret; } -void huge_pud_set_accessed (struct vm_fault *vmf,pud_t orig_pud) +bool huge_pud_set_accessed (struct vm_fault *vmf,pud_t orig_pud) {bool write = vmf->flags &FAULT_FLAG_WRITE ; - vmf->ptl =pud_lock (vmf->vma->vm_mm, vmf->pud);if (unlikely (!pud_same (*vmf->pud, orig_pud))) - goto unlock; +return false; -touch_pud (vmf->vma, vmf->address, vmf->pud, write); -unlock: -spin_unlock (vmf->ptl); +return touch_pud (vmf->vma, vmf->address, vmf->pud, write); }#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
mm/internal.h +1 -1
@@ -1311,7 +1311,7 @@ int __must_check try_grab_folio(struct folio *folio, int refs,/* * mm/huge_memory.c */ -void touch_pud (struct vm_area_struct *vma,unsigned long addr, +bool touch_pud (struct vm_area_struct *vma,unsigned long addr,pud_t *pud,bool write);bool touch_pmd (struct vm_area_struct *vma,unsigned long addr,pmd_t *pmd,bool write);
mm/memory.c +10 -4
@@ -6578,8 +6578,8 @@ static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud)* * Note: flush_tlb_fix_spurious_fault() is defined as flush_tlb_page() * by default and used as such on most architectures, while -* flush_tlb_fix_spurious_fault_pmd() is defined as NOP by default and -* used as such on most architectures. +* flush_tlb_fix_spurious_fault_pmd() and flush_tlb_fix_spurious_fault_pud() +* are defined as NOP by default and used as such on most architectures. */ static void fix_spurious_fault (struct vm_fault *vmf,enum pgtable_level ptlevel)
@@ -6597,9 +6597,12 @@ static void fix_spurious_fault(struct vm_fault *vmf,if (ptlevel ==PGTABLE_LEVEL_PTE )flush_tlb_fix_spurious_fault (vmf->vma, vmf->address, vmf->pte); -else +else if (ptlevel ==PGTABLE_LEVEL_PMD )flush_tlb_fix_spurious_fault_pmd (vmf->vma, vmf->address, vmf->pmd); +else +flush_tlb_fix_spurious_fault_pud (vmf->vma, vmf->address, + vmf->pud); } }/*
@@ -6757,7 +6760,10 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,if (!(ret &VM_FAULT_FALLBACK ))return ret; }else { -huge_pud_set_accessed (&vmf, orig_pud); + vmf.ptl =pud_lock (vmf.vma->vm_mm, vmf.pud); +if (!huge_pud_set_accessed (&vmf, orig_pud)) +fix_spurious_fault (&vmf,PGTABLE_LEVEL_PUD ); +spin_unlock (vmf.ptl);return 0 ; } }
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v2 3/4] mm: add spurious fault fixing support for huge pud
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] arm64: mm: unaligned address in flush_tlb_fix_spurious_fault_pud() flushes 2 PUDs --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] arm64: mm: unaligned address in flush_tlb_fix_spurious_fault_pud() flushes 2 PUDs
--
commit 89132c364112921acce9427dab367c258f86db04
Author: Jinjiang Tu <[email protected]>
mm: add spurious fault fixing support for huge pud
This commit adds spurious page fault TLB flushing support for huge PUD mappings and implements flush_tlb_fix_spurious_fault_pud() on arm64.
arch/arm64/include/asm/pgtable.h static inline void arch_leave_lazy_mmu_mode(void) · line 113
Quoted 12 lines
arch/arm64/include/asm/pgtable.h +4
-0@@ -107,6 +107,10 @@ static inline void arch_leave_lazy_mmu_mode(void)__flush_tlb_range (vma, address, address +PMD_SIZE ,PMD_SIZE ,2 , \ TLBF_NOBROADCAST | TLBF_NONOTIFY | TLBF_NOWALKCACHE) +#define flush_tlb_fix_spurious_fault_pud (vma, address, pudp) \ + __flush_tlb_range(vma, address, address + PUD_SIZE, PUD_SIZE, 1, \ + TLBF_NOBROADCAST | TLBF_NONOTIFY | TLBF_NOWALKCACHE) +
[Severity: Low]
Does flush_tlb_fix_spurious_fault_pud() flush two PUDs instead of one when the fault occurs at a non-zero page offset within the huge PUD?
fix_spurious_fault() passes vmf->address, which is only aligned to PAGE_MASK. __flush_tlb_range() then aligns start and end to stride (PUD_SIZE):
arch/arm64/include/asm/tlbflush.h:__flush_tlb_range() {
...
start = round_down(start, stride);
end = round_up(end, stride);
__do_flush_tlb_range(vma, start, end, stride, tlb_level, flags);
}Whenever address is not PUD_SIZE-aligned, round_down(address, PUD_SIZE) and round_up(address + PUD_SIZE, PUD_SIZE) span 2 * PUD_SIZE, invalidating the adjacent PUD range on the local CPU as well.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH v2 3/4] mm: add spurious fault fixing support for huge pud
Quoted 41 lines · 在 2026/11/18 10:03, [email protected] 写道:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] arm64: mm: unaligned address inflush_tlb_fix_spurious_fault_pud()flushes 2 PUDs
--commit
89132c364112921acce9427dab367c258f86db04
Author: Jinjiang Tu <[email protected]>mm: add spurious fault fixing support for huge pud
This commit adds spurious page fault TLB flushing support for huge PUD mappings and implements
flush_tlb_fix_spurious_fault_pud()on arm64.arch/arm64/include/asm/pgtable.h static inline void arch_leave_lazy_mmu_mode(void) · line 113
Quoted 12 lines
arch/arm64/include/asm/pgtable.h +4
-0@@ -107,6 +107,10 @@ static inline void arch_leave_lazy_mmu_mode(void)__flush_tlb_range (vma, address, address +PMD_SIZE ,PMD_SIZE ,2 , \ TLBF_NOBROADCAST | TLBF_NONOTIFY | TLBF_NOWALKCACHE) +#define flush_tlb_fix_spurious_fault_pud (vma, address, pudp) \ + __flush_tlb_range(vma, address, address + PUD_SIZE, PUD_SIZE, 1, \ + TLBF_NOBROADCAST | TLBF_NONOTIFY | TLBF_NOWALKCACHE) +[Severity: Low]
Doesflush_tlb_fix_spurious_fault_pud()flush two PUDs instead of one when the fault occurs at a non-zero page offset within the huge PUD?
fix_spurious_fault()passesvmf->address, which is only aligned toPAGE_MASK.__flush_tlb_range()then aligns start and end to stride (PUD_SIZE):arch/arm64/include/asm/tlbflush.h:__flush_tlb_range() { ... start = round_down(start, stride); end = round_up(end, stride); __do_flush_tlb_range(vma, start, end, stride, tlb_level, flags); }
Quoted 3 lines
Whenever address is not PUD_SIZE-aligned,
round_down(address, PUD_SIZE)andround_up(address + PUD_SIZE, PUD_SIZE)span 2 *PUD_SIZE, invalidating the adjacent PUD range on the local CPU as well.
Yes, confirmed. flush_tlb_fix_spurious_fault_pmd() has the same issue, which is pre-exist. I will fix both in v3.
PATCH v2 4/4 arm64: Add PUD THP support for DAX and pfnmap
In reply to Jinjiang TuAdd the necessary page table functions to deal with PUD THP, this enables PUD THP for DAX and pfnmap.
I tested with CONFIG_DEBUG_VM_PGTABLE and CONFIG_PAGE_TABLE_CHECK enabled. I wrote a test driver that invokes vmf_insert_pfn_pud() in the huge_fault callback. Page table setup works as expected with no errors, and userspace read/write access to the PUD-sized mapping is verified correct.
--- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/pgtable.h | 81 +++++++++++++++++++++++++++++--- 2 files changed, 76 insertions(+), 6 deletions(-)
arch/arm64/Kconfig +1 -0
@@ -173,6 +173,7 @@ config ARM64 select HAVE_ARCH_THREAD_STRUCT_WHITELIST select HAVE_ARCH_TRACEHOOK select HAVE_ARCH_TRANSPARENT_HUGEPAGE + select HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD if PAGE_SIZE_4KB select HAVE_ARCH_VMAP_STACK select HAVE_ARM_SMCCC select HAVE_ASM_MODVERSIONS
arch/arm64/include/asm/pgtable.h +75 -6
147 lines not on this page · raw message
Signature · 1 line
2.54.0