Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 00/18] Another attempt at HVO support on arm64
@ 2026-12-01 15:07 James Houghton
  2026-12-01 15:07 ` [PATCH v4 01/18] arm64/pgtable: Clear AF with LDCLR on supported systems James Houghton
                   ` (17 more replies)
  0 siblings, 18 replies; 19+ messages in thread
From: James Houghton @ 2026-12-01 15:07 UTC (permalink / raw)
  To: Will Deacon, Catalin Marinas, Muchun Song, Oscar Salvador,
	Andrew Morton
  Cc: Kefeng Wang, Nikos Nikoleris, Linu Cherian, Mark Rutland,
	David Hildenbrand, Ryan Roberts, Nanyong Sun, Yu Zhao,
	Frank van der Linden, David Rientjes, James Houghton,
	linux-kernel, linux-arm-kernel, linux-mm

Hi everyone,

This is v4 of the series to enable HVO on arm64.

This version of the series is based on mm-new (872ceec1e3ca).

I've uploaded the series here as well:
  http://github.com/48ca/linux hvo-v4

As with last time, the last 5 patches are optional, and I've tagged
them as RFC, as it's unclear if using the AF trick to do BBM-less block
-> table transitions is architecturally sound without BBML3. The patches
should be mergeable if it is in fact sound.

The biggest changes since v3:

- bbm_through_af is now an ARM64_CPUCAP_EARLY_LOCAL_CPU_FEATURE instead
  of a system feature. In v3, BBML3 support was only checked on the CPU
  that finalized the system capabilities, so a system where only some
  CPUs support BBML3 could have enabled HVO. Now every early CPU is
  checked, and late CPUs that lack the feature are rejected. (Thanks
  Catalin and Sashiko!)

- Renamed try_update_vmemmap_pte() to vmemmap_update_pte(),
  try_populate_vmemmap_pmd() to vmemmap_update_pmd(), and
  arch_support_vmemmap_remap() to arch_supports_vmemmap_remap(). The
  generic vmemmap_update_pte() and vmemmap_update_pmd() are now opt-out
  (architectures override them by defining the macro) instead of opt-in,
  so x86, riscv, and LoongArch no longer need to define
  ARCH_WANTS_GENERIC_POPULATE_VMEMMAP_{PTE,PMD}. (Thanks Muchun!)

- Dropped "hugetlb_vmemmap: Expose whether HVO is enabled to
  architecture code". Instead, arm64 now parses hugetlb_free_vmemmap=
  itself with its own early_param(), so that vmemmap_optimize_enabled can
  stay private to HugeTLB. (Thanks Muchun!)

And some smaller changes:

- vmemmap_remap_range() now only flushes the part of the range that was
  actually remapped, and doesn't flush at all if nothing was remapped.
  (Thanks Sashiko!)

- hugetlb_vmemmap_restore_folios() now also flushes the TLB when
  restoring a folio fails partway through. (Thanks Sashiko!)

- "hugetlb: Optimize bootmem folios that were not pre-HVOed" now
  restores the early return in hugetlb_vmemmap_optimize_folios() when
  there is nothing to optimize, and its comment is condensed. (Thanks
  Muchun and Sashiko!)

- The arm64 BUILD_BUG_ON() on VMEMMAP_START alignment is now guarded by
  compound_info_has_mask(). (Thanks Muchun!)

- Removed references to "pre-HVO" from the arm64 vmemmap_populate()
  comment and dropped an arm64-specific comment from
  vmemmap_split_pmd(). (Thanks Muchun!)

- New arch_supports_vmemmap_remap() doc comment. (Thanks Muchun!)

- The arm64 vmemmap_update_pmd() now invalidates a single page when
  clearing the AF on a block entry, instead of PMD_SIZE, which could
  escalate to flush_tlb_all() without FEAT_TLBIRANGE. (Thanks Sashiko!)

- The changelog for "arm64: Support hugetlb vmemmap optimization" now
  explains that BBML3 is needed because HVO shatters in-use vmemmap PMD
  block mappings.

