Another attempt at HVO support on arm64
Subsystems (8)
cover Another attempt at HVO support on arm64
Hi everyone,
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 four parts:
1. Some preparatory changes (patches 1-3)
1. Bare minimum HVO support (patches 4-10)
2. Drop BBML2_NOABORT requirement for HVO (patches 11-13)
3. Drop the user-configurable Kconfig for HVO (patches 14-18)
Parts 3 and 4 are technically optional. More details below.
The main functional caveat with this series is that bootmem HugeTLB pages are not "pre-HVOed". They will be HVOed, but because at pre-HVO time SMP CPUs have not been enabled, we cannot query for full system support.
This series is based on 7.2-rc2 (0e35b9b6ec0f).
This series almost 100% cleanly applies to mm-new, which has some of Muchun's HVO patches, with one trivial conflict. I imagine this series will conflict pretty heavily with some of Muchun's other patches[2].
-- 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 compatbility 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 PTE remains mapped normally (RW mapping to a real page of struct pages), but the remaining PTEs in the vmemmap are mapped read-only to a shared page of struct pages (that is, there is an OA change and a permissions change).
- When disabling HVO for a page, the RO PTEs are remapped back to RW PTEs that point to newly reallocated pages of struct pages. The PMD block -> table transition is not undone.
In patches 4-10 of this series, I use the Access Flag trick to do the PTE OA and permissions updates. We rely on BBML2_NOABORT for the PMD block -> table transition.
In patches 11-13, I re-use the Access Flag trick to do the PMD block -> table transition without needing BBML2_NOABORT. For systems that support BBML2_NOABORT, the logic is unchanged.
I am aware of Linu's BBML3 patches; I've opted not to rebase onto them for now, but I am happy to do so later.
-- Late-onlining of CPUs that do not support HW AF --
One of the complications with this series is how to handle late-onlining of CPUs that do not have HW AF when HVO is in use. Naively, if HVO (HW AF) is supported on all boot CPUs and the kernel is compiled with HVO support, late CPUs that do not support HVO will not be onlined. This is a regression.
This series provides two ways of dealing with this. First, add a default-off Kconfig for users to enable HVO support, avoiding the regression. This is not ideal.
Patches 14-18 get rid of the new Kconfig by allowing onlining of HVO-incompatible late CPUs as long as HVO is not actively in use.
-- 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 violate BBM requirements. *)
This test must be run with herdtools with Nikos's changes[3] to more accurately model BBM requirements. When tried, the output will notably *not* contain the "Warning-BBM-expected" flag.
-- Testing --
I haven't yet done extensive testing of this series. HVO is correctly freeing pages on my system, and the hugetlb-vmemmap test passes. Freeing HVOed HugeTLB pages also seems to function normally.
[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://github.com/herd/herdtools7/pull/1864
James Houghton (18):
hugetlb_vmemmap: Always flush TLB if needed upon PTE remapping
hugetlb_vmemmap: Move vmemmap_get_tail up
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 not to allow HVO at runtime
arm64: Rename cpu_has_hw_af to system_has_hw_af
arm64: Add system_supports_hvo
arm64: Implement try_update_vmemmap_pte using the AF trick
arm64: Prevent HVO if the HVO system feature is not enabled
arm64: Support hugetlb vmemmap optimization
hugetlb_vmemmap: Use try_populate_vmemmap_pmd for replacing in-use
PMDs
arm64: Implement try_populate_vmemmap_pmd using AF trick
arm64: Drop BBML2_NOABORT requirement for HVO
hugetlb_vmemmap: Rename mm/hugetlb_vmemmap.h to
mm/hugetlb_vmemmap_internal.h
hugetlb_vmemmap: Add a way to permanently disable HVO when needed
arm64: Allow "optional" CPU features to be required sometimes
arm64: Permit onlining of HVO-incompatible late CPUs if HVO is not in
use
arm64: Remove user-selectable HVO Kconfig
MAINTAINERS | 3 +-
arch/arm64/Kconfig | 1 +
arch/arm64/include/asm/cpucaps.h | 2 +
arch/arm64/include/asm/cpufeature.h | 39 ++-
arch/arm64/include/asm/hugetlb.h | 7 +
arch/arm64/include/asm/pgalloc.h | 54 ++++
arch/arm64/include/asm/pgtable.h | 57 ++++-
arch/arm64/kernel/cpufeature.c | 43 ++++
arch/arm64/tools/cpucaps | 1 +
arch/loongarch/include/asm/pgalloc.h | 8 +
arch/loongarch/include/asm/pgtable.h | 8 +
arch/riscv/include/asm/pgalloc.h | 8 +
arch/riscv/include/asm/pgtable.h | 8 +
arch/x86/include/asm/pgalloc.h | 8 +
arch/x86/include/asm/pgtable.h | 8 +
include/asm-generic/hugetlb.h | 7 +
include/linux/hugetlb_vmemmap.h | 20 ++
include/linux/pgalloc.h | 20 ++
include/linux/pgtable.h | 21 ++
mm/hugetlb.c | 2 +-
mm/hugetlb_sysfs.c | 2 +-
mm/hugetlb_vmemmap.c | 237 +++++++++++++-----
...b_vmemmap.h => hugetlb_vmemmap_internal.h} | 6 +-
mm/sparse-vmemmap.c | 2 +-
24 files changed, 489 insertions(+), 83 deletions(-)
create mode 100644 include/linux/hugetlb_vmemmap.h
rename mm/{hugetlb_vmemmap.h => hugetlb_vmemmap_internal.h} (95%)base-commit: 0e35b9b6ec0ffcc5e23cbdec09f5c622ad532b53
Signature · 1 line
2.55.0.795.g602f6c329a-goog
PATCH 1/18 hugetlb_vmemmap: Always flush TLB if needed upon PTE remapping
vmemmap_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
@@ -164,13 +164,11 @@ static int vmemmap_remap_range(unsigned long start, unsigned long end, ret =walk_kernel_page_table_range (start, end, &vmemmap_remap_ops,NULL , walk);mmap_read_unlock (&init_mm); -if (ret) -return ret;if (walk->remap_pte && !(walk->flags &VMEMMAP_REMAP_NO_TLB_FLUSH ))flush_tlb_kernel_range (start, end); -return 0 ; +return ret; }/*
Signature · 1 line
2.55.0.795.g602f6c329a-goog
PATCH 2/18 hugetlb_vmemmap: Move vmemmap_get_tail up
In reply to James HoughtonIn a follow-up patch, it will be used invmemmap_remap_alloc().
No functional change intended.
--- mm/hugetlb_vmemmap.c | 52 ++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 26 deletions(-)
mm/hugetlb_vmemmap.c +26 -26
@@ -355,6 +355,32 @@ static int alloc_vmemmap_page_list(unsigned long start, unsigned long end,return -ENOMEM ; } +static struct page *vmemmap_get_tail (unsigned int order,struct zone *zone) +{ +const unsigned int idx = order -VMEMMAP_TAIL_MIN_ORDER ; +struct page *tail, *p; +int node =zone_to_nid (zone); + + tail =READ_ONCE (zone->vmemmap_tails[idx]); +if (likely (tail)) +return tail; + + tail =alloc_pages_node (node,GFP_KERNEL | __GFP_ZERO,0 ); +if (!tail) +return NULL ; + + p =page_to_virt (tail); +for (int i =0 ; i <PAGE_SIZE /sizeof (struct page ); i++) +init_compound_tail (p + i,NULL , order, zone); + +if (cmpxchg (&zone->vmemmap_tails[idx],NULL , tail)) { +__free_page (tail); + tail =READ_ONCE (zone->vmemmap_tails[idx]); + } + +return tail; +} +/** * vmemmap_remap_alloc - remap the vmemmap virtual address range [@start, end) * to the page which is from the @vmemmap_pages
@@ -491,32 +517,6 @@ static bool vmemmap_should_optimize_folio(const struct hstate *h, struct folio *return true; } -static struct page *vmemmap_get_tail (unsigned int order,struct zone *zone) -{ -const unsigned int idx = order -VMEMMAP_TAIL_MIN_ORDER ; -struct page *tail, *p; -int node =zone_to_nid (zone); - - tail =READ_ONCE (zone->vmemmap_tails[idx]); -if (likely (tail)) -return tail; - - tail =alloc_pages_node (node,GFP_KERNEL | __GFP_ZERO,0 ); -if (!tail) -return NULL ; - - p =page_to_virt (tail); -for (int i =0 ; i <PAGE_SIZE /sizeof (struct page ); i++) -init_compound_tail (p + i,NULL , order, zone); - -if (cmpxchg (&zone->vmemmap_tails[idx],NULL , tail)) { -__free_page (tail); - tail =READ_ONCE (zone->vmemmap_tails[idx]); - } - -return tail; -} - static int __hugetlb_vmemmap_optimize_folio(const struct hstate *h, struct folio *folio, struct list_head *vmemmap_pages,
Signature · 1 line
2.55.0.795.g602f6c329a-goog
PATCH 3/18 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 | 49 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 11 deletions(-)
mm/hugetlb_vmemmap.c +38 -11
@@ -236,12 +236,15 @@ static void vmemmap_restore_pte(pte_t *pte, unsigned long addr, {struct page *src =pte_page (ptep_get (pte)), *dst; +if (WARN_ON_ONCE (!walk->vmemmap_tail)) +return ; +/* -* When rolling back vmemmap_remap_free(), keep the copied head page +* When restoring a partially-HVOed page, keep the copied head page * mapping and restore only PTEs currently pointing at the shared tail * page. */ -if (walk->vmemmap_tail && walk->vmemmap_tail != src) +if (walk->vmemmap_tail != src)return ;VM_WARN_ON_ONCE (PageHead ((const struct page *)addr));
@@ -277,6 +280,7 @@ static int vmemmap_remap_split(unsigned long start, unsigned long end)return vmemmap_remap_range (start, end, &walk); } +static const int VMEMMAP_REMAP_INCOMPLETE =1 ;/** * vmemmap_remap_free - remap the vmemmap virtual address range [@start, @end) * to use @vmemmap_head/tail, then free vmemmap which
@@ -291,7 +295,8 @@ static int vmemmap_remap_split(unsigned long start, unsigned long end)* responsibility to free pages. * @flags: modifications to vmemmap_remap_walk flags * -* Return: %0 on success, negative error code otherwise. +* Return: %0 on success, VMEMMAP_REMAP_INCOMPLETE if the page is incompletely +* optimized, negative error code otherwise. */ static int vmemmap_remap_free(unsigned long start, unsigned long end, struct page *vmemmap_head,
@@ -326,7 +331,8 @@ static int vmemmap_remap_free(unsigned long start, unsigned long end, .flags = 0, }; -vmemmap_remap_range (start, end, &walk); +if (vmemmap_remap_range (start, end, &walk)) +return VMEMMAP_REMAP_INCOMPLETE ;return ret; }
@@ -385,6 +391,8 @@ static struct page *vmemmap_get_tail(unsigned int order, struct zone *zone)* vmemmap_remap_alloc - remap the vmemmap virtual address range [@start, end) * to the page which is from the @vmemmap_pages * respectively. +* @h: the hstate for the folio whose vmemmap is getting remapped +* @folio: the folio whose vmemmap is getting remapped * @start: start address of the vmemmap virtual address range that we want * to remap. * @end: end address of the vmemmap virtual address range that we want to
@@ -393,20 +401,35 @@ static struct page *vmemmap_get_tail(unsigned int order, struct zone *zone)* * Return: %0 on success, negative error code otherwise. */ -static int vmemmap_remap_alloc (unsigned long start,unsigned long end, +static int vmemmap_remap_alloc (const struct hstate *h,struct folio *folio, +unsigned long start,unsigned long end,unsigned long flags) {LIST_HEAD (vmemmap_pages); -struct vmemmap_remap_walk walk = { +struct vmemmap_remap_walk walk; +struct page *vmemmap_tail; +int ret; + + vmemmap_tail =vmemmap_get_tail (h->order,folio_zone (folio)); +if (WARN_ON_ONCE (!vmemmap_tail)) +return -ENOMEM ; + +if (alloc_vmemmap_page_list (start, end, &vmemmap_pages)) +return -ENOMEM ; + + walk = (struct vmemmap_remap_walk ) { .remap_pte = vmemmap_restore_pte, + .vmemmap_tail = vmemmap_tail, .vmemmap_pages = &vmemmap_pages, .flags = flags, }; -if (alloc_vmemmap_page_list (start, end, &vmemmap_pages)) -return -ENOMEM ; + ret =vmemmap_remap_range (start, end, &walk); -return vmemmap_remap_range (start, end, &walk); +/* 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 );
@@ -439,7 +462,7 @@ static int __hugetlb_vmemmap_restore_folio(const struct hstate *h,* When a HugeTLB page is freed to the buddy allocator, previously * discarded vmemmap pages must be allocated and remapping. */ - ret =vmemmap_remap_alloc (vmemmap_start, vmemmap_end, flags); + ret =vmemmap_remap_alloc (h, folio, vmemmap_start, vmemmap_end, flags);if (!ret)folio_clear_hugetlb_vmemmap_optimized (folio);
@@ -572,7 +595,11 @@ static int __hugetlb_vmemmap_optimize_folio(const struct hstate *h, vmemmap_head, vmemmap_tail, vmemmap_pages, flags); out: -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.55.0.795.g602f6c329a-goog
Catalin Marinas
maintainerRe: [PATCH 03/18] hugetlb_vmemmap: Leave pages partially HVOed upon restore failure
Quoted 1 line · James Houghton
+static const int
VMEMMAP_REMAP_INCOMPLETE= 1;
Nitpick: I get it's all within a file but a static const int feels a bit weird. I'd use an enum or #define.
Signature · 1 line
Catalin
Re: [PATCH 03/18] hugetlb_vmemmap: Leave pages partially HVOed upon restore failure
Quoted 6 lines · Catalin Marinas
Quoted 1 line · James Houghton
+static const int
VMEMMAP_REMAP_INCOMPLETE= 1;Nitpick: I get it's all within a file but a static const int feels a bit weird. I'd use an enum or #define.=
Fixed now, thanks.
PATCH 4/18 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 x86, riscv, and loongarch implementations, 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 | 8 ++++ arch/riscv/include/asm/pgtable.h | 8 ++++ arch/x86/include/asm/pgtable.h | 8 ++++ include/linux/pgtable.h | 21 +++++++++++ mm/hugetlb_vmemmap.c | 56 +++++++++++++++++++--------- 5 files changed, 84 insertions(+), 17 deletions(-)
arch/loongarch/include/asm/pgtable.h +8 -0
@@ -638,6 +638,14 @@ 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 __HAVE_ARCH_TRY_UPDATE_VMEMMAP_PTE +static inline int try_update_vmemmap_pte (unsigned long addr,pte_t *ptep, +pte_t pte) +{ +set_pte (ptep, pte); +return 0 ; +} +/* * 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 +8 -0
@@ -1162,6 +1162,14 @@ static inline pud_t pud_modify(pud_t pud, pgprot_t newprot)#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ +#define __HAVE_ARCH_TRY_UPDATE_VMEMMAP_PTE +static inline int try_update_vmemmap_pte (unsigned long addr,pte_t *ptep, +pte_t pte) +{ +set_pte (ptep, pte); +return 0 ; +} +/* * Encode/decode swap entries and swap PTEs. Swap PTEs are all PTEs that * are !pte_none() && !pte_present().
arch/x86/include/asm/pgtable.h +8 -0
@@ -1372,6 +1372,14 @@ static inline pmd_t pmdp_establish(struct vm_area_struct *vma, }#endif +#define __HAVE_ARCH_TRY_UPDATE_VMEMMAP_PTE +static inline int try_update_vmemmap_pte (unsigned long addr,pte_t *ptep, +pte_t pte) +{ +set_pte (ptep, pte); +return 0 ; +} +#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 +21 -0
@@ -457,6 +457,27 @@ 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 __HAVE_ARCH_TRY_UPDATE_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) +{ +return -EOPNOTSUPP ; +} +#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 +39 -17
112 lines not on this page · raw message
Signature · 1 line
2.55.0.795.g602f6c329a-goog
PATCH 5/18 hugetlb_vmemmap: Allow architectures not to allow HVO at runtime
In reply to James HoughtonEven if HVO is enabled, architectures may at run-time determine that it cannot be used.
It may be the case that arch_supports_hugetlb_vmemmap_optimization() returns false at first but eventually returns true. This is the case on arm64. When this happens, bootmem hugepages won't be immediately HVOed, but they will be optimized in prep_and_add_bootmem_folios(), part of hugetlb_init().
--- include/asm-generic/hugetlb.h | 7 +++++++ mm/hugetlb_vmemmap.c | 12 ++++++++++++ 2 files changed, 19 insertions(+)
include/asm-generic/hugetlb.h +7 -0
@@ -128,4 +128,11 @@ static inline bool gigantic_page_runtime_supported(void) }#endif /* __HAVE_ARCH_GIGANTIC_PAGE_RUNTIME_SUPPORTED */ +#ifndef __HAVE_ARCH_HVO_SUPPORTED +static inline bool arch_hugetlb_vmemmap_optimization_supported (void ) +{ +return IS_ENABLED (CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP ); +} +#endif /* __HAVE_ARCH_HVO_SUPPORTED */ +#endif /* _ASM_GENERIC_HUGETLB_H */
mm/hugetlb_vmemmap.c +12 -0
@@ -17,6 +17,7 @@#include <linux/pagewalk.h> #include <linux/pgalloc.h> +#include "linux/hugetlb.h" #include <asm/tlbflush.h> #include "hugetlb_vmemmap.h" #include "internal.h"
@@ -556,6 +557,9 @@ static bool vmemmap_should_optimize_folio(const struct hstate *h, struct folio *if (!READ_ONCE (vmemmap_optimize_enabled))return false; +if (!arch_hugetlb_vmemmap_optimization_supported ()) +return false; +if (!hugetlb_vmemmap_optimizable (h))return false;
@@ -764,6 +768,14 @@ static bool vmemmap_should_optimize_bootmem_page(struct huge_bootmem_page *m)if (!READ_ONCE (vmemmap_optimize_enabled))return false; +/* +* Architectures may return false here but true by the time +* hugetlb_init() is called. In this case, although the folios will +* not be pre-HVOed, they will be optimized in hugetlb_init(). +*/ +if (!arch_hugetlb_vmemmap_optimization_supported ()) +return false; +if (!hugetlb_vmemmap_optimizable (m->hstate))return false;
Signature · 1 line
2.55.0.795.g602f6c329a-goog
PATCH 6/18 arm64: Rename cpu_has_hw_af to system_has_hw_af
In reply to James HoughtonIn a follow-up patch, a new system feature will need to check for the presence of HW AF on each CPU individually.
--- arch/arm64/include/asm/cpufeature.h | 26 ++++++++++++++++++++------ arch/arm64/include/asm/pgtable.h | 4 ++-- 2 files changed, 22 insertions(+), 8 deletions(-)
arch/arm64/include/asm/cpufeature.h +20 -6
@@ -910,22 +910,36 @@ static inline u32 id_aa64mmfr0_parange_to_phys_shift(int parange) }/* Check whether hardware update of the Access flag is supported */ -static inline bool cpu_has_hw_af (void ) +static inline bool supports_hw_af (int scope) {u64 mmfr1;if (!IS_ENABLED (CONFIG_ARM64_HW_AFDBM ))return false; -/* -* Use cached version to avoid emulated msr operation on KVM -* guests. -*/ - mmfr1 =read_sanitised_ftr_reg (SYS_ID_AA64MMFR1_EL1 ); +if (scope ==SCOPE_SYSTEM ) { +/* +* Use cached version to avoid emulated msr operation on KVM +* guests. +*/ + mmfr1 =read_sanitised_ftr_reg (SYS_ID_AA64MMFR1_EL1 ); + }else { + mmfr1 =read_cpuid (ID_AA64MMFR1_EL1 ); + }return cpuid_feature_extract_unsigned_field (mmfr1,ID_AA64MMFR1_EL1_HAFDBS_SHIFT ); } +static inline bool system_has_hw_af (void ) +{ +return supports_hw_af (SCOPE_SYSTEM ); +} + +static inline bool cpu_has_hw_af (void ) +{ +return supports_hw_af (SCOPE_LOCAL_CPU ); +} +static inline bool cpu_has_pan (void ) {u64 mmfr1 =read_cpuid (ID_AA64MMFR1_EL1 );
arch/arm64/include/asm/pgtable.h +2 -2
@@ -1598,7 +1598,7 @@ static inline void update_mmu_cache_range(struct vm_fault *vmf,* page after fork() + CoW for pfn mappings. We don't always have a * hardware-managed access flag on arm64. */ -#define arch_has_hw_pte_young cpu_has_hw_af +#define arch_has_hw_pte_young system_has_hw_af#ifdef CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG #define arch_has_hw_nonleaf_pmd_young system_supports_haft
@@ -1608,7 +1608,7 @@ static inline void update_mmu_cache_range(struct vm_fault *vmf,* Experimentally, it's cheap to set the access flag in hardware and we * benefit from prefaulting mappings as 'old' to start with. */ -#define arch_wants_old_prefaulted_pte cpu_has_hw_af +#define arch_wants_old_prefaulted_pte system_has_hw_af/* * Request exec memory is read into pagecache in at least 64K folios. This size
Signature · 1 line
2.55.0.795.g602f6c329a-goog
PATCH 7/18 arm64: Add system_supports_hvo
In reply to James HoughtonTrack HVO compatibility as its own system feature. If every boot CPU supports it, later onlining a CPU that doesn't support it won't be allowed.
HVO requires BBM Level 2 no abort 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.
Another possibility would be to disable HVO if an incompatible CPU were onlined; I haven't done this.
--- arch/arm64/include/asm/cpucaps.h | 2 ++ arch/arm64/include/asm/cpufeature.h | 5 +++++ arch/arm64/kernel/cpufeature.c | 16 ++++++++++++++++ arch/arm64/tools/cpucaps | 1 + 4 files changed, 24 insertions(+)
arch/arm64/include/asm/cpucaps.h +2 -0
@@ -75,6 +75,8 @@ cpucap_is_possible(const unsigned int cap)return IS_ENABLED (CONFIG_HW_PERF_EVENTS );case ARM64_HAS_LSUI :return IS_ENABLED (CONFIG_ARM64_LSUI ); +case ARM64_HVO_COMPATIBLE : +return IS_ENABLED (CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP ); }return true;
arch/arm64/include/asm/cpufeature.h +5 -0
@@ -885,6 +885,11 @@ static inline bool system_supports_bbml2_noabort(void)return alternative_has_cap_unlikely (ARM64_HAS_BBML2_NOABORT ); } +static inline bool system_supports_hvo (void ) +{ +return alternative_has_cap_unlikely (ARM64_HVO_COMPATIBLE ); +} +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 +16 -0
@@ -2172,6 +2172,16 @@ static bool has_bbml2_noabort(const struct arm64_cpu_capabilities *caps, int scoreturn cpu_supports_bbml2_noabort (); } +static bool hvo_compatible (const struct arm64_cpu_capabilities *caps,int scope) +{ +/* +* We need BBML2 to support Block -> Table transitions without taking +* faults, and we need HW AF support to support changing the OA without +* taking faults. +*/ +return cpu_supports_bbml2_noabort () &&supports_hw_af (scope); +} +static void cpu_enable_pan (const struct arm64_cpu_capabilities *__unused) {/*
@@ -3067,6 +3077,12 @@ static const struct arm64_cpu_capabilities arm64_features[] = { .type = ARM64_CPUCAP_EARLY_LOCAL_CPU_FEATURE, .matches = has_bbml2_noabort, }, + { + .desc = "HugeTLB Vmemmap Optimization Support", + .capability = ARM64_HVO_COMPATIBLE, + .type = ARM64_CPUCAP_SYSTEM_FEATURE, + .matches = hvo_compatible, + }, { .desc = "52-bit Virtual Addressing for KVM (LPA2)", .capability = ARM64_HAS_LPA2,
arch/arm64/tools/cpucaps +1 -0
@@ -70,6 +70,7 @@ HAS_VIRT_HOST_EXTN HAS_WFXT HAS_XNX HAFT +HVO_COMPATIBLE HW_DBM KVM_HVHE KVM_PROTECTED_MODE
Signature · 1 line
2.55.0.795.g602f6c329a-goog
Catalin Marinas
maintainerRe: [PATCH 07/18] arm64: Add system_supports_hvo
arch/arm64/include/asm/cpucaps.h cpucap_is_possible(const unsigned int cap) · line 80
Quoted 11 lines · James Houghton
arch/arm64/include/asm/cpucaps.h +2
-0@@ -75,6 +75,8 @@ cpucap_is_possible(const unsigned int cap)return IS_ENABLED (CONFIG_HW_PERF_EVENTS );case ARM64_HAS_LSUI :return IS_ENABLED (CONFIG_ARM64_LSUI ); +case ARM64_HVO_COMPATIBLE : +return IS_ENABLED (CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP ); }
We shouldn't use HVO, a software feature, as a CPU capability name, even if it's called HVO_COMPATIBLE. Maybe we can come up with a hardware related name, e.g. BBM_THROUGH_AF.
Quoted 7 lines
arch/arm64/kernel/cpufeature.c +0
-0@@ -2172,6 +2172,16 @@ static bool has_bbml2_noabort(const struct arm64_cpu_capabilities *caps, int scoreturn cpu_supports_bbml2_noabort (); }
arch/arm64/kernel/cpufeature.c static bool has_bbml2_noabort(const struct arm64_cpu_capabilities *caps, int sco · line 2183
Quoted 10 lines
arch/arm64/kernel/cpufeature.c +9
-0+static bool hvo_compatible (const struct arm64_cpu_capabilities *caps,int scope) +{ +/* +* We need BBML2 to support Block -> Table transitions without taking +* faults, and we need HW AF support to support changing the OA without +* taking faults. +*/ +return cpu_supports_bbml2_noabort () &&supports_hw_af (scope); +}
I think this is wrong. cpu_supports_bbml2_noabort() only checks the current CPU while hvo_compatible() will be run once on the sanitised register values (SCOPE_SYSTEM). I should be something like:
cpus_have_cap(ARM64_HAS_BBML2_NOABORT) && supports_hw_af(scope)
Signature · 1 line
Catalin
Re: [PATCH 07/18] arm64: Add system_supports_hvo
Quoted 17 lines · Catalin Marinas
arch/arm64/include/asm/cpucaps.h cpucap_is_possible(const unsigned int cap) · line 80
Quoted 11 lines · James Houghton
arch/arm64/include/asm/cpucaps.h +2
-0@@ -75,6 +75,8 @@ cpucap_is_possible(const unsigned int cap)return IS_ENABLED (CONFIG_HW_PERF_EVENTS );case ARM64_HAS_LSUI :return IS_ENABLED (CONFIG_ARM64_LSUI ); +case ARM64_HVO_COMPATIBLE : +return IS_ENABLED (CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP ); }We shouldn't use HVO, a software feature, as a CPU capability name, even if it's called
HVO_COMPATIBLE. Maybe we can come up with a hardware related name, e.g.BBM_THROUGH_AF.
I was struggling to come up with a good name. BBM_THROUGH_AF sounds good, thanks!
I think with that more generic name, the CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP reference here no longer makes sense, so I'll drop that.
Quoted 22 lines
Quoted 7 lines
arch/arm64/kernel/cpufeature.c +0
-0@@ -2172,6 +2172,16 @@ static bool has_bbml2_noabort(const struct arm64_cpu_capabilities *caps, int scoreturn cpu_supports_bbml2_noabort (); }arch/arm64/kernel/cpufeature.c static bool has_bbml2_noabort(const struct arm64_cpu_capabilities *caps, int sco · line 2183
Quoted 10 lines
arch/arm64/kernel/cpufeature.c +9
-0+static bool hvo_compatible (const struct arm64_cpu_capabilities *caps,int scope) +{ +/* +* We need BBML2 to support Block -> Table transitions without taking +* faults, and we need HW AF support to support changing the OA without +* taking faults. +*/ +return cpu_supports_bbml2_noabort () &&supports_hw_af (scope); +}I think this is wrong.
cpu_supports_bbml2_noabort()only checks the current CPU whilehvo_compatible()will be run once on the sanitised register values (SCOPE_SYSTEM). I should be something like:
Quoted 1 line
cpus_have_cap(ARM64_HAS_BBML2_NOABORT) && supports_hw_af(scope)
Yep, it's wrong. :( I think I had some things confused in my head.
With my newfound understanding of things, I don't think the previous patch (6) is doing anything. So I'll drop that patch and just write:
cpus_have_cap(ARM64_HAS_BBML2_NOABORT) && cpu_has_hw_af()
Thanks!
PATCH 8/18 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_hvo() will return false if any CPUs on the system do not support HW AF.
--- arch/arm64/include/asm/pgtable.h | 53 ++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-)
arch/arm64/include/asm/pgtable.h +50 -3
@@ -1302,8 +1302,7 @@ 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) +static inline pte_t __ptep_clear_young (pte_t *ptep) {pte_t old_pte, pte;
@@ -1315,7 +1314,13 @@ static inline bool __ptep_test_and_clear_young(struct vm_area_struct *vma, pte_val(old_pte), pte_val(pte)); }while (pte_val (pte) !=pte_val (old_pte)); -return pte_young (pte); +return pte; +} + +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,
@@ -1793,6 +1798,48 @@ static inline void pte_clear(struct mm_struct *mm,__pte_clear (mm, addr, ptep); } +#define __HAVE_ARCH_TRY_UPDATE_VMEMMAP_PTE +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_hvo ()) +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 clear_full_ptes clear_full_ptesstatic inline void clear_full_ptes (struct mm_struct *mm,unsigned long addr,pte_t *ptep,unsigned int nr,int full)
Signature · 1 line
2.55.0.795.g602f6c329a-goog
Catalin Marinas
maintainerRe: [PATCH 08/18] arm64: Implement try_update_vmemmap_pte using the AF trick
Quoted 15 lines · James Houghton
arch/arm64/include/asm/pgtable.h +1
-2@@ -1302,8 +1302,7 @@ 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) +static inline pte_t __ptep_clear_young (pte_t *ptep) {pte_t old_pte, pte;
arch/arm64/include/asm/pgtable.h static inline bool __ptep_test_and_clear_young(struct vm_area_struct *vma, · line 1318
Quoted 6 lines
arch/arm64/include/asm/pgtable.h +2
-1@@ -1315,7 +1314,13 @@ static inline bool __ptep_test_and_clear_young(struct vm_area_struct *vma, pte_val(old_pte), pte_val(pte)); }while (pte_val (pte) !=pte_val (old_pte)); -return pte_young (pte); +return pte; +}
At some point, we should use LSE atomics directly here if supported rather than a CAS loop (well, this is LSE as well if supported but it doesn't guarantee forward progress of the loop). I reckon we can replace it with some test_and_clear_bit_relaxed() call.
Quoted 32 lines
arch/arm64/include/asm/pgtable.h +25
-0+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,@@ -1793,6 +1798,48 @@ static inline void pte_clear(struct mm_struct *mm,__pte_clear (mm, addr, ptep); } +#define __HAVE_ARCH_TRY_UPDATE_VMEMMAP_PTE +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_hvo ()) +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; +
arch/arm64/include/asm/pgtable.h static inline void pte_clear(struct mm_struct *mm, · line 1831
Quoted 10 lines
arch/arm64/include/asm/pgtable.h +10
-0+/* 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 ); + }
I think this is going to do a lot of TLBIs given that the default kernel prot has PTE_AF. It somewhat defeats the VMEMMAP_REMAP_NO_TLB_FLUSH flag but I haven't figured exactly how this optimisation works.
If it becomes a problem, we could do a first pass to clear AF as an optimisation or later via vmemmap_split_pmd(), only map with AF=0. We still have some page copying that touches the vmemmap, bringing AF back. Of course, you'd still need the above flush, just wondering whether we can reduce/coalesce it.
arch/arm64/include/asm/pgtable.h static inline void pte_clear(struct mm_struct *mm, · line 1841
Quoted 10 lines
arch/arm64/include/asm/pgtable.h +10
-0+/* +* 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 ; +}
Here, indeed, we do need this bounded, otherwise some pathological cases may set the AF continuously.
Signature · 1 line
Catalin
Re: [PATCH 08/18] arm64: Implement try_update_vmemmap_pte using the AF trick
Quoted 20 lines · Catalin Marinas
Quoted 18 lines · James Houghton
arch/arm64/include/asm/pgtable.h +1
-2@@ -1302,8 +1302,7 @@ 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) +static inline pte_t __ptep_clear_young (pte_t *ptep) {pte_t old_pte, pte;@@ -1315,7 +1314,13 @@ static inline bool __ptep_test_and_clear_young(struct vm_area_struct *vma, pte_val(old_pte), pte_val(pte)); }while (pte_val (pte) !=pte_val (old_pte));
Quoted 8 lines
arch/arm64/include/asm/pgtable.h static inline bool __ptep_test_and_clear_young(struct vm_area_struct *vma, · line 1318
Quoted 3 lines
arch/arm64/include/asm/pgtable.h +2
-1-return pte_young (pte); +return pte; +}At some point, we should use LSE atomics directly here if supported rather than a CAS loop (well, this is LSE as well if supported but it doesn't guarantee forward progress of the loop). I reckon we can replace it with some
test_and_clear_bit_relaxed()call.
I'll put a patch (or two) at the front of this series to do this. :) Will mentioned it too in my offline discussions with him.
Will also pointed out that FEAT_LSE relaxed some of the forward progress guarantees[1], pretty strong justification to just use LDCLR. So unless you feel strongly, I'll skip performance testing for this change.
[1] B2.12.5 (M.c) The bullet that begins with "If FEAT_LSE is not implemented".
Quoted 47 lines
Quoted 32 lines
arch/arm64/include/asm/pgtable.h +25
-0+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,@@ -1793,6 +1798,48 @@ static inline void pte_clear(struct mm_struct *mm,__pte_clear (mm, addr, ptep); } +#define __HAVE_ARCH_TRY_UPDATE_VMEMMAP_PTE +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_hvo ()) +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; +arch/arm64/include/asm/pgtable.h static inline void pte_clear(struct mm_struct *mm, · line 1831
Quoted 10 lines
arch/arm64/include/asm/pgtable.h +10
-0+/* 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 ); + }I think this is going to do a lot of TLBIs given that the default kernel prot has
PTE_AF. It somewhat defeats theVMEMMAP_REMAP_NO_TLB_FLUSHflag but I haven't figured exactly how this optimisation works.
Quoted 5 lines
If it becomes a problem, we could do a first pass to clear AF as an optimisation or later via
vmemmap_split_pmd(), only map with AF=0. We still have some page copying that touches the vmemmap, bringing AF back. Of course, you'd still need the above flush, just wondering whether we can reduce/coalesce it.
We could reduce/coalesce the TLBIs (it should be very unlikely for the page structs of a to-be-optimized (or to-be-unoptimized) HugeTLB folio to be accessed, which is nice), but as far as I can tell, the TLBIs don't seem *too* slow yet.
On my test system (single-socket, 80 CPUs, Neoverse V2), the TLBIs seem to take about half the total time required to free the vmemmap. This isn't great, but the absolute time taken isn't a problem for me (maybe because my machine is single-socket?).
``` $ echo 0 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages $ time perf record -g -- bash -c "echo 288 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages" real 0m1.271s user 0m0.076s # this is just perf :) sys 0m1.180s Trimmed perf report: + 47.44% 47.44% bash [kernel.kallsyms] [k] try_update_vmemmap_pte # In try_update_vmemmap_pte, it's the TLBI + ISB taking all of the time + 33.66% 33.62% bash [kernel.kallsyms] [k] prep_new_page + 10.71% 10.71% bash [kernel.kallsyms] [k] _raw_spin_unlock_irqrestore + 8.57% 4.86% bash [kernel.kallsyms] [k] __free_pages ```
Also checking to see if HugeTLB-2M is problematic; it doesn't appear to be.
``` HugeTLB-2M $ echo 0 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages $ time echo 10000 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages real 0m0.080s user 0m0.001s sys 0m0.079s ```
Quoted 16 lines
arch/arm64/include/asm/pgtable.h static inline void pte_clear(struct mm_struct *mm, · line 1841
Quoted 10 lines
arch/arm64/include/asm/pgtable.h +10
-0+/* +* 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 ; +}Here, indeed, we do need this bounded, otherwise some pathological cases may set the AF continuously.
--
Catalin
PATCH 9/18 arm64: Prevent HVO if the HVO system feature is not enabled
In reply to James HoughtonThe HVO system feature checks that all CPUs support HW AF and BBML2_NOABORT, both of which are needed for HVO (as of this patch).
--- arch/arm64/include/asm/hugetlb.h | 7 +++++++ 1 file changed, 7 insertions(+)
arch/arm64/include/asm/hugetlb.h +7 -0
@@ -11,6 +11,7 @@#define __ASM_HUGETLB_H#include <asm/cacheflush.h> +#include <asm/cpufeature.h> #include <asm/mte.h> #include <asm/page.h>
@@ -65,6 +66,12 @@ extern void huge_ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, pte_t old_pte, pte_t new_pte); +#define __HAVE_ARCH_HVO_SUPPORTED +static inline bool arch_hugetlb_vmemmap_optimization_supported (void ) +{ +return system_supports_hvo (); +} +#include <asm-generic/hugetlb.h> static inline void __flush_hugetlb_tlb_range(struct vm_area_struct *vma,
Signature · 1 line
2.55.0.795.g602f6c329a-goog
PATCH 10/18 arm64: Support hugetlb vmemmap optimization
In reply to James HoughtonNow that HVO is architecturally sound for arm64, enable support. Add a new Kconfig for this support, as it comes with a regression, described below.
Add a comment about BBML2_NOABORT where HVO is relying on it.
If a late-onlined CPU does not support HVO (HW AF and BBML2_NOABORT) while all boot CPUs do support HVO, the late CPU won't be onlined after this commit when a kernel is compiled with HUGETLB_PAGE_OPTIMIZE_VMEMMAP=y.
--- arch/arm64/Kconfig | 12 ++++++++++++ mm/hugetlb_vmemmap.c | 4 ++++ 2 files changed, 16 insertions(+)
arch/arm64/Kconfig +12 -0
@@ -259,6 +259,7 @@ config ARM64 select USER_STACKTRACE_SUPPORT select VDSO_GETRANDOM select VMAP_STACK + select ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP if ARM64_WANT_OPTIMIZE_HUGETLB_VMEMMAP help ARM 64-bit (AArch64) Linux support.
@@ -1607,6 +1608,17 @@ config ARCH_SPARSEMEM_ENABLE def_bool y select SPARSEMEM_VMEMMAP_ENABLE +config ARM64_WANT_OPTIMIZE_HUGETLB_VMEMMAP + bool "Enable support for HugeTLB Vmemmap Optimization (HVO)" + default n + depends on HUGETLB_PAGE && SPARSEMEM_VMEMMAP && ARM64_HW_AFDBM + help + Enable support for HVO. When enabled, late-onlining of CPUs that do + not support features required by HVO will not be onlined if all boot + CPUs do have such support. + + If unsure, say N + config HW_PERF_EVENTS def_bool y depends on ARM_PMU
mm/hugetlb_vmemmap.c +4 -0
@@ -86,6 +86,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 BBML2_NOABORT. Its support has +* already been checked. +*/ pmd_populate_kernel (&init_mm, pmd, pgtable);if (!(walk->flags &VMEMMAP_SPLIT_NO_TLB_FLUSH ))flush_tlb_kernel_range (start, start +PMD_SIZE );
Signature · 1 line
2.55.0.795.g602f6c329a-goog
Catalin Marinas
maintainerRe: [PATCH 10/18] arm64: Support hugetlb vmemmap optimization
arch/arm64/Kconfig config ARM64 · line 262
Quoted 9 lines · James Houghton
arch/arm64/Kconfig +1
-0@@ -259,6 +259,7 @@ config ARM64 select USER_STACKTRACE_SUPPORT select VDSO_GETRANDOM select VMAP_STACK + select ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP if ARM64_WANT_OPTIMIZE_HUGETLB_VMEMMAP
Nit: we try to keep this list alphabetically ordered.
arch/arm64/Kconfig config ARCH_SPARSEMEM_ENABLE · line 1613
Quoted 10 lines
arch/arm64/Kconfig +3
-0help ARM 64-bit (AArch64) Linux support.@@ -1607,6 +1608,17 @@ config ARCH_SPARSEMEM_ENABLE def_bool y select SPARSEMEM_VMEMMAP_ENABLE +config ARM64_WANT_OPTIMIZE_HUGETLB_VMEMMAP + bool "Enable support for HugeTLB Vmemmap Optimization (HVO)" + default n
Nit: 'n' is the default already.
arch/arm64/Kconfig config ARCH_SPARSEMEM_ENABLE · line 1616
Quoted 3 lines
arch/arm64/Kconfig +3
-0+ depends on HUGETLB_PAGE && SPARSEMEM_VMEMMAP && ARM64_HW_AFDBM + help + Enable support for HVO. When enabled, late-onlining of CPUs that do
I'd drop 'of', not sure how it reads better. Maybe just 'late CPUs ... will not be onlined'.
Quoted 8 lines
arch/arm64/Kconfig +5
-0+ not support features required by HVO will not be onlined if all boot + CPUs do have such support. + + If unsure, say N + config HW_PERF_EVENTS def_bool y depends on ARM_PMU
Signature · 1 line
Catalin
Re: [PATCH 10/18] arm64: Support hugetlb vmemmap optimization
Quoted 27 lines · Catalin Marinas
arch/arm64/Kconfig config ARM64 · line 262
Quoted 9 lines · James Houghton
arch/arm64/Kconfig +1
-0@@ -259,6 +259,7 @@ config ARM64 select USER_STACKTRACE_SUPPORT select VDSO_GETRANDOM select VMAP_STACK + select ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP if ARM64_WANT_OPTIMIZE_HUGETLB_VMEMMAPNit: we try to keep this list alphabetically ordered.
arch/arm64/Kconfig config ARCH_SPARSEMEM_ENABLE · line 1613
Quoted 10 lines
arch/arm64/Kconfig +3
-0help ARM 64-bit (AArch64) Linux support.@@ -1607,6 +1608,17 @@ config ARCH_SPARSEMEM_ENABLE def_bool y select SPARSEMEM_VMEMMAP_ENABLE +config ARM64_WANT_OPTIMIZE_HUGETLB_VMEMMAP + bool "Enable support for HugeTLB Vmemmap Optimization (HVO)" + default nNit: 'n' is the default already.
Quoted 6 lines
arch/arm64/Kconfig config ARCH_SPARSEMEM_ENABLE · line 1616
Quoted 3 lines
arch/arm64/Kconfig +3
-0+ depends on HUGETLB_PAGE && SPARSEMEM_VMEMMAP && ARM64_HW_AFDBM + help + Enable support for HVO. When enabled, late-onlining of CPUs that doI'd drop 'of', not sure how it reads better. Maybe just 'late CPUs ... will not be onlined'.
Thanks for the feedback; I've incorporated it. But I think this patch is going away in v2 (with patches 14-18).
Quoted 11 lines
arch/arm64/Kconfig config ARCH_SPARSEMEM_ENABLE · line 1624
Quoted 8 lines
arch/arm64/Kconfig +5
-0+ not support features required by HVO will not be onlined if all boot + CPUs do have such support. + + If unsure, say N + config HW_PERF_EVENTS def_bool y depends on ARM_PMU--
Catalin
PATCH 11/18 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 BBML2_NOABORT.
--- arch/arm64/include/asm/pgalloc.h | 9 +++++++++ arch/loongarch/include/asm/pgalloc.h | 8 ++++++++ arch/riscv/include/asm/pgalloc.h | 8 ++++++++ arch/x86/include/asm/pgalloc.h | 8 ++++++++ include/linux/pgalloc.h | 20 ++++++++++++++++++++ mm/hugetlb_vmemmap.c | 28 ++++++++++++++++------------ 6 files changed, 69 insertions(+), 12 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 __HAVE_ARCH_TRY_POPULATE_VMEMMAP_PMD +static inline int try_populate_vmemmap_pmd (pmd_t *pmdp,pte_t *pgtable, +unsigned long addr) +{ +/* BBML2_NOABORT is required. Its presence has been checked. */ +pmd_populate_kernel (&init_mm, pmdp, pgtable); +return 0 ; +} +#endif
arch/loongarch/include/asm/pgalloc.h +8 -0
@@ -24,6 +24,14 @@ 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 __HAVE_ARCH_TRY_POPULATE_VMEMMAP_PMD +static inline int try_populate_vmemmap_pmd (pmd_t *pmdp,pte_t *pgtable, +unsigned long addr) +{ +pmd_populate_kernel (&init_mm, pmdp, pgtable); +return 0 ; +} +#ifndef __PAGETABLE_PMD_FOLDEDstatic inline void pud_populate (struct mm_struct *mm,pud_t *pud,pmd_t *pmd)
arch/riscv/include/asm/pgalloc.h +8 -0
@@ -31,6 +31,14 @@ static inline void pmd_populate(struct mm_struct *mm,set_pmd (pmd,__pmd ((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE)); } +#define __HAVE_ARCH_TRY_POPULATE_VMEMMAP_PMD +static inline int try_populate_vmemmap_pmd (pmd_t *pmdp,pte_t *pgtable, +unsigned long addr) +{ +pmd_populate_kernel (&init_mm, pmdp, pgtable); +return 0 ; +} +#ifndef __PAGETABLE_PMD_FOLDEDstatic inline void pud_populate (struct mm_struct *mm,pud_t *pud,pmd_t *pmd) {
arch/x86/include/asm/pgalloc.h +8 -0
@@ -82,6 +82,14 @@ static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd,set_pmd (pmd,__pmd (((pteval_t )pfn <<PAGE_SHIFT ) | _PAGE_TABLE)); } +#define __HAVE_ARCH_TRY_POPULATE_VMEMMAP_PMD +static inline int try_populate_vmemmap_pmd (pmd_t *pmdp,pte_t *pgtable, +unsigned long addr) +{ +pmd_populate_kernel (&init_mm, pmdp, pgtable); +return 0 ; +} +#if CONFIG_PGTABLE_LEVELS >2 extern void ___pmd_free_tlb (struct mmu_gather *tlb,pmd_t *pmd);
include/linux/pgalloc.h +20 -0
@@ -26,4 +26,24 @@arch_sync_kernel_mappings (addr, addr); \ } while (0) +#ifndef __HAVE_ARCH_TRY_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 (pmd_t *pmdp,pte_t *pgtable, +unsigned long addr) +{ +return -EOPNOTSUPP ; +} +#endif +#endif /* _LINUX_PGALLOC_H */
mm/hugetlb_vmemmap.c +16 -12
@@ -55,6 +55,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;
@@ -74,8 +75,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 (pmd, pgtable, start); +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
@@ -84,21 +92,17 @@ 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 BBML2_NOABORT. Its support has -* already been checked. -*/ -pmd_populate_kernel (&init_mm, pmd, pgtable);if (!(walk->flags &VMEMMAP_SPLIT_NO_TLB_FLUSH ))flush_tlb_kernel_range (start, start +PMD_SIZE ); - }else { -pte_free_kernel (&init_mm, pgtable); - } -spin_unlock (&init_mm.page_table_lock); + }else + goto free; -return 0 ; +out: +spin_unlock (&init_mm.page_table_lock); +return ret; +free: +pte_free_kernel (&init_mm, pgtable); + goto out; } static int vmemmap_pmd_entry(pmd_t *pmd, unsigned long addr,
Signature · 1 line
2.55.0.795.g602f6c329a-goog
Catalin Marinas
maintainerRe: [PATCH 11/18] hugetlb_vmemmap: Use try_populate_vmemmap_pmd for replacing in-use PMDs
Quoted 6 lines · James Houghton
arch/arm64/include/asm/pgalloc.h +0
-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);
arch/arm64/include/asm/pgalloc.h pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep) · line 131
Quoted 10 lines
arch/arm64/include/asm/pgalloc.h +8
-0} +#define __HAVE_ARCH_TRY_POPULATE_VMEMMAP_PMD +static inline int try_populate_vmemmap_pmd (pmd_t *pmdp,pte_t *pgtable, +unsigned long addr) +{ +/* BBML2_NOABORT is required. Its presence has been checked. */ +pmd_populate_kernel (&init_mm, pmdp, pgtable); +return 0 ; +}
This patch adds the same function in 4 different arch files. Can we not have a single generic implementation in linux/pgalloc.h, bracketed by some ARCH_WANTS_* (macro or config)?
I think the same goes for patch 4 with try_update_vmemmap_pte(), we can reduce the code duplication.
Quoted 13 lines
include/linux/pgalloc.h +5
-0@@ -26,4 +26,24 @@arch_sync_kernel_mappings (addr, addr); \ }while (0 ) +#ifndef __HAVE_ARCH_TRY_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.
include/linux/pgalloc.h line 43
Quoted 10 lines
include/linux/pgalloc.h +10
-0+* @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 (pmd_t *pmdp,pte_t *pgtable, +unsigned long addr)
Nit: if we follow try_update_vmemmap_pte(), we could place 'addr' first. It matches pgd_populate_kernel() as well in this file.
Quoted 52 lines
include/linux/pgalloc.h +5
-0+{ +return -EOPNOTSUPP ; +} +#endif +#endif /* _LINUX_PGALLOC_H */ mm/hugetlb_vmemmap.c +9
-11@@ -55,6 +55,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;@@ -74,8 +75,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 (pmd, pgtable, start); +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 @@ -84,21 +92,17 @@ 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 BBML2_NOABORT. Its support has -* already been checked. -*/ -pmd_populate_kernel (&init_mm, pmd, pgtable);if (!(walk->flags &VMEMMAP_SPLIT_NO_TLB_FLUSH ))flush_tlb_kernel_range (start, start +PMD_SIZE ); - }else { -pte_free_kernel (&init_mm, pgtable); - } -spin_unlock (&init_mm.page_table_lock); + }else
mm/hugetlb_vmemmap.c static int vmemmap_split_pmd(pmd_t *pmd, struct page *head, unsigned long start, · line 106
Quoted 10 lines
mm/hugetlb_vmemmap.c +7
-1+ goto free; -return 0 ; +out: +spin_unlock (&init_mm.page_table_lock); +return ret; +free: +pte_free_kernel (&init_mm, pgtable); + goto out; }
Would 'free' first with fall through 'out' read better?
Signature · 1 line
Catalin
Re: [PATCH 11/18] hugetlb_vmemmap: Use try_populate_vmemmap_pmd for replacing in-use PMDs
Quoted 23 lines · Catalin Marinas
Quoted 6 lines · James Houghton
arch/arm64/include/asm/pgalloc.h +0
-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);arch/arm64/include/asm/pgalloc.h pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep) · line 131
Quoted 10 lines
arch/arm64/include/asm/pgalloc.h +8
-0} +#define __HAVE_ARCH_TRY_POPULATE_VMEMMAP_PMD +static inline int try_populate_vmemmap_pmd (pmd_t *pmdp,pte_t *pgtable, +unsigned long addr) +{ +/* BBML2_NOABORT is required. Its presence has been checked. */ +pmd_populate_kernel (&init_mm, pmdp, pgtable); +return 0 ; +}This patch adds the same function in 4 different arch files. Can we not have a single generic implementation in
linux/pgalloc.h, bracketed by some ARCH_WANTS_* (macro or config)?
Quoted 2 lines
I think the same goes for patch 4 with
try_update_vmemmap_pte(), we can reduce the code duplication.
Yeah putting the generic version behind an ARCH_WANTS ifdef is so much better. Then I can just nix the EOPNOTSUPP branch; a compile error is always better.
Quoted 18 lines
Quoted 18 lines
include/linux/pgalloc.h +10
-0@@ -26,4 +26,24 @@arch_sync_kernel_mappings (addr, addr); \ }while (0 ) +#ifndef __HAVE_ARCH_TRY_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. +*
Quoted 8 lines
include/linux/pgalloc.h line 43
Quoted 5 lines
include/linux/pgalloc.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 range. +*/ +static inline int try_populate_vmemmap_pmd (pmd_t *pmdp,pte_t *pgtable, +unsigned long addr)Nit: if we follow
try_update_vmemmap_pte(), we could place 'addr' first. It matchespgd_populate_kernel()as well in this file.
Will do.
Quoted 54 lines
Quoted 54 lines
include/linux/pgalloc.h +5
-0+{ +return -EOPNOTSUPP ; +} +#endif +#endif /* _LINUX_PGALLOC_H */ mm/hugetlb_vmemmap.c +10
-11@@ -55,6 +55,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;@@ -74,8 +75,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 (pmd, pgtable, start); +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 @@ -84,21 +92,17 @@ 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 BBML2_NOABORT. Its support has -* already been checked. -*/ -pmd_populate_kernel (&init_mm, pmd, pgtable);if (!(walk->flags &VMEMMAP_SPLIT_NO_TLB_FLUSH ))flush_tlb_kernel_range (start, start +PMD_SIZE ); - }else { -pte_free_kernel (&init_mm, pgtable); - } -spin_unlock (&init_mm.page_table_lock); + }else + goto free;
Quoted 10 lines
mm/hugetlb_vmemmap.c static int vmemmap_split_pmd(pmd_t *pmd, struct page *head, unsigned long start, · line 106
Quoted 8 lines
mm/hugetlb_vmemmap.c +6
-1-return 0 ; +out: +spin_unlock (&init_mm.page_table_lock); +return ret; +free: +pte_free_kernel (&init_mm, pgtable); + goto out; }Would 'free' first with fall through 'out' read better?
Yes, yes it would. I think the current ordering is just a holdover from earlier attempts at writing this function.
Thanks!
PATCH 12/18 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 BBML2_NOABORT, 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
@@ -125,9 +125,54 @@ pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep)static inline int try_populate_vmemmap_pmd (pmd_t *pmdp,pte_t *pgtable,unsigned long addr) { -/* BBML2_NOABORT 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_hvo ()) +return -EOPNOTSUPP ; + +if (system_supports_bbml2_noabort ()) { +/* +* BBML2_NOABORT 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.55.0.795.g602f6c329a-goog
Catalin Marinas
maintainerRe: [PATCH 12/18] arm64: Implement try_populate_vmemmap_pmd using AF trick
Quoted 10 lines · James Houghton
This 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
BBML2_NOABORT, there is no need to use the AF trick.
This part needs even more text in the Arm ARM. With dropping of the BBML2_NOABORT requirement in patch 13, we are left with the D8.17.2 rules in the Arm ARM (M.c) requiring BBM. I'm not sure we can infer much from the AF=0 caching rules as here we talk about the walk caches. For example, a walk cache memorises the VA to block descriptor translation and that's irrespective of the leaf entry AF bit. If AF=0 and you skip the TLBI, nothing flushes the walk cache which may confuse the CPU when it suddenly sees a table entry where it expected a block one.
If it's not critical to the series, I suggest we leave this patch and the next one out for now.
Signature · 1 line
Catalin
Re: [PATCH 12/18] arm64: Implement try_populate_vmemmap_pmd using AF trick
Quoted 13 lines · Catalin Marinas
Quoted 10 lines · James Houghton
This 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
BBML2_NOABORT, there is no need to use the AF trick.
Quoted 8 lines
This part needs even more text in the Arm ARM. With dropping of the
BBML2_NOABORTrequirement in patch 13, we are left with the D8.17.2 rules in the Arm ARM (M.c) requiring BBM. I'm not sure we can infer much from the AF=0 caching rules as here we talk about the walk caches. For example, a walk cache memorises the VA to block descriptor translation and that's irrespective of the leaf entry AF bit. If AF=0 and you skip the TLBI, nothing flushes the walk cache which may confuse the CPU when it suddenly sees a table entry where it expected a block one.
The CPU must still respect block descriptors that become invalid, so it wouldn't be able to just assume "this is a valid Block descriptor", right? But I really don't know how implementations actually work here, so it could be possible that the walk cache could get confused during Block -> Table transitions without a BBM sequence.
I agree that this should be clarified. I'll try to prepare an HVO-less description of the required behavior and work with Will to properly ask about it.
Quoted 2 lines
If it's not critical to the series, I suggest we leave this patch and the next one out for now.
I originally included it because I didn't have any machines with BBML3, but I think I have a Neoverse N3 system which supports BBML3. :)
I think I'll still include these patches anyway (it's at least useful to continue to drive the discussion of whether or not the Block -> Table transition is legal); I'll structure the series such that they can be easily left off. And I'll note in the cover letter that they probably ought to be merged separately after more scrutiny.
PATCH 13/18 arm64: Drop BBML2_NOABORT requirement for HVO
In reply to James HoughtonWith PMD updates being done with the Access Flag trick, BBML2_NOABORT is no longer needed.
Dropping this requirement vastly widens the pool of systems this optimization is valid for.
Intentionally leave HVO as opt-in, as enabling HVO still introduces the following regression: if all boot CPUs support HW AF, then any late-onlined CPUs that do not will not be onlined.
--- arch/arm64/kernel/cpufeature.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
arch/arm64/kernel/cpufeature.c +3 -4
@@ -2175,11 +2175,10 @@ static bool has_bbml2_noabort(const struct arm64_cpu_capabilities *caps, int scostatic bool hvo_compatible (const struct arm64_cpu_capabilities *caps,int scope) {/* -* We need BBML2 to support Block -> Table transitions without taking -* faults, and we need HW AF support to support changing the OA without -* taking faults. +* We need HW AF support to support changing vmemmap mapping level and +* OA without taking faults. */ -return cpu_supports_bbml2_noabort () &&supports_hw_af (scope); +return supports_hw_af (scope); }static void cpu_enable_pan (const struct arm64_cpu_capabilities *__unused)
Signature · 1 line
2.55.0.795.g602f6c329a-goog
PATCH 14/18 hugetlb_vmemmap: Rename mm/hugetlb_vmemmap.h to mm/hugetlb_vmemmap_internal.h
In reply to James HoughtonThis avoids the confusion with the new header that will be added in late commit, at include/linux/hugetlb_vmemmap.h.
The file rename in this patch isn't really required, but the header guard (now named _MM_HUGETLB_VMEMMAP_INTERNAL_H) does need a rename.
---
MAINTAINERS | 2 +-
mm/hugetlb.c | 2 +-
mm/hugetlb_sysfs.c | 2 +-
mm/hugetlb_vmemmap.c | 4 ++--
mm/{hugetlb_vmemmap.h => hugetlb_vmemmap_internal.h} | 6 +++---
mm/sparse-vmemmap.c | 2 +-
6 files changed, 9 insertions(+), 9 deletions(-)
rename mm/{hugetlb_vmemmap.h => hugetlb_vmemmap_internal.h} (95%)MAINTAINERS +1 -1
@@ -12021,7 +12021,7 @@ F: mm/hugetlb_cma.h F: mm/hugetlb_sysctl.c F: mm/hugetlb_sysfs.c F: mm/hugetlb_vmemmap.c -F: mm/hugetlb_vmemmap.h +F: mm/hugetlb_vmemmap_internal.h F: tools/testing/selftests/cgroup/test_hugetlb_memcg.c HVA ST MEDIA DRIVER
mm/hugetlb.c +1 -1
@@ -47,7 +47,7 @@#include <linux/node.h> #include <linux/page_owner.h> #include "internal.h" -#include "hugetlb_vmemmap.h" +#include "hugetlb_vmemmap_internal.h" #include "hugetlb_cma.h" #include "hugetlb_internal.h" #include <linux/page-isolation.h>
mm/hugetlb_sysfs.c +1 -1
@@ -8,7 +8,7 @@#include <linux/page_owner.h> #include <linux/page-isolation.h> -#include "hugetlb_vmemmap.h" +#include "hugetlb_vmemmap_internal.h" #include "hugetlb_internal.h" #define HSTATE_ATTR_RO (_name) \
mm/hugetlb_vmemmap.c +2 -2
@@ -16,10 +16,10 @@#include <linux/mmdebug.h> #include <linux/pagewalk.h> #include <linux/pgalloc.h> +#include <linux/hugetlb.h> -#include "linux/hugetlb.h" #include <asm/tlbflush.h> -#include "hugetlb_vmemmap.h" +#include "hugetlb_vmemmap_internal.h" #include "internal.h" /**
mm/hugetlb_vmemmap_internal.h renamed from mm/hugetlb_vmemmap.h +3 -3
@@ -6,8 +6,8 @@* * Author: Muchun Song <[email protected]> */ -#ifndef _LINUX_HUGETLB_VMEMMAP_H -#define _LINUX_HUGETLB_VMEMMAP_H +#ifndef _MM_HUGETLB_VMEMMAP_INTERNAL_H +#define _MM_HUGETLB_VMEMMAP_INTERNAL_H#include <linux/hugetlb.h> #include <linux/io.h> #include <linux/memblock.h>
@@ -95,4 +95,4 @@ static inline bool hugetlb_vmemmap_optimizable(const struct hstate *h) {return hugetlb_vmemmap_optimizable_size (h) !=0 ; } -#endif /* _LINUX_HUGETLB_VMEMMAP_H */ +#endif /* _MM_HUGETLB_VMEMMAP_INTERNAL_H */
mm/sparse-vmemmap.c +1 -1
@@ -32,7 +32,7 @@#include <asm/dma.h> #include <asm/tlbflush.h> -#include "hugetlb_vmemmap.h" +#include "hugetlb_vmemmap_internal.h" /* * Flags for vmemmap_populate_range and friends.
Signature · 1 line
2.55.0.795.g602f6c329a-goog
PATCH 15/18 hugetlb_vmemmap: Add a way to permanently disable HVO when needed
In reply to James Houghtonhugetlb_vmemmap_optimization_try_disable() is used by callers to permanently disable HVO if it becomes impossible to use.
If HVO is already actively in use, hugetlb_vmemmap_optimization_try_disable() returns false to inform the caller to take appropriate action (e.g., for arm64, not to online an incompatible late CPU).
--- MAINTAINERS | 1 + include/linux/hugetlb_vmemmap.h | 20 +++++++++++++++++ mm/hugetlb_vmemmap.c | 40 +++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 include/linux/hugetlb_vmemmap.h
MAINTAINERS +1 -0
@@ -12013,6 +12013,7 @@ F: Documentation/mm/hugetlbfs_reserv.rst F: Documentation/mm/vmemmap_dedup.rst F: fs/hugetlbfs/ F: include/linux/hugetlb.h +F: include/linux/hugetlb_vmemmap.h F: include/trace/events/hugetlbfs.h F: mm/hugetlb.c F: mm/hugetlb_cgroup.c
include/linux/hugetlb_vmemmap.h new file +20 -0
@@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_HUGETLB_VMEMMAP_H +#define _LINUX_HUGETLB_VMEMMAP_H + +#include <linux/types.h> + +#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP + +bool hugetlb_vmemmap_optimization_try_disable (void ); + +#else /* CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP */ + +static inline bool hugetlb_vmemmap_optimization_try_disable (void ) +{ +return true; +} + +#endif + +#endif /* _LINUX_HUGETLB_VMEMMAP_H */
mm/hugetlb_vmemmap.c +40 -0
@@ -17,6 +17,7 @@#include <linux/pagewalk.h> #include <linux/pgalloc.h> #include <linux/hugetlb.h> +#include <linux/hugetlb_vmemmap.h> #include <asm/tlbflush.h> #include "hugetlb_vmemmap_internal.h"
@@ -463,6 +464,39 @@ static int vmemmap_remap_alloc(const struct hstate *h, struct folio *folio,return ret; } +enum hugetlb_hvo_status { +HVO_INACTIVE =0 , +HVO_ACTIVE , +HVO_PERMANENTLY_INACTIVE , +}; +static enum hugetlb_hvo_status hvo_status =HVO_INACTIVE ; + +static bool hugetlb_hvo_status_try_set (enum hugetlb_hvo_status status) +{ +enum hugetlb_hvo_status old; + + old =READ_ONCE (hvo_status); + +retry: +/* The current setting is what we want. */ +if (old == status) +return true; + +/* The current setting cannot be changed. */ +if (old !=HVO_INACTIVE ) +return false; + +if (!try_cmpxchg_relaxed (&hvo_status, &old, status)) + goto retry; + +return true; +} + +bool hugetlb_vmemmap_optimization_try_disable (void ) +{ +return hugetlb_hvo_status_try_set (HVO_PERMANENTLY_INACTIVE ); +} +static bool vmemmap_optimize_enabled =IS_ENABLED (CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON );static int __init hugetlb_vmemmap_optimize_param (char *buf) {
@@ -571,6 +605,9 @@ static bool vmemmap_should_optimize_folio(const struct hstate *h, struct folio *if (!hugetlb_vmemmap_optimizable (h))return false; +if (!hugetlb_hvo_status_try_set (HVO_ACTIVE )) +return false; +return true; }
@@ -809,6 +846,9 @@ static bool vmemmap_should_optimize_bootmem_page(struct huge_bootmem_page *m) !IS_ALIGNED(psize, pmd_vmemmap_size))return false; +if (!hugetlb_hvo_status_try_set (HVO_ACTIVE )) +return false; +return true; }
Signature · 1 line
2.55.0.795.g602f6c329a-goog
PATCH 16/18 arm64: Allow "optional" CPU features to be required sometimes
In reply to James HoughtonThe HugeTLB vmemmap optimization feature should be optional if HVO is not in fact in use. Otherwise, on systems where the boot CPUs support HVO (HW AF) but the late CPUs do not, simply compiling with HVO support will prevent late-onlining.
It is desirable to always compile in support for HVO without regressing late-onlining of CPUs on systems where HVO is not in use at run-time.
If HVO is in fact in use at late-online time, HVO must have a way to prevent such incompatible CPUs from being onlined.
Add the late_cpu_enable() callback to provide this "sometimes required" functionality.
--- arch/arm64/include/asm/cpufeature.h | 8 ++++++++ arch/arm64/kernel/cpufeature.c | 8 ++++++++ 2 files changed, 16 insertions(+)
arch/arm64/include/asm/cpufeature.h +8 -0
@@ -367,6 +367,14 @@ struct arm64_cpu_capabilities {* routine must check it before taking any action. */ void (*cpu_enable)(const struct arm64_cpu_capabilities *cap); +/* +* In rare cases, capabilities are *sometimes* optional for late CPUs. +* This callback allows a capability to prevent onlining of +* incompatible CPUs when the capability is in fact required. +* +* Returns true iff onlining the CPU is permitted. +*/ +bool (*late_cpu_enable)(const struct arm64_cpu_capabilities *cap); union { struct { /* To be used for erratum handling only */ struct midr_range midr_range;
arch/arm64/kernel/cpufeature.c +8 -0
@@ -3684,6 +3684,14 @@ static void verify_local_cpu_caps(u16 scope_mask)*/ if (!cpu_has_cap && !cpucap_late_cpu_optional (caps))break ; + +/* +* Some optional features may in fact be required due +* to particular runtime conditions. +*/ +if (caps->late_cpu_enable && !caps->late_cpu_enable (caps)) +break ; +/* * We have to issue cpu_enable() irrespective of * whether the CPU has it or not, as it is enabeld
Signature · 1 line
2.55.0.795.g602f6c329a-goog
PATCH 17/18 arm64: Permit onlining of HVO-incompatible late CPUs if HVO is not in use
In reply to James HoughtonFirst, HVO must have ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU so that incompatible CPUs can be onlined if HVO is not in use. The late_cpu_enable() callback is used to check if HVO is truly in use.
If the late CPU is incompatible with HVO and HVO is *not* in use, HVO becomes permanently disabled.
If the late CPU is incompatible with HVO and HVO *is* in use, the CPU is not onlined.
If the late CPU is compatible with HVO, there is no issue.
--- arch/arm64/kernel/cpufeature.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)
arch/arm64/kernel/cpufeature.c +21 -1
@@ -76,6 +76,7 @@#include <linux/kasan.h> #include <linux/percpu.h> #include <linux/sched/isolation.h> +#include <linux/hugetlb_vmemmap.h> #include <asm/arm_pmuv3.h> #include <asm/cpu.h>
@@ -2181,6 +2182,22 @@ static bool hvo_compatible(const struct arm64_cpu_capabilities *caps, int scope)return supports_hw_af (scope); } +static bool late_cpu_enable_hvo (const struct arm64_cpu_capabilities *__unused) +{ +#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP +if (cpu_has_hw_af ()) +return true; + +/* +* If the CPU does not support HW AF, we cannot online it if HVO is +* currently in use. +*/ +return hugetlb_vmemmap_optimization_try_disable (); +#else +return true; +#endif +} +static void cpu_enable_pan (const struct arm64_cpu_capabilities *__unused) {/*
@@ -3079,8 +3096,11 @@ static const struct arm64_cpu_capabilities arm64_features[] = { { .desc = "HugeTLB Vmemmap Optimization Support", .capability = ARM64_HVO_COMPATIBLE, - .type = ARM64_CPUCAP_SYSTEM_FEATURE, + .type =ARM64_CPUCAP_SCOPE_SYSTEM | +ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU | + ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU, .matches = hvo_compatible, + .late_cpu_enable = late_cpu_enable_hvo, }, { .desc = "52-bit Virtual Addressing for KVM (LPA2)",
Signature · 1 line
2.55.0.795.g602f6c329a-goog
Catalin Marinas
maintainerRe: [PATCH 17/18] arm64: Permit onlining of HVO-incompatible late CPUs if HVO is not in use
Quoted 9 lines · James Houghton
First, HVO must have
ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPUso that incompatible CPUs can be onlined if HVO is not in use. Thelate_cpu_enable()callback is used to check if HVO is truly in use.If the late CPU is incompatible with HVO and HVO is *not* in use, HVO becomes permanently disabled.
If the late CPU is incompatible with HVO and HVO *is* in use, the CPU is not onlined.
I haven't looked in detail at the logic here but do we actually care about this? We still need to solve the BBML2 requirement around patches 12 and 13. BBML2 won't be an optionally late feature for other reasons, so such CPUs won't be allowed. This leaves us with HW AF. In mainline, the check is done on the sanitised regs but IIRC these can still be overridden on late CPU onlining with a warning. We only use this check as an optimisation, so not an issue in mainline but can be come problematic with HVO.
If we don't have a strong use-case for mismatched HAFDBS support, I'd drop these last patches for now.
Signature · 1 line
Catalin
Re: [PATCH 17/18] arm64: Permit onlining of HVO-incompatible late CPUs if HVO is not in use
Quoted 12 lines · Catalin Marinas
Quoted 9 lines · James Houghton
First, HVO must have
ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPUso that incompatible CPUs can be onlined if HVO is not in use. Thelate_cpu_enable()callback is used to check if HVO is truly in use.If the late CPU is incompatible with HVO and HVO is *not* in use, HVO becomes permanently disabled.
If the late CPU is incompatible with HVO and HVO *is* in use, the CPU is not onlined.
Quoted 8 lines
I haven't looked in detail at the logic here but do we actually care about this? We still need to solve the BBML2 requirement around patches 12 and 13. BBML2 won't be an optionally late feature for other reasons, so such CPUs won't be allowed. This leaves us with HW AF. In mainline, the check is done on the sanitised regs but IIRC these can still be overridden on late CPU onlining with a warning. We only use this check as an optimisation, so not an issue in mainline but can be come problematic with HVO.
I just tried reading through cpufeature.c again, and I think, because ID_AA64MMFR1_EL1_HAFDBS is marked as FTR_STRICT, we will indeed get a taint if there is a mismatch for HAFDBS. When this happens, the sanitized register value should be set to the lowest value. This value should indeed be safe to use.
ARM64_HW_DBM implies that it's expected for CPUs to have mismatched support for HAFDBS (indeed, this was the impression I was under as well), so it seems like a bug that HAFDBS is labeled FTR_STRICT. What do you think?
I still need to work out exactly how cpufeature.c is going to learn about whether or not HVO has been enabled on the command line (hugetlb_free_vmemmap=1) for the purposes of preventing incompatible CPUs getting onlined; I'll figure something out.
Quoted 2 lines
If we don't have a strong use-case for mismatched HAFDBS support, I'd drop these last patches for now.
I'll drop these last few patches (14-18).
The only caveat is that by doing this, I don't see a way to keep HVO runtime-enableable. In other words, if the user booted without specifying hugetlb_free_vmemmap=1 (assuming HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON=n), they cannot dynamically turn it on after the fact. This is not a problem for me.
PATCH 18/18 arm64: Remove user-selectable HVO Kconfig
In reply to James HoughtonThe Kconfig is not ideal; it is niche and difficult to understand for users. The Kconfig was added to avoid the following regression: if HVO is compiled in and all boot CPUs support it, any late CPUs that do not will not be onlined.
The implementation now avoids this regression as long as HVO is not actively in use. Therefore, drop the Kconfig.
--- arch/arm64/Kconfig | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-)
arch/arm64/Kconfig +1 -12
@@ -259,7 +259,7 @@ config ARM64 select USER_STACKTRACE_SUPPORT select VDSO_GETRANDOM select VMAP_STACK - select ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP if ARM64_WANT_OPTIMIZE_HUGETLB_VMEMMAP + select ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP if ARM64_HW_AFDBM help ARM 64-bit (AArch64) Linux support.
@@ -1608,17 +1608,6 @@ config ARCH_SPARSEMEM_ENABLE def_bool y select SPARSEMEM_VMEMMAP_ENABLE -config ARM64_WANT_OPTIMIZE_HUGETLB_VMEMMAP - bool "Enable support for HugeTLB Vmemmap Optimization (HVO)" - default n - depends on HUGETLB_PAGE && SPARSEMEM_VMEMMAP && ARM64_HW_AFDBM - help - Enable support for HVO. When enabled, late-onlining of CPUs that do - not support features required by HVO will not be onlined if all boot - CPUs do have such support. - - If unsure, say N - config HW_PERF_EVENTS def_bool y depends on ARM_PMU
Signature · 1 line
2.55.0.795.g602f6c329a-goog
Muchun Song
maintainerQuoted 14 lines · James Houghton
Hi everyone,
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).
Hi,
Thanks for your ongoing efforts on supporting HVO on arm64. This is truly one of the most useful features that arm64 has been missing for a while.
Quoted 7 lines
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 four parts:
1. Some preparatory changes (patches 1-3)
1. Bare minimum HVO support (patches 4-10)
It looks like there is a typo in the number here.
Quoted 9 lines
2. Drop
BBML2_NOABORTrequirement for HVO (patches 11-13)
3. Drop the user-configurableKconfigfor HVO (patches 14-18)Parts 3 and 4 are technically optional. More details below.
The main functional caveat with this series is that bootmem HugeTLB pages are not "pre-HVOed". They will be HVOed, but because at pre-HVO time SMP CPUs have not been enabled, we cannot query for full system support.
Do you mean that the support for AF might vary across different CPUs? I'm not that familiar with arm64, so it seems a bit strange to me that such basic hardware features can differ so much from one CPU to another.
Quoted 6 lines
This series is based on 7.2-rc2 (
0e35b9b6ec0f).This series almost 100% cleanly applies to mm-new, which has some of Muchun's HVO patches, with one trivial conflict. I imagine this series will conflict pretty heavily with some of Muchun's other patches[2].
You know, as we iterate on HVO, the codebase is becoming increasingly complex and difficult to maintain. Aside from the work in [2] that you mentioned, I actually have some local patches aimed at refactoring hugetlb_vmemmap.c as well to reduce this maintenance complexity. I really want to avoid piling more code on top of it right now, as that would only make things worse.
Therefore, my personal suggestion is that if this approach is theoretically validated on arm64, it it better to wait until the HVO refactoring is complete, and then review your work based on the clean codebase. For now, what we need to focus on is evaluating the feasibility and soundness of the approach itself. What do you think?
Quoted 12 lines
-- The AF trick --
The trick is that translations with the AF unset cannot be cached in the TLB (see Rule
R_DWZCQin 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.
Quoted 4 lines
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 compatbility checks all CPUs for HW AF updates.
Sorry, I am not familiar with arm64, so I will leave it to the arm64 maintainers (or experts) to verify the feasibility here. Once the viability of the approach is confirmed, I will look into the HVO-related implementation details and consider how to support this feature on top of the refactored code in the future.
Quoted 10 lines
-- 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 PTE remains mapped normally (RW mapping to a real page of struct pages), but the remaining PTEs in the vmemmap are mapped read-only to a shared page of struct pages (that is, there is an OA change and a permissions change).
Just to clarify, does OA mean Output Address? Please spell it out for readers who are less familiar with this context.
Muchun,
Thanks.
Quoted 132 lines
- When disabling HVO for a page, the RO PTEs are remapped back to RW PTEs that point to newly reallocated pages of struct pages. The PMD block -> table transition is not undone.
In patches 4-10 of this series, I use the Access Flag trick to do the PTE OA and permissions updates. We rely on
BBML2_NOABORTfor the PMD block -> table transition.In patches 11-13, I re-use the Access Flag trick to do the PMD block -> table transition without needing
BBML2_NOABORT. For systems that supportBBML2_NOABORT, the logic is unchanged.I am aware of Linu's BBML3 patches; I've opted not to rebase onto them for now, but I am happy to do so later.
-- Late-onlining of CPUs that do not support HW AF --
One of the complications with this series is how to handle late-onlining of CPUs that do not have HW AF when HVO is in use. Naively, if HVO (HW AF) is supported on all boot CPUs and the kernel is compiled with HVO support, late CPUs that do not support HVO will not be onlined. This is a regression.
This series provides two ways of dealing with this. First, add a default-off
Kconfigfor users to enable HVO support, avoiding the regression. This is not ideal.Patches 14-18 get rid of the new
Kconfigby allowing onlining of HVO-incompatible late CPUs as long as HVO is not actively in use.-- Litmus test --
The following Herd litmus test demonstrates the PTE update routine:
AArch64 TTDFaultlessUpdate
Variant=vmsa
TTHM=HA
{
uint64_tx=1;
uint64_ty=2;
[PTE(x)]=(oa:PA(x), af:1);
0:X0=PTE(x); 1:X0=PTE(x);
0:X1=x; 1:X1=x;
pteval_t0:X2=(oa:PA(x), af:0);
pteval_t0: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 violate BBM requirements. *)
This test must be run with herdtools with Nikos's changes[3] to more accurately model BBM requirements. When tried, the output will notably *not* contain the "Warning-BBM-expected" flag.
-- Testing --
I haven't yet done extensive testing of this series. HVO is correctly freeing pages on my system, and the hugetlb-vmemmap test passes. Freeing HVOed HugeTLB pages also seems to function normally.
[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://github.com/herd/herdtools7/pull/1864James Houghton (18):
hugetlb_vmemmap: Always flush TLB if needed upon PTE remapping
hugetlb_vmemmap: Movevmemmap_get_tailup
hugetlb_vmemmap: Leave pages partially HVOed upon restore failure
hugetlb_vmemmap: Usetry_update_vmemmap_pteto update in-use PTEs
hugetlb_vmemmap: Allow architectures not to allow HVO at runtime
arm64: Renamecpu_has_hw_aftosystem_has_hw_af
arm64: Addsystem_supports_hvo
arm64: Implementtry_update_vmemmap_pteusing the AF trick
arm64: Prevent HVO if the HVO system feature is not enabled
arm64: Support hugetlb vmemmap optimization
hugetlb_vmemmap: Usetry_populate_vmemmap_pmdfor replacing in-use
PMDs
arm64: Implementtry_populate_vmemmap_pmdusing AF trick
arm64: DropBBML2_NOABORTrequirement for HVO
hugetlb_vmemmap: Renamemm/hugetlb_vmemmap.hto
mm/hugetlb_vmemmap_internal.h
hugetlb_vmemmap: Add a way to permanently disable HVO when needed
arm64: Allow "optional" CPU features to be required sometimes
arm64: Permit onlining of HVO-incompatible late CPUs if HVO is not in
use
arm64: Remove user-selectable HVOKconfigMAINTAINERS | 3 +- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/cpucaps.h | 2 + arch/arm64/include/asm/cpufeature.h | 39 ++- arch/arm64/include/asm/hugetlb.h | 7 + arch/arm64/include/asm/pgalloc.h | 54 ++++ arch/arm64/include/asm/pgtable.h | 57 ++++- arch/arm64/kernel/cpufeature.c | 43 ++++ arch/arm64/tools/cpucaps | 1 + arch/loongarch/include/asm/pgalloc.h | 8 + arch/loongarch/include/asm/pgtable.h | 8 + arch/riscv/include/asm/pgalloc.h | 8 + arch/riscv/include/asm/pgtable.h | 8 + arch/x86/include/asm/pgalloc.h | 8 + arch/x86/include/asm/pgtable.h | 8 + include/asm-generic/hugetlb.h | 7 + include/linux/hugetlb_vmemmap.h | 20 ++ include/linux/pgalloc.h | 20 ++ include/linux/pgtable.h | 21 ++ mm/hugetlb.c | 2 +- mm/hugetlb_sysfs.c | 2 +- mm/hugetlb_vmemmap.c | 237 +++++++++++++----- ...b_vmemmap.h => hugetlb_vmemmap_internal.h} | 6 +- mm/sparse-vmemmap.c | 2 +- 24 files changed, 489 insertions(+), 83 deletions(-)create mode 100644
include/linux/hugetlb_vmemmap.hrenamemm/{hugetlb_vmemmap.h=>hugetlb_vmemmap_internal.h} (95%)base-commit:
0e35b9b6ec0ffcc5e23cbdec09f5c622ad532b53Signature · 1 line
2.55.0.795.g602f6c329a-goog
Quoted 13 lines · Muchun Song
Quoted 12 lines · James Houghton
Hi everyone,
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).
Quoted 4 lines
Hi,
Thanks for your ongoing efforts on supporting HVO on arm64. This is truly one of the most useful features that arm64 has been missing for a while.
Hi Muchun,
Thanks for taking a look so quickly. :)
Quoted 9 lines
Quoted 7 lines
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 four parts:
1. Some preparatory changes (patches 1-3)
1. Bare minimum HVO support (patches 4-10)It looks like there is a typo in the number here.
Oh yes, so there is. I'll fix it in the next version of the cover letter.
Quoted 5 lines
Quoted 5 lines
2. Drop
BBML2_NOABORTrequirement for HVO (patches 11-13)
3. Drop the user-configurableKconfigfor HVO (patches 14-18)Parts 3 and 4 are technically optional. More details below.
Quoted 8 lines
Quoted 4 lines
The main functional caveat with this series is that bootmem HugeTLB pages are not "pre-HVOed". They will be HVOed, but because at pre-HVO time SMP CPUs have not been enabled, we cannot query for full system support.
Do you mean that the support for AF might vary across different CPUs? I'm not that familiar with arm64, so it seems a bit strange to me that such basic hardware features can differ so much from one CPU to another.
Yes, hardware updates of the Access Flag is a per-CPU feature. It is available for a CPU to use if TCR_EL1.HA is set. TCR_EL1 is a system register; each CPU has its own. (Linux will always enable HW AF for a CPU when it is onlined[1] if support is advertised, so we simply need to check if support is advertised to know that it is in fact enabled.)
These days it is not uncommon for a system to have two (or more?) different core implementations, like with "fast" cores and "efficient" cores.
[1] See the CONFIG_ARM64_HW_AFDBM bits in arch/arm64/mm/proc.S
Quoted 13 lines
Quoted 5 lines
This series is based on 7.2-rc2 (
0e35b9b6ec0f).This series almost 100% cleanly applies to mm-new, which has some of Muchun's HVO patches, with one trivial conflict. I imagine this series will conflict pretty heavily with some of Muchun's other patches[2].
You know, as we iterate on HVO, the codebase is becoming increasingly complex and difficult to maintain. Aside from the work in [2] that you mentioned, I actually have some local patches aimed at refactoring
hugetlb_vmemmap.cas well to reduce this maintenance complexity. I really want to avoid piling more code on top of it right now, as that would only make things worse.
Quoted 5 lines
Therefore, my personal suggestion is that if this approach is theoretically validated on arm64, it it better to wait until the HVO refactoring is complete, and then review your work based on the clean codebase. For now, what we need to focus on is evaluating the feasibility and soundness of the approach itself. What do you think?
I agree. The first order of business should be for the Arm folks to tell me if they think this approach is unsound (though I've been chatting with Will about this for quite some time :)).
I'm happy to rebase on top of your refactoring work when it is ready.
Quoted 17 lines
Quoted 12 lines
-- The AF trick --
The trick is that translations with the AF unset cannot be cached in the TLB (see Rule
R_DWZCQin 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.Quoted 4 lines
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 compatbility checks all CPUs for HW AF updates.
Quoted 5 lines
Sorry, I am not familiar with arm64, so I will leave it to the arm64 maintainers (or experts) to verify the feasibility here. Once the viability of the approach is confirmed, I will look into the HVO-related implementation details and consider how to support this feature on top of the refactored code in the future.
Sounds good, thanks Muchun.
I'm hoping you can look at patches 1-3 anyway. I think they're the right thing to do, even without the arm64 changes.
Quoted 10 lines
Quoted 9 lines
-- 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 PTE remains mapped normally (RW mapping to a real page of struct pages), but the remaining PTEs in the vmemmap are mapped read-only to a shared page of struct pages (that is, there is an OA change and a permissions change).
Quoted 2 lines
Just to clarify, does OA mean Output Address? Please spell it out for readers who are less familiar with this context.
That's right, sorry about that. I'll spell it out more clearly in future versions of the series.
Thanks, Muchun!
Muchun Song
maintainerQuoted 18 lines · James Houghton
Quoted 13 lines · Muchun Song
Quoted 12 lines · James Houghton
Hi everyone,
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).
Quoted 6 lines
Quoted 4 lines
Hi,
Thanks for your ongoing efforts on supporting HVO on arm64. This is truly one of the most useful features that arm64 has been missing for a while.
Hi Muchun,
Hi,
Quoted 35 lines
Thanks for taking a look so quickly. :)
Quoted 9 lines
Quoted 7 lines
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 four parts:
1. Some preparatory changes (patches 1-3)
1. Bare minimum HVO support (patches 4-10)It looks like there is a typo in the number here.
Oh yes, so there is. I'll fix it in the next version of the cover letter.
Quoted 5 lines
Quoted 5 lines
2. Drop
BBML2_NOABORTrequirement for HVO (patches 11-13)
3. Drop the user-configurableKconfigfor HVO (patches 14-18)Parts 3 and 4 are technically optional. More details below.
Quoted 8 lines
Quoted 4 lines
The main functional caveat with this series is that bootmem HugeTLB pages are not "pre-HVOed". They will be HVOed, but because at pre-HVO time SMP CPUs have not been enabled, we cannot query for full system support.
Do you mean that the support for AF might vary across different CPUs? I'm not that familiar with arm64, so it seems a bit strange to me that such basic hardware features can differ so much from one CPU to another.
Yes, hardware updates of the Access Flag is a per-CPU feature. It is available for a CPU to use if TCR_EL1.HA is set.
TCR_EL1is a system register; each CPU has its own. (Linux will always enable HW AF for a CPU when it is onlined[1] if support is advertised, so we simply need to check if support is advertised to know that it is in fact enabled.)
Quoted 5 lines
These days it is not uncommon for a system to have two (or more?) different core implementations, like with "fast" cores and "efficient" cores.
[1] See the
CONFIG_ARM64_HW_AFDBMbits inarch/arm64/mm/proc.S
Thanks for your detailed explanation. When enabling HVO via the cmdline, can we simply prevent CPUs that do not support AF from coming online? Would implementing it this way be much simpler? In practice, developers definitely know whether the current system is suitable for enabling HVO. If some CPUs do not support AF, they would just need to evaluate the trade-off between memory savings and having fewer online CPUs than expected.
For scenarios where HVO is enabled via sysctl, we simply need to check if all CPUs support AF. If any do not, the system should return an error.
Then, we can proceed with the Pre-HVO.
Quoted 49 lines
Quoted 14 lines
Quoted 6 lines
This series is based on 7.2-rc2 (
0e35b9b6ec0f).This series almost 100% cleanly applies to mm-new, which has some of Muchun's HVO patches, with one trivial conflict. I imagine this series will conflict pretty heavily with some of Muchun's other patches[2].
You know, as we iterate on HVO, the codebase is becoming increasingly complex and difficult to maintain. Aside from the work in [2] that you mentioned, I actually have some local patches aimed at refactoring
hugetlb_vmemmap.cas well to reduce this maintenance complexity. I really want to avoid piling more code on top of it right now, as that would only make things worse.Quoted 5 lines
Therefore, my personal suggestion is that if this approach is theoretically validated on arm64, it it better to wait until the HVO refactoring is complete, and then review your work based on the clean codebase. For now, what we need to focus on is evaluating the feasibility and soundness of the approach itself. What do you think?
I agree. The first order of business should be for the Arm folks to tell me if they think this approach is unsound (though I've been chatting with Will about this for quite some time :)).
I'm happy to rebase on top of your refactoring work when it is ready.
Quoted 17 lines
Quoted 12 lines
-- The AF trick --
The trick is that translations with the AF unset cannot be cached in the TLB (see Rule
R_DWZCQin 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.Quoted 4 lines
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 compatbility checks all CPUs for HW AF updates.
Quoted 5 lines
Sorry, I am not familiar with arm64, so I will leave it to the arm64 maintainers (or experts) to verify the feasibility here. Once the viability of the approach is confirmed, I will look into the HVO-related implementation details and consider how to support this feature on top of the refactored code in the future.
Quoted 4 lines
Sounds good, thanks Muchun.
I'm hoping you can look at patches 1-3 anyway. I think they're the right thing to do, even without the arm64 changes.
Yes, I've looked it over. I think that making modifications across several areas will be much simpler on top of my refactored codebase. For example:
- In patch 2: There is no longer a need to move vmemmap_get_tail().
- In patch 3: Handling partially-HVOed pages becomes straightforward because vmemmap_restore_pte() no longer relies on ->vmemmap_tail to detect whether a restoration is needed.
- In patch 4: I have updated the ->remap_pte callback to return an int as well, making it easy to adapt your changes.
Overall, the implementation is significantly cleaner, so I won't list every detail here.
Muchun,
Thanks.
Quoted 18 lines
Quoted 11 lines
Quoted 10 lines
-- 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 PTE remains mapped normally (RW mapping to a real page of struct pages), but the remaining PTEs in the vmemmap are mapped read-only to a shared page of struct pages (that is, there is an OA change and a permissions change).Quoted 2 lines
Just to clarify, does OA mean Output Address? Please spell it out for readers who are less familiar with this context.
That's right, sorry about that. I'll spell it out more clearly in future versions of the series.
Thanks, Muchun!
Quoted 26 lines · Muchun Song
Quoted 13 lines · James Houghton
Quoted 3 lines · Muchun Song
Do you mean that the support for AF might vary across different CPUs? I'm not that familiar with arm64, so it seems a bit strange to me that such basic hardware features can differ so much from one CPU to another.
Yes, hardware updates of the Access Flag is a per-CPU feature. It is available for a CPU to use if TCR_EL1.HA is set.
TCR_EL1is a system register; each CPU has its own. (Linux will always enable HW AF for a CPU when it is onlined[1] if support is advertised, so we simply need to check if support is advertised to know that it is in fact enabled.)Quoted 5 lines
These days it is not uncommon for a system to have two (or more?) different core implementations, like with "fast" cores and "efficient" cores.
[1] See the
CONFIG_ARM64_HW_AFDBMbits inarch/arm64/mm/proc.SThanks for your detailed explanation. When enabling HVO via the cmdline, can we simply prevent CPUs that do not support AF from coming online? Would implementing it this way be much simpler? In practice, developers definitely know whether the current system is suitable for enabling HVO. If some CPUs do not support AF, they would just need to evaluate the trade-off between memory savings and having fewer online CPUs than expected.
Quoted 4 lines
For scenarios where HVO is enabled via sysctl, we simply need to check if all CPUs support AF. If any do not, the system should return an error.
Then, we can proceed with the Pre-HVO.
I don't think it makes sense to try to implement pre-HVO.
We cannot do HVO if any boot CPUs do not support HW AF, as HW AF will be required to free the HugeTLB pages later, which we should continue to support. Pre-HVO (today anyway) happens before all boot CPUs are onlined. IMO it is not okay to prevent boot CPUs from onlining.
Let's say for a moment that HVO cannot be toggled at run-time, then the best we can do is:
- If a user does not specify hugetlb_free_vmemmap=1, we can always allow onlining of late CPUs
- If a user specifies hugetlb_free_vmemmap=1 but not all boot CPUs support HW AF, we can always allow onlining of late CPUs.
- If a user specifies hugetlb_free_vmemmap=1 and all boot CPUs support HW AF, we must not allow onlining incompatible late CPUs.
HVO compatibility has to be modeled as an Arm system feature. To support conditional onlining of late CPUs based on whether or not "HVO is being used", we still need the extra cpufeature.c logic.
So if HVO cannot be toggled at run-time, we can simplify the definition of "is HVO being used?" (for the purposes of determining if late CPUs can be onlined). We can simplify it from what it is now ("are there any optimized pages?") to "vmemmap_optimize_enabled==true?". That allows us to drop patch 15 (which then needs a slight change to patch 17), but that's about it. So this is a slight simplification, which is nice.
If you (or the Arm folks) feel strongly, I'm happy to write this simplification.
Quoted 14 lines
Quoted 2 lines
I'm hoping you can look at patches 1-3 anyway. I think they're the right thing to do, even without the arm64 changes.
Yes, I've looked it over. I think that making modifications across several areas will be much simpler on top of my refactored codebase. For example:
- In patch 2: There is no longer a need to move
vmemmap_get_tail().
- In patch 3: Handling partially-HVOed pages becomes straightforward becausevmemmap_restore_pte()no longer relies on ->vmemmap_tail to detect whether a restoration is needed.
- In patch 4: I have updated the ->remap_pte callback to return an int as well, making it easy to adapt your changes.
Quoted 2 lines
Overall, the implementation is significantly cleaner, so I won't list every detail here.
Thanks! Good to know I can basically drop those patches. :)
Please CC me on these HVO simplifications when you send them out, thanks!
Muchun Song
maintainerQuoted 34 lines · James Houghton
Quoted 29 lines · Muchun Song
Quoted 13 lines · James Houghton
Quoted 3 lines · Muchun Song
Do you mean that the support for AF might vary across different CPUs? I'm not that familiar with arm64, so it seems a bit strange to me that such basic hardware features can differ so much from one CPU to another.
Yes, hardware updates of the Access Flag is a per-CPU feature. It is available for a CPU to use if TCR_EL1.HA is set.
TCR_EL1is a system register; each CPU has its own. (Linux will always enable HW AF for a CPU when it is onlined[1] if support is advertised, so we simply need to check if support is advertised to know that it is in fact enabled.)Quoted 5 lines
These days it is not uncommon for a system to have two (or more?) different core implementations, like with "fast" cores and "efficient" cores.
[1] See the
CONFIG_ARM64_HW_AFDBMbits inarch/arm64/mm/proc.SThanks for your detailed explanation. When enabling HVO via the cmdline, can we simply prevent CPUs that do not support AF from coming online? Would implementing it this way be much simpler? In practice, developers definitely know whether the current system is suitable for enabling HVO. If some CPUs do not support AF, they would just need to evaluate the trade-off between memory savings and having fewer online CPUs than expected.
For scenarios where HVO is enabled via sysctl, we simply need to check if all CPUs support AF. If any do not, the system should return an error.
Quoted 8 lines
Quoted 1 line
Then, we can proceed with the Pre-HVO.
I don't think it makes sense to try to implement pre-HVO.
We cannot do HVO if any boot CPUs do not support HW AF, as HW AF will be required to free the HugeTLB pages later, which we should continue to support. Pre-HVO (today anyway) happens before all boot CPUs are onlined. IMO it is not okay to prevent boot CPUs from onlining.
When the boot CPU starts, it first checks if the hardware (the current boot CPU) supports AF (The selection of the Boot CPU is critical here If the user really wants to enable HVO). If AF is supported and HVO is enabled via cmdline, we perform a Pre-HVO. Subsequently, any CPUs that do not support AF will be prevented from coming online.
If the boot CPU does not support AF, enabling HVO is not permitted—neither via cmdline nor sysctl. All CPUs are allowed to be online.
Once the system has started, there is only one way to enable HVO, and that is through sysctl. 1) If the current system includes any CPUs that do not support AF, enabling HVO is not permitted (unless the user chooses to take these CPUs that don't support AF offline). 2) If all online CPUs in the current system support AF, then HVO can be enabled. In this case, the system must also block any CPUs that do not support AF from coming online in the future. Of course, if a user specifically wants those CPUs to be allowed to come online, they can choose to disable HVO first (At the same time, this means that the HVO-optimized HugeTLB must be freed first as well).
My proposal assumes that even though the system supports different types of CPUs, their support for AF (I suppose this is a very basic feature nowadays) is generally the same. I believe that for servers (Only servers stand to benefit more from HVO), there shouldn't be a mix of those that support AF and those that don't. At the very least, such a situation would be extremely uncommon. I believe this may be acceptable. That said, if we go this route, is there a way to simplify the code implementation even further?. Of course, this is just my personal speculation. Please let me know if I've missed anything.
Quoted 38 lines
Let's say for a moment that HVO cannot be toggled at run-time, then the best we can do is:
- If a user does not specify hugetlb_free_vmemmap=1, we can always allow onlining of late CPUs
- If a user specifies hugetlb_free_vmemmap=1 but not all boot CPUs support HW AF, we can always allow onlining of late CPUs.
- If a user specifies hugetlb_free_vmemmap=1 and all boot CPUs support HW AF, we must not allow onlining incompatible late CPUs.HVO compatibility has to be modeled as an Arm system feature. To support conditional onlining of late CPUs based on whether or not "HVO is being used", we still need the extra
cpufeature.clogic.So if HVO cannot be toggled at run-time, we can simplify the definition of "is HVO being used?" (for the purposes of determining if late CPUs can be onlined). We can simplify it from what it is now ("are there any optimized pages?") to "vmemmap_optimize_enabled==true?". That allows us to drop patch 15 (which then needs a slight change to patch 17), but that's about it. So this is a slight simplification, which is nice.
If you (or the Arm folks) feel strongly, I'm happy to write this simplification.
Quoted 14 lines
Quoted 2 lines
I'm hoping you can look at patches 1-3 anyway. I think they're the right thing to do, even without the arm64 changes.
Yes, I've looked it over. I think that making modifications across several areas will be much simpler on top of my refactored codebase. For example:
- In patch 2: There is no longer a need to move
vmemmap_get_tail().
- In patch 3: Handling partially-HVOed pages becomes straightforward becausevmemmap_restore_pte()no longer relies on ->vmemmap_tail to detect whether a restoration is needed.
- In patch 4: I have updated the ->remap_pte callback to return an int as well, making it easy to adapt your changes.
Quoted 6 lines
Quoted 2 lines
Overall, the implementation is significantly cleaner, so I won't list every detail here.
Thanks! Good to know I can basically drop those patches. :)
Please CC me on these HVO simplifications when you send them out, thanks!
No problem. Will do.
Thanks.
Quoted 42 lines · Muchun Song
Quoted 33 lines · James Houghton
Quoted 29 lines · Muchun Song
Quoted 13 lines · James Houghton
Quoted 3 lines · Muchun Song
Do you mean that the support for AF might vary across different CPUs? I'm not that familiar with arm64, so it seems a bit strange to me that such basic hardware features can differ so much from one CPU to another.
Yes, hardware updates of the Access Flag is a per-CPU feature. It is available for a CPU to use if TCR_EL1.HA is set.
TCR_EL1is a system register; each CPU has its own. (Linux will always enable HW AF for a CPU when it is onlined[1] if support is advertised, so we simply need to check if support is advertised to know that it is in fact enabled.)Quoted 5 lines
These days it is not uncommon for a system to have two (or more?) different core implementations, like with "fast" cores and "efficient" cores.
[1] See the
CONFIG_ARM64_HW_AFDBMbits inarch/arm64/mm/proc.SThanks for your detailed explanation. When enabling HVO via the cmdline, can we simply prevent CPUs that do not support AF from coming online? Would implementing it this way be much simpler? In practice, developers definitely know whether the current system is suitable for enabling HVO. If some CPUs do not support AF, they would just need to evaluate the trade-off between memory savings and having fewer online CPUs than expected.
For scenarios where HVO is enabled via sysctl, we simply need to check if all CPUs support AF. If any do not, the system should return an error.
Quoted 8 lines
Quoted 1 line
Then, we can proceed with the Pre-HVO.
I don't think it makes sense to try to implement pre-HVO.
We cannot do HVO if any boot CPUs do not support HW AF, as HW AF will be required to free the HugeTLB pages later, which we should continue to support. Pre-HVO (today anyway) happens before all boot CPUs are onlined. IMO it is not okay to prevent boot CPUs from onlining.
Quoted 5 lines
When the boot CPU starts, it first checks if the hardware (the current boot CPU) supports AF (The selection of the Boot CPU is critical here If the user really wants to enable HVO). If AF is supported and HVO is enabled via cmdline, we perform a Pre-HVO. Subsequently, any CPUs that do not support AF will be prevented from coming online.
I understand. I don't think we should do this. I think it's much better to always be able to start all boot CPUs[1] and simply rely on "normal" HVO.
[1] Just to be clear on terminology, I'm using "boot CPUs" to refer to all CPUs that are onlined at boot-time, not just to *the* boot CPU that starts the kernel.
Quoted 11 lines
If the boot CPU does not support AF, enabling HVO is not permitted—neither via cmdline nor sysctl. All CPUs are allowed to be online.
Once the system has started, there is only one way to enable HVO, and that is through sysctl. 1) If the current system includes any CPUs that do not support AF, enabling HVO is not permitted (unless the user chooses to take these CPUs that don't support AF offline). 2) If all online CPUs in the current system support AF, then HVO can be enabled. In this case, the system must also block any CPUs that do not support AF from coming online in the future.
An approach similar to this is implemented in this series: if there are (or were) any HVOed folios, onlining a CPU without HW AF is not allowed. If HW AF is not supported by any onlined CPU, HVO cannot proceed (though it will appear to be enabled).
I probably should have integrated arch_hugetlb_vmemmap_optimization_supported() and a hvo_status check into the sysctl handler for HVO. I'll do this if it is still relevant in v2.
Quoted 3 lines
Of course, if a user specifically wants those CPUs to be
allowed to come online, they can choose to disable HVO first (At the same time, this means that the HVO-optimized HugeTLB must be freed first as well).
I could make it so that, if there are no HVOed pages anymore, onlining incompatible CPUs becomes permitted again. I don't really see the need to do this.
Quoted 9 lines
My proposal assumes that even though the system supports different types of CPUs, their support for AF (I suppose this is a very basic feature nowadays) is generally the same. I believe that for servers (Only servers stand to benefit more from HVO), there shouldn't be a mix of those that support AF and those that don't. At the very least, such a situation would be extremely uncommon. I believe this may be acceptable. That said, if we go this route, is there a way to simplify the code implementation even further?. Of course, this is just my personal speculation. Please let me know if I've missed anything.
I agree that systems where HVO is relevant are very unlikely to have mixed support for HW AF.
I don't think the approach you're suggesting in this email (as I've understood it) simplifies this series.
I don't want to mess with the arm64 cpufeature bits more than I've done here; preventing boot CPUs from onlining will certainly be more work. :) And then your other suggestions seem to be quite similar to what is already in these patches.
I am happy to get rid of the "disable HVO if it is unused and a CPU without support is onlined" part though and replace it with something similar (the simplification from the other thread[2]).
Muchun Song
maintainerQuoted 53 lines · James Houghton
Quoted 42 lines · Muchun Song
Quoted 33 lines · James Houghton
Quoted 29 lines · Muchun Song
Quoted 13 lines · James Houghton
Quoted 3 lines · Muchun Song
Do you mean that the support for AF might vary across different CPUs? I'm not that familiar with arm64, so it seems a bit strange to me that such basic hardware features can differ so much from one CPU to another.
Yes, hardware updates of the Access Flag is a per-CPU feature. It is available for a CPU to use if TCR_EL1.HA is set.
TCR_EL1is a system register; each CPU has its own. (Linux will always enable HW AF for a CPU when it is onlined[1] if support is advertised, so we simply need to check if support is advertised to know that it is in fact enabled.)Quoted 5 lines
These days it is not uncommon for a system to have two (or more?) different core implementations, like with "fast" cores and "efficient" cores.
[1] See the
CONFIG_ARM64_HW_AFDBMbits inarch/arm64/mm/proc.SThanks for your detailed explanation. When enabling HVO via the cmdline, can we simply prevent CPUs that do not support AF from coming online? Would implementing it this way be much simpler? In practice, developers definitely know whether the current system is suitable for enabling HVO. If some CPUs do not support AF, they would just need to evaluate the trade-off between memory savings and having fewer online CPUs than expected.
For scenarios where HVO is enabled via sysctl, we simply need to check if all CPUs support AF. If any do not, the system should return an error.
Quoted 8 lines
Quoted 1 line
Then, we can proceed with the Pre-HVO.
I don't think it makes sense to try to implement pre-HVO.
We cannot do HVO if any boot CPUs do not support HW AF, as HW AF will be required to free the HugeTLB pages later, which we should continue to support. Pre-HVO (today anyway) happens before all boot CPUs are onlined. IMO it is not okay to prevent boot CPUs from onlining.
Quoted 5 lines
When the boot CPU starts, it first checks if the hardware (the current boot CPU) supports AF (The selection of the Boot CPU is critical here If the user really wants to enable HVO). If AF is supported and HVO is enabled via cmdline, we perform a Pre-HVO. Subsequently, any CPUs that do not support AF will be prevented from coming online.
Quoted 7 lines
I understand. I don't think we should do this. I think it's much better to always be able to start all boot CPUs[1] and simply rely on "normal" HVO.
[1] Just to be clear on terminology, I'm using "boot CPUs" to refer to all CPUs that are onlined at boot-time, not just to *the* boot CPU that starts the kernel.
Thanks for the clarification. We previously had different understandings of the boot CPU, but that should be resolved now.
Quoted 47 lines
Quoted 5 lines
If the boot CPU does not support AF, enabling HVO is not permitted—neither via cmdline nor sysctl. All CPUs are allowed to be online.
Quoted 7 lines
Once the system has started, there is only one way to enable HVO, and that is through sysctl. 1) If the current system includes any CPUs that do not support AF, enabling HVO is not permitted (unless the user chooses to take these CPUs that don't support AF offline). 2) If all online CPUs in the current system support AF, then HVO can be enabled. In this case, the system must also block any CPUs that do not support AF from coming online in the future.
An approach similar to this is implemented in this series: if there are (or were) any HVOed folios, onlining a CPU without HW AF is not allowed. If HW AF is not supported by any onlined CPU, HVO cannot proceed (though it will appear to be enabled).
I probably should have integrated
arch_hugetlb_vmemmap_optimization_supported()and ahvo_statuscheck into the sysctl handler for HVO. I'll do this if it is still relevant in v2.Quoted 3 lines
Of course, if a user specifically wants those CPUs to be
allowed to come online, they can choose to disable HVO first (At the same time, this means that the HVO-optimized HugeTLB must be freed first as well).I could make it so that, if there are no HVOed pages anymore, onlining incompatible CPUs becomes permitted again. I don't really see the need to do this.
Quoted 9 lines
My proposal assumes that even though the system supports different types of CPUs, their support for AF (I suppose this is a very basic feature nowadays) is generally the same. I believe that for servers (Only servers stand to benefit more from HVO), there shouldn't be a mix of those that support AF and those that don't. At the very least, such a situation would be extremely uncommon. I believe this may be acceptable. That said, if we go this route, is there a way to simplify the code implementation even further?. Of course, this is just my personal speculation. Please let me know if I've missed anything.
I agree that systems where HVO is relevant are very unlikely to have mixed support for HW AF.
I don't think the approach you're suggesting in this email (as I've understood it) simplifies this series.
Quoted 8 lines
I don't want to mess with the arm64 cpufeature bits more than I've done here; preventing boot CPUs from onlining will certainly be more work. :) And then your other suggestions seem to be quite similar to what is already in these patches.
I am happy to get rid of the "disable HVO if it is unused and a CPU without support is onlined" part though and replace it with something similar (the simplification from the other thread[2]).
Thanks for the explanation! My main goal right now is to keep the initial implementation as simple as possible. If any of my suggestions aren't quite on point, feel free to adjust them as you see fit.
Thanks.
Quoted 2 lines
Muchun Song
maintainerQuoted 56 lines · James Houghton
Quoted 26 lines · Muchun Song
Quoted 13 lines · James Houghton
Quoted 3 lines · Muchun Song
Do you mean that the support for AF might vary across different CPUs? I'm not that familiar with arm64, so it seems a bit strange to me that such basic hardware features can differ so much from one CPU to another.
Yes, hardware updates of the Access Flag is a per-CPU feature. It is available for a CPU to use if TCR_EL1.HA is set.
TCR_EL1is a system register; each CPU has its own. (Linux will always enable HW AF for a CPU when it is onlined[1] if support is advertised, so we simply need to check if support is advertised to know that it is in fact enabled.)Quoted 5 lines
These days it is not uncommon for a system to have two (or more?) different core implementations, like with "fast" cores and "efficient" cores.
[1] See the
CONFIG_ARM64_HW_AFDBMbits inarch/arm64/mm/proc.SThanks for your detailed explanation. When enabling HVO via the cmdline, can we simply prevent CPUs that do not support AF from coming online? Would implementing it this way be much simpler? In practice, developers definitely know whether the current system is suitable for enabling HVO. If some CPUs do not support AF, they would just need to evaluate the trade-off between memory savings and having fewer online CPUs than expected.
Quoted 4 lines
For scenarios where HVO is enabled via sysctl, we simply need to check if all CPUs support AF. If any do not, the system should return an error.
Then, we can proceed with the Pre-HVO.
I don't think it makes sense to try to implement pre-HVO.
We cannot do HVO if any boot CPUs do not support HW AF, as HW AF will be required to free the HugeTLB pages later, which we should continue to support. Pre-HVO (today anyway) happens before all boot CPUs are onlined. IMO it is not okay to prevent boot CPUs from onlining.
Let's say for a moment that HVO cannot be toggled at run-time, then the best we can do is:
- If a user does not specify hugetlb_free_vmemmap=1, we can always allow onlining of late CPUs
- If a user specifies hugetlb_free_vmemmap=1 but not all boot CPUs support HW AF, we can always allow onlining of late CPUs.
- If a user specifies hugetlb_free_vmemmap=1 and all boot CPUs support HW AF, we must not allow onlining incompatible late CPUs.HVO compatibility has to be modeled as an Arm system feature. To support conditional onlining of late CPUs based on whether or not "HVO is being used", we still need the extra
cpufeature.clogic.
Quoted 5 lines
So if HVO cannot be toggled at run-time, we can simplify the definition of "is HVO being used?" (for the purposes of determining if late CPUs can be onlined). We can simplify it from what it is now ("are there any optimized pages?") to "vmemmap_optimize_enabled==true?". That allows us to drop patch 15
I believe this conversion logic is sound. If a user proactively enables HVO via the command line, it clearly indicates they intend to use it. Otherwise, there would be no reason for them to enable it and then leave it idle.
Quoted 4 lines
(which then needs a slight change to patch 17), but that's about it. So this is a slight simplification, which is nice.
If you (or the Arm folks) feel strongly, I'm happy to write this simplification.
I really like the simplification here. To keep this series as simple as possible for the first step, we also could focus strictly on bringing HVO support to ARM64 by making it depend entirely on BBML2_NOABORT. This keeps the initial scope highly contained, which should make it much easier to review.
Muchun
Quoted 21 lines
Quoted 15 lines
Quoted 3 lines
I'm hoping you can look at patches 1-3 anyway. I think they're the right thing to do, even without the arm64 changes.
Yes, I've looked it over. I think that making modifications across several areas will be much simpler on top of my refactored codebase. For example:
- In patch 2: There is no longer a need to move
vmemmap_get_tail().
- In patch 3: Handling partially-HVOed pages becomes straightforward becausevmemmap_restore_pte()no longer relies on ->vmemmap_tail to detect whether a restoration is needed.
- In patch 4: I have updated the ->remap_pte callback to return an int as well, making it easy to adapt your changes.Quoted 2 lines
Overall, the implementation is significantly cleaner, so I won't list every detail here.
Thanks! Good to know I can basically drop those patches. :)
Please CC me on these HVO simplifications when you send them out, thanks!
Quoted 68 lines · Muchun Song
Quoted 54 lines · James Houghton
Quoted 26 lines · Muchun Song
Quoted 13 lines · James Houghton
Quoted 3 lines · Muchun Song
Do you mean that the support for AF might vary across different CPUs? I'm not that familiar with arm64, so it seems a bit strange to me that such basic hardware features can differ so much from one CPU to another.
Yes, hardware updates of the Access Flag is a per-CPU feature. It is available for a CPU to use if TCR_EL1.HA is set.
TCR_EL1is a system register; each CPU has its own. (Linux will always enable HW AF for a CPU when it is onlined[1] if support is advertised, so we simply need to check if support is advertised to know that it is in fact enabled.)Quoted 5 lines
These days it is not uncommon for a system to have two (or more?) different core implementations, like with "fast" cores and "efficient" cores.
[1] See the
CONFIG_ARM64_HW_AFDBMbits inarch/arm64/mm/proc.SThanks for your detailed explanation. When enabling HVO via the cmdline, can we simply prevent CPUs that do not support AF from coming online? Would implementing it this way be much simpler? In practice, developers definitely know whether the current system is suitable for enabling HVO. If some CPUs do not support AF, they would just need to evaluate the trade-off between memory savings and having fewer online CPUs than expected.
Quoted 4 lines
For scenarios where HVO is enabled via sysctl, we simply need to check if all CPUs support AF. If any do not, the system should return an error.
Then, we can proceed with the Pre-HVO.
I don't think it makes sense to try to implement pre-HVO.
We cannot do HVO if any boot CPUs do not support HW AF, as HW AF will be required to free the HugeTLB pages later, which we should continue to support. Pre-HVO (today anyway) happens before all boot CPUs are onlined. IMO it is not okay to prevent boot CPUs from onlining.
Let's say for a moment that HVO cannot be toggled at run-time, then the best we can do is:
- If a user does not specify hugetlb_free_vmemmap=1, we can always allow onlining of late CPUs
- If a user specifies hugetlb_free_vmemmap=1 but not all boot CPUs support HW AF, we can always allow onlining of late CPUs.
- If a user specifies hugetlb_free_vmemmap=1 and all boot CPUs support HW AF, we must not allow onlining incompatible late CPUs.HVO compatibility has to be modeled as an Arm system feature. To support conditional onlining of late CPUs based on whether or not "HVO is being used", we still need the extra
cpufeature.clogic.Quoted 5 lines
So if HVO cannot be toggled at run-time, we can simplify the definition of "is HVO being used?" (for the purposes of determining if late CPUs can be onlined). We can simplify it from what it is now ("are there any optimized pages?") to "vmemmap_optimize_enabled==true?". That allows us to drop patch 15
I believe this conversion logic is sound. If a user proactively enables HVO via the command line, it clearly indicates they intend to use it. Otherwise, there would be no reason for them to enable it and then leave it idle.
Quoted 3 lines
(which then needs a slight change to patch 17), but that's about it. So this is a slight simplification, which is nice.
Quoted 6 lines
Quoted 1 line
If you (or the Arm folks) feel strongly, I'm happy to write this simplification.
I really like the simplification here. To keep this series as simple as possible for the first step, we also could focus strictly on bringing HVO support to ARM64 by making it depend entirely on
BBML2_NOABORT. This keeps the initial scope highly contained, which should make it much easier to review.
Okay I'll do this then. I'll probably still include the BBML2_NOABORT patches anyway, as I don't have access to any systems that have both HW AF and BBML2_NOABORT / BBML3 (even with the expanded list[1]). I'll continue to structure it where the series can be partially applied and still be useful; i.e., the patches to drop the BBML2_NOABORT dependency can be ignored.
[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
David Hildenbrand (Arm)
maintainerQuoted 11 lines
-- The AF trick --
The trick is that translations with the AF unset cannot be cached in the TLB (see Rule
R_DWZCQin 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.
Quoted 4 lines
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 compatbility checks all CPUs for HW AF updates.
Pretty nice trick, I hope this will fly :)
Signature · 3 lines
Cheers, David
Muchun Song
maintainerQuoted 9 lines · James Houghton
Hi everyone,
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.
Hi James,
I was just looking into the Arm ARM specification regarding the TLB caching behavior to better understand the "AF trick" used here, and came across a few sections that got me a bit confused.
Specifically, Section D8.5.1 [1] states Rule RDWZCQ:
"Descriptors with AF set to zero can never be cached in a TLB. For more
information about when translation table entries are permitted to be cached in a TLB, see Translation Lookaside Buffers."
This indeed seems to support the core assumption of the AF trick. However, when following the reference to Section D8.16 "Translation Lookaside Buffers" [2], it defines the following rule for permitted caching (RSQBCS):
"When address translation is enabled, a translation table entry for an
in-context translation regime that does not cause a Translation fault, an Address size fault, or an Access flag fault is permitted to be cached in a TLB or intermediate TLB caching structure as the result of an explicit or speculative access."
This is exactly where my confusion lies, as these two descriptions in the spec feel directly contradictory when FEAT_HAF(hardware management of the Access flag) is enabled.
On one hand, Section D8.5.1 [1] explicitly uses the word "never" for AF=0 descriptors. On the other hand, under FEAT_HAF, an entry with AF=0 does not cause an Access flag fault anymore, because the hardware is capable of updating the AF automatically. This technically makes it eligible for speculative caching under Rule RSQBCS in Section D8.16 [2].
My question is: which part of the spec reflects the actual architectural intent when FEAT_HAF is enabled? Is an entry with AF=0 permitted to be speculatively cached in the TLB by the hardware under FEAT_HAF, or does the "never" in RDWZCQ still hold absolute priority?
Please let me know if I have misread the specification or missed some overriding constraints here.
Best regards,
Muchun
References:
[1] https://developer.arm.com/documentation/ddi0487/mb/-Part-D-The-AArch64-System-Level-Architecture/-Chapter-D8-The-AArch64-Virtual-Memory-System-Architecture/-D8-5-Hardware-updates-to-the-translation-tables/-D8-5-1-The-Access-flag
[2] https://developer.arm.com/documentation/ddi0487/mb/-Part-D-The-AArch64-System-Level-Architecture/-Chapter-D8-The-AArch64-Virtual-Memory-System-Architecture/-D8-16-Translation-Lookaside-Buffers?lang=en#mdsec_translation_lookaside_buffers
Quoted 185 lines
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 four parts:
1. Some preparatory changes (patches 1-3)
1. Bare minimum HVO support (patches 4-10)
2. DropBBML2_NOABORTrequirement for HVO (patches 11-13)
3. Drop the user-configurableKconfigfor HVO (patches 14-18)Parts 3 and 4 are technically optional. More details below.
The main functional caveat with this series is that bootmem HugeTLB pages are not "pre-HVOed". They will be HVOed, but because at pre-HVO time SMP CPUs have not been enabled, we cannot query for full system support.
This series is based on 7.2-rc2 (
0e35b9b6ec0f).This series almost 100% cleanly applies to mm-new, which has some of Muchun's HVO patches, with one trivial conflict. I imagine this series will conflict pretty heavily with some of Muchun's other patches[2].
-- The AF trick --
The trick is that translations with the AF unset cannot be cached in the TLB (see Rule
R_DWZCQin 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 compatbility 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 PTE remains mapped normally (RW mapping to a real page of struct pages), but the remaining PTEs in the vmemmap are mapped read-only to a shared page of struct pages (that is, there is an OA change and a permissions change).
- When disabling HVO for a page, the RO PTEs are remapped back to RW PTEs that point to newly reallocated pages of struct pages. The PMD block -> table transition is not undone.In patches 4-10 of this series, I use the Access Flag trick to do the PTE OA and permissions updates. We rely on
BBML2_NOABORTfor the PMD block -> table transition.In patches 11-13, I re-use the Access Flag trick to do the PMD block -> table transition without needing
BBML2_NOABORT. For systems that supportBBML2_NOABORT, the logic is unchanged.I am aware of Linu's BBML3 patches; I've opted not to rebase onto them for now, but I am happy to do so later.
-- Late-onlining of CPUs that do not support HW AF --
One of the complications with this series is how to handle late-onlining of CPUs that do not have HW AF when HVO is in use. Naively, if HVO (HW AF) is supported on all boot CPUs and the kernel is compiled with HVO support, late CPUs that do not support HVO will not be onlined. This is a regression.
This series provides two ways of dealing with this. First, add a default-off
Kconfigfor users to enable HVO support, avoiding the regression. This is not ideal.Patches 14-18 get rid of the new
Kconfigby allowing onlining of HVO-incompatible late CPUs as long as HVO is not actively in use.-- Litmus test --
The following Herd litmus test demonstrates the PTE update routine:
AArch64 TTDFaultlessUpdate
Variant=vmsa
TTHM=HA
{
uint64_tx=1;
uint64_ty=2;
[PTE(x)]=(oa:PA(x), af:1);
0:X0=PTE(x); 1:X0=PTE(x);
0:X1=x; 1:X1=x;
pteval_t0:X2=(oa:PA(x), af:0);
pteval_t0: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 violate BBM requirements. *)
This test must be run with herdtools with Nikos's changes[3] to more accurately model BBM requirements. When tried, the output will notably *not* contain the "Warning-BBM-expected" flag.
-- Testing --
I haven't yet done extensive testing of this series. HVO is correctly freeing pages on my system, and the hugetlb-vmemmap test passes. Freeing HVOed HugeTLB pages also seems to function normally.
[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://github.com/herd/herdtools7/pull/1864James Houghton (18):
hugetlb_vmemmap: Always flush TLB if needed upon PTE remapping
hugetlb_vmemmap: Movevmemmap_get_tailup
hugetlb_vmemmap: Leave pages partially HVOed upon restore failure
hugetlb_vmemmap: Usetry_update_vmemmap_pteto update in-use PTEs
hugetlb_vmemmap: Allow architectures not to allow HVO at runtime
arm64: Renamecpu_has_hw_aftosystem_has_hw_af
arm64: Addsystem_supports_hvo
arm64: Implementtry_update_vmemmap_pteusing the AF trick
arm64: Prevent HVO if the HVO system feature is not enabled
arm64: Support hugetlb vmemmap optimization
hugetlb_vmemmap: Usetry_populate_vmemmap_pmdfor replacing in-use
PMDs
arm64: Implementtry_populate_vmemmap_pmdusing AF trick
arm64: DropBBML2_NOABORTrequirement for HVO
hugetlb_vmemmap: Renamemm/hugetlb_vmemmap.hto
mm/hugetlb_vmemmap_internal.h
hugetlb_vmemmap: Add a way to permanently disable HVO when needed
arm64: Allow "optional" CPU features to be required sometimes
arm64: Permit onlining of HVO-incompatible late CPUs if HVO is not in
use
arm64: Remove user-selectable HVOKconfigMAINTAINERS | 3 +- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/cpucaps.h | 2 + arch/arm64/include/asm/cpufeature.h | 39 ++- arch/arm64/include/asm/hugetlb.h | 7 + arch/arm64/include/asm/pgalloc.h | 54 ++++ arch/arm64/include/asm/pgtable.h | 57 ++++- arch/arm64/kernel/cpufeature.c | 43 ++++ arch/arm64/tools/cpucaps | 1 + arch/loongarch/include/asm/pgalloc.h | 8 + arch/loongarch/include/asm/pgtable.h | 8 + arch/riscv/include/asm/pgalloc.h | 8 + arch/riscv/include/asm/pgtable.h | 8 + arch/x86/include/asm/pgalloc.h | 8 + arch/x86/include/asm/pgtable.h | 8 + include/asm-generic/hugetlb.h | 7 + include/linux/hugetlb_vmemmap.h | 20 ++ include/linux/pgalloc.h | 20 ++ include/linux/pgtable.h | 21 ++ mm/hugetlb.c | 2 +- mm/hugetlb_sysfs.c | 2 +- mm/hugetlb_vmemmap.c | 237 +++++++++++++----- ...b_vmemmap.h => hugetlb_vmemmap_internal.h} | 6 +- mm/sparse-vmemmap.c | 2 +- 24 files changed, 489 insertions(+), 83 deletions(-)create mode 100644
include/linux/hugetlb_vmemmap.hrenamemm/{hugetlb_vmemmap.h=>hugetlb_vmemmap_internal.h} (95%)base-commit:
0e35b9b6ec0ffcc5e23cbdec09f5c622ad532b53Signature · 1 line
2.55.0.795.g602f6c329a-goog
Quoted 30 lines · Muchun Song
Quoted 7 lines · James Houghton
Hi everyone,
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.
Hi James,
I was just looking into the Arm ARM specification regarding the TLB caching behavior to better understand the "AF trick" used here, and came across a few sections that got me a bit confused.
Specifically, Section D8.5.1 [1] states Rule RDWZCQ:
"Descriptors with AF set to zero can never be cached in a TLB. For more
information about when translation table entries are permitted to be cached in a TLB, see Translation Lookaside Buffers."This indeed seems to support the core assumption of the AF trick. However, when following the reference to Section D8.16 "Translation Lookaside Buffers" [2], it defines the following rule for permitted caching (RSQBCS):
"When address translation is enabled, a translation table entry for an
in-context translation regime that does not cause a Translation fault, an Address size fault, or an Access flag fault is permitted to be cached in a TLB or intermediate TLB caching structure as the result of an explicit or speculative access."
Quoted 3 lines
This is exactly where my confusion lies, as these two descriptions in the spec feel directly contradictory when
FEAT_HAF(hardware management of the Access flag)is enabled.
I don't think it intends to say that "translation table entries with AF=0 may be cached" just because they don't cause Access flag faults.
It is likely that this clause was written before FEAT_HAFDBS/FEAT_HAF existed. Re-reading it with that in mind makes the meaning clearer:
PTEs with AF=1 may be cached if the architecture otherwise allows it.
There is another clause, in D8.5 "Hardware updates of the translation tables", I_RGQLZ: "If hardware does not update the AF from 0 to 1, then the descriptor is not permitted to be cached in a TLB." I think this supports my interpretation.
I am convinced that it is impossible for an implementation not to conform to Rule R_DWZCQ (PTEs with AF=0 cannot be cached) while supporting HW AF correctly. Let me try to explain why.
For HW AF to function correctly:
1. Setting AF=1 must be done atomically, otherwise hardware could set
a bit in an invalid PTE. (This would break Linux swp_entrys.)
2. The AF=0 translation cannot be used, otherwise we could
potentially drop AF updates.
If an implementation used the translation before coherently setting AF=1 to perform an architectural memory access, then it must choose either to set AF=1 on a PTE that may have since changed (perhaps to an invalid PTE) or drop the AF update entirely. Either way HW AF is broken.
Please also see I_GXGPN: "When the translation of an architecturally executed memory access occurs, the architecture requires that AF is set to 1."
Quoted 6 lines
On one hand, Section D8.5.1 [1] explicitly uses the word "never" for AF=0 descriptors. On the other hand, under
FEAT_HAF, an entry with AF=0 does not cause an Access flag fault anymore, because the hardware is capable of updating the AF automatically. This technically makes it eligible for speculative caching under Rule RSQBCS in Section D8.16 [2].
Quoted 7 lines
My question is: which part of the spec reflects the actual architectural intent when
FEAT_HAFis enabled? Is an entry with AF=0 permitted to be speculatively cached in the TLB by the hardware underFEAT_HAF, or does the "never" in RDWZCQ still hold absolute priority?Please let me know if I have misread the specification or missed some overriding constraints here.
Thanks for making me look a little harder into this; it's a good question. I hope my reply above makes you more comfortable with the AF trick. I also hope to hear more from the Arm folks as well.
Thanks Muchun!
Muchun Song
maintainerQuoted 67 lines · James Houghton
Quoted 30 lines · Muchun Song
Quoted 7 lines · James Houghton
Hi everyone,
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.
Hi James,
I was just looking into the Arm ARM specification regarding the TLB caching behavior to better understand the "AF trick" used here, and came across a few sections that got me a bit confused.
Specifically, Section D8.5.1 [1] states Rule RDWZCQ:
"Descriptors with AF set to zero can never be cached in a TLB. For more
information about when translation table entries are permitted to be cached in a TLB, see Translation Lookaside Buffers."This indeed seems to support the core assumption of the AF trick. However, when following the reference to Section D8.16 "Translation Lookaside Buffers" [2], it defines the following rule for permitted caching (RSQBCS):
"When address translation is enabled, a translation table entry for an
in-context translation regime that does not cause a Translation fault, an Address size fault, or an Access flag fault is permitted to be cached in a TLB or intermediate TLB caching structure as the result of an explicit or speculative access."Quoted 3 lines
This is exactly where my confusion lies, as these two descriptions in the spec feel directly contradictory when
FEAT_HAF(hardware management of the Access flag)is enabled.I don't think it intends to say that "translation table entries with AF=0 may be cached" just because they don't cause Access flag faults.
It is likely that this clause was written before FEAT_HAFDBS/FEAT_HAF existed. Re-reading it with that in mind makes the meaning clearer:
PTEs with AF=1 may be cached if the architecture otherwise allows it.There is another clause, in D8.5 "Hardware updates of the translation tables",
I_RGQLZ: "If hardware does not update the AF from 0 to 1, then the descriptor is not permitted to be cached in a TLB." I think this supports my interpretation.I am convinced that it is impossible for an implementation not to conform to Rule
R_DWZCQ(PTEs with AF=0 cannot be cached) while supporting HW AF correctly. Let me try to explain why.For HW AF to function correctly:
1. Setting AF=1 must be done atomically, otherwise hardware could set a bit in an invalid PTE. (This would break Linuxswp_entrys.)
2. The AF=0 translation cannot be used, otherwise we could potentially drop AF updates.If an implementation used the translation before coherently setting AF=1 to perform an architectural memory access, then it must choose either to set AF=1 on a PTE that may have since changed (perhaps to an invalid PTE) or drop the AF update entirely. Either way HW AF is broken.
Quoted 3 lines
Please also see
I_GXGPN: "When the translation of an architecturally executed memory access occurs, the architecture requires that AF is set to 1."
Thanks for your excellent explanation.
Quoted 12 lines
Quoted 12 lines
On one hand, Section D8.5.1 [1] explicitly uses the word "never" for AF=0 descriptors. On the other hand, under
FEAT_HAF, an entry with AF=0 does not cause an Access flag fault anymore, because the hardware is capable of updating the AF automatically. This technically makes it eligible for speculative caching under Rule RSQBCS in Section D8.16 [2].My question is: which part of the spec reflects the actual architectural intent when
FEAT_HAFis enabled? Is an entry with AF=0 permitted to be speculatively cached in the TLB by the hardware underFEAT_HAF, or does the "never" in RDWZCQ still hold absolute priority?
Quoted 5 lines
Quoted 2 lines
Please let me know if I have misread the specification or missed some overriding constraints here.
Thanks for making me look a little harder into this; it's a good question. I hope my reply above makes you more comfortable with the AF
At least to me, what you're saying makes sense.
Quoted 1 line
trick. I also hope to hear more from the Arm folks as well.
Yes, it would be best to have an ARM expert here to confirm that our understanding of the manual is completely accurate.
Thanks.
Quoted 2 lines
Thanks Muchun!
Quoted 91 lines · Muchun Song
Quoted 67 lines · James Houghton
Quoted 30 lines · Muchun Song
Quoted 7 lines · James Houghton
Hi everyone,
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.
Hi James,
I was just looking into the Arm ARM specification regarding the TLB caching behavior to better understand the "AF trick" used here, and came across a few sections that got me a bit confused.
Specifically, Section D8.5.1 [1] states Rule RDWZCQ:
"Descriptors with AF set to zero can never be cached in a TLB. For more
information about when translation table entries are permitted to be cached in a TLB, see Translation Lookaside Buffers."This indeed seems to support the core assumption of the AF trick. However, when following the reference to Section D8.16 "Translation Lookaside Buffers" [2], it defines the following rule for permitted caching (RSQBCS):
"When address translation is enabled, a translation table entry for an
in-context translation regime that does not cause a Translation fault, an Address size fault, or an Access flag fault is permitted to be cached in a TLB or intermediate TLB caching structure as the result of an explicit or speculative access."Quoted 3 lines
This is exactly where my confusion lies, as these two descriptions in the spec feel directly contradictory when
FEAT_HAF(hardware management of the Access flag)is enabled.I don't think it intends to say that "translation table entries with AF=0 may be cached" just because they don't cause Access flag faults.
It is likely that this clause was written before FEAT_HAFDBS/FEAT_HAF existed. Re-reading it with that in mind makes the meaning clearer:
PTEs with AF=1 may be cached if the architecture otherwise allows it.There is another clause, in D8.5 "Hardware updates of the translation tables",
I_RGQLZ: "If hardware does not update the AF from 0 to 1, then the descriptor is not permitted to be cached in a TLB." I think this supports my interpretation.I am convinced that it is impossible for an implementation not to conform to Rule
R_DWZCQ(PTEs with AF=0 cannot be cached) while supporting HW AF correctly. Let me try to explain why.For HW AF to function correctly:
1. Setting AF=1 must be done atomically, otherwise hardware could set a bit in an invalid PTE. (This would break Linuxswp_entrys.)
2. The AF=0 translation cannot be used, otherwise we could potentially drop AF updates.If an implementation used the translation before coherently setting AF=1 to perform an architectural memory access, then it must choose either to set AF=1 on a PTE that may have since changed (perhaps to an invalid PTE) or drop the AF update entirely. Either way HW AF is broken.
Quoted 3 lines
Please also see
I_GXGPN: "When the translation of an architecturally executed memory access occurs, the architecture requires that AF is set to 1."Thanks for your excellent explanation.
Quoted 12 lines
Quoted 12 lines
On one hand, Section D8.5.1 [1] explicitly uses the word "never" for AF=0 descriptors. On the other hand, under
FEAT_HAF, an entry with AF=0 does not cause an Access flag fault anymore, because the hardware is capable of updating the AF automatically. This technically makes it eligible for speculative caching under Rule RSQBCS in Section D8.16 [2].My question is: which part of the spec reflects the actual architectural intent when
FEAT_HAFis enabled? Is an entry with AF=0 permitted to be speculatively cached in the TLB by the hardware underFEAT_HAF, or does the "never" in RDWZCQ still hold absolute priority?Quoted 5 lines
Quoted 2 lines
Please let me know if I have misread the specification or missed some overriding constraints here.
Thanks for making me look a little harder into this; it's a good question. I hope my reply above makes you more comfortable with the AF
Quoted 12 lines
At least to me, what you're saying makes sense.
Quoted 1 line
trick. I also hope to hear more from the Arm folks as well.
Yes, it would be best to have an ARM expert here to confirm that our understanding of the manual is completely accurate.
Thanks.
Quoted 2 lines
Thanks Muchun!
Disclaimer: *NOT* an Arm expert, but it would also be good to prove the conjecture:
"If we guarantee that there is no caching structure holding a stale translation, then we do not require BBM". Meaning, the only reason we do BBM is to prevent two execution contexts from seeing two different translations, and such a situation can *only* happen via TLB or intermediate caching structures.
D8.17.1, rule I_THWDH says:
"Use of a break-before-make sequence to ensure that old and new translation table entries are never simultaneously visible to different execution threads is guaranteed to prevent all of the following problems:
Creating multiple TLB entries that apply to the same address.
The effects of TLB caching breaking coherency.
The effects of TLB caching breaking single-copy atomicity properties.
The effects of TLB caching breaking ordering guarantees or uniprocessor semantics.
The effects of TLB caching causing a failure to clear the Exclusives monitors."
All these problems are derived from "TLB caching", so the conjecture seems to be true.
Quoted 2 lines
Hello,
Quoted 35 lines · James Houghton
Hi everyone,
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 four parts:
1. Some preparatory changes (patches 1-3)
1. Bare minimum HVO support (patches 4-10)
2. DropBBML2_NOABORTrequirement for HVO (patches 11-13)
3. Drop the user-configurableKconfigfor HVO (patches 14-18)Parts 3 and 4 are technically optional. More details below.
The main functional caveat with this series is that bootmem HugeTLB pages are not "pre-HVOed". They will be HVOed, but because at pre-HVO time SMP CPUs have not been enabled, we cannot query for full system support.
This series is based on 7.2-rc2 (
0e35b9b6ec0f).This series almost 100% cleanly applies to mm-new, which has some of Muchun's HVO patches, with one trivial conflict. I imagine this series will conflict pretty heavily with some of Muchun's other patches[2].
-- The AF trick --
Quoted 9 lines
The trick is that translations with the AF unset cannot be cached in the TLB (see Rule
R_DWZCQin 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.
So, this sounds really cool but I'm not sure if its correct. Looking at D8.17.1 Using break-before-make when updating translation table entries:
Per IGXGZY:
For a translation stage with AF hardware management enabled, if a translation table entry is modified and the break-before-make sequence is not followed, then all of the following failures associated with AF hardware updates can occur:
- When a memory location associated with that translation table entry is accessed, the AF is not set.
- When hardware updates to that translation table entry are followed by stores appearing later in program order, the ordering required is not followed.
So apparently per the ARM ARM you can possibly never observe AF=0 even if it's cached.
And then other semantics concerns may arise e.g I'm not sure if AF=0 implies that the walk cache has nothing for that address, or if it's just that particular translation that's not in the TLB. Though this might not particularly matter since we're not collapsing the PTEs back to a PMD block mapping.
Right? Tell me I'm wrong :)
Signature · 1 line
Pedro
Quoted 24 lines · Pedro Falcato
Hello,
Quoted 11 lines · James Houghton
-- The AF trick --
The trick is that translations with the AF unset cannot be cached in the TLB (see Rule
R_DWZCQin 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.So, this sounds really cool but I'm not sure if its correct. Looking at D8.17.1 Using break-before-make when updating translation table entries:
Per IGXGZY:
For a translation stage with AF hardware management enabled, if a translation table entry is modified and the break-before-make sequence is not followed, then all of the following failures associated with AF hardware updates can occur:
Quoted 2 lines
- When a memory location associated with that translation table entry is accessed, the AF is not set.
I think this clause is referring to the case where a CPU updates a PTE using something like STR (as opposed to SWP or CAS). If CPU 0 STRs a new PTE while CPU 1 accesses memory through the old AF=0 PTE, the AF=1 update will be overwritten by the STR of the new PTE.
This clause might be suggesting that implementations are free to implement something like: "if the cacheline for the PTE cannot be grabbed exclusively, drop the AF update but proceed with the access". To me, this interpretation is incompatible with the rules in D8.5.1, summarized in I_GXGPN: "When the translation of an architecturally executed memory access occurs, the architecture requires that AF is set to 1."
But IMO dropping the AF update isn't actually important. It's okay for the kernel to use the vmemmap while we are updating it, we just need the translation not to be cached. AF=0 does that for us (I claim).
Quoted 2 lines
- When hardware updates to that translation table entry are followed by stores appearing later in program order, the ordering required is not followed.
I assume the "ordering required" is that the AF=1 update is ordered-before the later stores. I interpret this as a natural consequence of the earlier statement. If the AF=0 update is "dropped" (overwritten by a STR), then seeing the later stores (using the old translation) does not necessarily mean the new translation will have AF=1. Maybe I'm interpreting this wrong (I am up late tonight...).
Quoted 8 lines
So apparently per the ARM ARM you can possibly never observe AF=0 even if it's cached.
And then other semantics concerns may arise e.g I'm not sure if AF=0 implies that the walk cache has nothing for that address, or if it's just that particular translation that's not in the TLB. Though this might not particularly matter since we're not collapsing the PTEs back to a PMD block mapping.
I assume that when the Arm ARM says "cannot be cached", it means "no TLBI is needed (to avoid TLB conflict aborts)". In other words, partial walk caches need not be invalidated.
D8.5.1, I_HQBRF: "When software sets the AF to 1, there is no requirement to perform TLB invalidation after setting the flag because entries
with an AF set to 0 are never held in a TLB."
If an implementation caches a partial walk that includes a AF=0 translation such that a newer translation can conflict with the cached partial walk, then a TLBI would be needed and the implementation wouldn't be compliant.
Quoted 1 line
Right? Tell me I'm wrong :)
Thanks for looking at this Pedro. It is really late for me (I had a long day, but I wanted to get back to you), so sorry if what I've written is a little incoherent.
Catalin Marinas
maintainerHi James,
Quoted 28 lines · James Houghton
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))
Quoted 1 line
(* This test should not violate BBM requirements. *)
We definitely need a clear statement in the Arm ARM around this. As already raised in this thread, we can assume that AF=0 entries won't be cached in TLB but there's more to the BBM rules. For the dirty bit, we have R_SGJBL, we might need something similar for AF in addition to the BBM rules update. In theory, I think your approach works but we need the memory model experts confirmation and the Arm ARM updated.
I'm not entirely clear what the above litmus test guarantees other than not giving a warning. On P1, X2 can be either 1 or 2, otherwise fault but that won't happen with HA. I'd rather have something in the 'exists' rule explicitly (like a TLB conflict abort; not supported AFAIK but maybe that's what fault(P1) was meant to show?).
In addition to the above test, I wonder whether we could have P1 do two consecutive reads from [X1] and check the read values. Maybe go through a third mapping, OA(z) with AF=0 after the TLBI or just start with AF=0 on x. Basically any other tests for coherency like read-after-read etc., check that the behaviour is preserved through AF=0 just like going through valid=0 with BBM (that's something that should eventually be runnable on actual hardware to validate where we won't have a BBM warning message).
Signature · 1 line
Catalin
Quoted 2 lines · Catalin Marinas
Hi James,
Hi Catalin, thanks for your feedback on this series!
Quoted 29 lines · James Houghton
Quoted 28 lines
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))
Quoted 8 lines
Quoted 1 line
(* This test should not violate BBM requirements. *)
We definitely need a clear statement in the Arm ARM around this. As already raised in this thread, we can assume that AF=0 entries won't be cached in TLB but there's more to the BBM rules. For the dirty bit, we have
R_SGJBL, we might need something similar for AF in addition to the BBM rules update. In theory, I think your approach works but we need the memory model experts confirmation and the Arm ARM updated.
I agree; it would be nice if the BBM rules were more explicit about PTEs with AF=0.
The question I find myself wanting an answer to is: "is there ever a case where a TLBI must be issued for a guaranteed-uncached TTD before it can be modified?"
I'm pretty convinced that, for Page->Page, there are no such cases (but I can't really say for sure).
For Block->Table, it's certainly more plausible for there to be such cases, like your comment on patch 12[1].
[1] https://lore.kernel.org/linux-mm/[email protected]/
Quoted 5 lines
I'm not entirely clear what the above litmus test guarantees other than not giving a warning. On P1, X2 can be either 1 or 2, otherwise fault but that won't happen with HA. I'd rather have something in the 'exists' rule explicitly (like a TLB conflict abort; not supported AFAIK but maybe that's what fault(P1) was meant to show?).
I should have clarified that the exists clause in this case should always be negative. So I read it is:
"It should never be the case that: the CAS succeeds AND (P1 takes a fault OR P1 reads a bogus value)."
It's really useful for HVO not to introduce a window where Linux (e.g. get_page_unless_zero() callers) might take a fault on the vmemmap; this is what the fault(P1) part of the clause is conveying (even if it is obvious). This is actually the main point of the test: to verify we don't take a fault. The bogus value part is just kind of a sanity check.
I don't think fault() captures TLB conflict aborts. The lack of a BBM warning is supposed to indicate that the litmus test has followed BBM rules properly => TLB conflict aborts should not occur.
Quoted 9 lines
In addition to the above test, I wonder whether we could have P1 do two consecutive reads from [X1] and check the read values. Maybe go through a third mapping, OA(z) with AF=0 after the TLBI or just start with AF=0 on x. Basically any other tests for coherency like read-after-read etc., check that the behaviour is preserved through AF=0 just like going through valid=0 with BBM (that's something that should eventually be runnable on actual hardware to validate where we won't have a BBM warning message).
Changing P1 to:
LDR X2,[X1];
LDR X3,[X1];
and using the exists clause:
0:X5=0:X4 /\ (fault(P1:L0) \/ (1:X3=1 /\ 1:X2=2))
remains always false => P1 never reads in reverse order.
Test TTDFaultlessUpdateRAR Allowed
States 3
0:X4=(oa:PA(x)); 0:X5=(oa:PA(x)); 1:X2=1; 1:X3=1; ~Fault(P1:L0);
0:X4=(oa:PA(x)); 0:X5=(oa:PA(x)); 1:X2=1; 1:X3=2; ~Fault(P1:L0);
0:X4=(oa:PA(x)); 0:X5=(oa:PA(x)); 1:X2=2; 1:X3=2; ~Fault(P1:L0);
No
Witnesses
Positive: 0 Negative: 140
Flag Maintenance-scope-for-DSB-ST-is-deprecatedI've tried to write more interesting tests than this, but I can't get any of them to run in any reasonable amount of time. (This test with P1 doing two reads took 2.5 minutes.)
Here's one that I was trying:
AArch64 TTDFaultlessUpdateRAR2
Variant=vmsa
TTHM=HA
{
uint64_t x=0;
uint64_t y=2;
[PTE(x)]=(oa:PA(x), af:0);
0:X0=PTE(x);
1:X1=x; 2:X1=x;
2:X2=1;
pteval_t 0:X2=(oa:PA(x), af:0);
pteval_t 0:X3=(oa:PA(y), af:0);
pteval_t 0:X4=(oa:PA(x), af:1);
}
P0 | P1 | P2 ;
MOV X6,X2 | LDR X2,[X1] | STR X2,[X1] ;
CAS X2,X3,[X0] | LDR X3,[X1] | ;
MOV X7,X3 | | ;
CAS X3,X4,[X0] | | ;
| | ;
| | ;
exists
0:X6=0:X2 /\ (* First CAS must succeed *)
0:X7=0:X3 /\ (* Second CAS must succeed *)
(1:X2=2 \/ 1:X3=2 \/ (1:X3=0 /\ 1:X2=1))Let me know if you have any more specific ideas about tests to try.
Thanks!