Another attempt at HVO support on arm64
Subsystems (13)
v3 cover Another attempt at HVO support on arm64
Hi everyone,
This is v3 of the series to enable HVO on arm64.
This version of the series is based on mm-new (15f114dac9a7) plus Muchun's "mm: Unify memmap initialization for compound pages" v3[2]. The first patch from v2 was sent standalone[3] and is currently in mm-new, so it is not included in this series.
I realize this series might be difficult to reapply later, so I've also uploaded it here: http://github.com/48ca/linux hvo-v3
As with last time, the last 5 patches are optional, and I've tagged them as DO-NOT-MERGE, as it's unclear if using the AF trick to do BBM-less block -> table transitions is architecturally sound without BBML3. The patches should be mergeable if it is in fact sound.
The biggest changes since v2:
- Replaced "hugetlb: Fully initialize tail struct pages of non-pre-HVOed bootmem folios" with "hugetlb: Optimize bootmem folios that were not pre-HVOed". Muchun's series now initializes all tail struct pages of such folios, but it also drops the boot-time bulk optimization pass, assuming that all boot-time HVO folios are pre-HVOed. On arm64, HVO support is not known yet when bootmem folios are allocated, so they are not pre-HVOed and must be optimized when they are gathered. Muchun, I'd appreciate your thoughts on this one.
- Reworked "Leave pages partially HVOed upon restore failure" and "Use try_update_vmemmap_pte to update in-use PTEs" for the folio-oriented code. Restoring a folio now only remaps PTEs that still point at the shared tail page and frees any replacement pages that were not consumed, and vmemmap_pages list updates only happen once the PTE update has succeeded.
- bbm_through_af now checks the local CPU's ID_AA64MMFR1_EL1 when verifying a late CPU, instead of the sanitised value.
- New patch: "arm64/mm: Align vmemmap to maximal folio size". With CONFIG_VMEMMAP_OPTIMIZATION, compound_head() relies on head struct pages being naturally aligned, which arm64 did not guarantee (e.g. 16G CONT_PMD folios with 64K pages). This mirrors the changes for riscv[4] and LoongArch[5]. (Thanks Muchun and Kefeng!)
- In "Allow architectures to dynamically disallow HVO": the arch hook is now the generic arch_support_vmemmap_remap(), and arm64 now disables the section-based vmemmap optimization with section_vmemmap_optimization_disable() in vmemmap_populate() instead of hugetlb_vmemmap_optimize_bootmem_page() checking for arch support. (Thanks Muchun!)
And some smaller changes:
- "arm64: Add bbm_through_af capability" now selects ARCH_HUGETLB_VMEMMAP_OPTIMIZATION_SYSCTL_RO itself, fixing a build failure (BUILD_BUG_ON) when bisecting.
- Renamed ARCH_WANT_HUGETLB_VMEMMAP_RO_AFTER_INIT to ARCH_HUGETLB_VMEMMAP_OPTIMIZATION_SYSCTL_RO. It now only makes the hugetlb_optimize_vmemmap sysctl read-only; vmemmap_optimize_enabled is no longer __ro_after_init.
- Removed an unused __HAVE_ARCH_TRY_UPDATE_VMEMMAP_PTE define.
- hugetlb_vmemmap_optimizable_size() now returns 0 when the architecture does not support HVO, so such hstates are no longer reported or treated as optimizable.
- Dropped "hugetlb_vmemmap: Disable HVO sysctl if arch doesn't support HVO". Now that hugetlb_vmemmap_optimizable_size() checks for arch support, the sysctl is not registered on such systems anyway.
- try_update_vmemmap_pte() is now defined outside of the CONFIG_ARM64_CONTPTE block, fixing the build with CONFIG_ARM64_CONTPTE=n.
- Documented fail_hugetlb_vmemmap_pte= and fail_hugetlb_vmemmap_pmd= in kernel-parameters.txt.
- Selftest: stop all worker processes (including in-flight dd/cat) at the end of each phase, so that they cannot race with the accounting checks, without blocking if a worker does not exit; drain surplus hugepages on cleanup; and don't treat "unmovable page" dumps from failed memory offlining as errors when running with -m.
v2: https://lore.kernel.org/linux-mm/[email protected]/
v1: https://lore.kernel.org/linux-mm/[email protected]/
Claude Opus 5.5 was used while drafting this version of the series. Like the last version, it was used to draft the selftests (hugely helpful), and in this version of the series, it helped with finding some of the smaller fixes and drafting some of the new patches. A local Sashiko run pointed out the bbm_through_af error that is now fixed in this version.
Thanks!
And now for the rest of the background of these patches, updated from v2's cover letter:
-- Background and Structure --
This patch series uses a trick with the Access Flag on CPUs that support hardware update of the AF to update vmemmap page table entries without introducing a time window where CPUs accessing the vmemmap might fault.
By avoiding faults, the HugeTLB vmemmap optimization (HVO) can be implemented correctly on arm64 in a much more straightforward way than previously attempted, most recently here[1] (please see [1] for a breakdown of the other approaches attempted before).
For large-memory systems that allocate most of their available memory to HugeTLB, HVO saves a huge amount of memory (1.5% of system memory).
This series has a few parts:
1. Use LDCLR to clear the AF for PTEs (patch 1).
2. Some preliminary HVO changes to support HVO on Arm (patches 2-8).
3. arm64 changes to enable HVO (patches 9-12).
4. Fault injection and a stress test (patches 13-14).
5. Drop the BBML3 requirement for HVO (patches 15-19).
Part 5 is optional, and whether or not the claims it makes about the Arm memory model are accurate is not 100% clear.
-- The AF trick --
The trick is that translations with the AF unset cannot be cached in the TLB (see Rule R_DWZCQ in the Arm ARM), so they can be atomically updated without needing a full break-before-make sequence.
So the PTE update sequence becomes:
1. Atomically clear the AF on the existing PTE.
2. Invalidate the TLB.
3. cmpxchg the AF=0 PTE with the new PTE. If this fails, goto 1.
If there is a CPU on the system that does not support hardware access flag updates, clearing the AF is problematic, as those CPUs might fault on the vmemmap usage. Therefore, HVO compatibility checks all CPUs for HW AF updates.
-- Application to HVO --
HVO relies on the following page table transitions:
- When enabling HVO for a page, PMD block entries in the vmemmap are shattered into PMD table entries. The first vmemmap page of the folio is copied to a newly allocated page and remapped to it, and the remaining PTEs are remapped to a shared page of tail struct pages (that is, there is an OA change for every PTE).
- When disabling HVO for a page, the PTEs that map the shared tail page are remapped to newly reallocated pages of struct pages. The PMD block -> table transition is not undone.
In part 3 of this series, I use the Access Flag trick to do the PTE updates. We rely on BBML3 for the PMD block -> table transition.
In part 5, I re-use the Access Flag trick to do the PMD block -> table transition without needing BBML3. For systems that support BBML3, the logic is unchanged.
Because the in-place updates can fail (if the cmpxchg keeps losing the race with hardware AF updates), HVO now handles failures at any point:
a failed optimization is rolled back, and if the rollback or a later restore fails, the folio is left partially optimized (and remains marked as such) rather than being freed with a broken vmemmap.
-- Late-onlining of CPUs that do not support HW AF --
HVO support is modeled as a system feature (called "BBM through AF"). If not all early CPUs support the feature, attempts to HVO pages will fail, and the HVO sysctl will be hidden.
To avoid penalizing systems that have asymmetric support for BBM-through-AF (i.e., HW AF, as BBML3 cannot be mismatched), BBM-through-AF will check that HVO is in fact enabled.
Because BBM-through-AF needs to know if HVO will be enabled when system features are finalized, HVO cannot be dynamically enabled, hence the patches that do this. (There are other ways to solve this, please see v1).
-- Litmus test --
The following Herd litmus test demonstrates the PTE update routine:
AArch64 TTDFaultlessUpdate
Variant=vmsa
TTHM=HA
{
uint64_t x=1;
uint64_t y=2;
[PTE(x)]=(oa:PA(x), af:1);
0:X0=PTE(x); 1:X0=PTE(x);
0:X1=x; 1:X1=x;
pteval_t 0:X2=(oa:PA(x), af:0);
pteval_t 0:X3=(oa:PA(y), af:1);
}
P0 | P1 ;
LDR X4,[X0] | L0: ;
MOV X5,X4 | LDR X2,[X1] ;
CAS X4,X2,[X0] | ;
DSB ISHST | ;
LSR X9,X1,#12 | ;
TLBI VAALE1IS,X9 | ;
DSB ISH | ;
ISB | ;
CAS X2,X3,[X0] | ;
exists
0:X5=0:X4 /\ (* First CAS must succeed *)
(fault(P1:L0) \/ ~(1:X2=2 \/ 1:X2=1))(* This test should not contain "Warning-BBM-expected". *)
-- Testing --
I've tested this series with the included selftest, which stresses the optimization, unoptimizing, and failure cases.
[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/linux-mm/[email protected]/
[4] commit 476849b0fba4 ("riscv/mm: align vmemmap to maximal folio size")
[5] commit 2969b42c8f99 ("LoongArch/mm: align vmemmap to maximal folio size")
James Houghton (19):
arm64/pgtable: Clear AF with LDCLR on supported systems
hugetlb_vmemmap: Always flush TLB if needed upon PTE remapping
hugetlb_vmemmap: Leave pages partially HVOed upon restore failure
hugetlb_vmemmap: Use try_update_vmemmap_pte to update in-use PTEs
hugetlb_vmemmap: Allow architectures to dynamically disallow HVO
hugetlb: Optimize bootmem folios that were not pre-HVOed
hugetlb_vmemmap: Allow architectures to make HVO enablement boot-time
only
hugetlb_vmemmap: Expose whether HVO is enabled to architecture code
arm64: Add bbm_through_af capability
arm64: Implement try_update_vmemmap_pte using the AF trick arm64/mm: Align vmemmap to maximal folio size
arm64: Support hugetlb vmemmap optimization
hugetlb_vmemmap: Add fault injection for in-place vmemmap PTE updates selftests/mm: Add HugeTLB vmemmap optimization stress test
hugetlb_vmemmap: Use try_populate_vmemmap_pmd for replacing in-use
PMDs
arm64: Implement try_populate_vmemmap_pmd using AF trick
arm64: Drop BBML3 requirement for HVO
hugetlb_vmemmap: Add fault injection for in-place vmemmap PMD splits selftests/mm: Add HVO pmd-split fault injection tests
.../admin-guide/kernel-parameters.txt | 2 + Documentation/admin-guide/sysctl/vm.rst | 3 + .../fault-injection/fault-injection.rst | 8 + arch/arm64/Kconfig | 2 + arch/arm64/include/asm/cpufeature.h | 5 + arch/arm64/include/asm/pgalloc.h | 53 +++ arch/arm64/include/asm/pgtable.h | 73 +++- arch/arm64/kernel/cpufeature.c | 32 ++ arch/arm64/mm/init.c | 18 +- arch/arm64/mm/mmu.c | 9 + arch/arm64/tools/cpucaps | 1 + arch/loongarch/include/asm/pgalloc.h | 2 + arch/loongarch/include/asm/pgtable.h | 2 + arch/riscv/include/asm/pgalloc.h | 2 + arch/riscv/include/asm/pgtable.h | 2 + arch/x86/include/asm/pgalloc.h | 2 + arch/x86/include/asm/pgtable.h | 2 + fs/Kconfig | 3 +- include/linux/hugetlb.h | 9 + include/linux/pgalloc.h | 21 + include/linux/pgtable.h | 37 ++ lib/Kconfig.debug | 9 + mm/Kconfig | 7 + mm/hugetlb.c | 11 + mm/hugetlb_vmemmap.c | 164 +++++++- mm/hugetlb_vmemmap.h | 2 + tools/testing/selftests/mm/Makefile | 2 + .../selftests/mm/hugetlb_vmemmap_stress.sh | 384 ++++++++++++++++++ .../selftests/mm/ksft_hugetlb_vmemmap.sh | 4 + tools/testing/selftests/mm/run_vmtests.sh | 4 + 30 files changed, 839 insertions(+), 36 deletions(-) create mode 100755 tools/testing/selftests/mm/hugetlb_vmemmap_stress.sh create mode 100755 tools/testing/selftests/mm/ksft_hugetlb_vmemmap.sh
base-commit: 15f114dac9a7bde1796f0e54e22726fd31cfa8a5
prerequisite-patch-id: 79139d48d73311a6b93f3ef4fcf35e81fa62b439
prerequisite-patch-id: 1decdc13c91eb4c111978d0a81264c80ee150f5c
prerequisite-patch-id: 3e89010bdfb5265dd7629e0ac0cdd4188e1e0fcb
prerequisite-patch-id: a46914935c0d5a34a2fe8fc5894372a6f4d3a9b9
prerequisite-patch-id: f5cc7ae1c18a204b0fdcd3d5a8ca2dc4ab02ab9c
prerequisite-patch-id: fa40c383a05f7713030e8e45f2c1d32b1891d59f
prerequisite-patch-id: e4996c7f982db0996d3a6a223d866ba303ae9b38
prerequisite-patch-id: 6e33595454fa72593f0f87a84190b2960e95d28b
prerequisite-patch-id: b6d144f2e3d6166cd6361f8095f7b1fc723c989a
prerequisite-patch-id: 36ea05703be8c54a4adf89a86457a4c73f16bae5
prerequisite-patch-id: 94bb8e10c55ce80b9eef1314b387a9dd5840faea
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
PATCH v3 1/19 arm64/pgtable: Clear AF with LDCLR on supported systems
Replace the cmpxchg loop with atomic64_fetch_andnot_relaxed, which, on systems with LSE atomics, should end up as LDCLR.
This is somewhat overdue. In recent versions of the architecture, if a system supports FEAT_LSE, forward progress guarantees for exclusive monitors and cmpxchg loops are relaxed.
Mechanically, I would love to write test_and_clear_bit_relaxed(), but no such function exists (test_and_clear_bit() has full ordering, which is also undesirable). Implementing test_and_clear_bit_relaxed() properly is a pretty large change; use atomic64 instead, as pte_t contains a u64.
--- arch/arm64/include/asm/pgtable.h | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-)
arch/arm64/include/asm/pgtable.h +4 -10
@@ -1252,17 +1252,11 @@ static inline void __pte_clear(struct mm_struct *mm,static inline bool __ptep_test_and_clear_young (struct vm_area_struct *vma,unsigned long address,pte_t *ptep) { -pte_t old_pte, pte; +atomic64_t *pteval = (atomic64_t *)&pte_val (*ptep); +s64 af_mask =PTE_AF ; - pte =__ptep_get (ptep); -do { - old_pte = pte; - pte =pte_mkold (pte); -pte_val (pte) =cmpxchg_relaxed (&pte_val (*ptep), -pte_val (old_pte),pte_val (pte)); - }while (pte_val (pte) !=pte_val (old_pte)); - -return pte_young (pte); +/* Atomically clear PTE_AF, checking that it was set before. */ +return af_mask &atomic64_fetch_andnot_relaxed (af_mask, pteval); } static inline bool __ptep_clear_flush_young(struct vm_area_struct *vma,
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
PATCH v3 2/19 hugetlb_vmemmap: Always flush TLB if needed upon PTE remapping
In reply to James Houghtonvmemmap_remap_range() can potentially leave the vmemmap in a partially optimized state. In this case, a TLB flush should be done if the walk caller asked for it, even if the optimization was not fully applied.
A follow-up patch will more completely deal with partially-optimized folios.
--- mm/hugetlb_vmemmap.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
mm/hugetlb_vmemmap.c +1 -3
@@ -163,13 +163,11 @@ static int vmemmap_remap_range(unsigned long start, unsigned long end, ret =walk_kernel_page_table_range_lockless (start, end, &vmemmap_remap_ops,NULL , walk); -if (ret) -return ret;if (walk->remap_pte && !(walk->flags &VMEMMAP_DEFER_TLB_FLUSH ))flush_tlb_kernel_range (start, end); -return 0 ; +return ret; }/*
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
[email protected]
AI reviewRe: [PATCH v3 02/19] hugetlb_vmemmap: Always flush TLB if needed upon PTE remapping
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] mm/hugetlb_vmemmap: unnecessary TLB flush in vmemmap_remap_range() on error - [Medium]…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] mm/hugetlb_vmemmap: unnecessary TLB flush in vmemmap_remap_range() on error
- [Medium] mm/hugetlb_vmemmap: partial HVO state retained in vmemmap_remap_range()
--
commit ec5d75eb023f87eb7c8518a7fd62d721c52bb9db
Author: James Houghton <[email protected]>
hugetlb_vmemmap: Always flush TLB if needed upon PTE remapping
This commit removes the early return on error in vmemmap_remap_range() so that flush_tlb_kernel_range() still runs when walk_kernel_page_table_range_lockless() returns a non-zero error code.
Quoted 7 lines
mm/hugetlb_vmemmap.c +0
-0@@ -163,13 +163,11 @@ static int vmemmap_remap_range(unsigned long start, unsigned long end, ret = walk_kernel_page_table_range_lockless(start, end, &vmemmap_remap_ops,
mm/hugetlb_vmemmap.c static int vmemmap_remap_range(unsigned long start, unsigned long end, · line 171
Quoted 10 lines
mm/hugetlb_vmemmap.c +1
-3NULL, walk); -if (ret) -return ret;if (walk->remap_pte && !(walk->flags &VMEMMAP_DEFER_TLB_FLUSH ))flush_tlb_kernel_range (start, end); -return 0 ; +return ret; }
[Severity: Medium]
Does this issue an unnecessary broadcast TLB flush across [start, end) when walk_kernel_page_table_range_lockless() fails with walk->nr_walked == 0?
With memory_hotplug.memmap_on_memory enabled, every runtime single-folio allocation via alloc_fresh_hugetlb_folio() -> hugetlb_vmemmap_optimize_folio() hits PageVmemmapSelfHosted() in vmemmap_pmd_entry() and returns -ENOTSUPP on the first PMD with walk.nr_walked == 0, triggering a full flush_tlb_kernel_range(start, end) despite modifying no PTEs. When a walk fails partway through with walk->nr_walked > 0, this also flushes the full [start, end) range rather than [start, start + walk->nr_walked * PAGE_SIZE), and vmemmap_optimize_folio() immediately flushes that prefix a second time via vmemmap_restore_range().
Should the flush be gated on walk->nr_walked > 0 and bounded to start + walk->nr_walked * PAGE_SIZE?
[Severity: Medium]
As reported by Muchun Song on v2 [1], does this change and the follow-up patches in the series still retain partially optimized vmemmap states instead of preserving all-or-nothing HVO semantics?
Later patches in this series (commit "hugetlb_vmemmap: Leave pages partially HVOed upon restore failure" and commit "arm64: Implement try_update_vmemmap_pte using the AF trick") cap cmpxchg retries in try_update_vmemmap_pte() and leave folios in a partially optimized state (VMEMMAP_REMAP_INCOMPLETE) on failure, which the subsystem maintainer asked to avoid by not bounding the cmpxchg retry loop.
[1] https://lore.kernel.org/r/[email protected]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v3 3/19 hugetlb_vmemmap: Leave pages partially HVOed upon restore failure
In reply to James HoughtonRight now it is assumed that the restore routine in vmemmap_remap_free() will not fail; that is, if we fail to fully optimize a folio, it is assumed that restoring the folio to its pre-HVO state is guaranteed to succeed.
Although vmemmap restoration may never fail in practice, properly handle such failure cases now. This will leave folios in a partially HVOed state. In a follow-up patch, failure certainly becomes possible.
In the event we end up with partially-HVOed folios, make sure to:
1. Leave the HVO page flag in place.
2. Free any unused vmemmap pages.
Always pass the vmemmap_tail page to the restore routine to avoid leaking the non-optimized vmemmap pages for a partially HVOed page.
--- mm/hugetlb_vmemmap.c | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-)
mm/hugetlb_vmemmap.c +28 -6
@@ -274,12 +274,22 @@ static int vmemmap_restore_pte(pte_t *pte, unsigned long addr, {const struct page *page = (const struct page *)addr;const struct folio *folio =page_folio (page); -struct page *src =pte_page (ptep_get (pte)), *dst; +struct page *src =pte_page (ptep_get (pte)), *dst, *tail;if (unlikely (!vmemmap_optimizable_pfn (page_to_pfn (page),folio_order (folio))))return 0 ; -VM_WARN_ON_ONCE (folio_vmemmap_shared_tail_page (folio) != src); + tail =folio_vmemmap_shared_tail_page (folio); +if (WARN_ON_ONCE (!tail)) +return -EINVAL ; + +/* +* The folio may be only partially optimized, so restore only PTEs +* currently pointing at the shared tail page. +*/ +if (src != tail) +return 0 ; + dst =vmemmap_dup_page_from_list (src, walk->vmemmap_pages);set_pte_at (&init_mm, addr, pte,mk_pte (dst,PAGE_KERNEL ));
@@ -325,6 +335,7 @@ static int vmemmap_pteify_folio(const struct folio *folio, unsigned long flags)return vmemmap_remap_range (start, end, &walk); } +#define VMEMMAP_REMAP_INCOMPLETE 1/** * vmemmap_optimize_folio - optimize the vmemmap backing of a folio * @folio: folio whose vmemmap pages should be optimized.
@@ -340,7 +351,8 @@ static int vmemmap_pteify_folio(const struct folio *folio, unsigned long flags)* Note: The caller must hold vmemmap_remap_rwsem. If TLB flushing is deferred, * the lock must remain held until the final TLB flush. * -* Return: %0 on success, negative error code otherwise. +* Return: %0 on success, VMEMMAP_REMAP_INCOMPLETE if the folio is incompletely +* optimized, negative error code otherwise. */ static int vmemmap_optimize_folio (const struct folio *folio,struct list_head *vmemmap_pages,unsigned long flags)
@@ -372,7 +384,8 @@ static int vmemmap_optimize_folio(const struct folio *folio,return ret; end = start + walk.nr_walked *PAGE_SIZE ; -vmemmap_restore_range (start, end, vmemmap_pages,0 ); +if (vmemmap_restore_range (start, end, vmemmap_pages,0 )) +return VMEMMAP_REMAP_INCOMPLETE ;return ret; }
@@ -402,7 +415,12 @@ static int vmemmap_restore_folio(const struct folio *folio, unsigned long flags)return ret;guard (rwsem_read)(&vmemmap_remap_rwsem); -return vmemmap_restore_range (start, end, &vmemmap_pages, flags); + ret =vmemmap_restore_range (start, end, &vmemmap_pages, flags); + +/* Not all pages may have been consumed */ +free_vmemmap_page_list (&vmemmap_pages); + +return ret; }static bool vmemmap_optimize_enabled =IS_ENABLED (CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON );
@@ -526,7 +544,11 @@ static int __hugetlb_vmemmap_optimize_folio(const struct hstate *h,*/ folio_set_hugetlb_vmemmap_optimized (folio); ret =vmemmap_optimize_folio (folio, vmemmap_pages, flags); -if (ret) +/* +* If ret == VMEMMAP_REMAP_INCOMPLETE, the folio might be partially +* HVOed. Leave the HVO page folio flag in place. +*/ +if (ret <0 )folio_clear_hugetlb_vmemmap_optimized (folio);return ret;
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
PATCH v3 4/19 hugetlb_vmemmap: Use try_update_vmemmap_pte to update in-use PTEs
In reply to James Houghtonset_pte_at() cannot be used to replace in-use vmemmap PTEs on arm64, so replace it with a more specific routine, try_update_vmemmap_pte().
try_update_vmemmap_pte() is used for modifying page table entries such that there is a guarantee that no fault will be taken.
For the generic implementation, please note one difference: set_pte() does not invoke page_table_check_ptes_set(), but set_pte_at(), the call we are about to replace, does. However, this is a functional no-op because page_table_check_ptes_set() does nothing for init_mm PTEs, which vmemmap PTEs are.
--- arch/loongarch/include/asm/pgtable.h | 2 ++ arch/riscv/include/asm/pgtable.h | 2 ++ arch/x86/include/asm/pgtable.h | 2 ++ include/linux/pgtable.h | 22 +++++++++++++++++++ mm/hugetlb_vmemmap.c | 32 +++++++++++++++++++++++----- 5 files changed, 55 insertions(+), 5 deletions(-)
arch/loongarch/include/asm/pgtable.h +2 -0
@@ -633,6 +633,8 @@ static inline long pmd_protnone(pmd_t pmd)#define pmd_leaf (pmd) ((pmd_val(pmd) & _PAGE_HUGE) != 0)#define pud_leaf (pud) ((pud_val(pud) & _PAGE_HUGE) != 0) +#define ARCH_WANTS_GENERIC_POPULATE_VMEMMAP_PTE +/* * We provide our own get_unmapped area to cope with the virtual aliasing * constraints placed on us by the cache architecture.
arch/riscv/include/asm/pgtable.h +2 -0
@@ -1172,6 +1172,8 @@ static inline pud_t pud_modify(pud_t pud, pgprot_t newprot)#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ +#define ARCH_WANTS_GENERIC_POPULATE_VMEMMAP_PTE +/* * Encode/decode swap entries and swap PTEs. Swap PTEs are all PTEs that * are !pte_none() && !pte_present().
arch/x86/include/asm/pgtable.h +2 -0
@@ -1372,6 +1372,8 @@ static inline pmd_t pmdp_establish(struct vm_area_struct *vma, }#endif +#define ARCH_WANTS_GENERIC_POPULATE_VMEMMAP_PTE +#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)
include/linux/pgtable.h +22 -0
@@ -458,6 +458,28 @@ static inline void set_ptes(struct mm_struct *mm, unsigned long addr,#endif #define set_pte_at (mm, addr, ptep, pte) set_ptes(mm, addr, ptep, pte, 1) +#ifdef ARCH_WANTS_GENERIC_POPULATE_VMEMMAP_PTE +/* +* try_update_vmemmap_pte - Remap PTEs used by the vmemmap. +* @addr: Base address of the remapped PTE. +* @ptep: Page table pointer to be overwritten. +* @pte: Page table entry to write. +* +* This function is only to be used to update PTEs that map the vmemmap. The +* only valid transitions supported by this function are: leaf-level +* (PAGE_SIZE), valid-to-valid. The pfn and prot bits may be changed. +* +* Implementations of this function must ensure that, while the update is taking +* place, CPUs will not fault on the remapped virtual address. +*/ +static inline int try_update_vmemmap_pte (unsigned long addr,pte_t *ptep, +pte_t pte) +{ +set_pte (ptep, pte); +return 0 ; +} +#endif +#ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS extern int ptep_set_access_flags(struct vm_area_struct *vma, unsigned long address, pte_t *ptep,
mm/hugetlb_vmemmap.c +27 -5
@@ -218,15 +218,19 @@ static int alloc_vmemmap_page_list(struct list_head *head,return -ENOMEM ; } +/* +* Copy @src into the first page on @vmemmap_pages and return it. The page is +* left on the list; the caller must remove it only once it has successfully +* been installed in the vmemmap. +*/ static struct page *vmemmap_dup_page_from_list (const struct page *src,struct list_head *vmemmap_pages) { struct page *dst = list_first_entry(vmemmap_pages, struct page, lru); -list_del (&dst->lru);copy_page (page_address (dst),page_address (src)); -/* Ensure updates to @dst page are visible before set_pte_at(). */ +/* Ensure updates to @dst page are visible before try_update_vmemmap_pte(). */ smp_wmb ();return dst;
@@ -248,6 +252,8 @@ static int vmemmap_optimize_pte(pte_t *pte, unsigned long addr,const struct page *page = (const struct page *)addr;const struct folio *folio =page_folio (page);struct page *src =pte_page (ptep_get (pte)), *dst; +bool dup = false; +int ret;if (likely (vmemmap_optimizable_pfn (page_to_pfn (page),folio_order (folio)))) { dst =folio_vmemmap_shared_tail_page (folio);
@@ -255,16 +261,27 @@ static int vmemmap_optimize_pte(pte_t *pte, unsigned long addr,return -ENOMEM ; }else { dst =vmemmap_dup_page_from_list (src, walk->vmemmap_pages); + dup = true; } -list_add (&src->lru, walk->vmemmap_pages);/* * Map the shared tail page writable intentionally. A lockless get that * read a non-zero refcount before the remap can still do its cmpxchg * after it. The refcount is zero, so the cmpxchg fails and leaves the * page unchanged. */ -set_pte_at (&init_mm, addr, pte,mk_pte (dst,PAGE_KERNEL )); + ret =try_update_vmemmap_pte (addr, pte,mk_pte (dst,PAGE_KERNEL )); +if (ret) +return ret; + +/* +* We successfully overwrote the vmemmap PTE, so the page that was just +* unmapped can be freed, and if we mapped a duplicated page, remove it +* from the list so that it doesn't get freed later. +*/ +if (dup) +list_del (&dst->lru); +list_add (&src->lru, walk->vmemmap_pages);return 0 ; }
@@ -275,6 +292,7 @@ static int vmemmap_restore_pte(pte_t *pte, unsigned long addr,const struct page *page = (const struct page *)addr;const struct folio *folio =page_folio (page);struct page *src =pte_page (ptep_get (pte)), *dst, *tail; +int ret;if (unlikely (!vmemmap_optimizable_pfn (page_to_pfn (page),folio_order (folio))))return 0 ;
@@ -291,7 +309,11 @@ static int vmemmap_restore_pte(pte_t *pte, unsigned long addr,return 0 ; dst =vmemmap_dup_page_from_list (src, walk->vmemmap_pages); -set_pte_at (&init_mm, addr, pte,mk_pte (dst,PAGE_KERNEL )); + ret =try_update_vmemmap_pte (addr, pte,mk_pte (dst,PAGE_KERNEL )); +if (ret) +return ret; + +list_del (&dst->lru);return 0 ; }
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Muchun Song
maintainerRe: [PATCH v3 04/19] hugetlb_vmemmap: Use try_update_vmemmap_pte to update in-use PTEs
Quoted 69 lines · James Houghton
set_pte_at()cannot be used to replace in-use vmemmap PTEs on arm64, so replace it with a more specific routine,try_update_vmemmap_pte().
try_update_vmemmap_pte()is used for modifying page table entries such that there is a guarantee that no fault will be taken.For the generic implementation, please note one difference:
set_pte()does not invokepage_table_check_ptes_set(), butset_pte_at(), the call we are about to replace, does. However, this is a functional no-op becausepage_table_check_ptes_set()does nothing forinit_mmPTEs, which vmemmap PTEs are.Signed-off-by James Houghton <[email protected]>---
arch/loongarch/include/asm/pgtable.h | 2 ++ arch/riscv/include/asm/pgtable.h | 2 ++ arch/x86/include/asm/pgtable.h | 2 ++ include/linux/pgtable.h | 22 +++++++++++++++++++ mm/hugetlb_vmemmap.c | 32 +++++++++++++++++++++++----- 5 files changed, 55 insertions(+), 5 deletions(-)arch/loongarch/include/asm/pgtable.h +2
-0@@ -633,6 +633,8 @@ static inline long pmd_protnone(pmd_t pmd)#define pmd_leaf (pmd) ((pmd_val(pmd) & _PAGE_HUGE) != 0)#define pud_leaf (pud) ((pud_val(pud) & _PAGE_HUGE) != 0) +#define ARCH_WANTS_GENERIC_POPULATE_VMEMMAP_PTE +/* * We provide our own get_unmapped area to cope with the virtual aliasing * constraints placed on us by the cache architecture. arch/riscv/include/asm/pgtable.h +2
-0@@ -1172,6 +1172,8 @@ static inline pud_t pud_modify(pud_t pud, pgprot_t newprot)#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ +#define ARCH_WANTS_GENERIC_POPULATE_VMEMMAP_PTE +/* * Encode/decode swap entries and swap PTEs. Swap PTEs are all PTEs that * are !pte_none() && !pte_present(). arch/x86/include/asm/pgtable.h +2
-0@@ -1372,6 +1372,8 @@ static inline pmd_t pmdp_establish(struct vm_area_struct *vma, }#endif +#define ARCH_WANTS_GENERIC_POPULATE_VMEMMAP_PTE +#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)include/linux/pgtable.h +0
-0
include/linux/pgtable.h static inline void set_ptes(struct mm_struct *mm, unsigned long addr, · line 461
Quoted 4 lines
include/linux/pgtable.h +1
-0@@ -458,6 +458,28 @@ static inline void set_ptes(struct mm_struct *mm, unsigned long addr,#endif #define set_pte_at (mm, addr, ptep, pte) set_ptes(mm, addr, ptep, pte, 1) +#ifdef ARCH_WANTS_GENERIC_POPULATE_VMEMMAP_PTE
Why not ARCH_WANTS_VMEMMAP_UPDATE_PTE? Then x86, loogarch and RISC-V do not need define this macro in their ptable.h.
Quoted 5 lines
include/linux/pgtable.h +5
-0+/* +* try_update_vmemmap_pte - Remap PTEs used by the vmemmap. +* @addr: Base address of the remapped PTE. +* @ptep: Page table pointer to be overwritten. +* @pte: Page table entry to write.
include/linux/pgtable.h static inline void set_ptes(struct mm_struct *mm, unsigned long addr, · line 476
Quoted 10 lines
include/linux/pgtable.h +10
-0+* +* This function is only to be used to update PTEs that map the vmemmap. The +* only valid transitions supported by this function are: leaf-level +* (PAGE_SIZE), valid-to-valid. The pfn and prot bits may be changed. +* +* Implementations of this function must ensure that, while the update is taking +* place, CPUs will not fault on the remapped virtual address. +*/ +static inline int try_update_vmemmap_pte (unsigned long addr,pte_t *ptep, +pte_t pte)
Please rename it to vmemmap_update_pte, all HVO-related helpers will be prefixed with vmemmap.
Again, I still recommend having this function return void. That will make the overall code simpler. If we actually run into related issues in the future, we can add support for it then. I don't want to introduce unnecessary code maintenance now.
Thanks,
Muchun
Quoted 98 lines
include/linux/pgtable.h +6
-0+{ +set_pte (ptep, pte); +return 0 ; +} +#endif +#ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS extern int ptep_set_access_flags(struct vm_area_struct *vma, unsigned long address, pte_t *ptep,mm/hugetlb_vmemmap.c +27
-5@@ -218,15 +218,19 @@ static int alloc_vmemmap_page_list(struct list_head *head,return -ENOMEM ; } +/* +* Copy @src into the first page on @vmemmap_pages and return it. The page is +* left on the list; the caller must remove it only once it has successfully +* been installed in the vmemmap. +*/ static struct page *vmemmap_dup_page_from_list (const struct page *src,struct list_head *vmemmap_pages) { struct page *dst = list_first_entry(vmemmap_pages, struct page, lru); -list_del (&dst->lru);copy_page (page_address (dst),page_address (src)); -/* Ensure updates to @dst page are visible before set_pte_at(). */ +/* Ensure updates to @dst page are visible before try_update_vmemmap_pte(). */ smp_wmb ();return dst;@@ -248,6 +252,8 @@ static int vmemmap_optimize_pte(pte_t *pte, unsigned long addr,const struct page *page = (const struct page *)addr;const struct folio *folio =page_folio (page);struct page *src =pte_page (ptep_get (pte)), *dst; +bool dup = false; +int ret;if (likely (vmemmap_optimizable_pfn (page_to_pfn (page),folio_order (folio)))) { dst =folio_vmemmap_shared_tail_page (folio);@@ -255,16 +261,27 @@ static int vmemmap_optimize_pte(pte_t *pte, unsigned long addr,return -ENOMEM ; }else { dst =vmemmap_dup_page_from_list (src, walk->vmemmap_pages); + dup = true; } -list_add (&src->lru, walk->vmemmap_pages);/* * Map the shared tail page writable intentionally. A lockless get that * read a non-zero refcount before the remap can still do its cmpxchg * after it. The refcount is zero, so the cmpxchg fails and leaves the * page unchanged. */ -set_pte_at (&init_mm, addr, pte,mk_pte (dst,PAGE_KERNEL )); + ret =try_update_vmemmap_pte (addr, pte,mk_pte (dst,PAGE_KERNEL )); +if (ret) +return ret; + +/* +* We successfully overwrote the vmemmap PTE, so the page that was just +* unmapped can be freed, and if we mapped a duplicated page, remove it +* from the list so that it doesn't get freed later. +*/ +if (dup) +list_del (&dst->lru); +list_add (&src->lru, walk->vmemmap_pages);return 0 ; }@@ -275,6 +292,7 @@ static int vmemmap_restore_pte(pte_t *pte, unsigned long addr,const struct page *page = (const struct page *)addr;const struct folio *folio =page_folio (page);struct page *src =pte_page (ptep_get (pte)), *dst, *tail; +int ret;if (unlikely (!vmemmap_optimizable_pfn (page_to_pfn (page),folio_order (folio))))return 0 ;@@ -291,7 +309,11 @@ static int vmemmap_restore_pte(pte_t *pte, unsigned long addr,return 0 ; dst =vmemmap_dup_page_from_list (src, walk->vmemmap_pages); -set_pte_at (&init_mm, addr, pte,mk_pte (dst,PAGE_KERNEL )); + ret =try_update_vmemmap_pte (addr, pte,mk_pte (dst,PAGE_KERNEL )); +if (ret) +return ret; + +list_del (&dst->lru);return 0 ; }Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Re: [PATCH v3 04/19] hugetlb_vmemmap: Use try_update_vmemmap_pte to update in-use PTEs
Quoted 73 lines · Muchun Song
Quoted 73 lines · James Houghton
set_pte_at()cannot be used to replace in-use vmemmap PTEs on arm64, so replace it with a more specific routine,try_update_vmemmap_pte().
try_update_vmemmap_pte()is used for modifying page table entries such that there is a guarantee that no fault will be taken.For the generic implementation, please note one difference:
set_pte()does not invokepage_table_check_ptes_set(), butset_pte_at(), the call we are about to replace, does. However, this is a functional no-op becausepage_table_check_ptes_set()does nothing forinit_mmPTEs, which vmemmap PTEs are.Signed-off-by James Houghton <[email protected]>---
arch/loongarch/include/asm/pgtable.h | 2 ++ arch/riscv/include/asm/pgtable.h | 2 ++ arch/x86/include/asm/pgtable.h | 2 ++ include/linux/pgtable.h | 22 +++++++++++++++++++ mm/hugetlb_vmemmap.c | 32 +++++++++++++++++++++++----- 5 files changed, 55 insertions(+), 5 deletions(-)arch/loongarch/include/asm/pgtable.h +2
-0@@ -633,6 +633,8 @@ static inline long pmd_protnone(pmd_t pmd)#define pmd_leaf (pmd) ((pmd_val(pmd) & _PAGE_HUGE) != 0)#define pud_leaf (pud) ((pud_val(pud) & _PAGE_HUGE) != 0) +#define ARCH_WANTS_GENERIC_POPULATE_VMEMMAP_PTE +/* * We provide our own get_unmapped area to cope with the virtual aliasing * constraints placed on us by the cache architecture. arch/riscv/include/asm/pgtable.h +2
-0@@ -1172,6 +1172,8 @@ static inline pud_t pud_modify(pud_t pud, pgprot_t newprot)#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ +#define ARCH_WANTS_GENERIC_POPULATE_VMEMMAP_PTE +/* * Encode/decode swap entries and swap PTEs. Swap PTEs are all PTEs that * are !pte_none() && !pte_present(). arch/x86/include/asm/pgtable.h +2
-0@@ -1372,6 +1372,8 @@ static inline pmd_t pmdp_establish(struct vm_area_struct *vma, }#endif +#define ARCH_WANTS_GENERIC_POPULATE_VMEMMAP_PTE +#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)include/linux/pgtable.h +0
-0@@ -458,6 +458,28 @@ static inline void set_ptes(struct mm_struct *mm, unsigned long addr,#endif #define set_pte_at (mm, addr, ptep, pte) set_ptes(mm, addr, ptep, pte, 1)
Quoted 4 lines
include/linux/pgtable.h static inline void set_ptes(struct mm_struct *mm, unsigned long addr, · line 461
Quoted 1 line
include/linux/pgtable.h +1
-0+#ifdef ARCH_WANTS_GENERIC_POPULATE_VMEMMAP_PTE Why not
ARCH_WANTS_VMEMMAP_UPDATE_PTE? Then x86, loogarch and RISC-V do not need define this macro in theirptable.h.
You're basically asking to make this opt-out instead of opt-in, right?
I like making the different architectures explicitly opt in to the generic implementation; it forces any new architecture to decide if the generic implementation is correct for them. But I don't feel strongly.
I'll switch it to opt-out in v4 unless you or someone else tells me otherwise.
Quoted 11 lines
Quoted 11 lines
include/linux/pgtable.h +10
-0+/* +* try_update_vmemmap_pte - Remap PTEs used by the vmemmap. +* @addr: Base address of the remapped PTE. +* @ptep: Page table pointer to be overwritten. +* @pte: Page table entry to write. +* +* This function is only to be used to update PTEs that map the vmemmap. The +* only valid transitions supported by this function are: leaf-level +* (PAGE_SIZE), valid-to-valid. The pfn and prot bits may be changed. +*
Quoted 8 lines
include/linux/pgtable.h static inline void set_ptes(struct mm_struct *mm, unsigned long addr, · line 476
Quoted 5 lines
include/linux/pgtable.h +5
-0+* Implementations of this function must ensure that, while the update is taking +* place, CPUs will not fault on the remapped virtual address. +*/ +static inline int try_update_vmemmap_pte (unsigned long addr,pte_t *ptep, +pte_t pte)Please rename it to
vmemmap_update_pte, all HVO-related helpers will be prefixed with vmemmap.
Okay, will do.
Quoted 5 lines
Again, I still recommend having this function return void. That will make the overall code simpler. If we actually run into related issues in the future, we can add support for it then. I don't want to introduce unnecessary code maintenance now.
Thanks. I will just point back to my reply from v2[1]. I hope Catalin or Will have thoughts on this.
[1] https://lore.kernel.org/linux-mm/CADrL8HWNaurYHGnpvC9qwr6bVU74YhhucCuhoVYcvqmSX+R6+Q@mail.gmail.com/
Quoted 103 lines
Thanks,
MuchunQuoted 98 lines
include/linux/pgtable.h +6
-0+{ +set_pte (ptep, pte); +return 0 ; +} +#endif +#ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS extern int ptep_set_access_flags(struct vm_area_struct *vma, unsigned long address, pte_t *ptep,mm/hugetlb_vmemmap.c +27
-6@@ -218,15 +218,19 @@ static int alloc_vmemmap_page_list(struct list_head *head,return -ENOMEM ; } +/* +* Copy @src into the first page on @vmemmap_pages and return it. The page is +* left on the list; the caller must remove it only once it has successfully +* been installed in the vmemmap. +*/ static struct page *vmemmap_dup_page_from_list (const struct page *src,struct list_head *vmemmap_pages) { struct page *dst = list_first_entry(vmemmap_pages, struct page, lru); -list_del (&dst->lru);copy_page (page_address (dst),page_address (src)); -/* Ensure updates to @dst page are visible before set_pte_at(). */ +/* Ensure updates to @dst page are visible before try_update_vmemmap_pte(). */ smp_wmb ();return dst;@@ -248,6 +252,8 @@ static int vmemmap_optimize_pte(pte_t *pte, unsigned long addr,const struct page *page = (const struct page *)addr;const struct folio *folio =page_folio (page);struct page *src =pte_page (ptep_get (pte)), *dst; +bool dup = false; +int ret;if (likely (vmemmap_optimizable_pfn (page_to_pfn (page),folio_order (folio)))) { dst =folio_vmemmap_shared_tail_page (folio);@@ -255,16 +261,27 @@ static int vmemmap_optimize_pte(pte_t *pte, unsigned long addr,return -ENOMEM ; }else { dst =vmemmap_dup_page_from_list (src, walk->vmemmap_pages); + dup = true; } -list_add (&src->lru, walk->vmemmap_pages);/* * Map the shared tail page writable intentionally. A lockless get that * read a non-zero refcount before the remap can still do its cmpxchg * after it. The refcount is zero, so the cmpxchg fails and leaves the * page unchanged. */ -set_pte_at (&init_mm, addr, pte,mk_pte (dst,PAGE_KERNEL )); + ret =try_update_vmemmap_pte (addr, pte,mk_pte (dst,PAGE_KERNEL )); +if (ret) +return ret; + +/* +* We successfully overwrote the vmemmap PTE, so the page that was just +* unmapped can be freed, and if we mapped a duplicated page, remove it +* from the list so that it doesn't get freed later. +*/ +if (dup) +list_del (&dst->lru); +list_add (&src->lru, walk->vmemmap_pages);return 0 ; }@@ -275,6 +292,7 @@ static int vmemmap_restore_pte(pte_t *pte, unsigned long addr,const struct page *page = (const struct page *)addr;const struct folio *folio =page_folio (page);struct page *src =pte_page (ptep_get (pte)), *dst, *tail; +int ret;if (unlikely (!vmemmap_optimizable_pfn (page_to_pfn (page),folio_order (folio))))return 0 ;@@ -291,7 +309,11 @@ static int vmemmap_restore_pte(pte_t *pte, unsigned long addr,return 0 ; dst =vmemmap_dup_page_from_list (src, walk->vmemmap_pages); -set_pte_at (&init_mm, addr, pte,mk_pte (dst,PAGE_KERNEL )); + ret =try_update_vmemmap_pte (addr, pte,mk_pte (dst,PAGE_KERNEL )); +if (ret) +return ret; + +list_del (&dst->lru);return 0 ; } --2.56.1.809.ga0b61035b5-goog
[email protected]
AI reviewRe: [PATCH v3 04/19] hugetlb_vmemmap: Use try_update_vmemmap_pte to update in-use PTEs
In reply to James HoughtonThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mm/hugetlb_vmemmap: missing TLB flush on partial vmemmap_restore_pte() failure --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mm/hugetlb_vmemmap: missing TLB flush on partial vmemmap_restore_pte() failure
--
commit 8b77f3aefb06415df030fdd341c5e065dabec71c
Author: James Houghton <[email protected]>
hugetlb_vmemmap: Use try_update_vmemmap_pte to update in-use PTEs
This commit introduces try_update_vmemmap_pte() and uses it in place of set_pte_at() when remapping in-use vmemmap PTEs during hugetlb vmemmap optimization and restoration.
Quoted 4 lines
mm/hugetlb_vmemmap.c +0
-0
[ ... ]
mm/hugetlb_vmemmap.c static int vmemmap_restore_pte(pte_t *pte, unsigned long addr, · line 319
Quoted 13 lines
mm/hugetlb_vmemmap.c +5
-1@@ -291,7 +309,11 @@ static int vmemmap_restore_pte(pte_t *pte, unsigned long addr,return 0 ; dst =vmemmap_dup_page_from_list (src, walk->vmemmap_pages); -set_pte_at (&init_mm, addr, pte,mk_pte (dst,PAGE_KERNEL )); + ret =try_update_vmemmap_pte (addr, pte,mk_pte (dst,PAGE_KERNEL )); +if (ret) +return ret; + +list_del (&dst->lru);return 0 ; }
[Severity: Medium]
Can a partial failure in vmemmap_restore_pte() skip the TLB flush for PTEs that were already remapped?
Previously, __hugetlb_vmemmap_restore_folio() could only fail in alloc_vmemmap_page_list() before modifying any PTEs. Now, try_update_vmemmap_pte() can fail after earlier vmemmap PTEs of the same folio have been remapped to new writable pages.
When called from hugetlb_vmemmap_restore_folios(), VMEMMAP_DEFER_TLB_FLUSH causes vmemmap_remap_range() to defer the flush to its caller, which only flushes when at least one folio was fully restored:
mm/hugetlb_vmemmap.c:hugetlb_vmemmap_restore_folios() {
...
if (folio_test_hugetlb_vmemmap_optimized(folio)) {
ret = __hugetlb_vmemmap_restore_folio(h, folio, flags);
if (ret)
break;
restored++;
}
...
if (restored)
flush_tlb_all();
}If the first optimized folio fails midway through vmemmap_restore_pte(), the loop breaks with restored == 0 and skips flush_tlb_all(), leaving stale TLB entries pointing to the old shared tail page for the already remapped vmemmap PTEs when the partially restored folio is returned to the hugetlb pool.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH v3 5/19 hugetlb_vmemmap: Allow architectures to dynamically disallow HVO
In reply to James HoughtonFor arm64, only some platforms support HVO. Add an arch hook, arch_support_vmemmap_remap(), to prevent HVO from taking effect if the architecture lacks support. The hook is not HugeTLB-specific, so it can also be used by other users of in-place vmemmap remapping (e.g. FS-DAX).
Check it in hugetlb_vmemmap_optimizable_size(), so that hstates are not reported or treated as optimizable (e.g. by report_hugepages() and the HugeTLB allocation and freeing paths) when the architecture does not support HVO.
It may be the case that arch_support_vmemmap_remap() returns false at first but eventually returns true. This is the case on arm64, where support depends on a system-wide CPU capability that is not yet known when bootmem hugepages are allocated. Such architectures cannot pre-HVO bootmem hugepages, so they must disable the section-based vmemmap optimization in their vmemmap population path with section_vmemmap_optimization_disable(). Their bootmem hugepages are then optimized when they are gathered instead.
--- include/linux/pgtable.h | 15 +++++++++++++++ mm/hugetlb_vmemmap.h | 2 ++ 2 files changed, 17 insertions(+)
include/linux/pgtable.h +15 -0
@@ -480,6 +480,21 @@ static inline int try_update_vmemmap_pte(unsigned long addr, pte_t *ptep, }#endif +#ifndef arch_support_vmemmap_remap +/* +* arch_support_vmemmap_remap - Whether in-use vmemmap mappings may be remapped. +* +* Vmemmap optimizations, such as the HugeTLB vmemmap optimization (HVO), +* remap vmemmap page table entries while they are in use. Architectures that +* can only do this on some systems may override this hook. It may return false +* early during boot and true later, once system-wide features are known. +*/ +static inline bool arch_support_vmemmap_remap (void ) +{ +return IS_ENABLED (CONFIG_VMEMMAP_OPTIMIZATION ); +} +#endif +#ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS extern int ptep_set_access_flags(struct vm_area_struct *vma, unsigned long address, pte_t *ptep,
mm/hugetlb_vmemmap.h +2 -0
@@ -48,6 +48,8 @@ static inline unsigned int hugetlb_vmemmap_optimizable_size(const struct hstateif (!is_power_of_2 (sizeof (struct page )))return 0 ; +if (!arch_support_vmemmap_remap ()) +return 0 ; return size > 0 ? size : 0; }#else
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Muchun Song
maintainerRe: [PATCH v3 05/19] hugetlb_vmemmap: Allow architectures to dynamically disallow HVO
Quoted 34 lines · James Houghton
For arm64, only some platforms support HVO. Add an arch hook,
arch_support_vmemmap_remap(), to prevent HVO from taking effect if the architecture lacks support. The hook is not HugeTLB-specific, so it can also be used by other users of in-place vmemmap remapping (e.g. FS-DAX).Check it in
hugetlb_vmemmap_optimizable_size(), so that hstates are not reported or treated as optimizable (e.g. byreport_hugepages()and the HugeTLB allocation and freeing paths) when the architecture does not support HVO.It may be the case that
arch_support_vmemmap_remap()returns false at first but eventually returns true. This is the case on arm64, where support depends on a system-wide CPU capability that is not yet known when bootmem hugepages are allocated. Such architectures cannot pre-HVO bootmem hugepages, so they must disable the section-based vmemmap optimization in their vmemmap population path withsection_vmemmap_optimization_disable(). Their bootmem hugepages are then optimized when they are gathered instead.Signed-off-by James Houghton <[email protected]>---
include/linux/pgtable.h| 15 +++++++++++++++mm/hugetlb_vmemmap.h| 2 ++2 files changed, 17 insertions(+)include/linux/pgtable.h +0
-0
include/linux/pgtable.h static inline int try_update_vmemmap_pte(unsigned long addr, pte_t *ptep, · line 483
Quoted 4 lines
include/linux/pgtable.h +1
-0@@ -480,6 +480,21 @@ static inline int try_update_vmemmap_pte(unsigned long addr, pte_t *ptep, }#endif +#ifndef arch_support_vmemmap_remap
We usually add a 's' to 'support'. Like arch_supports_vmemmap_remap.
include/linux/pgtable.h static inline int try_update_vmemmap_pte(unsigned long addr, pte_t *ptep, · line 489
Quoted 6 lines
include/linux/pgtable.h +6
-0+/* +* arch_support_vmemmap_remap - Whether in-use vmemmap mappings may be remapped. +* +* Vmemmap optimizations, such as the HugeTLB vmemmap optimization (HVO), +* remap vmemmap page table entries while they are in use. Architectures that +* can only do this on some systems may override this hook. It may return false
"Some systems" is not clear for me, How about rewrite this sentence with:
May override this hook based on architecture-specific runtime conditions.
include/linux/pgtable.h static inline int try_update_vmemmap_pte(unsigned long addr, pte_t *ptep, · line 490
Quoted 1 line
include/linux/pgtable.h +1
-0+* early during boot and true later, once system-wide features are known.
I think the description should be general, so the last sentence I think should be removed.
Thanks,
Muchun
Quoted 26 lines
include/linux/pgtable.h +7
-0+*/ +static inline bool arch_support_vmemmap_remap (void ) +{ +return IS_ENABLED (CONFIG_VMEMMAP_OPTIMIZATION ); +} +#endif +#ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS extern int ptep_set_access_flags(struct vm_area_struct *vma, unsigned long address, pte_t *ptep,mm/hugetlb_vmemmap.h +2
-0@@ -48,6 +48,8 @@ static inline unsigned int hugetlb_vmemmap_optimizable_size(const struct hstateif (!is_power_of_2 (sizeof (struct page )))return 0 ; +if (!arch_support_vmemmap_remap ()) +return 0 ; return size > 0 ? size : 0; }#else Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Re: [PATCH v3 05/19] hugetlb_vmemmap: Allow architectures to dynamically disallow HVO
Quoted 38 lines · Muchun Song
Quoted 38 lines · James Houghton
For arm64, only some platforms support HVO. Add an arch hook,
arch_support_vmemmap_remap(), to prevent HVO from taking effect if the architecture lacks support. The hook is not HugeTLB-specific, so it can also be used by other users of in-place vmemmap remapping (e.g. FS-DAX).Check it in
hugetlb_vmemmap_optimizable_size(), so that hstates are not reported or treated as optimizable (e.g. byreport_hugepages()and the HugeTLB allocation and freeing paths) when the architecture does not support HVO.It may be the case that
arch_support_vmemmap_remap()returns false at first but eventually returns true. This is the case on arm64, where support depends on a system-wide CPU capability that is not yet known when bootmem hugepages are allocated. Such architectures cannot pre-HVO bootmem hugepages, so they must disable the section-based vmemmap optimization in their vmemmap population path withsection_vmemmap_optimization_disable(). Their bootmem hugepages are then optimized when they are gathered instead.Signed-off-by James Houghton <[email protected]>---
include/linux/pgtable.h| 15 +++++++++++++++mm/hugetlb_vmemmap.h| 2 ++2 files changed, 17 insertions(+)include/linux/pgtable.h +0
-0@@ -480,6 +480,21 @@ static inline int try_update_vmemmap_pte(unsigned long addr, pte_t *ptep, }#endif
Quoted 3 lines
include/linux/pgtable.h static inline int try_update_vmemmap_pte(unsigned long addr, pte_t *ptep, · line 483
Quoted 1 line
include/linux/pgtable.h +1
-0+#ifndef arch_support_vmemmap_remapWe usually add a 's' to 'support'. Like
arch_supports_vmemmap_remap.
Oh yeah, I'll fix that. Thanks, Muchun!
Quoted 8 lines
include/linux/pgtable.h static inline int try_update_vmemmap_pte(unsigned long addr, pte_t *ptep, · line 489
Quoted 7 lines
include/linux/pgtable.h +6
-0+/* +* arch_support_vmemmap_remap - Whether in-use vmemmap mappings may be remapped. +* +* Vmemmap optimizations, such as the HugeTLB vmemmap optimization (HVO), +* remap vmemmap page table entries while they are in use. Architectures that +* can only do this on some systems may override this hook. It may return false
Quoted 8 lines
"Some systems" is not clear for me, How about rewrite this sentence with:
May override this hook based on architecture-specific runtime conditions.
include/linux/pgtable.h static inline int try_update_vmemmap_pte(unsigned long addr, pte_t *ptep, · line 490
Quoted 1 line
include/linux/pgtable.h +1
-0+* early during boot and true later, once system-wide features are known. I think the description should be general, so the last sentence I think should be removed.
I think it's important to clarify that the return value can transition from false to true, but not from true to false.
Okay how about this:
* Vmemmap optimizations, such as the HugeTLB vmemmap optimization (HVO),
* remap vmemmap page table entries while they are in use. Architectures that
* can only do this under certain runtime conditions may override
* this hook. It may return false early during boot and true later. Once
* it becomes true, it must not start returning false.
What do you think?
Thanks!
Quoted 31 lines
Thanks,
MuchunQuoted 26 lines
include/linux/pgtable.h +7
-0+*/ +static inline bool arch_support_vmemmap_remap (void ) +{ +return IS_ENABLED (CONFIG_VMEMMAP_OPTIMIZATION ); +} +#endif +#ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS extern int ptep_set_access_flags(struct vm_area_struct *vma, unsigned long address, pte_t *ptep,mm/hugetlb_vmemmap.h +2
-1@@ -48,6 +48,8 @@ static inline unsigned int hugetlb_vmemmap_optimizable_size(const struct hstateif (!is_power_of_2 (sizeof (struct page )))return 0 ; +if (!arch_support_vmemmap_remap ()) +return 0 ; return size > 0 ? size : 0; }#else --2.56.1.809.ga0b61035b5-goog
PATCH v3 6/19 hugetlb: Optimize bootmem folios that were not pre-HVOed
In reply to James HoughtonBoot-time gigantic folios are expected to be pre-HVOed through the section-based vmemmap optimization, so the bulk optimization pass for bootmem folios was dropped.
However, arch_support_vmemmap_remap() may not yet
return true when bootmem folios are allocated (e.g. on arm64, where support depends on a system-wide CPU capability), while it does return true by the time the bootmem folios are gathered. Such architectures disable the section-based vmemmap optimization in their vmemmap population path (see section_vmemmap_optimization_disable()), so the folios are not pre-HVOed and would never be optimized.
Optimize such folios when gathering bootmem folios. Their tail struct pages have all been initialized by memmap_init_compound_page(), as they were not pre-HVOed. Pre-HVOed folios are skipped by vmemmap_should_optimize_folio().
--- mm/hugetlb.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
mm/hugetlb.c +11 -0
@@ -3325,6 +3325,17 @@ static void __init prep_and_add_bootmem_folios(struct hstate *h,unsigned long flags;struct folio *folio, *tmp_f; +/* +* Bootmem folios are normally pre-HVOed through the section-based +* vmemmap optimization. However, an architecture that can only +* determine whether it supports HVO after bootmem allocation disables +* the section-based optimization (see +* section_vmemmap_optimization_disable()), in which case the folios +* were not pre-HVOed and must be optimized now. Pre-HVOed folios are +* skipped. +*/ +hugetlb_vmemmap_optimize_folios (h, folio_list); +list_for_each_entry_safe (folio, tmp_f, folio_list, lru) {/* Subdivide locks to achieve better parallel performance */ spin_lock_irqsave (&hugetlb_lock, flags);
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Muchun Song
maintainerRe: [PATCH v3 06/19] hugetlb: Optimize bootmem folios that were not pre-HVOed
Quoted 8 lines · James Houghton
Boot-time gigantic folios are expected to be pre-HVOed through the section-based vmemmap optimization, so the bulk optimization pass for bootmem folios was dropped.
Quoted 7 lines
However,
arch_support_vmemmap_remap()may not yet
return true when bootmem folios are allocated (e.g. on arm64, where support depends on a system-wide CPU capability), while it does return true by the time the bootmem folios are gathered. Such architectures disable the section-based vmemmap optimization in their vmemmap population path (seesection_vmemmap_optimization_disable()), so the folios are not pre-HVOed and would never be optimized.
Yeah, when I removed support for this part of the code, I figured you might add a new patch in the future to add support back. It is fine.
Quoted 18 lines
Optimize such folios when gathering bootmem folios. Their tail struct pages have all been initialized by
memmap_init_compound_page(), as they were not pre-HVOed. Pre-HVOed folios are skipped byvmemmap_should_optimize_folio().Signed-off-by James Houghton <[email protected]>---
mm/hugetlb.c| 11 +++++++++++1 file changed, 11 insertions(+)mm/hugetlb.c +0
-0@@ -3325,6 +3325,17 @@ static void __init prep_and_add_bootmem_folios(struct hstate *h,unsigned long flags;struct folio *folio, *tmp_f;
mm/hugetlb.c static void __init prep_and_add_bootmem_folios(struct hstate *h, · line 3336
Quoted 10 lines
mm/hugetlb.c +9
-0+/* +* Bootmem folios are normally pre-HVOed through the section-based +* vmemmap optimization. However, an architecture that can only +* determine whether it supports HVO after bootmem allocation disables +* the section-based optimization (see +* section_vmemmap_optimization_disable()), in which case the folios +* were not pre-HVOed and must be optimized now. Pre-HVOed folios are +* skipped. +*/
I'd like to condense these comments to something like:
/*
* Some architectures do not support section-based vmemmap optimization,
* so their bootmem folios should be optimized here with folio-based
* optimization.
*/
Thanks,
Muchun
Quoted 8 lines
mm/hugetlb.c +2
-0+hugetlb_vmemmap_optimize_folios (h, folio_list); +list_for_each_entry_safe (folio, tmp_f, folio_list, lru) {/* Subdivide locks to achieve better parallel performance */ spin_lock_irqsave (&hugetlb_lock, flags);Signature · 1 line
2.56.1.809.ga0b61035b5-goog
[email protected]
AI reviewRe: [PATCH v3 06/19] hugetlb: Optimize bootmem folios that were not pre-HVOed
In reply to James HoughtonThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mm/hugetlb: avoidable TLB flushes in hugetlb_vmemmap_optimize_folios() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mm/hugetlb: avoidable TLB flushes in hugetlb_vmemmap_optimize_folios()
--
commit 5763c0ece0fd843280e0e8e1b6561cb66598d64b
Author: James Houghton <[email protected]>
hugetlb: Optimize bootmem folios that were not pre-HVOed
This commit restores calling hugetlb_vmemmap_optimize_folios() in prep_and_add_bootmem_folios() so that architectures disabling section-based vmemmap optimization can still optimize bootmem folios once HVO support is determined.
Quoted 12 lines
mm/hugetlb.c +4
-0@@ -3325,6 +3325,17 @@ static void __init prep_and_add_bootmem_folios(struct hstate *h,unsigned long flags;struct folio *folio, *tmp_f; +/* +* Bootmem folios are normally pre-HVOed through the section-based +* vmemmap optimization. However, an architecture that can only +* determine whether it supports HVO after bootmem allocation disables
mm/hugetlb.c static void __init prep_and_add_bootmem_folios(struct hstate *h, · line 3341
Quoted 10 lines
mm/hugetlb.c +7
-0+* the section-based optimization (see +* section_vmemmap_optimization_disable()), in which case the folios +* were not pre-HVOed and must be optimized now. Pre-HVOed folios are +* skipped. +*/ +hugetlb_vmemmap_optimize_folios (h, folio_list); +list_for_each_entry_safe (folio, tmp_f, folio_list, lru) {/* Subdivide locks to achieve better parallel performance */ spin_lock_irqsave (&hugetlb_lock, flags);
[Severity: Medium]
Does calling hugetlb_vmemmap_optimize_folios() here cause avoidable vmemmap_remap_rwsem contention and two global flush_tlb_all() broadcasts per NUMA node when all folios on folio_list were already pre-HVOed (or when HVO is disabled)?
When 629d87ed65ef ("mm/hugetlb: drop boot-time HVO handling for gigantic folios") removed the call from prep_and_add_bootmem_folios(), it also removed the nr_to_optimize tracking and early return from hugetlb_vmemmap_optimize_folios(). On architectures where gather_bootmem_prealloc_node() already marks every bootmem folio with folio_set_hugetlb_vmemmap_optimized(), each node worker in hugetlb_bootmem_struct_page_init() now serializes on down_write(&vmemmap_remap_rwsem), walks folio_list twice, and runs two unconditional flush_tlb_all() shootdowns without modifying any page tables.
Restoring the early return in hugetlb_vmemmap_optimize_folios() when no folios on folio_list satisfy vmemmap_should_optimize_folio() would avoid the extra walk and TLB flushes.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
PATCH v3 7/19 hugetlb_vmemmap: Allow architectures to make HVO enablement boot-time only
In reply to James HoughtonArchitectures may need to configure the system at boot depending on whether HVO is enabled. For example, arm64 needs all CPUs to support hardware updates of the access flag to safely update the vmemmap in place, and may want to refuse to online CPUs that lack it, but only if HVO may be used.
Such configuration is only valid if HVO cannot be enabled later at runtime through the vm.hugetlb_optimize_vmemmap sysctl.
Add CONFIG_ARCH_HUGETLB_VMEMMAP_OPTIMIZATION_SYSCTL_RO for architectures to select. When selected, the vm.hugetlb_optimize_vmemmap sysctl is read-only, so HVO can only be enabled or disabled with the hugetlb_free_vmemmap= kernel command line parameter.
--- Documentation/admin-guide/sysctl/vm.rst | 3 +++ fs/Kconfig | 3 ++- mm/Kconfig | 7 +++++++ mm/hugetlb_vmemmap.c | 3 ++- 4 files changed, 14 insertions(+), 2 deletions(-)
Documentation/admin-guide/sysctl/vm.rst +3 -0
@@ -699,6 +699,9 @@ pages. So, those surplus pages are still optimized until they are no longer in use. You would need to wait for those surplus pages to be released before there are no optimized pages in the system. +On some architectures, this knob is read-only, and HVO can only be enabled or +disabled with the hugetlb_free_vmemmap= kernel command line parameter. + nr_hugepages_mempolicy ======================
fs/Kconfig +2 -1
@@ -267,7 +267,8 @@ config HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON help The HugeTLB Vmemmap Optimization (HVO) defaults to off. Say Y here to enable HVO by default. It can be disabled via hugetlb_free_vmemmap=off - (boot command line) or hugetlb_optimize_vmemmap (sysctl). + (boot command line) or hugetlb_optimize_vmemmap (sysctl, if the + architecture allows changing it at runtime). endif # HUGETLBFS config HUGETLB_PAGE
mm/Kconfig +7 -0
@@ -466,6 +466,13 @@ config ARCH_WANT_OPTIMIZE_DAX_VMEMMAP config ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP bool +# +# Select this from the architecture Kconfig if the hugetlb_optimize_vmemmap +# sysctl must be read-only. +# +config ARCH_HUGETLB_VMEMMAP_OPTIMIZATION_SYSCTL_RO + bool + config HAVE_MEMBLOCK_PHYS_MAP bool
mm/hugetlb_vmemmap.c +2 -1
@@ -669,7 +669,8 @@ static const struct ctl_table hugetlb_vmemmap_sysctls[] = { .procname = "hugetlb_optimize_vmemmap", .data = &vmemmap_optimize_enabled, .maxlen = sizeof(vmemmap_optimize_enabled), - .mode = 0644, + .mode = IS_ENABLED(CONFIG_ARCH_HUGETLB_VMEMMAP_OPTIMIZATION_SYSCTL_RO) ? + 0444 : 0644, .proc_handler = proc_dobool, }, };
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Re: [PATCH v3 07/19] hugetlb_vmemmap: Allow architectures to make HVO enablement boot-time only
Quoted 13 lines · James Houghton
Architectures may need to configure the system at boot depending on whether HVO is enabled. For example, arm64 needs all CPUs to support hardware updates of the access flag to safely update the vmemmap in place, and may want to refuse to online CPUs that lack it, but only if HVO may be used.
Such configuration is only valid if HVO cannot be enabled later at runtime through the vm.hugetlb_optimize_vmemmap sysctl.
Quoted 6 lines
Add
CONFIG_ARCH_HUGETLB_VMEMMAP_OPTIMIZATION_SYSCTL_ROfor architectures to select. When selected, the vm.hugetlb_optimize_vmemmap sysctl is read-only, so HVO can only be enabled or disabled with the hugetlb_free_vmemmap= kernel command line parameter.Signed-off-by James Houghton <[email protected]>
Thanks.
PATCH v3 8/19 hugetlb_vmemmap: Expose whether HVO is enabled to architecture code
In reply to James HoughtonArchitectures that select CONFIG_ARCH_HUGETLB_VMEMMAP_OPTIMIZATION_SYSCTL_RO may configure the system at boot depending on whether HVO is enabled, e.g. arm64 may require all CPUs to support hardware access flag updates only if HVO is enabled.
Add hugetlb_vmemmap_optimize_enabled() so that they can check. It is declared before <asm/hugetlb.h> is included, so that it can also be used from inline helpers there.
It only reflects the hugetlb_free_vmemmap= kernel command line parameter (and the sysctl, when writable), not whether the architecture supports HVO.
--- include/linux/hugetlb.h | 9 +++++++++ mm/hugetlb_vmemmap.c | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+)
include/linux/hugetlb.h +9 -0
@@ -22,6 +22,15 @@ struct node;void free_huge_folio (struct folio *folio); +#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP +bool hugetlb_vmemmap_optimize_enabled (void ); +#else +static inline bool hugetlb_vmemmap_optimize_enabled (void ) +{ +return false; +} +#endif +#ifdef CONFIG_HUGETLB_PAGE #include <linux/pagemap.h>
mm/hugetlb_vmemmap.c +18 -0
@@ -452,6 +452,24 @@ static int __init hugetlb_vmemmap_optimize_param(char *buf) }early_param ("hugetlb_free_vmemmap" , hugetlb_vmemmap_optimize_param); +/** +* hugetlb_vmemmap_optimize_enabled - whether HVO is enabled +* +* This only reflects the hugetlb_free_vmemmap= kernel command line parameter +* and the vm.hugetlb_optimize_vmemmap sysctl, not whether the architecture +* supports HVO (see arch_support_vmemmap_remap()). +* +* The value is final once early parameters have been parsed only if the +* architecture selects CONFIG_ARCH_HUGETLB_VMEMMAP_OPTIMIZATION_SYSCTL_RO; +* otherwise it may change at any time through the sysctl. +* +* Return: true if HVO is enabled. +*/ +bool hugetlb_vmemmap_optimize_enabled (void ) +{ +return READ_ONCE (vmemmap_optimize_enabled); +} +static int __hugetlb_vmemmap_restore_folio (const struct hstate *h,struct folio *folio,unsigned long flags) {
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Muchun Song
maintainerRe: [PATCH v3 08/19] hugetlb_vmemmap: Expose whether HVO is enabled to architecture code
Quoted 55 lines · James Houghton
Architectures that select
CONFIG_ARCH_HUGETLB_VMEMMAP_OPTIMIZATION_SYSCTL_ROmay configure the system at boot depending on whether HVO is enabled, e.g. arm64 may require all CPUs to support hardware access flag updates only if HVO is enabled.Add
hugetlb_vmemmap_optimize_enabled()so that they can check. It is declared before <asm/hugetlb.h> is included, so that it can also be used from inline helpers there.It only reflects the hugetlb_free_vmemmap= kernel command line parameter (and the sysctl, when writable), not whether the architecture supports HVO.
Signed-off-by James Houghton <[email protected]>---
include/linux/hugetlb.h| 9 +++++++++mm/hugetlb_vmemmap.c| 18 ++++++++++++++++++2 files changed, 27 insertions(+)include/linux/hugetlb.h +9
-0@@ -22,6 +22,15 @@ struct node;void free_huge_folio (struct folio *folio); +#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP +bool hugetlb_vmemmap_optimize_enabled (void ); +#else +static inline bool hugetlb_vmemmap_optimize_enabled (void ) +{ +return false; +} +#endif +#ifdef CONFIG_HUGETLB_PAGE #include <linux/pagemap.h> mm/hugetlb_vmemmap.c +3
-0@@ -452,6 +452,24 @@ static int __init hugetlb_vmemmap_optimize_param(char *buf) }early_param ("hugetlb_free_vmemmap" , hugetlb_vmemmap_optimize_param); +/** +* hugetlb_vmemmap_optimize_enabled - whether HVO is enabled +*
mm/hugetlb_vmemmap.c static int __init hugetlb_vmemmap_optimize_param(char *buf) · line 467
Quoted 10 lines
mm/hugetlb_vmemmap.c +10
-0+* This only reflects the hugetlb_free_vmemmap= kernel command line parameter +* and the vm.hugetlb_optimize_vmemmap sysctl, not whether the architecture +* supports HVO (see arch_support_vmemmap_remap()). +* +* The value is final once early parameters have been parsed only if the +* architecture selects CONFIG_ARCH_HUGETLB_VMEMMAP_OPTIMIZATION_SYSCTL_RO; +* otherwise it may change at any time through the sysctl. +* +* Return: true if HVO is enabled. +*/
I don't think hugetlb_vmemmap_optimize_enabled() has clear semantics.
Its name suggests that it reports whether HVO is actually enabled and usable, but it only returns the policy bit set by Kconfig, the command line, or the sysctl. It may return true even when arch_support_vmemmap_remap() is false. This is particularly confusing because arm64 calls it while determining the capability on which arch_support_vmemmap_remap() itself depends. Therefore, the helper really reports whether HVO was requested, rather than whether it is enabled.
Could we keep vmemmap_optimize_enabled private and let arm64 retain the boot-time setting locally instead? For example, arm64 can use a private hvo_enabled_at_boot variable with a core_param(hugetlb_free_vmemmap, ...) registration. The system capability is evaluated after core parameters have been parsed, and arm64 makes the HVO sysctl read-only, so the value remains stable for system capability and late CPU verification.
What do you think?
+static bool hvo_enabled_at_boot __ro_after_init =
+ IS_ENABLED(CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON);
+core_param(hugetlb_free_vmemmap, hvo_enabled_at_boot, bool, 0);
+
static bool has_bbm_through_af(const struct arm64_cpu_capabilities *caps, int scope)
{
/* The kernel only enables HW AF updates with CONFIG_ARM64_HW_AFDBM. */@@ -2055,7 +2059,8 @@ static bool has_bbm_through_af(const struct arm64_cpu_capabilities *caps, int sc
* BBM-through-AF is only needed for HVO. If HVO is not in use, don't
* penalize systems with mismatched support.
*/
- if (!hugetlb_vmemmap_optimize_enabled()) {
+ if (!IS_ENABLED(CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP) ||
+ !hvo_enabled_at_boot) {
/* Only valid if HVO cannot be enabled later via the sysctl. */
BUILD_BUG_ON(!IS_ENABLED(CONFIG_ARCH_HUGETLB_VMEMMAP_OPTIMIZATION_SYSCTL_RO));
return false;Thanks,
Muchun
Quoted 11 lines
mm/hugetlb_vmemmap.c +5
-0+bool hugetlb_vmemmap_optimize_enabled (void ) +{ +return READ_ONCE (vmemmap_optimize_enabled); +} +static int __hugetlb_vmemmap_restore_folio (const struct hstate *h,struct folio *folio,unsigned long flags) {Signature · 1 line
2.56.1.809.ga0b61035b5-goog
PATCH v3 9/19 arm64: Add bbm_through_af capability
In reply to James HoughtonHVO requires BBM Level 3 for the block -> table transitions when optimizing the vmemmap, and it requires hardware AF management to avoid taking page faults on the !AF translation that is installed as part of the RW table -> RO + new OA table transitions.
Call this requirement "BBM through AF"; i.e., all CPUs support BBM-less, in-place TTD updates. Model this as a system feature, as all CPUs must support it for the kernel to use it.
It may seem odd to check for HVO support directly in bbm_through_af(). If this weren't done, any system with mismatched HW AF will no longer be able to start late CPUs that are missing HW AF if all early CPUs have HW AF (and BBML3).
This depends on HVO enablement not changing after boot, so select ARCH_HUGETLB_VMEMMAP_OPTIMIZATION_SYSCTL_RO.
--- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/cpufeature.h | 5 +++++ arch/arm64/kernel/cpufeature.c | 33 +++++++++++++++++++++++++++++ arch/arm64/tools/cpucaps | 1 + 4 files changed, 40 insertions(+)
arch/arm64/Kconfig +1 -0
@@ -61,6 +61,7 @@ config ARM64 select ARCH_HAVE_ELF_PROT select ARCH_HAVE_NMI_SAFE_CMPXCHG select ARCH_HAVE_TRACE_MMIO_ACCESS + select ARCH_HUGETLB_VMEMMAP_OPTIMIZATION_SYSCTL_RO select ARCH_KEEP_MEMBLOCK select ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE select ARCH_USE_CMPXCHG_LOCKREF
arch/arm64/include/asm/cpufeature.h +5 -0
@@ -909,6 +909,11 @@ static inline bool system_supports_bbml3(void)return alternative_has_cap_unlikely (ARM64_HAS_BBML3 ); } +static inline bool system_supports_bbm_through_af (void ) +{ +return alternative_has_cap_unlikely (ARM64_HAS_BBM_THROUGH_AF ); +} +int do_emulate_mrs (struct pt_regs *regs,u32 sys_reg,u32 rt);bool try_emulate_mrs (struct pt_regs *regs,u32 isn);
arch/arm64/kernel/cpufeature.c +33 -0
@@ -76,6 +76,7 @@#include <linux/kasan.h> #include <linux/percpu.h> #include <linux/sched/isolation.h> +#include <linux/hugetlb.h> #include <asm/arm_pmuv3.h> #include <asm/cpu.h>
@@ -2044,6 +2045,31 @@ static bool has_bbml3(const struct arm64_cpu_capabilities *caps, int scope)return cpu_supports_bbml3 (); } +static bool has_bbm_through_af (const struct arm64_cpu_capabilities *caps,int scope) +{ +/* The kernel only enables HW AF updates with CONFIG_ARM64_HW_AFDBM. */ +if (!IS_ENABLED (CONFIG_ARM64_HW_AFDBM )) +return false; + +/* +* BBM-through-AF is only needed for HVO. If HVO is not in use, don't +* penalize systems with mismatched support. +*/ +if (!hugetlb_vmemmap_optimize_enabled ()) { +/* Only valid if HVO cannot be enabled later via the sysctl. */ +BUILD_BUG_ON (!IS_ENABLED (CONFIG_ARCH_HUGETLB_VMEMMAP_OPTIMIZATION_SYSCTL_RO )); +return false; + } + +/* +* We need BBML3 to support Block -> Table transitions without taking +* faults, and we need HW AF support to support changing the OA without +* taking faults. has_cpuid_feature() checks HAFDBS >= AF, reading the +* local CPU's register when verifying a late CPU. +*/ +return cpu_supports_bbml3 () &&has_cpuid_feature (caps, scope); +} +static void cpu_enable_pan (const struct arm64_cpu_capabilities *__unused) {/*
@@ -2953,6 +2979,13 @@ static const struct arm64_cpu_capabilities arm64_features[] = { .type = ARM64_CPUCAP_EARLY_LOCAL_CPU_FEATURE, .matches = has_bbml3, }, + { + .desc = "BBM-less TTD updates via Access Flag", + .capability = ARM64_HAS_BBM_THROUGH_AF, + .type = ARM64_CPUCAP_SYSTEM_FEATURE, + .matches = has_bbm_through_af, +ARM64_CPUID_FIELDS (ID_AA64MMFR1_EL1 ,HAFDBS ,AF ) + }, { .desc ="52-bit Virtual-Physical Addressing for S1 and S2 (LPA2)" , .capability = ARM64_HAS_LPA2,
arch/arm64/tools/cpucaps +1 -0
@@ -15,6 +15,7 @@ HAS_ADDRESS_AUTH_IMP_DEF HAS_AMU_EXTN HAS_ARMv8_4_TTL HAS_BBML3 +HAS_BBM_THROUGH_AF HAS_CACHE_DIC HAS_CACHE_IDC HAS_CNP
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
[email protected]
AI reviewRe: [PATCH v3 09/19] arm64: Add bbm_through_af capability
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] arm64: cpufeature: has_bbm_through_af() checks BBML3 only on local CPU --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] arm64: cpufeature: has_bbm_through_af() checks BBML3 only on local CPU
--
commit 65766d54bb753e1e16346fb12f4bb107c66114eb
Author: James Houghton <[email protected]>
arm64: Add bbm_through_af capability
This commit adds the ARM64_HAS_BBM_THROUGH_AF system capability to detect when all CPUs support BBM Level 3 and hardware Access Flag updates for HugeTLB vmemmap optimization (HVO).
Quoted 4 lines
arch/arm64/kernel/cpufeature.c +0
-0
[ ... ]
Quoted 18 lines
arch/arm64/kernel/cpufeature.c +14
-0@@ -2044,6 +2045,31 @@ static bool has_bbml3(const struct arm64_cpu_capabilities *caps, int scope)return cpu_supports_bbml3 (); } +static bool has_bbm_through_af (const struct arm64_cpu_capabilities *caps,int scope) +{ +/* The kernel only enables HW AF updates with CONFIG_ARM64_HW_AFDBM. */ +if (!IS_ENABLED (CONFIG_ARM64_HW_AFDBM )) +return false; + +/* +* BBM-through-AF is only needed for HVO. If HVO is not in use, don't +* penalize systems with mismatched support. +*/ +if (!hugetlb_vmemmap_optimize_enabled ()) { +/* Only valid if HVO cannot be enabled later via the sysctl. */ +BUILD_BUG_ON (!IS_ENABLED (CONFIG_ARCH_HUGETLB_VMEMMAP_OPTIMIZATION_SYSCTL_RO )); +return false;
arch/arm64/kernel/cpufeature.c static bool has_bbml3(const struct arm64_cpu_capabilities *caps, int scope) · line 2071
Quoted 10 lines
arch/arm64/kernel/cpufeature.c +10
-0+ } + +/* +* We need BBML3 to support Block -> Table transitions without taking +* faults, and we need HW AF support to support changing the OA without +* taking faults. has_cpuid_feature() checks HAFDBS >= AF, reading the +* local CPU's register when verifying a late CPU. +*/ +return cpu_supports_bbml3 () &&has_cpuid_feature (caps, scope); +}
[Severity: High]
As reported by Catalin Marinas on v1 [1], can has_bbm_through_af() falsely enable ARM64_HAS_BBM_THROUGH_AF on systems with mismatched BBML3 support?
ARM64_HAS_BBM_THROUGH_AF uses ARM64_CPUCAP_SYSTEM_FEATURE, so setup_system_capabilities() evaluates has_bbm_through_af() only once under SCOPE_SYSTEM on whichever CPU happens to run smp_cpus_done(). Unlike has_cpuid_feature(caps, scope), cpu_supports_bbml3() ignores scope and reads the current CPU's local ID_AA64MMFR2_EL1 and MIDR_EL1 registers.
On a heterogeneous system where all early CPUs support hardware AF updates but only some CPUs support BBML3, running setup_system_capabilities() on a BBML3-capable CPU enables ARM64_HAS_BBM_THROUGH_AF system-wide. HVO will then perform live block-to-table vmemmap PMD transitions while non-BBML3 CPUs are online, and re-onlining a non-BBML3 CPU via hotplug will fail verify_local_cpu_caps() and call cpu_die_early().
The later DO-NOT-MERGE commit "arm64: Drop BBML3 requirement for HVO" in this series drops the cpu_supports_bbml3() call, but if the BBML3 check is kept, should ARM64_HAS_BBM_THROUGH_AF use ARM64_CPUCAP_EARLY_LOCAL_CPU_FEATURE (or check cpus_have_cap(ARM64_HAS_BBML3) when scope == SCOPE_SYSTEM)?
[1] https://lore.kernel.org/r/[email protected]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
Re: [PATCH v3 09/19] arm64: Add bbm_through_af capability
Quoted 67 lines · On Sun, Nov 22, 2026 at 11:08 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] arm64: cpufeature:has_bbm_through_af()checks BBML3 only on local CPU
--commit
65766d54bb753e1e16346fb12f4bb107c66114eb
Author: James Houghton <[email protected]>arm64: Add
bbm_through_afcapabilityThis commit adds the
ARM64_HAS_BBM_THROUGH_AFsystem capability to detect when all CPUs support BBM Level 3 and hardware Access Flag updates for HugeTLB vmemmap optimization (HVO).Quoted 4 lines
arch/arm64/kernel/cpufeature.c +0
-0[ ... ]
Quoted 18 lines
arch/arm64/kernel/cpufeature.c +14
-0@@ -2044,6 +2045,31 @@ static bool has_bbml3(const struct arm64_cpu_capabilities *caps, int scope)return cpu_supports_bbml3 (); } +static bool has_bbm_through_af (const struct arm64_cpu_capabilities *caps,int scope) +{ +/* The kernel only enables HW AF updates with CONFIG_ARM64_HW_AFDBM. */ +if (!IS_ENABLED (CONFIG_ARM64_HW_AFDBM )) +return false; + +/* +* BBM-through-AF is only needed for HVO. If HVO is not in use, don't +* penalize systems with mismatched support. +*/ +if (!hugetlb_vmemmap_optimize_enabled ()) { +/* Only valid if HVO cannot be enabled later via the sysctl. */ +BUILD_BUG_ON (!IS_ENABLED (CONFIG_ARCH_HUGETLB_VMEMMAP_OPTIMIZATION_SYSCTL_RO )); +return false;arch/arm64/kernel/cpufeature.c static bool has_bbml3(const struct arm64_cpu_capabilities *caps, int scope) · line 2071
Quoted 10 lines
arch/arm64/kernel/cpufeature.c +10
-0+ } + +/* +* We need BBML3 to support Block -> Table transitions without taking +* faults, and we need HW AF support to support changing the OA without +* taking faults. has_cpuid_feature() checks HAFDBS >= AF, reading the +* local CPU's register when verifying a late CPU. +*/ +return cpu_supports_bbml3 () &&has_cpuid_feature (caps, scope); +}[Severity: High]
As reported by Catalin Marinas on v1 [1], canhas_bbm_through_af()falsely enableARM64_HAS_BBM_THROUGH_AFon systems with mismatched BBML3 support?
ARM64_HAS_BBM_THROUGH_AFusesARM64_CPUCAP_SYSTEM_FEATURE, sosetup_system_capabilities()evaluateshas_bbm_through_af()only once underSCOPE_SYSTEMon whichever CPU happens to runsmp_cpus_done(). Unlikehas_cpuid_feature(caps, scope),cpu_supports_bbml3()ignores scope and reads the current CPU's localID_AA64MMFR2_EL1andMIDR_EL1registers.On a heterogeneous system where all early CPUs support hardware AF updates but only some CPUs support BBML3, running
setup_system_capabilities()on a BBML3-capable CPU enablesARM64_HAS_BBM_THROUGH_AFsystem-wide. HVO will then perform live block-to-table vmemmap PMD transitions while non-BBML3 CPUs are online, and re-onlining a non-BBML3 CPU via hotplug will failverify_local_cpu_caps()and callcpu_die_early().
Quoted 7 lines
The later DO-NOT-MERGE commit "arm64: Drop BBML3 requirement for HVO" in this series drops the
cpu_supports_bbml3()call, but if the BBML3 check is kept, shouldARM64_HAS_BBM_THROUGH_AFuseARM64_CPUCAP_EARLY_LOCAL_CPU_FEATURE(or checkcpus_have_cap(ARM64_HAS_BBML3)when scope ==SCOPE_SYSTEM)?
Argh, yes Sashiko and Catalin are right: how I am detecting support for BBML3 here is wrong. IIUC, cpus_have_cap() (Catalin's suggestion) is right, but I think I prefer Sashiko's suggestion to use ARM64_CPUCAP_EARLY_LOCAL_CPU_FEATURE instead.
Leaving the new cap as a SYSTEM_FEATURE and using cpus_have_cap(ARM64_HAS_BBML3), late CPUs without BBML3 are rejected because the BBML3 cap will reject them (if BBML3 is enabled on the system). I think it's better for bbm_through_af not to rely on that and instead *itself* reject late CPUs that do not support BBML3. A minor thing.
Sorry for missing this feedback, Catalin. I'll respin a v4 soon-ish (next week?).
Quoted 3 lines
--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
PATCH v3 10/19 arm64: Implement try_update_vmemmap_pte using the AF trick
In reply to James Houghtontry_update_vmemmap_pte() must modify vmemmap PTEs without introducing a time window where other CPUs on the system might fault.
Normally a break-before-make sequence is required to avoid conflicts with cached translations. However, if we can guarantee that the existing translation cannot be cached, a BBM sequence is not needed.
Translations with the AF unset may not be cached (see Arm ARM Rule DWZCQ); the implementation of try_update_vmemmap_pte() on arm64 takes advantage of this fact to replace a PTE without BBM and therefore without leaving a window open where PE might fault on this translation.
Of course, if some CPUs on the system do not support HW AF management, clearing the AF will introduce potential faults. system_supports_bbm_through_af() will return false if any CPUs on the system do not support HW AF.
--- arch/arm64/include/asm/pgtable.h | 59 +++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 4 deletions(-)
arch/arm64/include/asm/pgtable.h +55 -4
@@ -1249,14 +1249,24 @@ static inline void __pte_clear(struct mm_struct *mm,__set_pte (ptep,__pte (0 )); } -static inline bool __ptep_test_and_clear_young (struct vm_area_struct *vma, -unsigned long address,pte_t *ptep) +/* +* Atomically clear the Accessed flag. Return the old value of the PTE. +*/ +static inline pte_t __ptep_clear_young (pte_t *ptep) {atomic64_t *pteval = (atomic64_t *)&pte_val (*ptep);s64 af_mask =PTE_AF ; -/* Atomically clear PTE_AF, checking that it was set before. */ -return af_mask &atomic64_fetch_andnot_relaxed (af_mask, pteval); +/* Atomically clear PTE_AF. */ +u64 oldval =atomic64_fetch_andnot_relaxed (af_mask, pteval); + +return __pte (oldval); +} + +static inline bool __ptep_test_and_clear_young (struct vm_area_struct *vma, +unsigned long address,pte_t *ptep) +{ +return pte_young (__ptep_clear_young (ptep)); } static inline bool __ptep_clear_flush_young(struct vm_area_struct *vma,
@@ -1921,6 +1931,47 @@ static inline void clear_young_dirty_ptes(struct vm_area_struct *vma,#endif /* CONFIG_ARM64_CONTPTE */ +static inline int try_update_vmemmap_pte (unsigned long addr,pte_t *ptep, +const pte_t pte) +{ +const int max_attempts =16 ; +int attempts =0 ; +pte_t old_pte; + +if (!system_supports_bbm_through_af ()) +return -EOPNOTSUPP ; + +/* This routine is only to be used for valid-to-valid transitions. */ +if (WARN_ON_ONCE (!pte_valid (pte))) +return -EINVAL ; + + old_pte =__ptep_get (ptep); + +do { +if (WARN_ON_ONCE (!pte_valid (old_pte))) +return -EINVAL ; + +/* We should never get a contiguous PTE here. */ +if (WARN_ON_ONCE (pte_valid_cont (old_pte))) +return -EINVAL ; + +if (pte_young (old_pte)) { +/* __ptep_clear_young() returns the overwritten PTE */ + old_pte =pte_mkold (__ptep_clear_young (ptep)); + +flush_tlb_kernel_range (addr, addr +PAGE_SIZE ); + } +/* +* Translations without AF cannot be cached, so we can replace +* them without BBM. +*/ + }while (!try_cmpxchg_relaxed (&pte_val (*ptep), &pte_val (old_pte), +pte_val (pte)) && + ++attempts < max_attempts); + +return attempts == max_attempts ? -EAGAIN :0 ; +} +#define __HAVE_ARCH_VMAP_PTE_SET_HUGEvoid vmap_pte_set_huge (pte_t *ptep,unsigned long addr,phys_addr_t phys,pgprot_t prot,unsigned long size);
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
PATCH v3 11/19 arm64/mm: Align vmemmap to maximal folio size
In reply to James HoughtonWith CONFIG_VMEMMAP_OPTIMIZATION, compound_head() derives a tail page's head by masking its address. This requires the head struct page of every folio to be naturally aligned to the folio's vmemmap size, up to MAX_FOLIO_VMEMMAP_ALIGN.
On arm64, the vmemmap is offset by memstart_addr, which is only aligned to ARM64_MEMSTART_ALIGN: as little as 512M with 64K pages, where 16G CONT_PMD hugetlb folios exist. VMEMMAP_START itself is sufficiently aligned for all supported page sizes and VA sizes.
Raise ARM64_MEMSTART_ALIGN to cover MAX_FOLIO_VMEMMAP_ALIGN, and assert at build time that VMEMMAP_START is suitably aligned. This is a no-op until arm64 enables HVO, which selects CONFIG_VMEMMAP_OPTIMIZATION.
--- arch/arm64/mm/init.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)
arch/arm64/mm/init.c +16 -2
@@ -35,6 +35,7 @@#include <linux/kmemleak.h> #include <linux/execmem.h> #include <linux/arm-rsi-cmds.h> +#include <linux/vmemmap-optimization.h> #include <asm/boot.h>
@@ -92,11 +93,22 @@ phys_addr_t __ro_after_init arm64_dma_phys_limit;* in the virtual address space. */ #if ARM64_MEMSTART_SHIFT <SECTION_SIZE_BITS -#define ARM64_MEMSTART_ALIGN (1UL << SECTION_SIZE_BITS) +#define ARM64_MEMSTART_BASE_ALIGN (1UL << SECTION_SIZE_BITS)#else -#define ARM64_MEMSTART_ALIGN (1UL << ARM64_MEMSTART_SHIFT) +#define ARM64_MEMSTART_BASE_ALIGN (1UL << ARM64_MEMSTART_SHIFT)#endif +/* +* With CONFIG_VMEMMAP_OPTIMIZATION, compound_head() masks the address of a +* tail page to find its head, so the head struct page of every folio must be +* naturally aligned to the folio's vmemmap size. The vmemmap is offset by +* memstart_addr, so memstart_addr must be aligned to the largest folio size. +*/ +#define ARM64_VMEMMAP_OPT_ALIGN \ + PFN_PHYS(MAX_FOLIO_VMEMMAP_ALIGN / sizeof(struct page)) +#define ARM64_MEMSTART_ALIGN \ + max_t(phys_addr_t, ARM64_MEMSTART_BASE_ALIGN, ARM64_VMEMMAP_OPT_ALIGN) +static void __init arch_reserve_crashkernel (void ) {unsigned long long crash_base, crash_size, cma_size =0 ;
@@ -218,6 +230,8 @@ void __init arm64_memblock_init(void)/* * Select a suitable value for the base of physical memory. */ +BUILD_BUG_ON (MAX_FOLIO_VMEMMAP_ALIGN && + !IS_ALIGNED (VMEMMAP_START ,MAX_FOLIO_VMEMMAP_ALIGN )); memstart_addr =round_down (memblock_start_of_DRAM (),ARM64_MEMSTART_ALIGN );
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Muchun Song
maintainerRe: [PATCH v3 11/19] arm64/mm: Align vmemmap to maximal folio size
Quoted 61 lines · James Houghton
With
CONFIG_VMEMMAP_OPTIMIZATION,compound_head()derives a tail page's head by masking its address. This requires the head struct page of every folio to be naturally aligned to the folio's vmemmap size, up toMAX_FOLIO_VMEMMAP_ALIGN.On arm64, the vmemmap is offset by
memstart_addr, which is only aligned toARM64_MEMSTART_ALIGN: as little as 512M with 64K pages, where 16GCONT_PMDhugetlb folios exist.VMEMMAP_STARTitself is sufficiently aligned for all supported page sizes and VA sizes.Raise
ARM64_MEMSTART_ALIGNto coverMAX_FOLIO_VMEMMAP_ALIGN, and assert at build time thatVMEMMAP_STARTis suitably aligned. This is a no-op until arm64 enables HVO, which selectsCONFIG_VMEMMAP_OPTIMIZATION.Signed-off-by James Houghton <[email protected]>---
arch/arm64/mm/init.c| 18 ++++++++++++++++--1 file changed, 16 insertions(+), 2 deletions(-)arch/arm64/mm/init.c +14
-2@@ -35,6 +35,7 @@#include <linux/kmemleak.h> #include <linux/execmem.h> #include <linux/arm-rsi-cmds.h> +#include <linux/vmemmap-optimization.h> #include <asm/boot.h> @@ -92,11 +93,22 @@ phys_addr_t __ro_after_init arm64_dma_phys_limit;* in the virtual address space. */ #if ARM64_MEMSTART_SHIFT <SECTION_SIZE_BITS -#define ARM64_MEMSTART_ALIGN (1UL << SECTION_SIZE_BITS) +#define ARM64_MEMSTART_BASE_ALIGN (1UL << SECTION_SIZE_BITS)#else -#define ARM64_MEMSTART_ALIGN (1UL << ARM64_MEMSTART_SHIFT) +#define ARM64_MEMSTART_BASE_ALIGN (1UL << ARM64_MEMSTART_SHIFT)#endif +/* +* With CONFIG_VMEMMAP_OPTIMIZATION, compound_head() masks the address of a +* tail page to find its head, so the head struct page of every folio must be +* naturally aligned to the folio's vmemmap size. The vmemmap is offset by +* memstart_addr, so memstart_addr must be aligned to the largest folio size. +*/ +#define ARM64_VMEMMAP_OPT_ALIGN \ + PFN_PHYS(MAX_FOLIO_VMEMMAP_ALIGN / sizeof(struct page)) +#define ARM64_MEMSTART_ALIGN \ + max_t(phys_addr_t, ARM64_MEMSTART_BASE_ALIGN, ARM64_VMEMMAP_OPT_ALIGN) +static void __init arch_reserve_crashkernel (void ) {unsigned long long crash_base, crash_size, cma_size =0 ;
arch/arm64/mm/init.c void __init arm64_memblock_init(void) · line 234
Quoted 5 lines
arch/arm64/mm/init.c +2
-0@@ -218,6 +230,8 @@ void __init arm64_memblock_init(void)/* * Select a suitable value for the base of physical memory. */ +BUILD_BUG_ON (MAX_FOLIO_VMEMMAP_ALIGN && + !IS_ALIGNED (VMEMMAP_START ,MAX_FOLIO_VMEMMAP_ALIGN ));
We already have a sanity check in spares_init(). Doesn't it work? If we really need a check here, I think we should use compound_info_has_mask() for the guard instead of MAX_FOLIO_VMEMMAP_ALIGN.
Thanks,
Muchun
Quoted 6 lines
arch/arm64/mm/init.c +0
-0memstart_addr =round_down (memblock_start_of_DRAM (),ARM64_MEMSTART_ALIGN );Signature · 1 line
2.56.1.809.ga0b61035b5-goog
PATCH v3 12/19 arm64: Support hugetlb vmemmap optimization
In reply to James HoughtonSupport HVO if HW AF and BBML3 are supported. These are the required features for the vmemmap to be modified in place.
On arm64 systems that do not have the required hardware features to support HVO, the HVO sysctl will not appear, and any attempts to enable HVO (either via the command line or via CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON=y) will have no effect.
Because HVO support is only known after the vmemmap has been populated, disable the section-based vmemmap optimization in vmemmap_populate(), as LoongArch and powerpc do. Boot-time HugeTLB folios are then optimized when they are gathered.
--- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/pgtable.h | 6 ++++++ arch/arm64/mm/mmu.c | 9 +++++++++ mm/hugetlb_vmemmap.c | 4 ++++ 4 files changed, 20 insertions(+)
arch/arm64/Kconfig +1 -0
@@ -95,6 +95,7 @@ config ARM64 select ARCH_WANT_FRAME_POINTERS select ARCH_WANT_HUGE_PMD_SHARE if ARM64_4K_PAGES || (ARM64_16K_PAGES && !ARM64_VA_BITS_36) select ARCH_WANT_LD_ORPHAN_WARN + select ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP if ARM64_HW_AFDBM select ARCH_WANTS_EXECMEM_LATE select ARCH_WANTS_NO_INSTR select ARCH_WANTS_THP_SWAP if ARM64_4K_PAGES
arch/arm64/include/asm/pgtable.h +6 -0
@@ -1931,6 +1931,12 @@ static inline void clear_young_dirty_ptes(struct vm_area_struct *vma,#endif /* CONFIG_ARM64_CONTPTE */ +#define arch_support_vmemmap_remap arch_support_vmemmap_remap +static inline bool arch_support_vmemmap_remap (void ) +{ +return system_supports_bbm_through_af (); +} +static inline int try_update_vmemmap_pte (unsigned long addr,pte_t *ptep,const pte_t pte) {
arch/arm64/mm/mmu.c +9 -0
@@ -30,6 +30,7 @@#include <linux/mm_inline.h> #include <linux/pagewalk.h> #include <linux/stop_machine.h> +#include <linux/vmemmap-optimization.h> #include <asm/barrier.h> #include <asm/cputype.h>
@@ -1843,6 +1844,14 @@ int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,/* [start, end] should be within one section */ WARN_ON_ONCE (end - start >PAGES_PER_SECTION *sizeof (struct page )); +/* +* Whether HVO is supported is only known once the system-wide CPU +* features have been finalized, which is after the vmemmap has been +* populated, so the section-based vmemmap optimization (pre-HVO) is +* not supported. Boot-time HugeTLB folios are optimized later instead. +*/ +section_vmemmap_optimization_disable (start, end); +if (!IS_ENABLED (CONFIG_ARM64_4K_PAGES ) || (end - start <PAGES_PER_SECTION *sizeof (struct page )))return vmemmap_populate_basepages (start, end, node, altmap);
mm/hugetlb_vmemmap.c +4 -0
@@ -79,6 +79,10 @@ static int vmemmap_split_pmd(pmd_t *pmd, struct page *head, unsigned long start,/* Make pte visible before pmd. See comment in pmd_install(). */ smp_wmb (); +/* +* On arm64, this requires BBML3. Its support has already been +* checked. +*/ pmd_populate_kernel (&init_mm, pmd, pgtable);/* * Modern MMUs allow overlapping TLB entries with different mapping
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Muchun Song
maintainerRe: [PATCH v3 12/19] arm64: Support hugetlb vmemmap optimization
Quoted 67 lines · James Houghton
Support HVO if HW AF and BBML3 are supported. These are the required features for the vmemmap to be modified in place.
On arm64 systems that do not have the required hardware features to support HVO, the HVO sysctl will not appear, and any attempts to enable HVO (either via the command line or via
CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON=y) will have no effect.Because HVO support is only known after the vmemmap has been populated, disable the section-based vmemmap optimization in
vmemmap_populate(), as LoongArch and powerpc do. Boot-time HugeTLB folios are then optimized when they are gathered.Signed-off-by James Houghton <[email protected]>---
arch/arm64/Kconfig | 1 + arch/arm64/include/asm/pgtable.h | 6 ++++++ arch/arm64/mm/mmu.c | 9 +++++++++ mm/hugetlb_vmemmap.c | 4 ++++ 4 files changed, 20 insertions(+)arch/arm64/Kconfig +1
-0@@ -95,6 +95,7 @@ config ARM64 select ARCH_WANT_FRAME_POINTERS select ARCH_WANT_HUGE_PMD_SHARE if ARM64_4K_PAGES || (ARM64_16K_PAGES && !ARM64_VA_BITS_36) select ARCH_WANT_LD_ORPHAN_WARN + select ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP if ARM64_HW_AFDBM select ARCH_WANTS_EXECMEM_LATE select ARCH_WANTS_NO_INSTR select ARCH_WANTS_THP_SWAP if ARM64_4K_PAGESarch/arm64/include/asm/pgtable.h +6
-0@@ -1931,6 +1931,12 @@ static inline void clear_young_dirty_ptes(struct vm_area_struct *vma,#endif /* CONFIG_ARM64_CONTPTE */ +#define arch_support_vmemmap_remap arch_support_vmemmap_remap +static inline bool arch_support_vmemmap_remap (void ) +{ +return system_supports_bbm_through_af (); +} +static inline int try_update_vmemmap_pte (unsigned long addr,pte_t *ptep,const pte_t pte) {arch/arm64/mm/mmu.c +1
-0@@ -30,6 +30,7 @@#include <linux/mm_inline.h> #include <linux/pagewalk.h> #include <linux/stop_machine.h> +#include <linux/vmemmap-optimization.h> #include <asm/barrier.h> #include <asm/cputype.h>
arch/arm64/mm/mmu.c int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node, · line 1850
Quoted 7 lines
arch/arm64/mm/mmu.c +4
-0@@ -1843,6 +1844,14 @@ int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,/* [start, end] should be within one section */ WARN_ON_ONCE (end - start >PAGES_PER_SECTION *sizeof (struct page )); +/* +* Whether HVO is supported is only known once the system-wide CPU +* features have been finalized, which is after the vmemmap has been +* populated, so the section-based vmemmap optimization (pre-HVO) is
It's better to avoid the term of "pre-HVO", It's an obsolete term. All infrastructure of it are removed now.
Quoted 12 lines
arch/arm64/mm/mmu.c +4
-0+* not supported. Boot-time HugeTLB folios are optimized later instead. +*/ +section_vmemmap_optimization_disable (start, end); +if (!IS_ENABLED (CONFIG_ARM64_4K_PAGES ) || (end - start <PAGES_PER_SECTION *sizeof (struct page )))return vmemmap_populate_basepages (start, end, node, altmap);mm/hugetlb_vmemmap.c +0
-0
mm/hugetlb_vmemmap.c static int vmemmap_split_pmd(pmd_t *pmd, struct page *head, unsigned long start, · line 85
Quoted 7 lines
mm/hugetlb_vmemmap.c +4
-0@@ -79,6 +79,10 @@ static int vmemmap_split_pmd(pmd_t *pmd, struct page *head, unsigned long start,/* Make pte visible before pmd. See comment in pmd_install(). */ smp_wmb (); +/* +* On arm64, this requires BBML3. Its support has already been +* checked. +*/
Please don't add an architecture specific comment here. That is something architecture itself needs to consider.
Thanks,
Muchun
Quoted 6 lines
mm/hugetlb_vmemmap.c +0
-0pmd_populate_kernel (&init_mm, pmd, pgtable);/* * Modern MMUs allow overlapping TLB entries with different mapping Signature · 1 line
2.56.1.809.ga0b61035b5-goog
[email protected]
AI reviewRe: [PATCH v3 12/19] arm64: Support hugetlb vmemmap optimization
In reply to James HoughtonThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] arm64/mm: arch_support_vmemmap_remap() only checks BBML3 on boot CPU --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] arm64/mm: arch_support_vmemmap_remap() only checks BBML3 on boot CPU
--
commit d9205a2d3f06bed3d56fccb15aefe364db523337
Author: James Houghton <[email protected]>
arm64: Support hugetlb vmemmap optimization
This commit enables HugeTLB vmemmap optimization (HVO) on arm64 systems that support hardware access flag updates and BBML3, disabling early section-based vmemmap optimization in vmemmap_populate().
Quoted 7 lines
arch/arm64/include/asm/pgtable.h +0
-0@@ -1931,6 +1931,12 @@ static inline void clear_young_dirty_ptes(struct vm_area_struct *vma,#endif /* CONFIG_ARM64_CONTPTE */
arch/arm64/include/asm/pgtable.h static inline void clear_young_dirty_ptes(struct vm_area_struct *vma, · line 1942
Quoted 10 lines
arch/arm64/include/asm/pgtable.h +6
-0+#define arch_support_vmemmap_remap arch_support_vmemmap_remap +static inline bool arch_support_vmemmap_remap (void ) +{ +return system_supports_bbm_through_af (); +} +static inline int try_update_vmemmap_pte (unsigned long addr,pte_t *ptep,const pte_t pte) {
[Severity: High]
Can arch_support_vmemmap_remap() return true on heterogeneous systems where the boot CPU supports BBML3 but secondary CPUs do not?
ARM64_HAS_BBM_THROUGH_AF (added in commit "arm64: Add bbm_through_af capability") is registered as ARM64_CPUCAP_SYSTEM_FEATURE, so has_bbm_through_af() is evaluated once with SCOPE_SYSTEM during setup_system_capabilities():
arch/arm64/kernel/cpufeature.c:has_bbm_through_af() {
...
return cpu_supports_bbml3() && has_cpuid_feature(caps, scope);
}While has_cpuid_feature() checks the system-wide sanitised ID_AA64MMFR1_EL1 value for SCOPE_SYSTEM, cpu_supports_bbml3() only checks ID_AA64MMFR2_EL1 and MIDR_EL1 on the local CPU running setup_system_capabilities() rather than checking cpus_have_cap(ARM64_HAS_BBML3) across all early CPUs. If a secondary CPU lacks BBML3, vmemmap_split_pmd() can still perform live in-place PMD block-to-table transitions via pmd_populate_kernel() without break-before-make, risking TLB conflict aborts on the vmemmap region. (Note that commit "arm64: Drop BBML3 requirement for HVO" later in this series removes the cpu_supports_bbml3() check, though that part of the series is marked DO-NOT-MERGE.)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
PATCH v3 13/19 hugetlb_vmemmap: Add fault injection for in-place vmemmap PTE updates
In reply to James Houghtontry_update_vmemmap_pte() may fail, e.g. on arm64 when the update keeps racing with hardware access flag updates. HVO handles such failures by rolling back the optimization, or by leaving folios partially optimized if the rollback or a later restore fails. These paths are rare to hit normally.
Add a fault-injection capability, fail_hugetlb_vmemmap_pte, under CONFIG_FAIL_HUGETLB_VMEMMAP. When a fault is injected, the PTE update fails with -EAGAIN without touching the page tables, as if the in-place update had given up.
It can be configured through debugfs or through the fail_hugetlb_vmemmap_pte= boot option, the latter allowing failures to be injected when optimizing bootmem folios.
--- .../admin-guide/kernel-parameters.txt | 1 + .../fault-injection/fault-injection.rst | 6 +++ lib/Kconfig.debug | 9 +++++ mm/hugetlb_vmemmap.c | 38 ++++++++++++++++++- 4 files changed, 52 insertions(+), 2 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +1 -0
@@ -1694,6 +1694,7 @@ Kernel parameters fail_usercopy= fail_page_alloc= fail_skb_realloc= + fail_hugetlb_vmemmap_pte= fail_make_request=[KNL] General fault injection mechanism. Format: <interval>,<probability>,<space>,<times>
Documentation/fault-injection/fault-injection.rst +6 -0
@@ -16,6 +16,11 @@ Available fault injection capabilities injects page allocation failures. (alloc_pages(), get_free_pages(), ...) +- fail_hugetlb_vmemmap_pte + + injects failures of the in-place vmemmap PTE remaps done by HugeTLB vmemmap + optimization. (try_update_vmemmap_pte()) + - fail_usercopy injects failures in user memory access functions. (copy_from_user(), get_user(), ...)
@@ -263,6 +268,7 @@ use the boot option:: failslab= fail_page_alloc= + fail_hugetlb_vmemmap_pte= fail_usercopy= fail_make_request= fail_futex=
lib/Kconfig.debug +9 -0
@@ -2098,6 +2098,15 @@ config FAIL_PAGE_ALLOC help Provide fault-injection capability for alloc_pages(). +config FAIL_HUGETLB_VMEMMAP + bool "Fault-injection capability for HugeTLB vmemmap optimization" + depends on FAULT_INJECTION && HUGETLB_PAGE_OPTIMIZE_VMEMMAP + help + Provide fault-injection capability for the in-place vmemmap page + table updates done by HugeTLB vmemmap optimization (HVO), i.e. + try_update_vmemmap_pte(). This exercises the rollback and + partially-optimized folio paths. + config FAULT_INJECTION_USERCOPY bool "Fault injection capability for usercopy functions" depends on FAULT_INJECTION
mm/hugetlb_vmemmap.c +36 -2
@@ -17,6 +17,7 @@#include <linux/pgalloc.h> #include <linux/rwsem.h> #include <linux/vmemmap-optimization.h> +#include <linux/fault-inject.h> #include <asm/tlbflush.h> #include "hugetlb_vmemmap.h"
@@ -44,6 +45,39 @@ struct vmemmap_remap_walk {DECLARE_RWSEM (vmemmap_remap_rwsem); +#ifdef CONFIG_FAIL_HUGETLB_VMEMMAP +static DECLARE_FAULT_ATTR (fail_hugetlb_vmemmap_pte); + +static int __init setup_fail_hugetlb_vmemmap_pte (char *str) +{ +return setup_fault_attr (&fail_hugetlb_vmemmap_pte, str); +} +__setup ("fail_hugetlb_vmemmap_pte=" , setup_fail_hugetlb_vmemmap_pte); + +#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS +static int __init fail_hugetlb_vmemmap_debugfs (void ) +{ +fault_create_debugfs_attr ("fail_hugetlb_vmemmap_pte" ,NULL , + &fail_hugetlb_vmemmap_pte); +return 0 ; +} +late_initcall (fail_hugetlb_vmemmap_debugfs); +#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */ + +/* +* Inject failures as if the in-place update lost a race too many times +* (see the arm64 implementations), without touching the page tables. +*/ +static int hvo_update_vmemmap_pte (unsigned long addr,pte_t *ptep,pte_t pte) +{ +if (should_fail (&fail_hugetlb_vmemmap_pte,PAGE_SIZE )) +return -EAGAIN ; +return try_update_vmemmap_pte (addr, ptep, pte); +} +#else +#define hvo_update_vmemmap_pte try_update_vmemmap_pte +#endif /* CONFIG_FAIL_HUGETLB_VMEMMAP */ +static int vmemmap_split_pmd (pmd_t *pmd,struct page *head,unsigned long start,struct vmemmap_remap_walk *walk) {
@@ -274,7 +308,7 @@ static int vmemmap_optimize_pte(pte_t *pte, unsigned long addr,* after it. The refcount is zero, so the cmpxchg fails and leaves the * page unchanged. */ - ret =try_update_vmemmap_pte (addr, pte,mk_pte (dst,PAGE_KERNEL )); + ret =hvo_update_vmemmap_pte (addr, pte,mk_pte (dst,PAGE_KERNEL ));if (ret)return ret;
@@ -313,7 +347,7 @@ static int vmemmap_restore_pte(pte_t *pte, unsigned long addr,return 0 ; dst =vmemmap_dup_page_from_list (src, walk->vmemmap_pages); - ret =try_update_vmemmap_pte (addr, pte,mk_pte (dst,PAGE_KERNEL )); + ret =hvo_update_vmemmap_pte (addr, pte,mk_pte (dst,PAGE_KERNEL ));if (ret)return ret;
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
PATCH v3 14/19 selftests/mm: Add HugeTLB vmemmap optimization stress test
In reply to James HoughtonAdd a script that stresses HugeTLB vmemmap optimization (HVO), in particular on architectures that update the vmemmap in place while it may be concurrently accessed:
- Check that optimizing N folios frees exactly N * (vmemmap pages per folio - 1) vmemmap pages (per nr_memmap_pages and nr_memmap_boot_pages), and that restoring them gives them back.
- Repeatedly optimize and restore folios by resizing the hugepage pool, while concurrently reading struct pages through /proc/kpageflags and /proc/kpagecount, compacting memory, and optionally reading page_owner and offlining/onlining memory blocks.
- Do the same with fail_hugetlb_vmemmap_pte fault injection enabled, if available, to exercise the rollback and partially-optimized folio paths.
After each phase, the pool must shrink back to its original size and the memmap accounting must return to its baseline. Finally, the kernel log must not contain warnings or oopses.
--- tools/testing/selftests/mm/Makefile | 2 + .../selftests/mm/hugetlb_vmemmap_stress.sh | 380 ++++++++++++++++++ .../selftests/mm/ksft_hugetlb_vmemmap.sh | 4 + tools/testing/selftests/mm/run_vmtests.sh | 4 + 4 files changed, 390 insertions(+) create mode 100755 tools/testing/selftests/mm/hugetlb_vmemmap_stress.sh create mode 100755 tools/testing/selftests/mm/ksft_hugetlb_vmemmap.sh
tools/testing/selftests/mm/Makefile +2 -0
@@ -150,6 +150,7 @@ TEST_PROGS += ksft_cow.sh TEST_PROGS += ksft_gup_test.sh TEST_PROGS += ksft_hmm.sh TEST_PROGS += ksft_hugetlb.sh +TEST_PROGS += ksft_hugetlb_vmemmap.sh TEST_PROGS += ksft_hugevm.sh TEST_PROGS += ksft_kmemleak_confirm.sh TEST_PROGS += ksft_kmemleak_dedup.sh
@@ -182,6 +183,7 @@ TEST_FILES += test_hmm.sh TEST_FILES += va_high_addr_switch.sh TEST_FILES += charge_reserved_hugetlb.sh TEST_FILES += hugetlb_reparenting_test.sh +TEST_FILES += hugetlb_vmemmap_stress.sh TEST_FILES += test_page_frag.sh TEST_FILES += run_vmtests.sh
tools/testing/selftests/mm/hugetlb_vmemmap_stress.sh new file +380 -0
380 lines not on this page · raw message
tools/testing/selftests/mm/ksft_hugetlb_vmemmap.sh new file +4 -0
@@ -0,0 +1,4 @@ +#!/bin/sh -e +# SPDX-License-Identifier: GPL-2.0 + +./run_vmtests.sh -t hugetlb_vmemmap
tools/testing/selftests/mm/run_vmtests.sh +4 -0
@@ -79,6 +79,8 @@ separated by spaces: test transparent huge pages - hugetlb test hugetlbfs huge pages +- hugetlb_vmemmap + test the hugetlb vmemmap optimization - migration invoke move_pages(2) to exercise the migration entry code paths in the kernel
@@ -327,6 +329,8 @@ echo "$enable_soft_offline" > /proc/sys/vm/enable_soft_offline CATEGORY="hugetlb" run_test ./hugetlb-read-hwpoison fi +CATEGORY="hugetlb_vmemmap" run_test ./hugetlb_vmemmap_stress.sh + if [ $VADDR64 -ne 0 ]; then # va high address boundary switch test CATEGORY="hugevm" run_test bash ./va_high_addr_switch.sh
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
[email protected]
AI reviewRe: [PATCH v3 14/19] selftests/mm: Add HugeTLB vmemmap optimization stress test
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] selftests/mm: offlined memory blocks not re-onlined in stress_phase() - [Low] selftests/mm…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] selftests/mm: offlined memory blocks not re-onlined in stress_phase()
- [Low] selftests/mm: unchecked mktemp failure in hugetlb_vmemmap_stress.sh
--
commit 15090370906fbd4c082668bfc18a1ce3c873bcb2
Author: James Houghton <[email protected]>
selftests/mm: Add HugeTLB vmemmap optimization stress test
This commit adds a kselftest script that stresses HugeTLB vmemmap optimization (HVO) across accounting, concurrent access, and fault-injection phases.
Quoted 5 lines
tools/testing/selftests/mm/hugetlb_vmemmap_stress.sh new file +0
-0
[ ... ]
tools/testing/selftests/mm/hugetlb_vmemmap_stress.sh line 104
Quoted 5 lines
tools/testing/selftests/mm/hugetlb_vmemmap_stress.sh new file +5
-0+orig_nr=$(cat "$hp_dir/nr_hugepages") +target_nr=$(( orig_nr + nr )) +marker="hvo-stress-$$-$(date +%s)" +tmpdir=$(mktemp -d) +pids=()
[Severity: Low]
What happens if mktemp -d fails here? Because only set -u is active, tmpdir becomes empty and hotplugger() appends to /hotplug in the root directory (which cleanup() does not remove).
[ ... ]
Quoted 20 lines
tools/testing/selftests/mm/hugetlb_vmemmap_stress.sh new file +20
-0+hotplugger() { + local blk state removable + + while :; do + for blk in /sys/devices/system/memory/memory*; do + state=$(cat "$blk/state" 2>/dev/null) + removable=$(cat "$blk/removable" 2>/dev/null || echo 1) + [ "$state" = online ] || continue + [ "$removable" = 1 ] || continue + echo "$blk" >> "$tmpdir/hotplug" + # A signal aborts a pending offline_pages(). Use + # --foreground so that the writer stays in the worker's + # process group and is killed by stop_workers(). + timeout --foreground 10 \ + sh -c "echo offline > $blk/state" 2>/dev/null + echo online > "$blk/state" 2>/dev/null + sleep 1 + done + done +}
[ ... ]
Quoted 40 lines
tools/testing/selftests/mm/hugetlb_vmemmap_stress.sh new file +40
-0+cleanup() { + local blk t + + stop_workers + for t in fail_hugetlb_vmemmap_pte; do + [ -d "$(fa_dir $t)" ] && fa_disable $t > /dev/null + done + if [ -f "$tmpdir/hotplug" ]; then + sort -u "$tmpdir/hotplug" | while read -r blk; do + [ "$(cat "$blk/state")" = online ] || + echo online > "$blk/state" 2>/dev/null + done + fi + # drain() also frees free surplus pages left by failed restores. + drain > /dev/null + rm -rf "$tmpdir" +} +trap cleanup EXIT +trap 'exit $KSFT_FAIL' INT TERM + +# Run a stress phase. $1: name, $2: fault attr to enable ("" for none). +stress_phase() { + local name=$1 fa=$2 base after injected + + if [ -n "$fa" ] && [ ! -d "$(fa_dir "$fa")" ]; then + echo "SKIP: $name (no $(fa_dir "$fa"))" + return + fi + + log "phase $name: ${duration}s" + base=$(memmap_total) + [ -n "$fa" ] && fa_enable "$fa" + start_workers + sleep "$duration" + stop_workers + if [ -n "$fa" ]; then + injected=$(fa_disable "$fa") + log "$name: injected $injected failures" + [ "$injected" -gt 0 ] || + log "WARNING: $name: no failures injected"
tools/testing/selftests/mm/hugetlb_vmemmap_stress.sh line 332
Quoted 10 lines
tools/testing/selftests/mm/hugetlb_vmemmap_stress.sh new file +10
-0+ fi + + drain || return + after=$(memmap_total) + if [ "$after" -ne "$base" ]; then + fail "$name: memmap pages $after after drain, expected $base" + else + pass "$name" + fi +}
[Severity: Medium]
Should memory blocks recorded in $tmpdir/hotplug be re-onlined at the end of each stress_phase() (or in stop_workers()) rather than only in cleanup() on script exit?
If SIGTERM from stop_workers() terminates hotplugger() after offlining a block and before "echo online > $blk/state" runs, that block stays offline during drain(), the post-phase memmap_total() check, and any subsequent phase. When page_ext is active (such as with -o and -m together), offlining a block frees its section page_ext table and decrements nr_memmap_pages until the block is re-onlined:
mm/page_ext.c:free_page_ext() {
...
table_size = page_ext_size * PAGES_PER_SECTION;
memmap_pages_add(-1L * (DIV_ROUND_UP(table_size, PAGE_SIZE)));
...
}As a result, stress_phase() reads a lower memmap_total() than base and reports a false accounting failure.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
PATCH v3 15/19 hugetlb_vmemmap: Use try_populate_vmemmap_pmd for replacing in-use PMDs
In reply to James HoughtonThis routine is to be used for updating in-use, leaf-level PMDs in the vmemmap without introducing a window where vmemmap accesses might fault.
Because try_populate_vmemmap_pmd() can fail, the split_page() call needs to be moved to after the PMD update. Because both split_page() and the PMD update are done under the page table lock, this rearrangement is safe.
Only leaf-level PMD to non-leaf PMD (mapping the same physical pages) is the only transition that needs to be supported.
Collapsing a non-leaf PMD to a leaf-level PMD is not used by HVO, so try_populate_vmemmap_pmd() need not support it.
This patch allows arm64's implementation to replaced with one that does not require BBML3.
--- arch/arm64/include/asm/pgalloc.h | 8 ++++++++ arch/loongarch/include/asm/pgalloc.h | 2 ++ arch/riscv/include/asm/pgalloc.h | 2 ++ arch/x86/include/asm/pgalloc.h | 2 ++ include/linux/pgalloc.h | 21 +++++++++++++++++++++ mm/hugetlb_vmemmap.c | 25 ++++++++++++++----------- 6 files changed, 49 insertions(+), 11 deletions(-)
arch/arm64/include/asm/pgalloc.h +8 -0
@@ -121,4 +121,12 @@ pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep) PMD_TYPE_TABLE | PMD_TABLE_AF | PMD_TABLE_PXN); } +static inline int try_populate_vmemmap_pmd (unsigned long addr,pmd_t *pmdp, +pte_t *pgtable) +{ +/* BBLM3 is required. Its presence has been checked. */ +pmd_populate_kernel (&init_mm, pmdp, pgtable); +return 0 ; +} +#endif
arch/loongarch/include/asm/pgalloc.h +2 -0
@@ -21,6 +21,8 @@ static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd, pgtable_t pte)set_pmd (pmd,__pmd ((unsigned long )page_address (pte))); } +#define ARCH_WANTS_GENERIC_POPULATE_VMEMMAP_PMD +#ifndef __PAGETABLE_PMD_FOLDEDstatic inline void pud_populate (struct mm_struct *mm,pud_t *pud,pmd_t *pmd)
arch/riscv/include/asm/pgalloc.h +2 -0
@@ -31,6 +31,8 @@ static inline void pmd_populate(struct mm_struct *mm,set_pmd (pmd,__pmd ((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE)); } +#define ARCH_WANTS_GENERIC_POPULATE_VMEMMAP_PMD +#ifndef __PAGETABLE_PMD_FOLDEDstatic inline void pud_populate (struct mm_struct *mm,pud_t *pud,pmd_t *pmd) {
arch/x86/include/asm/pgalloc.h +2 -0
@@ -82,6 +82,8 @@ static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd,set_pmd (pmd,__pmd (((pteval_t )pfn <<PAGE_SHIFT ) | _PAGE_TABLE)); } +#define ARCH_WANTS_GENERIC_POPULATE_VMEMMAP_PMD +#if CONFIG_PGTABLE_LEVELS >2 extern void ___pmd_free_tlb (struct mmu_gather *tlb,pmd_t *pmd);
include/linux/pgalloc.h +21 -0
@@ -26,4 +26,25 @@arch_sync_kernel_mappings (addr, addr); \ } while (0) +#ifdef ARCH_WANTS_GENERIC_POPULATE_VMEMMAP_PMD +/* +* try_populate_vmemmap_pmd - Populate a PMD that is in use by the vmemmap. +* @addr: Base address of the remapped PMD. +* @pmdp: Page table pointer to be overwritten. +* @pgtable: Pointer to the page table that the new PMD will point to. +* +* This function is only to be used to update PMDs that map the vmemmap to +* point to a page of already-populated PTEs that map the same pages. +* +* Implementations of this function must ensure that, while the update is taking +* place, CPUs will not fault on the remapped virtual address range. +*/ +static inline int try_populate_vmemmap_pmd (unsigned long addr,pmd_t *pmdp, +pte_t *pgtable) +{ +pmd_populate_kernel (&init_mm, pmdp, pgtable); +return 0 ; +} +#endif +#endif /* _LINUX_PGALLOC_H */
mm/hugetlb_vmemmap.c +14 -11
@@ -82,6 +82,7 @@ static int vmemmap_split_pmd(pmd_t *pmd, struct page *head, unsigned long start, struct vmemmap_remap_walk *walk) {pmd_t __pmd; +int ret;int i;unsigned long addr = start;pte_t *pgtable;
@@ -101,8 +102,15 @@ static int vmemmap_split_pmd(pmd_t *pmd, struct page *head, unsigned long start,set_pte_at (&init_mm, addr, pte, entry); } + ret =0 ;spin_lock (&init_mm.page_table_lock);if (likely (pmd_leaf (*pmd))) { +/* Make pte visible before pmd. See comment in pmd_install(). */ +smp_wmb (); + ret =try_populate_vmemmap_pmd (start, pmd, pgtable); +if (ret) + goto free; +/* * Higher order allocations from buddy allocator must be able to * be treated as independent small pages (as they can be freed
@@ -111,13 +119,6 @@ static int vmemmap_split_pmd(pmd_t *pmd, struct page *head, unsigned long start,if (!PageReserved (head))split_page (head,get_order (PMD_SIZE )); -/* Make pte visible before pmd. See comment in pmd_install(). */ -smp_wmb (); -/* -* On arm64, this requires BBML3. Its support has already been -* checked. -*/ -pmd_populate_kernel (&init_mm, pmd, pgtable);/* * Modern MMUs allow overlapping TLB entries with different mapping * granularities only if they map the same physical address. So
@@ -125,12 +126,14 @@ static int vmemmap_split_pmd(pmd_t *pmd, struct page *head, unsigned long start,*/ if (walk->remap_pte || !(walk->flags &VMEMMAP_DEFER_TLB_FLUSH ))flush_tlb_kernel_range (start, start +PMD_SIZE ); - }else { -pte_free_kernel (&init_mm, pgtable); + goto out; } -spin_unlock (&init_mm.page_table_lock); -return 0 ; +free: +pte_free_kernel (&init_mm, pgtable); +out: +spin_unlock (&init_mm.page_table_lock); +return ret; } static int vmemmap_pmd_entry(pmd_t *pmd, unsigned long addr,
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
PATCH v3 16/19 arm64: Implement try_populate_vmemmap_pmd using AF trick
In reply to James HoughtonThis routine is to be used for updating in-use, leaf-level PMDs in the vmemmap without introducing a window where vmemmap accesses might fault.
Implementing this on arm64 requires some care: use the same access flag trick that is used for vmemmap PTE updates. HAFT is not needed and the TLB flushing routine remains identical, as we are not overwriting a non-leaf PMD.
For systems that support BBML3, there is no need to use the AF trick.
--- arch/arm64/include/asm/pgalloc.h | 51 ++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 3 deletions(-)
arch/arm64/include/asm/pgalloc.h +48 -3
@@ -124,9 +124,54 @@ pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep)static inline int try_populate_vmemmap_pmd (unsigned long addr,pmd_t *pmdp,pte_t *pgtable) { -/* BBLM3 is required. Its presence has been checked. */ -pmd_populate_kernel (&init_mm, pmdp, pgtable); -return 0 ; +const int max_attempts =16 ; +int attempts =0 ; +pmd_t old_pmd, new_pmd; + +if (!system_supports_bbm_through_af ()) +return -EOPNOTSUPP ; + +if (system_supports_bbml3 ()) { +/* +* BBML3 allows block->table transitions if the PTEs underneath +* do not conflict with existing, potentially cached +* translations. +*/ +pmd_populate_kernel (&init_mm, pmdp, pgtable); +return 0 ; + } + + new_pmd =__pmd (__phys_to_pmd_val (__pa (pgtable)) | +PMD_TYPE_TABLE |PMD_TABLE_AF |PMD_TABLE_UXN ); + + old_pmd =pmdp_get (pmdp); + +do { +if (WARN_ON_ONCE (!pmd_valid (old_pmd))) +return -EINVAL ; + +if (WARN_ON_ONCE (!pmd_leaf (old_pmd))) +return -EINVAL ; + +/* We should never get a contiguous PMD here. */ +if (WARN_ON_ONCE (pmd_cont (old_pmd))) +return -EINVAL ; + +if (pmd_young (old_pmd)) { +/* __ptep_clear_young() returns the overwritten PTE */ + old_pmd =pte_pmd (pte_mkold (__ptep_clear_young ((pte_t *)pmdp))); + +flush_tlb_kernel_range (addr, addr +PMD_SIZE ); + } +/* +* Translations without AF cannot be cached, so we can replace +* them without BBM. +*/ + }while (!try_cmpxchg_relaxed (&pmd_val (*pmdp), &pmd_val (old_pmd), +pmd_val (new_pmd)) && + ++attempts < max_attempts); + +return attempts == max_attempts ? -EAGAIN :0 ; }#endif
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
[email protected]
AI reviewRe: [PATCH v3 16/19 DO-NOT-MERGE] arm64: Implement try_populate_vmemmap_pmd using AF trick
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] arm64/mm: over-broad TLB flush in try_populate_vmemmap_pmd() - [Low] arm64: direct AF=0…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] arm64/mm: over-broad TLB flush in try_populate_vmemmap_pmd()
- [Low] arm64: direct AF=0 block-to-table transition in try_populate_vmemmap_pmd()
--
commit 1319c4a7862ad5f3b810c4beca866ee98bd48fd7
Author: James Houghton <[email protected]>
arm64: Implement try_populate_vmemmap_pmd using AF trick
This commit implements try_populate_vmemmap_pmd() on arm64 to replace in-use leaf vmemmap PMD block entries with table entries by clearing the access flag instead of using a break-before-make sequence when BBML3 is not supported.
Quoted 40 lines
arch/arm64/include/asm/pgalloc.h +29
-3@@ -124,9 +124,54 @@ pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep)static inline int try_populate_vmemmap_pmd (unsigned long addr,pmd_t *pmdp,pte_t *pgtable) { -/* BBLM3 is required. Its presence has been checked. */ -pmd_populate_kernel (&init_mm, pmdp, pgtable); -return 0 ; +const int max_attempts =16 ; +int attempts =0 ; +pmd_t old_pmd, new_pmd; + +if (!system_supports_bbm_through_af ()) +return -EOPNOTSUPP ; + +if (system_supports_bbml3 ()) { +/* +* BBML3 allows block->table transitions if the PTEs underneath +* do not conflict with existing, potentially cached +* translations. +*/ +pmd_populate_kernel (&init_mm, pmdp, pgtable); +return 0 ; + } + + new_pmd =__pmd (__phys_to_pmd_val (__pa (pgtable)) | +PMD_TYPE_TABLE |PMD_TABLE_AF |PMD_TABLE_UXN ); + + old_pmd =pmdp_get (pmdp); + + do { + if (WARN_ON_ONCE(!pmd_valid(old_pmd))) + return -EINVAL; + + if (WARN_ON_ONCE(!pmd_leaf(old_pmd))) + return -EINVAL; +
arch/arm64/include/asm/pgalloc.h pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep) · line 165
Quoted 10 lines
arch/arm64/include/asm/pgalloc.h +10
-0+/* We should never get a contiguous PMD here. */ +if (WARN_ON_ONCE (pmd_cont (old_pmd))) +return -EINVAL ; + +if (pmd_young (old_pmd)) { +/* __ptep_clear_young() returns the overwritten PTE */ + old_pmd =pte_pmd (pte_mkold (__ptep_clear_young ((pte_t *)pmdp))); + +flush_tlb_kernel_range (addr, addr +PMD_SIZE ); + }
[Severity: Medium]
Should this pass addr + PAGE_SIZE instead of addr + PMD_SIZE to flush_tlb_kernel_range()?
Invalidating a single non-contiguous PMD block entry only needs one overlapping page-granular TLB invalidation, as in unmap_hotplug_pmd_range(). With a range of PMD_SIZE and stride of PAGE_SIZE, pages equals PTRS_PER_PTE (MAX_DVM_OPS), so __flush_tlb_range_limit_excess() returns true on CPUs without FEAT_TLBIRANGE and escalates the flush to flush_tlb_all().
arch/arm64/include/asm/pgalloc.h pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep) · line 172
Quoted 7 lines
arch/arm64/include/asm/pgalloc.h +7
-0+/* +* Translations without AF cannot be cached, so we can replace +* them without BBM. +*/ + }while (!try_cmpxchg_relaxed (&pmd_val (*pmdp), &pmd_val (old_pmd), +pmd_val (new_pmd)) && + ++attempts < max_attempts);
[Severity: Low]
As reported by Catalin Marinas on v1 [1], this rests on an architecture detail the review could not confirm from the kernel source: can a CPU walk cache or TLB retain information from a valid leaf block PMD descriptor when AF=0, or is a TLB invalidation and break-before-make sequence still required before converting a valid AF=0 block descriptor into a table descriptor without BBML3?
If it can, skipping flush_tlb_kernel_range() when pmd_young(old_pmd) is already false or directly overwriting the valid AF=0 block descriptor with new_pmd leaves stale walk-cache or TLB state active while a table entry is installed.
[1] https://lore.kernel.org/r/[email protected]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
PATCH v3 17/19 arm64: Drop BBML3 requirement for HVO
In reply to James HoughtonWith PMD updates being done with the Access Flag trick, BBML3 is no longer needed.
Dropping this requirement vastly widens the pool of systems this optimization is valid for.
--- arch/arm64/kernel/cpufeature.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
arch/arm64/kernel/cpufeature.c +4 -5
@@ -2062,12 +2062,11 @@ static bool has_bbm_through_af(const struct arm64_cpu_capabilities *caps, int sc }/* -* We need BBML3 to support Block -> Table transitions without taking -* faults, and we need HW AF support to support changing the OA without -* taking faults. has_cpuid_feature() checks HAFDBS >= AF, reading the -* local CPU's register when verifying a late CPU. +* We need HW AF support to support changing the vmemmap mapping level +* and OA without taking faults. has_cpuid_feature() checks HAFDBS >= AF, +* reading the local CPU's register when verifying a late CPU. */ -return cpu_supports_bbml3 () &&has_cpuid_feature (caps, scope); +return has_cpuid_feature (caps, scope); }static void cpu_enable_pan (const struct arm64_cpu_capabilities *__unused)
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
PATCH v3 18/19 hugetlb_vmemmap: Add fault injection for in-place vmemmap PMD splits
In reply to James HoughtonLike try_update_vmemmap_pte(), try_populate_vmemmap_pmd() may fail, e.g. on arm64 when replacing the block mapping keeps racing with hardware access flag updates. HVO handles such failures by not optimizing the folio.
Add a fault-injection capability, fail_hugetlb_vmemmap_pmd, alongside fail_hugetlb_vmemmap_pte. When a fault is injected, the PMD split fails with -EAGAIN without touching the page tables.
It can be configured through debugfs or through the fail_hugetlb_vmemmap_pmd= boot option. Since vmemmap PMDs are only split once, the boot option is the most effective way to exercise this path.
--- .../admin-guide/kernel-parameters.txt | 1 + .../fault-injection/fault-injection.rst | 8 +++++--- lib/Kconfig.debug | 4 ++-- mm/hugetlb_vmemmap.c | 20 ++++++++++++++++++- 4 files changed, 27 insertions(+), 6 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +1 -0
@@ -1695,6 +1695,7 @@ Kernel parameters fail_page_alloc= fail_skb_realloc= fail_hugetlb_vmemmap_pte= + fail_hugetlb_vmemmap_pmd= fail_make_request=[KNL] General fault injection mechanism. Format: <interval>,<probability>,<space>,<times>
Documentation/fault-injection/fault-injection.rst +5 -3
@@ -16,10 +16,11 @@ Available fault injection capabilities injects page allocation failures. (alloc_pages(), get_free_pages(), ...) -- fail_hugetlb_vmemmap_pte +- fail_hugetlb_vmemmap_pte, fail_hugetlb_vmemmap_pmd - injects failures of the in-place vmemmap PTE remaps done by HugeTLB vmemmap - optimization. (try_update_vmemmap_pte()) + injects failures of the in-place vmemmap PTE remaps and PMD splits done by + HugeTLB vmemmap optimization. (try_update_vmemmap_pte(), + try_populate_vmemmap_pmd()) - fail_usercopy
@@ -269,6 +270,7 @@ use the boot option:: failslab= fail_page_alloc= fail_hugetlb_vmemmap_pte= + fail_hugetlb_vmemmap_pmd= fail_usercopy= fail_make_request= fail_futex=
lib/Kconfig.debug +2 -2
@@ -2104,8 +2104,8 @@ config FAIL_HUGETLB_VMEMMAP help Provide fault-injection capability for the in-place vmemmap page table updates done by HugeTLB vmemmap optimization (HVO), i.e. - try_update_vmemmap_pte(). This exercises the rollback and - partially-optimized folio paths. + try_update_vmemmap_pte() and try_populate_vmemmap_pmd(). This + exercises the rollback and partially-optimized folio paths. config FAULT_INJECTION_USERCOPY bool "Fault injection capability for usercopy functions"
mm/hugetlb_vmemmap.c +19 -1
@@ -47,6 +47,7 @@ DECLARE_RWSEM(vmemmap_remap_rwsem);#ifdef CONFIG_FAIL_HUGETLB_VMEMMAP static DECLARE_FAULT_ATTR (fail_hugetlb_vmemmap_pte); +static DECLARE_FAULT_ATTR (fail_hugetlb_vmemmap_pmd);static int __init setup_fail_hugetlb_vmemmap_pte (char *str) {
@@ -54,11 +55,19 @@ static int __init setup_fail_hugetlb_vmemmap_pte(char *str) }__setup ("fail_hugetlb_vmemmap_pte=" , setup_fail_hugetlb_vmemmap_pte); +static int __init setup_fail_hugetlb_vmemmap_pmd (char *str) +{ +return setup_fault_attr (&fail_hugetlb_vmemmap_pmd, str); +} +__setup ("fail_hugetlb_vmemmap_pmd=" , setup_fail_hugetlb_vmemmap_pmd); +#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS static int __init fail_hugetlb_vmemmap_debugfs (void ) {fault_create_debugfs_attr ("fail_hugetlb_vmemmap_pte" ,NULL , &fail_hugetlb_vmemmap_pte); +fault_create_debugfs_attr ("fail_hugetlb_vmemmap_pmd" ,NULL , + &fail_hugetlb_vmemmap_pmd);return 0 ; }late_initcall (fail_hugetlb_vmemmap_debugfs);
@@ -74,8 +83,17 @@ static int hvo_update_vmemmap_pte(unsigned long addr, pte_t *ptep, pte_t pte)return -EAGAIN ;return try_update_vmemmap_pte (addr, ptep, pte); } + +static int hvo_populate_vmemmap_pmd (unsigned long addr,pmd_t *pmdp, +pte_t *pgtable) +{ +if (should_fail (&fail_hugetlb_vmemmap_pmd,PMD_SIZE )) +return -EAGAIN ; +return try_populate_vmemmap_pmd (addr, pmdp, pgtable); +}#else #define hvo_update_vmemmap_pte try_update_vmemmap_pte +#define hvo_populate_vmemmap_pmd try_populate_vmemmap_pmd#endif /* CONFIG_FAIL_HUGETLB_VMEMMAP */ static int vmemmap_split_pmd(pmd_t *pmd, struct page *head, unsigned long start,
@@ -107,7 +125,7 @@ static int vmemmap_split_pmd(pmd_t *pmd, struct page *head, unsigned long start,if (likely (pmd_leaf (*pmd))) {/* Make pte visible before pmd. See comment in pmd_install(). */ smp_wmb (); - ret =try_populate_vmemmap_pmd (start, pmd, pgtable); + ret =hvo_populate_vmemmap_pmd (start, pmd, pgtable);if (ret) goto free;
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
PATCH v3 19/19 selftests/mm: Add HVO pmd-split fault injection tests
In reply to James HoughtonRun the stress test with PMD splitting failure injection enabled (if supported). Keep in mind that vmemmap PMDs are only ever split, never collapsed back to PMDs, so run this step first, before the main stress test.
--- tools/testing/selftests/mm/hugetlb_vmemmap_stress.sh | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
tools/testing/selftests/mm/hugetlb_vmemmap_stress.sh +8 -4
@@ -4,13 +4,16 @@ # Stress test for HugeTLB vmemmap optimization (HVO). # # Phases: -# 1. accounting: allocate N hugepages, check that nr_memmap_pages + +# 1. pmd-inject: churn the pool with fail_hugetlb_vmemmap_pmd enabled. +# Runs first because vmemmap PMDs are only split once, so later phases +# would leave few PMD splits to fail. +# 2. accounting: allocate N hugepages, check that nr_memmap_pages + # nr_memmap_boot_pages drops by exactly N * (vmemmap pages per folio - 1), # then free them and check it returns to the baseline. -# 2. stress: churn the pool (optimize/restore) while concurrently reading +# 3. stress: churn the pool (optimize/restore) while concurrently reading # struct pages (/proc/kpageflags, /proc/kpagecount), compacting memory, # and optionally reading page_owner and offlining/onlining memory. -# 3. pte-inject: like 2, with fail_hugetlb_vmemmap_pte enabled; this hits +# 4. pte-inject: like 2, with fail_hugetlb_vmemmap_pte enabled; this hits # both the optimize rollback and the restore (partial HVO) paths. # # After each phase the pool is drained back to its original size, memmap
@@ -284,7 +287,7 @@ cleanup() { local blk t stop_workers - for t in fail_hugetlb_vmemmap_pte; do + for t in fail_hugetlb_vmemmap_pte fail_hugetlb_vmemmap_pmd; do [ -d "$(fa_dir $t)" ] && fa_disable $t > /dev/null done if [ -f "$tmpdir/hotplug" ]; then
@@ -366,6 +369,7 @@ accounting_phase() { echo "$marker" > /dev/kmsg +stress_phase pmd-inject fail_hugetlb_vmemmap_pmd accounting_phase stress_phase stress "" stress_phase pte-inject fail_hugetlb_vmemmap_pte
Signature · 1 line
2.56.1.809.ga0b61035b5-goog