- Selftest: re-online memory blocks at the end of every stress phase
  instead of only on exit, and fail if mktemp -d fails. (Thanks
  Sashiko!)

I have not changed vmemmap_update_pte() to return void, as Muchun
suggested; please see my reply from v2[2]. I'd appreciate any thoughts
from the arm64 folks on this.

v3: https://lore.kernel.org/linux-mm/20261122184453.2668189-1-jthoughton@google.com/
v2: https://lore.kernel.org/linux-mm/20261003002123.505555-1-jthoughton@google.com/
v1: https://lore.kernel.org/linux-mm/20260708031129.3503195-1-jthoughton@google.com/

Claude Opus 5.5 was used while drafting this version of the series.
Like the last versions, it was used to draft the selftests, and in this
version, it helped address review feedback.

Thanks!

And now for the rest of the background of these patches, updated from
v3's cover letter:

-- Background and Structure --

This patch series uses a trick with the Access Flag on CPUs that support
hardware update of the AF to update vmemmap page table entries without
introducing a time window where CPUs accessing the vmemmap might fault.

By avoiding faults, the HugeTLB vmemmap optimization (HVO) can be
implemented correctly on arm64 in a much more straightforward way than
previously attempted, most recently here[1] (please see [1] for a
breakdown of the other approaches attempted before).

For large-memory systems that allocate most of their available memory to
HugeTLB, HVO saves a huge amount of memory (1.5% of system memory).

This series has a few parts:
  1. Use LDCLR to clear the AF for PTEs (patch 1).
  2. Some preliminary HVO changes to support HVO on Arm (patches 2-7).
  3. arm64 changes to enable HVO (patches 8-11).
  4. Fault injection and a stress test (patches 12-13).
  5. Drop the BBML3 requirement for HVO (patches 14-18).

Part 5 is optional, and whether or not the claims it makes about the Arm
memory model are accurate is not 100% clear.

-- The AF trick --

The trick is that translations with the AF unset cannot be cached in the
TLB (see Rule R_DWZCQ in the Arm ARM), so they can be atomically updated
without needing a full break-before-make sequence.

So the PTE update sequence becomes:
  1. Atomically clear the AF on the existing PTE.
  2. Invalidate the TLB.
  3. cmpxchg the AF=0 PTE with the new PTE. If this fails, goto 1.

If there is a CPU on the system that does not support hardware access
flag updates, clearing the AF is problematic, as those CPUs might fault
on the vmemmap usage. Therefore, HVO compatibility checks all CPUs for
HW AF updates.

-- Application to HVO --

HVO relies on the following page table transitions:
  - When enabling HVO for a page, PMD block entries in the vmemmap are
    shattered into PMD table entries. The first vmemmap page of the
    folio is copied to a newly allocated page and remapped to it, and
    the remaining PTEs are remapped to a shared page of tail struct
    pages (that is, there is an OA change for every PTE).
  - When disabling HVO for a page, the PTEs that map the shared tail
    page are remapped to newly reallocated pages of struct pages. The
    PMD block -> table transition is not undone.

In part 3 of this series, I use the Access Flag trick to do the PTE
updates. We rely on BBML3 for the PMD block -> table transition.

In part 5, I re-use the Access Flag trick to do the PMD block -> table
transition without needing BBML3. For systems that support BBML3, the
logic is unchanged.

Because the in-place updates can fail (if the cmpxchg keeps losing the
race with hardware AF updates), HVO now handles failures at any point:
a failed optimization is rolled back, and if the rollback or a later
restore fails, the folio is left partially optimized (and remains marked
as such) rather than being freed with a broken vmemmap.

-- Late-onlining of CPUs that do not support HW AF --

