Skip to contentSkip to conversation

arm64: Add PUD THP support for DAX and pfnmap

PATCH RESEND Superseded Jinjiang Tu 3 participants 7 messages Oct 28 – Nov 17

Subsystems (7)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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 PUD page table helpers and selects HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD.

Jinjiang Tu (3):
mm: define generic pudp_invalidate() implementation
arm64: simplify pmd_special and pmd_mkspecial
arm64: Add PUD THP support for DAX and pfnmap

 arch/arm64/Kconfig                           |  1 +
 arch/arm64/include/asm/pgtable.h             | 71 +++++++++++++++-----
 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 ++++
 9 files changed, 77 insertions(+), 44 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH RESEND 1/3 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
index dff8790a047d..267d5e24f3b0 100644
@@ -1412,6 +1412,7 @@ static inline pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm,
 #define __HAVE_ARCH_PMDP_INVALIDATE
 extern pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
 			     pmd_t *pmdp);
+#define __HAVE_ARCH_PUDP_INVALIDATE
 extern pud_t pudp_invalidate(struct vm_area_struct *vma, unsigned long address,
 			     pud_t *pudp);
 
arch/riscv/include/asm/pgtable.h +0 -3
index d48f90140841..0126d6ca5643 100644
@@ -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
index 9c4427d0b187..156e385e7db4 100644
@@ -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
index 000fc6c1ca37..ad7a40abef7d 100644
@@ -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
index 4a105f283cfb..591931d144f2 100644
@@ -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
index 780fe849ff8b..0021eaf88e1d 100644
@@ -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_SAME
 static inline int pte_same(pte_t pte_a, pte_t pte_b)
 {
mm/pgtable-generic.c +14 -0
index 6e83ce3801b8..1b7081fc4a75 100644
@@ -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_flush
 pmd_t pmdp_collapse_flush(struct vm_area_struct *vma, unsigned long address,
 			  pmd_t *pmdp)
Signature · 1 line
2.43.0
#2 lore raw

PATCH RESEND 2/3 arm64: simplify pmd_special and pmd_mkspecial

In reply to Jinjiang Tu

Use 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
index 763c5a411d64..7de2487e4d35 100644
@@ -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.43.0
#3 lore raw

PATCH RESEND 3/3 arm64: Add PUD THP support for DAX and pfnmap

In reply to Jinjiang Tu

Add 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 | 52 ++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)
arch/arm64/Kconfig +1 -0
index cfef22aee709..4f988f23acfb 100644
@@ -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 +52 -0

102 lines not on this page · raw message

Signature · 1 line
2.43.0
#4 lore raw
kernel test robot noticed the following build errors:

Hi Jinjiang,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-everything]
[also build test ERROR on arm64/for-next/core powerpc/next powerpc/fixes tip/x86/core tip/x86/mm linus/master v7.3 next-20261027] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Jinjiang-Tu/mm-define-generic-pudp_invalidate-implementation/20261028-201402
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20261028121404.853944-4-tujinjiang%40huawei.com
patch subject: [PATCH RESEND 3/3] arm64: Add PUD THP support for DAX and pfnmap
config: arm64-allnoconfig (https://download.01.org/0day-ci/archive/20261029/[email protected]/config)
compiler: aarch64-linux-gcc (GCC) 16.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261029/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from arch/arm64/include/asm/io.h:13,

                    from include/linux/io.h:12,
                    from include/linux/irq.h:20,
                    from include/asm-generic/hardirq.h:17,
                    from arch/arm64/include/asm/hardirq.h:17,
                    from include/linux/hardirq.h:11,
                    from include/linux/interrupt.h:11,
                    from include/linux/trace_recursion.h:5,
                    from include/linux/ftrace.h:10,
                    from arch/arm64/kernel/asm-offsets.c:13:
>> include/linux/pgtable.h:2098:19: error: redefinition of 'pud_trans_huge'

2098 | static inline int pud_trans_huge(pud_t pud)
| ^~~~~~~~~~~~~~
In file included from include/linux/pgtable.h:6:
arch/arm64/include/asm/pgtable.h:842:19: note: previous definition of 'pud_trans_huge' with type 'int(pud_t)'
842 | static inline int pud_trans_huge(pud_t pud)
| ^~~~~~~~~~~~~~
make[3]: *** [scripts/Makefile.build:185: arch/arm64/kernel/asm-offsets.s] Error 1
make[3]: Target 'prepare' not remade because of errors.
make[2]: *** [Makefile:1416: prepare0] Error 2
make[2]: Target 'prepare' not remade because of errors.
make[1]: *** [Makefile:248: __sub-make] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:248: __sub-make] Error 2
make: Target 'prepare' not remade because of errors.

vim +/pud_trans_huge +2098 include/linux/pgtable.h

1501899a898dfb5 include/asm-generic/pgtable.h Dan Williams   2017-11-29  2095  
a00cc7d9dd93d66 include/asm-generic/pgtable.h Matthew Wilcox 2017-02-24  2096  #if !defined(CONFIG_TRANSPARENT_HUGEPAGE) || \
bcd0dea5f4fb3d0 include/linux/pgtable.h       Liu Shixin     2022-08-29  2097  	!defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
a00cc7d9dd93d66 include/asm-generic/pgtable.h Matthew Wilcox 2017-02-24 @2098  static inline int pud_trans_huge(pud_t pud)
a00cc7d9dd93d66 include/asm-generic/pgtable.h Matthew Wilcox 2017-02-24  2099  {
a00cc7d9dd93d66 include/asm-generic/pgtable.h Matthew Wilcox 2017-02-24  2100  	return 0;
a00cc7d9dd93d66 include/asm-generic/pgtable.h Matthew Wilcox 2017-02-24  2101  }
a00cc7d9dd93d66 include/asm-generic/pgtable.h Matthew Wilcox 2017-02-24  2102  #endif

a00cc7d9dd93d66 include/asm-generic/pgtable.h Matthew Wilcox 2017-02-24 2103

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#5 lore raw
In reply to Jinjiang Tu
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] arm64: mm: local-only TLB flush in pudp_set_access_flags() causes fault livelock - [High] arm64…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] arm64: mm: local-only TLB flush in pudp_set_access_flags() causes fault livelock
- [High] arm64: mm: missing pudp_set_wrprotect() loses HAFDBS dirty bits and triggers warning
- [High] arm64: mm: missing pudp_huge_get_and_clear() loses concurrent HAFDBS A/D updates
- [Low] arm64: mm: redefinition of pud_trans_huge() when CONFIG_TRANSPARENT_HUGEPAGE=n --

commit 7dc812ccee714c3818da8b998f785b9073deeacd
Author: Jinjiang Tu <[email protected]>

arm64: Add PUD THP support for DAX and pfnmap

This patch adds PUD transparent hugepage page table helpers on arm64 and enables HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD for 4KB page size configurations to support PUD THP for DAX and pfnmap mappings.

Quoted 4 lines
arch/arm64/include/asm/pgtable.h +0 -0
index 7de2487e4d353..feca481b20795 100644

[ ... ]

arch/arm64/include/asm/pgtable.h static inline bool pud_leaf(pud_t pud) · line 846

Quoted 10 lines
arch/arm64/include/asm/pgtable.h +6 -0
@@ -826,6 +838,13 @@ static inline bool pud_leaf(pud_t pud)
 #define pud_user(pud)		pte_user(pud_pte(pud))
 #define pud_user_exec(pud)	pte_user_exec(pud_pte(pud))
 
+#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
+static inline int pud_trans_huge(pud_t pud)
+{
+	return pud_present(pud) && !pud_table(__pud(pud_val(pud) | PTE_VALID));
+}
+#endif

[Severity: Low]
Will this cause a redefinition error for pud_trans_huge() when building with CONFIG_PAGE_SIZE_4KB=y and CONFIG_TRANSPARENT_HUGEPAGE=n (for example, allnoconfig or tinyconfig)?

In arch/arm64/Kconfig, config ARM64 selects HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD whenever PAGE_SIZE_4KB is enabled, even when CONFIG_TRANSPARENT_HUGEPAGE is disabled:

select HAVE_ARCH_TRANSPARENT_HUGEPAGE
select HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD if PAGE_SIZE_4KB

However, include/linux/pgtable.h includes asm/pgtable.h and then provides a fallback definition of pud_trans_huge() whenever CONFIG_TRANSPARENT_HUGEPAGE is not defined:

#if !defined(CONFIG_TRANSPARENT_HUGEPAGE) || \
!defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
static inline int pud_trans_huge(pud_t pud)

{
	return 0;
}

#endif

Should pud_trans_huge() in arch/arm64/include/asm/pgtable.h also be guarded by CONFIG_TRANSPARENT_HUGEPAGE (matching pmd_trans_huge() in the same file)?

[ ... ]

arch/arm64/include/asm/pgtable.h static inline int pmdp_set_access_flags(struct vm_area_struct *vma, · line 1242

Quoted 13 lines
arch/arm64/include/asm/pgtable.h +9 -0
@@ -1207,6 +1231,16 @@ static inline int pmdp_set_access_flags(struct vm_area_struct *vma,
 }
 #endif
 
+#ifdef CONFIG_ARCH_SUPPORTS_PUD_PFNMAP
+static inline int pudp_set_access_flags(struct vm_area_struct *vma,
+					unsigned long address, pud_t *pudp,
+					pud_t entry, int dirty)
+{
+	return __ptep_set_access_flags_anysz(vma, address, (pte_t *)pudp,
+					     pud_pte(entry), dirty, PUD_SIZE);
+}
+#endif

[Severity: High]
Can delegating pudp_set_access_flags() to __ptep_set_access_flags_anysz() lead to an infinite spurious write page-fault livelock on remote CPUs?

In arch/arm64/mm/fault.c, __ptep_set_access_flags_anysz() only invalidates the local CPU's TLB via TLBF_NOBROADCAST when upgrading a read-only entry to writable/dirty, relying on remote CPUs to invalidate their stale read-only TLB entries on a subsequent permission fault:

	if (pte_same(pte, entry))
		return 0;
	...
	/*
	 * Invalidate the local stale read-only entry.  Remote stale entries
	 * may still cause page faults and be invalidated via
	 * flush_tlb_fix_spurious_fault().
	 */
	if (dirty) {
		...
		__flush_tlb_range(vma, address, address + pgsize, pgsize, level,
				  TLBF_NOWALKCACHE | TLBF_NOBROADCAST);
	}

Suppose two CPUs fault in a shared PUD mapping as hardware read-only (PTE_RDONLY=1, such as on an initial read fault for fsdax, a PFNMAP/devdax mapping on a CPU without hardware DBM, or after copy_huge_pud() write-protects the PUD), and CPU A then writes to the mapping. CPU A updates *pudp via wp_huge_pud() -> insert_pud() -> pudp_set_access_flags() or huge_pud_set_accessed() -> touch_pud() -> pudp_set_access_flags(), flushing only CPU A's local TLB.

When CPU B subsequently writes to the same PUD range, it takes a permission fault from its stale read-only local TLB entry and enters:

__handle_mm_fault()
huge_pud_set_accessed()

    touch_pud()
      pudp_set_access_flags()
        __ptep_set_access_flags_anysz()

Because *pudp in memory already matches entry (with PTE_AF, PTE_DIRTY, and PTE_WRITE set and PTE_RDONLY cleared), pte_same(pte, entry) is true and __ptep_set_access_flags_anysz() returns 0 without flushing CPU B's TLB.

Unlike the PTE and PMD paths, which call flush_tlb_fix_spurious_fault() and flush_tlb_fix_spurious_fault_pmd() when ptep_set_access_flags() or pmdp_set_access_flags() returns 0, neither touch_pud(), huge_pud_set_accessed(), __handle_mm_fault(), nor insert_pud() performs a local TLB flush on a spurious PUD fault. Wouldn't CPU B return to userspace with the stale read-only PUD TLB entry still in place and fault in an infinite loop?

[ ... ]

Quoted 5 lines
arch/arm64/include/asm/pgtable.h +1 -0
@@ -1281,6 +1315,14 @@ static inline bool pmdp_test_and_clear_young(struct vm_area_struct *vma,
 }
 #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG */
 
+#ifdef CONFIG_ARCH_SUPPORTS_PUD_PFNMAP

arch/arm64/include/asm/pgtable.h static inline bool pmdp_test_and_clear_young(struct vm_area_struct *vma, · line 1328

Quoted 10 lines
arch/arm64/include/asm/pgtable.h +7 -0
+static inline bool pudp_test_and_clear_young(struct vm_area_struct *vma,
+		unsigned long address, pud_t *pudp)
+{
+	return __ptep_test_and_clear_young(vma, address, (pte_t *)pudp);
+}
+#endif
+
 static inline pte_t __ptep_get_and_clear_anysz(struct mm_struct *mm,
 					       unsigned long address,
 					       pte_t *ptep,

[Severity: High]
Should arm64 also define __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEAR and implement pudp_huge_get_and_clear() via __ptep_get_and_clear_anysz(..., PUD_SIZE)?

__ptep_get_and_clear_anysz() in arch/arm64/include/asm/pgtable.h already uses an atomic xchg_relaxed() and has a PUD_SIZE case for page_table_check_pud_clear(). Without an arch definition of pudp_huge_get_and_clear(), arm64 falls back to the generic implementation in include/linux/pgtable.h:

#ifndef __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEAR
static inline pud_t pudp_huge_get_and_clear(struct mm_struct *mm,
unsigned long address, pud_t *pudp)

{
	pud_t pud = *pudp;

	pud_clear(pudp);
	page_table_check_pud_clear(mm, address, pud);

	return pud;
}

#endif

When splitting a DAX PUD via split_huge_pud() -> __split_huge_pud_locked() -> pudp_huge_clear_flush() -> pudp_huge_get_and_clear(), __split_huge_pud_locked() propagates the dirty and young bits from old_pud to the folio:

	old_pud = pudp_huge_clear_flush(vma, haddr, pud);

	if (!vma_is_dax(vma))
		return;

	page = pud_page(old_pud);
	folio = page_folio(page);

	if (!folio_test_dirty(folio) && pud_dirty(old_pud))
		folio_mark_dirty(folio);
	if (!folio_test_referenced(folio) && pud_young(old_pud))
		folio_set_referenced(folio);

On systems with FEAT_HAFDBS, couldn't a concurrent hardware access or dirty state update between the non-atomic read of *pudp and pud_clear(pudp) be lost?

[ ... ]

arch/arm64/include/asm/pgtable.h static inline pmd_t pmdp_establish(struct vm_area_struct *vma, · line 1495

Quoted 12 lines
arch/arm64/include/asm/pgtable.h +8 -0
@@ -1443,6 +1485,15 @@ static inline pmd_t pmdp_establish(struct vm_area_struct *vma,
 }
 #endif
 
+#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
+static inline pud_t pudp_establish(struct vm_area_struct *vma,
+		unsigned long address, pud_t *pudp, pud_t pud)
+{
+	page_table_check_pud_set(vma->vm_mm, address, pudp, pud);
+	return __pud(xchg_relaxed(&pud_val(*pudp), pud_val(pud)));
+}
+#endif

[Severity: High]
Is __HAVE_ARCH_PUDP_SET_WRPROTECT with pudp_set_wrprotect() also needed here, matching pmdp_set_wrprotect() right above pmdp_establish()?

Without an architecture-specific pudp_set_wrprotect() wrapping __ptep_set_wrprotect(), arm64 uses the generic fallback in include/linux/pgtable.h:

static inline void pudp_set_wrprotect(struct mm_struct *mm,
unsigned long address, pud_t *pudp)

{
	pud_t old_pud = *pudp;

	set_pud_at(mm, address, pudp, pud_wrprotect(old_pud));
}

When fork() copies a COW PUD via copy_page_range() -> copy_p4d_range() -> copy_pud_range() -> copy_huge_pud() -> pudp_set_wrprotect():

1. The non-atomic read of *pudp followed by set_pud_at() races with hardware AF/DBM updates (FEAT_HAFDBS), unlike __ptep_set_wrprotect() which uses a cmpxchg_relaxed() loop to atomically transfer hardware dirty state to PTE_DIRTY while setting PTE_RDONLY and clearing PTE_WRITE.

2. Updating a live valid PUD in place via set_pud_at() -> __set_ptes_anysz() calls __check_safe_pte_update() in arch/arm64/mm/mmu.c, which triggers VM_WARN_ONCE() when the entry is old or writable-clean on a live mm:

	VM_WARN_ONCE(!pte_young(pte),
		     "%s: racy access flag clearing: %s -> %s",
		     __func__, pte_str_old, pte_str);
	VM_WARN_ONCE(pte_write(old_pte) && !pte_dirty(pte),
		     "%s: racy dirty state clearing: %s -> %s",
		     __func__, pte_str_old, pte_str);

3. set_pud_at() also runs __sync_cache_and_tags() across PUD_SIZE >> PAGE_SHIFT (262,144) pages unnecessarily when only write-protecting an existing entry.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#6 lore raw
Quoted 6 lines · 在 2026/10/29 20:14, [email protected] 写道:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] arm64: mm: local-only TLB flush in pudp_set_access_flags() causes fault livelock
- [High] arm64: mm: missing pudp_set_wrprotect() loses HAFDBS dirty bits and triggers warning
- [High] arm64: mm: missing pudp_huge_get_and_clear() loses concurrent HAFDBS A/D updates
- [Low] arm64: mm: redefinition of pud_trans_huge() when CONFIG_TRANSPARENT_HUGEPAGE=n --

These issues are confirmed and have been fixed in v2.

https://lore.kernel.org/all/[email protected]/

Quoted 232 lines

commit 7dc812ccee714c3818da8b998f785b9073deeacd
Author: Jinjiang Tu <[email protected]>

arm64: Add PUD THP support for DAX and pfnmap

This patch adds PUD transparent hugepage page table helpers on arm64 and enables HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD for 4KB page size configurations to support PUD THP for DAX and pfnmap mappings.

Quoted 4 lines
arch/arm64/include/asm/pgtable.h +0 -0
index 7de2487e4d353..feca481b20795 100644

[ ... ]

arch/arm64/include/asm/pgtable.h static inline bool pud_leaf(pud_t pud) · line 846

Quoted 10 lines
arch/arm64/include/asm/pgtable.h +6 -0
@@ -826,6 +838,13 @@ static inline bool pud_leaf(pud_t pud)
  #define pud_user(pud)		pte_user(pud_pte(pud))
  #define pud_user_exec(pud)	pte_user_exec(pud_pte(pud))
 
+#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
+static inline int pud_trans_huge(pud_t pud)
+{
+	return pud_present(pud) && !pud_table(__pud(pud_val(pud) | PTE_VALID));
+}
+#endif

[Severity: Low]
Will this cause a redefinition error for pud_trans_huge() when building with CONFIG_PAGE_SIZE_4KB=y and CONFIG_TRANSPARENT_HUGEPAGE=n (for example, allnoconfig or tinyconfig)?

In arch/arm64/Kconfig, config ARM64 selects HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD whenever PAGE_SIZE_4KB is enabled, even when CONFIG_TRANSPARENT_HUGEPAGE is disabled:

select HAVE_ARCH_TRANSPARENT_HUGEPAGE
select HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD if PAGE_SIZE_4KB

However, include/linux/pgtable.h includes asm/pgtable.h and then provides a fallback definition of pud_trans_huge() whenever CONFIG_TRANSPARENT_HUGEPAGE is not defined:

#if !defined(CONFIG_TRANSPARENT_HUGEPAGE) || \
!defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
static inline int pud_trans_huge(pud_t pud)

{
	return 0;
}

#endif

Should pud_trans_huge() in arch/arm64/include/asm/pgtable.h also be guarded by CONFIG_TRANSPARENT_HUGEPAGE (matching pmd_trans_huge() in the same file)?

[ ... ]

arch/arm64/include/asm/pgtable.h static inline int pmdp_set_access_flags(struct vm_area_struct *vma, · line 1242

Quoted 13 lines
arch/arm64/include/asm/pgtable.h +9 -0
@@ -1207,6 +1231,16 @@ static inline int pmdp_set_access_flags(struct vm_area_struct *vma,
  }
  #endif
 
+#ifdef CONFIG_ARCH_SUPPORTS_PUD_PFNMAP
+static inline int pudp_set_access_flags(struct vm_area_struct *vma,
+					unsigned long address, pud_t *pudp,
+					pud_t entry, int dirty)
+{
+	return __ptep_set_access_flags_anysz(vma, address, (pte_t *)pudp,
+					     pud_pte(entry), dirty, PUD_SIZE);
+}
+#endif

[Severity: High]
Can delegating pudp_set_access_flags() to __ptep_set_access_flags_anysz() lead to an infinite spurious write page-fault livelock on remote CPUs?

In arch/arm64/mm/fault.c, __ptep_set_access_flags_anysz() only invalidates the local CPU's TLB via TLBF_NOBROADCAST when upgrading a read-only entry to writable/dirty, relying on remote CPUs to invalidate their stale read-only TLB entries on a subsequent permission fault:

	if (pte_same(pte, entry))
		return 0;
	...
	/*
	 * Invalidate the local stale read-only entry.  Remote stale entries
	 * may still cause page faults and be invalidated via
	 * flush_tlb_fix_spurious_fault().
	 */
	if (dirty) {
		...
		__flush_tlb_range(vma, address, address + pgsize, pgsize, level,
				  TLBF_NOWALKCACHE | TLBF_NOBROADCAST);
	}

Suppose two CPUs fault in a shared PUD mapping as hardware read-only (PTE_RDONLY=1, such as on an initial read fault for fsdax, a PFNMAP/devdax mapping on a CPU without hardware DBM, or after copy_huge_pud() write-protects the PUD), and CPU A then writes to the mapping. CPU A updates *pudp via wp_huge_pud() -> insert_pud() -> pudp_set_access_flags() or huge_pud_set_accessed() -> touch_pud() -> pudp_set_access_flags(), flushing only CPU A's local TLB.

When CPU B subsequently writes to the same PUD range, it takes a permission fault from its stale read-only local TLB entry and enters:

__handle_mm_fault()
huge_pud_set_accessed()

     touch_pud()
       pudp_set_access_flags()
         __ptep_set_access_flags_anysz()

Because *pudp in memory already matches entry (with PTE_AF, PTE_DIRTY, and PTE_WRITE set and PTE_RDONLY cleared), pte_same(pte, entry) is true and __ptep_set_access_flags_anysz() returns 0 without flushing CPU B's TLB.

Unlike the PTE and PMD paths, which call flush_tlb_fix_spurious_fault() and flush_tlb_fix_spurious_fault_pmd() when ptep_set_access_flags() or pmdp_set_access_flags() returns 0, neither touch_pud(), huge_pud_set_accessed(), __handle_mm_fault(), nor insert_pud() performs a local TLB flush on a spurious PUD fault. Wouldn't CPU B return to userspace with the stale read-only PUD TLB entry still in place and fault in an infinite loop?

[ ... ]

Quoted 5 lines
arch/arm64/include/asm/pgtable.h +1 -0
@@ -1281,6 +1315,14 @@ static inline bool pmdp_test_and_clear_young(struct vm_area_struct *vma,
  }
  #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG */
 
+#ifdef CONFIG_ARCH_SUPPORTS_PUD_PFNMAP

arch/arm64/include/asm/pgtable.h static inline bool pmdp_test_and_clear_young(struct vm_area_struct *vma, · line 1328

Quoted 10 lines
arch/arm64/include/asm/pgtable.h +7 -0
+static inline bool pudp_test_and_clear_young(struct vm_area_struct *vma,
+		unsigned long address, pud_t *pudp)
+{
+	return __ptep_test_and_clear_young(vma, address, (pte_t *)pudp);
+}
+#endif
+
  static inline pte_t __ptep_get_and_clear_anysz(struct mm_struct *mm,
  					       unsigned long address,
  					       pte_t *ptep,

[Severity: High]
Should arm64 also define __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEAR and implement pudp_huge_get_and_clear() via __ptep_get_and_clear_anysz(..., PUD_SIZE)?

__ptep_get_and_clear_anysz() in arch/arm64/include/asm/pgtable.h already uses an atomic xchg_relaxed() and has a PUD_SIZE case for page_table_check_pud_clear(). Without an arch definition of pudp_huge_get_and_clear(), arm64 falls back to the generic implementation in include/linux/pgtable.h:

#ifndef __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEAR
static inline pud_t pudp_huge_get_and_clear(struct mm_struct *mm,
unsigned long address, pud_t *pudp)

{
	pud_t pud = *pudp;

	pud_clear(pudp);
	page_table_check_pud_clear(mm, address, pud);

	return pud;
}

#endif

When splitting a DAX PUD via split_huge_pud() -> __split_huge_pud_locked() -> pudp_huge_clear_flush() -> pudp_huge_get_and_clear(), __split_huge_pud_locked() propagates the dirty and young bits from old_pud to the folio:

	old_pud = pudp_huge_clear_flush(vma, haddr, pud);

	if (!vma_is_dax(vma))
		return;

	page = pud_page(old_pud);
	folio = page_folio(page);

	if (!folio_test_dirty(folio) && pud_dirty(old_pud))
		folio_mark_dirty(folio);
	if (!folio_test_referenced(folio) && pud_young(old_pud))
		folio_set_referenced(folio);

On systems with FEAT_HAFDBS, couldn't a concurrent hardware access or dirty state update between the non-atomic read of *pudp and pud_clear(pudp) be lost?

[ ... ]

arch/arm64/include/asm/pgtable.h static inline pmd_t pmdp_establish(struct vm_area_struct *vma, · line 1495

Quoted 12 lines
arch/arm64/include/asm/pgtable.h +8 -0
@@ -1443,6 +1485,15 @@ static inline pmd_t pmdp_establish(struct vm_area_struct *vma,
  }
  #endif
 
+#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
+static inline pud_t pudp_establish(struct vm_area_struct *vma,
+		unsigned long address, pud_t *pudp, pud_t pud)
+{
+	page_table_check_pud_set(vma->vm_mm, address, pudp, pud);
+	return __pud(xchg_relaxed(&pud_val(*pudp), pud_val(pud)));
+}
+#endif

[Severity: High]
Is __HAVE_ARCH_PUDP_SET_WRPROTECT with pudp_set_wrprotect() also needed here, matching pmdp_set_wrprotect() right above pmdp_establish()?

Without an architecture-specific pudp_set_wrprotect() wrapping __ptep_set_wrprotect(), arm64 uses the generic fallback in include/linux/pgtable.h:

static inline void pudp_set_wrprotect(struct mm_struct *mm,
unsigned long address, pud_t *pudp)

{
	pud_t old_pud = *pudp;

	set_pud_at(mm, address, pudp, pud_wrprotect(old_pud));
}

When fork() copies a COW PUD via copy_page_range() -> copy_p4d_range() -> copy_pud_range() -> copy_huge_pud() -> pudp_set_wrprotect():

1. The non-atomic read of *pudp followed by set_pud_at() races with hardware AF/DBM updates (FEAT_HAFDBS), unlike __ptep_set_wrprotect() which uses a cmpxchg_relaxed() loop to atomically transfer hardware dirty state to PTE_DIRTY while setting PTE_RDONLY and clearing PTE_WRITE.

2. Updating a live valid PUD in place via set_pud_at() -> __set_ptes_anysz()

    calls __check_safe_pte_update() in arch/arm64/mm/mmu.c, which triggers
    VM_WARN_ONCE() when the entry is old or writable-clean on a live mm:

	VM_WARN_ONCE(!pte_young(pte),
		     "%s: racy access flag clearing: %s -> %s",
		     __func__, pte_str_old, pte_str);
	VM_WARN_ONCE(pte_write(old_pte) && !pte_dirty(pte),
		     "%s: racy dirty state clearing: %s -> %s",
		     __func__, pte_str_old, pte_str);

3. set_pud_at() also runs __sync_cache_and_tags() across PUD_SIZE >> PAGE_SHIFT (262,144) pages unnecessarily when only write-protecting an existing entry.