Another attempt at HVO support on arm64
Subsystems (10)
v4 cover Another attempt at HVO support on arm64
Hi everyone,
This is v4 of the series to enable HVO on arm64.
This version of the series is based on mm-new (872ceec1e3ca).
I've uploaded the series here as well:
http://github.com/48ca/linux hvo-v4
As with last time, the last 5 patches are optional, and I've tagged them as RFC, 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 v3:
- bbm_through_af is now an ARM64_CPUCAP_EARLY_LOCAL_CPU_FEATURE instead of a system feature. In v3, BBML3 support was only checked on the CPU that finalized the system capabilities, so a system where only some CPUs support BBML3 could have enabled HVO. Now every early CPU is checked, and late CPUs that lack the feature are rejected. (Thanks Catalin and Sashiko!)
- Renamed try_update_vmemmap_pte() to vmemmap_update_pte(), try_populate_vmemmap_pmd() to vmemmap_update_pmd(), and arch_support_vmemmap_remap() to arch_supports_vmemmap_remap(). The generic vmemmap_update_pte() and vmemmap_update_pmd() are now opt-out (architectures override them by defining the macro) instead of opt-in, so x86, riscv, and LoongArch no longer need to define ARCH_WANTS_GENERIC_POPULATE_VMEMMAP_{PTE,PMD}. (Thanks Muchun!)
- Dropped "hugetlb_vmemmap: Expose whether HVO is enabled to architecture code". Instead, arm64 now parses hugetlb_free_vmemmap= itself with its own early_param(), so that vmemmap_optimize_enabled can stay private to HugeTLB. (Thanks Muchun!)
And some smaller changes:
- vmemmap_remap_range() now only flushes the part of the range that was actually remapped, and doesn't flush at all if nothing was remapped. (Thanks Sashiko!)
- hugetlb_vmemmap_restore_folios() now also flushes the TLB when restoring a folio fails partway through. (Thanks Sashiko!)
- "hugetlb: Optimize bootmem folios that were not pre-HVOed" now restores the early return in hugetlb_vmemmap_optimize_folios() when there is nothing to optimize, and its comment is condensed. (Thanks Muchun and Sashiko!)
- The arm64 BUILD_BUG_ON() on VMEMMAP_START alignment is now guarded by compound_info_has_mask(). (Thanks Muchun!)
- Removed references to "pre-HVO" from the arm64 vmemmap_populate() comment and dropped an arm64-specific comment from vmemmap_split_pmd(). (Thanks Muchun!)
- New arch_supports_vmemmap_remap() doc comment. (Thanks Muchun!)
- The arm64 vmemmap_update_pmd() now invalidates a single page when clearing the AF on a block entry, instead of PMD_SIZE, which could escalate to flush_tlb_all() without FEAT_TLBIRANGE. (Thanks Sashiko!)
- The changelog for "arm64: Support hugetlb vmemmap optimization" now explains that BBML3 is needed because HVO shatters in-use vmemmap PMD block mappings.
- Selftest: re-online memory blocks at the end of every stress phase instead of only on exit, and fail if mktemp -d fails. (Thanks Sashiko!)
I have not changed vmemmap_update_pte() to return void, as Muchun suggested; please see my reply from v2[2]. I'd appreciate any thoughts from the arm64 folks on this.
v3: https://lore.kernel.org/linux-mm/[email protected]/
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 versions, it was used to draft the selftests, and in this version, it helped address review feedback.
Thanks!
And now for the rest of the background of these patches, updated from v3'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-7).
3. arm64 changes to enable HVO (patches 8-11).
4. Fault injection and a stress test (patches 12-13).
5. Drop the BBML3 requirement for HVO (patches 14-18).
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 an early local CPU feature (called "BBM through AF"). It is checked on every early CPU. If not all early CPUs support the feature, attempts to HVO pages will fail, and the HVO sysctl will be hidden. If it is enabled, late CPUs that do not support it are rejected.
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 CPU features are detected, 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/CADrL8HWNaurYHGnpvC9qwr6bVU74YhhucCuhoVYcvqmSX+R6+Q@mail.gmail.com/
James Houghton (18):
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 vmemmap_update_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
arm64: Add bbm_through_af capability
arm64: Implement vmemmap_update_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 vmemmap_update_pmd for replacing in-use PMDs
arm64: Implement vmemmap_update_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 | 61 +++ arch/arm64/include/asm/pgtable.h | 74 +++- arch/arm64/kernel/cpufeature.c | 48 +++ arch/arm64/mm/init.c | 18 +- arch/arm64/mm/mmu.c | 9 + arch/arm64/tools/cpucaps | 1 + fs/Kconfig | 3 +- include/linux/pgalloc.h | 23 + include/linux/pgtable.h | 39 ++ lib/Kconfig.debug | 9 + mm/Kconfig | 7 + mm/hugetlb.c | 7 + mm/hugetlb_vmemmap.c | 170 ++++++-- mm/hugetlb_vmemmap.h | 2 + tools/testing/selftests/mm/Makefile | 2 + .../selftests/mm/hugetlb_vmemmap_stress.sh | 392 ++++++++++++++++++ .../selftests/mm/ksft_hugetlb_vmemmap.sh | 4 + tools/testing/selftests/mm/run_vmtests.sh | 4 + 23 files changed, 853 insertions(+), 40 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: 872ceec1e3caf227655a58f7407a9c8493dcf56f
Signature · 1 line
2.56.1.839.g22a8290db9-goog
PATCH v4 1/18 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.839.g22a8290db9-goog
PATCH v4 2/18 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 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
mm/hugetlb_vmemmap.c +5 -5
@@ -163,13 +163,13 @@ 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); +if (walk->remap_pte && walk->nr_walked && + !(walk->flags &VMEMMAP_DEFER_TLB_FLUSH )) +flush_tlb_kernel_range (start, + start + walk->nr_walked *PAGE_SIZE ); -return 0 ; +return ret; }/*
Signature · 1 line
2.56.1.839.g22a8290db9-goog
[email protected]
AI reviewRe: [PATCH v4 02/18] hugetlb_vmemmap: Always flush TLB if needed upon PTE remapping
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mm/hugetlb_vmemmap: partial HVO state retained in vmemmap_remap_range() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mm/hugetlb_vmemmap: partial HVO state retained in vmemmap_remap_range()
--
commit c2897cd265b1fd91de3160f5c85081502ce0ae01
Author: James Houghton <[email protected]>
hugetlb_vmemmap: Always flush TLB if needed upon PTE remapping
This commit updates vmemmap_remap_range() to flush the TLB for the walked subrange even when walk_kernel_page_table_range_lockless() returns an error after partially remapping the vmemmap.
Quoted 11 lines
mm/hugetlb_vmemmap.c +0
-2@@ -163,13 +163,13 @@ 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;
mm/hugetlb_vmemmap.c static int vmemmap_remap_range(unsigned long start, unsigned long end, · line 173
Quoted 10 lines
mm/hugetlb_vmemmap.c +5
-3-if (walk->remap_pte && !(walk->flags &VMEMMAP_DEFER_TLB_FLUSH )) -flush_tlb_kernel_range (start, end); +if (walk->remap_pte && walk->nr_walked && + !(walk->flags &VMEMMAP_DEFER_TLB_FLUSH )) +flush_tlb_kernel_range (start, + start + walk->nr_walked *PAGE_SIZE ); -return 0 ; +return ret; }
[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?
Subsequent patches in the series still cap vmemmap_update_pte() to 16 cmpxchg attempts and leave folios in a partially optimized state (VMEMMAP_REMAP_INCOMPLETE) when PTE updates or rollback restorations fail, rather than making vmemmap_update_pte() retry until it succeeds so partial optimization handling is unnecessary.
[1] https://lore.kernel.org/r/[email protected]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v4 3/18 hugetlb_vmemmap: Leave pages partially HVOed upon restore failure
In reply to James HoughtonWhen vmemmap_optimize_folio() fails partway through, it rolls back the PTEs it has already remapped with vmemmap_restore_range(). Right now it is assumed that this rollback will not fail; that is, if we fail to fully optimize a folio, it is assumed that restoring the folio's vmemmap 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. vmemmap_optimize_folio() now returns VMEMMAP_REMAP_INCOMPLETE in this case.
2. Free any replacement vmemmap pages that vmemmap_restore_folio() did not consume.
Because a folio may now be only partially optimized, vmemmap_restore_pte() only restores PTEs that still map the shared tail page (as returned by folio_vmemmap_shared_tail_page()), instead of warning about PTEs that map anything else.
--- mm/hugetlb_vmemmap.c | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-)
mm/hugetlb_vmemmap.c +28 -6
@@ -276,12 +276,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 ));
@@ -327,6 +337,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.
@@ -342,7 +353,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)
@@ -374,7 +386,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; }
@@ -404,7 +417,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 );
@@ -528,7 +546,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.839.g22a8290db9-goog
PATCH v4 4/18 hugetlb_vmemmap: Use vmemmap_update_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, vmemmap_update_pte().
vmemmap_update_pte() is used for modifying page table entries such that there is a guarantee that no fault will be taken.
The generic implementation still uses set_pte_at(). Architectures that cannot safely update in-use vmemmap PTEs with it must override vmemmap_update_pte().
--- include/linux/pgtable.h | 23 +++++++++++++++++++++++ mm/hugetlb_vmemmap.c | 35 +++++++++++++++++++++++++++++------ 2 files changed, 52 insertions(+), 6 deletions(-)
include/linux/pgtable.h +23 -0
@@ -458,6 +458,29 @@ 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) +#ifndef vmemmap_update_pte +/* +* vmemmap_update_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. Architectures on +* which set_pte_at() does not guarantee this must override this function. +*/ +static inline int vmemmap_update_pte (unsigned long addr,pte_t *ptep, +pte_t pte) +{ +set_pte_at (&init_mm, addr, 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 +29 -6
@@ -220,15 +220,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 vmemmap_update_pte(). */ smp_wmb ();return dst;
@@ -250,6 +254,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);
@@ -257,16 +263,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 =vmemmap_update_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 ; }
@@ -277,6 +294,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 ;
@@ -293,7 +311,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 =vmemmap_update_pte (addr, pte,mk_pte (dst,PAGE_KERNEL )); +if (ret) +return ret; + +list_del (&dst->lru);return 0 ; }
@@ -499,7 +521,8 @@ long hugetlb_vmemmap_restore_folios(const struct hstate *h,list_move (&folio->lru, non_hvo_folios); } -if (restored) +/* A failed restore may have remapped part of the folio. */ +if (restored || ret)flush_tlb_all ();if (!ret) ret = restored;
Signature · 1 line
2.56.1.839.g22a8290db9-goog
PATCH v4 5/18 hugetlb_vmemmap: Allow architectures to dynamically disallow HVO
In reply to James HoughtonFor arm64, only some platforms support HVO. Add an arch hook, arch_supports_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_supports_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 | 16 ++++++++++++++++ mm/hugetlb_vmemmap.h | 2 ++ 2 files changed, 18 insertions(+)
include/linux/pgtable.h +16 -0
@@ -481,6 +481,22 @@ static inline int vmemmap_update_pte(unsigned long addr, pte_t *ptep, }#endif +#ifndef arch_supports_vmemmap_remap +/* +* arch_supports_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 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. +*/ +static inline bool arch_supports_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_supports_vmemmap_remap ()) +return 0 ; return size > 0 ? size : 0; }#else
Signature · 1 line
2.56.1.839.g22a8290db9-goog
PATCH v4 6/18 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_supports_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().
Also restore the early return in hugetlb_vmemmap_optimize_folios() when no folio needs to be optimized, so that systems where all bootmem folios were optimized through the section-based optimization (or where HVO is disabled) do not take vmemmap_remap_rwsem for write and flush the TLB twice for nothing.
--- mm/hugetlb.c | 7 +++++++ mm/hugetlb_vmemmap.c | 15 ++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-)
mm/hugetlb.c +7 -0
@@ -3364,6 +3364,13 @@ static void __init prep_and_add_bootmem_folios(struct hstate *h,unsigned long flags;struct folio *folio, *tmp_f; +/* +* Some architectures do not support section-based vmemmap optimization, +* so their bootmem folios should be optimized here with folio-based +* optimization. +*/ +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);
mm/hugetlb_vmemmap.c +14 -1
@@ -610,6 +610,7 @@ static int hugetlb_vmemmap_pteify_folio(const struct hstate *h, struct folio *fovoid hugetlb_vmemmap_optimize_folios (struct hstate *h,struct list_head *folio_list) {struct folio *folio; +unsigned long nr_to_optimize =0 ;LIST_HEAD (vmemmap_pages);unsigned long flags =VMEMMAP_DEFER_TLB_FLUSH ;
@@ -618,7 +619,14 @@ void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_ldown_write (&vmemmap_remap_rwsem);list_for_each_entry (folio, folio_list, lru) { -int ret =hugetlb_vmemmap_pteify_folio (h, folio, flags); +int ret; + +if (!vmemmap_should_optimize_folio (h, folio)) +continue ; + + nr_to_optimize++; + + ret =hugetlb_vmemmap_pteify_folio (h, folio, flags);/* * Splitting the PMD requires allocating a page, thus let's fail
@@ -630,6 +638,11 @@ void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_lbreak ; } +if (!nr_to_optimize) { +up_write (&vmemmap_remap_rwsem); +return ; + } +flush_tlb_all ();downgrade_write (&vmemmap_remap_rwsem);
Signature · 1 line
2.56.1.839.g22a8290db9-goog
[email protected]
AI reviewRe: [PATCH v4 06/18] hugetlb: Optimize bootmem folios that were not pre-HVOed
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mm: hugetlb_vmemmap_optimize_folios() locks rwsem when no folio is optimized --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mm: hugetlb_vmemmap_optimize_folios() locks rwsem when no folio is optimized
--
commit 871677a9222b058989a147e4c2acde5f983b9645
Author: James Houghton <[email protected]>
hugetlb: Optimize bootmem folios that were not pre-HVOed
This commit calls hugetlb_vmemmap_optimize_folios() in prep_and_add_bootmem_folios() so bootmem folios that were not pre-HVOed by section-based vmemmap optimization get optimized, and adds an early return when no folio in the list needs optimization.
Quoted 29 lines
mm/hugetlb_vmemmap.c +9
-1@@ -610,6 +610,7 @@ static int hugetlb_vmemmap_pteify_folio(const struct hstate *h, struct folio *fovoid hugetlb_vmemmap_optimize_folios (struct hstate *h,struct list_head *folio_list) {struct folio *folio; +unsigned long nr_to_optimize =0 ;LIST_HEAD (vmemmap_pages);unsigned long flags =VMEMMAP_DEFER_TLB_FLUSH ;@@ -618,7 +619,14 @@ void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_ldown_write (&vmemmap_remap_rwsem);list_for_each_entry (folio, folio_list, lru) { -int ret =hugetlb_vmemmap_pteify_folio (h, folio, flags); +int ret; + +if (!vmemmap_should_optimize_folio (h, folio)) +continue ; + + nr_to_optimize++; + + ret =hugetlb_vmemmap_pteify_folio (h, folio, flags);/* * Splitting the PMD requires allocating a page, thus let's fail
mm/hugetlb_vmemmap.c void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_l · line 644
Quoted 7 lines
mm/hugetlb_vmemmap.c +4
-0@@ -630,6 +638,11 @@ void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_lbreak ; } +if (!nr_to_optimize) { +up_write (&vmemmap_remap_rwsem); +return ; + }
[Severity: Medium]
Does hugetlb_vmemmap_optimize_folios() still take vmemmap_remap_rwsem for write when no folio needs to be optimized?
The commit message notes that this early return avoids taking vmemmap_remap_rwsem for write when all bootmem folios are already pre-HVOed or when HVO is disabled. Because down_write() happens before the loop, the per-node padata threads in hugetlb_bootmem_struct_page_init() still serialize on vmemmap_remap_rwsem while walking folio_list. Checking vmemmap_optimize_enabled and hugetlb_vmemmap_optimizable(h) upfront (or taking vmemmap_remap_rwsem lazily on the first optimizable folio) would avoid the lock acquisition.
Also, hugetlb_vmemmap_pteify_folio() checks vmemmap_should_optimize_folio() again after the loop has already checked it.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
PATCH v4 7/18 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
@@ -685,7 +685,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.839.g22a8290db9-goog
PATCH v4 8/18 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. All CPUs must support it for the kernel to use it, and BBML3 support may only be known from a CPU's MIDR, so model this as an early local CPU feature: it is checked on every early CPU, and if it is enabled, late CPUs that lack it are rejected.
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.
To know whether HVO has been requested, parse hugetlb_free_vmemmap= here as well. Early parameters are parsed in setup_arch(), before the boot CPU's features are detected, and all early parameter handlers with a matching name are called, so this does not interfere with HugeTLB's own handling of the parameter.
--- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/cpufeature.h | 5 +++ arch/arm64/kernel/cpufeature.c | 50 +++++++++++++++++++++++++++++ arch/arm64/tools/cpucaps | 1 + 4 files changed, 57 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
@@ -914,6 +914,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 +50 -0
62 lines not on this page · raw message
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.839.g22a8290db9-goog
PATCH v4 9/18 arm64: Implement vmemmap_update_pte using the AF trick
In reply to James Houghtonvmemmap_update_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 vmemmap_update_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 | 60 +++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 4 deletions(-)
arch/arm64/include/asm/pgtable.h +56 -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,48 @@ static inline void clear_young_dirty_ptes(struct vm_area_struct *vma,#endif /* CONFIG_ARM64_CONTPTE */ +#define vmemmap_update_pte vmemmap_update_pte +static inline int vmemmap_update_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.839.g22a8290db9-goog
PATCH v4 10/18 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 (compound_info_has_mask () && + !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.839.g22a8290db9-goog
PATCH v4 11/18 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: HW AF is needed to remap in-use vmemmap PTEs with the AF trick in vmemmap_update_pte(), and BBML3 is needed because HVO shatters in-use vmemmap PMD block mappings into PTE tables (see vmemmap_split_pmd()). BBML3 allows this block -> table transition without break-before-make.
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 +++++++++ 3 files changed, 16 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_supports_vmemmap_remap arch_supports_vmemmap_remap +static inline bool arch_supports_vmemmap_remap (void ) +{ +return system_supports_bbm_through_af (); +} +#define vmemmap_update_pte vmemmap_update_ptestatic inline int vmemmap_update_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 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);
Signature · 1 line
2.56.1.839.g22a8290db9-goog
PATCH v4 12/18 hugetlb_vmemmap: Add fault injection for in-place vmemmap PTE updates
In reply to James Houghtonvmemmap_update_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
@@ -1697,6 +1697,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. (vmemmap_update_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. + vmemmap_update_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 vmemmap_update_pte (addr, ptep, pte); +} +#else +#define hvo_update_vmemmap_pte vmemmap_update_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) {
@@ -272,7 +306,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 =vmemmap_update_pte (addr, pte,mk_pte (dst,PAGE_KERNEL )); + ret =hvo_update_vmemmap_pte (addr, pte,mk_pte (dst,PAGE_KERNEL ));if (ret)return ret;
@@ -311,7 +345,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 =vmemmap_update_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.839.g22a8290db9-goog
PATCH v4 13/18 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 | 388 ++++++++++++++++++ .../selftests/mm/ksft_hugetlb_vmemmap.sh | 4 + tools/testing/selftests/mm/run_vmtests.sh | 4 + 4 files changed, 398 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 +388 -0
388 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.839.g22a8290db9-goog
RFC PATCH v4 14/18 hugetlb_vmemmap: Use vmemmap_update_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 vmemmap_update_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 vmemmap_update_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 | 9 +++++++++ include/linux/pgalloc.h | 23 +++++++++++++++++++++++ mm/hugetlb_vmemmap.c | 21 ++++++++++++++------- 3 files changed, 46 insertions(+), 7 deletions(-)
arch/arm64/include/asm/pgalloc.h +9 -0
@@ -121,4 +121,13 @@ pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep) PMD_TYPE_TABLE | PMD_TABLE_AF | PMD_TABLE_PXN); } +#define vmemmap_update_pmd vmemmap_update_pmd +static inline int vmemmap_update_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
include/linux/pgalloc.h +23 -0
@@ -26,4 +26,27 @@arch_sync_kernel_mappings (addr, addr); \ } while (0) +#ifndef vmemmap_update_pmd +/* +* vmemmap_update_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. +* Architectures on which pmd_populate_kernel() does not guarantee this must +* override this function. +*/ +static inline int vmemmap_update_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 -7
@@ -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 =vmemmap_update_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,9 +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 (); -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
@@ -121,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.839.g22a8290db9-goog
[email protected]
AI reviewRe: [RFC PATCH v4 14/18] hugetlb_vmemmap: Use vmemmap_update_pmd for replacing in-use PMDs
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] mm: pgalloc: unguarded vmemmap_update_pmd() breaks !CONFIG_MMU builds --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] mm: pgalloc: unguarded vmemmap_update_pmd() breaks !CONFIG_MMU builds
--
commit a3d7576c0c3addc7b2e755448281986800bebe5a
Author: James Houghton <[email protected]>
hugetlb_vmemmap: Use vmemmap_update_pmd for replacing in-use PMDs
This commit introduces vmemmap_update_pmd() to replace in-use vmemmap leaf PMDs with page tables in vmemmap_split_pmd() without faulting, and moves split_page() after the PMD update.
Quoted 22 lines
include/linux/pgalloc.h +14
-0@@ -26,4 +26,27 @@arch_sync_kernel_mappings (addr, addr); \ }while (0 ) +#ifndef vmemmap_update_pmd +/* +* vmemmap_update_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. +* Architectures on which pmd_populate_kernel() does not guarantee this must +* override this function.
include/linux/pgalloc.h line 52
Quoted 10 lines
include/linux/pgalloc.h +9
-0+*/ +static inline int vmemmap_update_pmd (unsigned long addr,pmd_t *pmdp, +pte_t *pgtable) +{ +pmd_populate_kernel (&init_mm, pmdp, pgtable); +return 0 ; +} +#endif +#endif /* _LINUX_PGALLOC_H */
[Severity: High]
Does defining vmemmap_update_pmd() without an #ifdef CONFIG_MMU guard break CONFIG_MMU=n builds?
Files compiled unconditionally even when CONFIG_MMU is disabled, such as mm/filemap.c and kernel/fork.c, include <linux/pgalloc.h>, whereas architectures supporting nommu (including arm, riscv, m68k and xtensa) and include/asm-generic/pgalloc.h only declare pmd_populate_kernel() under #ifdef CONFIG_MMU. Guarding vmemmap_update_pmd() with #ifdef CONFIG_MMU (matching vmemmap_update_pte() in include/linux/pgtable.h) would avoid an undeclared function error on nommu builds.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
RFC PATCH v4 15/18 arm64: Implement vmemmap_update_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 | 58 ++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 3 deletions(-)
arch/arm64/include/asm/pgalloc.h +55 -3
@@ -125,9 +125,61 @@ pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep)static inline int vmemmap_update_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))); + +/* +* A non-contiguous block entry is cached as a single +* TLB entry, and invalidating any address it maps +* removes it (see unmap_hotplug_pmd_range()). Flushing +* the whole PMD_SIZE range is unnecessary and may +* escalate to flush_tlb_all(). +*/ +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 (&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.839.g22a8290db9-goog
[email protected]
AI reviewRe: [RFC PATCH v4 15/18] arm64: Implement vmemmap_update_pmd using AF trick
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] arm64: direct AF=0 block-to-table transition in vmemmap_update_pmd() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] arm64: direct AF=0 block-to-table transition in vmemmap_update_pmd()
--
commit ef64645c78de81cfd597c18e0356d261300b74d4
Author: James Houghton <[email protected]>
arm64: Implement vmemmap_update_pmd using AF trick
This commit implements vmemmap_update_pmd() on arm64 to convert in-use leaf block PMDs in the vmemmap to table descriptors by clearing the Access Flag when BBML3 is not supported.
Quoted 54 lines
arch/arm64/include/asm/pgalloc.h +43
-3@@ -125,9 +125,61 @@ pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep)static inline int vmemmap_update_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))); + + /* + * A non-contiguous block entry is cached as a single + * TLB entry, and invalidating any address it maps + * removes it (see unmap_hotplug_pmd_range()). Flushing + * the whole PMD_SIZE range is unnecessary and may + * escalate to flush_tlb_all().
arch/arm64/include/asm/pgalloc.h pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep) · line 180
Quoted 10 lines
arch/arm64/include/asm/pgalloc.h +10
-0+*/ +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 (&pmd_val (*pmdp), &pmd_val (old_pmd), +pmd_val (new_pmd)) && + ++attempts < max_attempts);
[Severity: Low]
As reported by Catalin Marinas on v1 [1], can a CPU walk cache or TLB retain information from a valid leaf block PMD descriptor with AF=0 across a direct block-to-table overwrite without Break-Before-Make on systems without BBML3?
This question rests on an architecture detail that cannot be confirmed from the kernel source. 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, which could confuse the walk cache or trigger a TLB conflict abort during concurrent vmemmap accesses.
[1] https://lore.kernel.org/r/[email protected]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
RFC PATCH v4 16/18 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 | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
arch/arm64/kernel/cpufeature.c +3 -5
@@ -2076,12 +2076,10 @@ 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. Both are properties of the local CPU (BBML3 support -* may only be known from its MIDR), so this is checked on every CPU. +* We need HW AF support to support changing the vmemmap mapping level +* and OA without taking faults. This is checked on every 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.839.g22a8290db9-goog
RFC PATCH v4 17/18 hugetlb_vmemmap: Add fault injection for in-place vmemmap PMD splits
In reply to James HoughtonLike vmemmap_update_pte(), vmemmap_update_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
@@ -1698,6 +1698,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. (vmemmap_update_pte()) + injects failures of the in-place vmemmap PTE remaps and PMD splits done by + HugeTLB vmemmap optimization. (vmemmap_update_pte(), + vmemmap_update_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. - vmemmap_update_pte(). This exercises the rollback and - partially-optimized folio paths. + vmemmap_update_pte() and vmemmap_update_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 vmemmap_update_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 vmemmap_update_pmd (addr, pmdp, pgtable); +}#else #define hvo_update_vmemmap_pte vmemmap_update_pte +#define hvo_populate_vmemmap_pmd vmemmap_update_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 =vmemmap_update_pmd (start, pmd, pgtable); + ret =hvo_populate_vmemmap_pmd (start, pmd, pgtable);if (ret) goto free;
Signature · 1 line
2.56.1.839.g22a8290db9-goog
RFC PATCH v4 18/18 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
@@ -296,7 +299,7 @@ cleanup() { local 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 reonline_blocks
@@ -374,6 +377,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.839.g22a8290db9-goog