HVO support is modeled as an early local CPU feature (called "BBM
through AF"). It is checked on every early CPU. If not all early CPUs
support the feature, attempts to HVO pages will fail, and the HVO sysctl
will be hidden. If it is enabled, late CPUs that do not support it are
rejected.

To avoid penalizing systems that have asymmetric support for
BBM-through-AF (i.e., HW AF, as BBML3 cannot be mismatched),
BBM-through-AF will check that HVO is in fact enabled.

Because BBM-through-AF needs to know if HVO will be enabled when CPU
features are detected, HVO cannot be dynamically enabled, hence the
patches that do this. (There are other ways to solve this, please see
v1).

-- Litmus test --

The following Herd litmus test demonstrates the PTE update routine:

  AArch64 TTDFaultlessUpdate
  Variant=vmsa
  TTHM=HA
  {
   uint64_t x=1;
   uint64_t y=2;
   [PTE(x)]=(oa:PA(x), af:1);
   0:X0=PTE(x); 1:X0=PTE(x);
   0:X1=x; 1:X1=x;
   pteval_t 0:X2=(oa:PA(x), af:0);
   pteval_t 0:X3=(oa:PA(y), af:1);
  }
   P0                | P1             ;
   LDR X4,[X0]       | L0:            ;
   MOV X5,X4         | LDR X2,[X1]    ;
   CAS X4,X2,[X0]    |                ;
   DSB ISHST         |                ;
   LSR X9,X1,#12     |                ;
   TLBI VAALE1IS,X9  |                ;
   DSB ISH           |                ;
   ISB               |                ;
   CAS X2,X3,[X0]    |                ;
  exists
    0:X5=0:X4 /\ (* First CAS must succeed *)
    (fault(P1:L0) \/ ~(1:X2=2 \/ 1:X2=1))

  (* This test should not contain "Warning-BBM-expected". *)

-- Testing --

I've tested this series with the included selftest, which stresses
the optimization, unoptimizing, and failure cases.

[1] https://lore.kernel.org/linux-arm-kernel/20241107202033.2721681-1-yuzhao@google.com/
[2] https://lore.kernel.org/linux-mm/CADrL8HWNaurYHGnpvC9qwr6bVU74YhhucCuhoVYcvqmSX+R6+Q@mail.gmail.com/

James Houghton (18):
  arm64/pgtable: Clear AF with LDCLR on supported systems
  hugetlb_vmemmap: Always flush TLB if needed upon PTE remapping
  hugetlb_vmemmap: Leave pages partially HVOed upon restore failure
  hugetlb_vmemmap: Use vmemmap_update_pte to update in-use PTEs
  hugetlb_vmemmap: Allow architectures to dynamically disallow HVO
  hugetlb: Optimize bootmem folios that were not pre-HVOed
  hugetlb_vmemmap: Allow architectures to make HVO enablement boot-time
    only
  arm64: Add bbm_through_af capability
  arm64: Implement vmemmap_update_pte using the AF trick
  arm64/mm: Align vmemmap to maximal folio size
  arm64: Support hugetlb vmemmap optimization
  hugetlb_vmemmap: Add fault injection for in-place vmemmap PTE updates
  selftests/mm: Add HugeTLB vmemmap optimization stress test
  hugetlb_vmemmap: Use vmemmap_update_pmd for replacing in-use PMDs
  arm64: Implement vmemmap_update_pmd using AF trick
  arm64: Drop BBML3 requirement for HVO
  hugetlb_vmemmap: Add fault injection for in-place vmemmap PMD splits
  selftests/mm: Add HVO pmd-split fault injection tests

 .../admin-guide/kernel-parameters.txt         |   2 +
 Documentation/admin-guide/sysctl/vm.rst       |   3 +
 .../fault-injection/fault-injection.rst       |   8 +
 arch/arm64/Kconfig                            |   2 +
 arch/arm64/include/asm/cpufeature.h           |   5 +
 arch/arm64/include/asm/pgalloc.h              |  61 +++
 arch/arm64/include/asm/pgtable.h              |  74 +++-
 arch/arm64/kernel/cpufeature.c                |  48 +++
 arch/arm64/mm/init.c                          |  18 +-
 arch/arm64/mm/mmu.c                           |   9 +
 arch/arm64/tools/cpucaps                      |   1 +
 fs/Kconfig                                    |   3 +-
 include/linux/pgalloc.h                       |  23 +
 include/linux/pgtable.h                       |  39 ++
 lib/Kconfig.debug                             |   9 +
 mm/Kconfig                                    |   7 +
 mm/hugetlb.c                                  |   7 +
 mm/hugetlb_vmemmap.c                          | 170 ++++++--
 mm/hugetlb_vmemmap.h                          |   2 +
 tools/testing/selftests/mm/Makefile           |   2 +
 .../selftests/mm/hugetlb_vmemmap_stress.sh    | 392 ++++++++++++++++++
 .../selftests/mm/ksft_hugetlb_vmemmap.sh      |   4 +
 tools/testing/selftests/mm/run_vmtests.sh     |   4 +
 23 files changed, 853 insertions(+), 40 deletions(-)
 create mode 100755 tools/testing/selftests/mm/hugetlb_vmemmap_stress.sh
 create mode 100755 tools/testing/selftests/mm/ksft_hugetlb_vmemmap.sh


base-commit: 872ceec1e3caf227655a58f7407a9c8493dcf56f
-- 
2.56.1.839.g22a8290db9-goog



^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2026-12-01 15:09 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 15:07 [PATCH v4 00/18] Another attempt at HVO support on arm64 James Houghton
2026-12-01 15:07 ` [PATCH v4 01/18] arm64/pgtable: Clear AF with LDCLR on supported systems James Houghton
2026-12-01 15:07 ` [PATCH v4 02/18] hugetlb_vmemmap: Always flush TLB if needed upon PTE remapping James Houghton
2026-12-01 15:07 ` [PATCH v4 03/18] hugetlb_vmemmap: Leave pages partially HVOed upon restore failure James Houghton
2026-12-01 15:07 ` [PATCH v4 04/18] hugetlb_vmemmap: Use vmemmap_update_pte to update in-use PTEs James Houghton
2026-12-01 15:07 ` [PATCH v4 05/18] hugetlb_vmemmap: Allow architectures to dynamically disallow HVO James Houghton
2026-12-01 15:07 ` [PATCH v4 06/18] hugetlb: Optimize bootmem folios that were not pre-HVOed James Houghton
2026-12-01 15:07 ` [PATCH v4 07/18] hugetlb_vmemmap: Allow architectures to make HVO enablement boot-time only James Houghton
2026-12-01 15:07 ` [PATCH v4 08/18] arm64: Add bbm_through_af capability James Houghton
2026-12-01 15:07 ` [PATCH v4 09/18] arm64: Implement vmemmap_update_pte using the AF trick James Houghton
2026-12-01 15:07 ` [PATCH v4 10/18] arm64/mm: Align vmemmap to maximal folio size James Houghton
2026-12-01 15:07 ` [PATCH v4 11/18] arm64: Support hugetlb vmemmap optimization James Houghton
2026-12-01 15:07 ` [PATCH v4 12/18] hugetlb_vmemmap: Add fault injection for in-place vmemmap PTE updates James Houghton
2026-12-01 15:07 ` [PATCH v4 13/18] selftests/mm: Add HugeTLB vmemmap optimization stress test James Houghton
2026-12-01 15:07 ` [RFC PATCH v4 14/18] hugetlb_vmemmap: Use vmemmap_update_pmd for replacing in-use PMDs James Houghton
2026-12-01 15:07 ` [RFC PATCH v4 15/18] arm64: Implement vmemmap_update_pmd using AF trick James Houghton
2026-12-01 15:07 ` [RFC PATCH v4 16/18] arm64: Drop BBML3 requirement for HVO James Houghton
2026-12-01 15:07 ` [RFC PATCH v4 17/18] hugetlb_vmemmap: Add fault injection for in-place vmemmap PMD splits James Houghton
2026-12-01 15:08 ` [RFC PATCH v4 18/18] selftests/mm: Add HVO pmd-split fault injection tests James Houghton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox