From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C87C4606A32A for ; Tue, 1 Dec 2026 15:08:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=bpIdOgJQ; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=KaK4aK2o DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:Mime-Version:Date:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Unsubscribe-Post:List-Owner; bh=g6HuuWJXG3L2qdyxrVyieOil30FIQQxHb26Cc0nVy54=; b=bpIdOgJQb3NgkIgoWuq97FbgKQ OcpfTOezvU0lzur+JfHm+JB4d8n6VX5xz2kVfVCRGWBGf3dxXvKjeZFBvIpzhD+ogyh071vp4vdA2 M1BhERMFflTa+O3ELzOIzGpYtS2mV8hTcI/nrIOVGXrhQQsf2S6vqFGQjEe5inkvmYj7ldZ7uaVBd VlCuNfZo3fkXLxpbkRMs94EwxHGWVf2h13ah/f2zoBumU+MnLPXSL2nG2hv5L7CmwzgKsVH/Y8FOb UpOWnO8DJkJDcUhmJcfYGYKR07ucprynIDfIZ4ORaJvssRr8abMaQMP35jMJM0SsETyqeGmr/r6SP N6HmCTkA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYPSm-00000008U2A-3OKV; Tue, 01 Dec 2026 15:08:12 +0000 Received: from mail-pl1-x645.google.com ([2607:f8b0:4864:20::645]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYPSk-00000008U1K-2rny for linux-arm-kernel@lists.infradead.org; Tue, 01 Dec 2026 15:08:12 +0000 Received: by mail-pl1-x645.google.com with SMTP id d9443c01a7336-2f05c68629cso86163045ad.1 for ; Tue, 01 Dec 2026 07:08:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796137688; x=1796742488; darn=lists.infradead.org; h=content-type:cc:to:from:subject:message-id:mime-version:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=g6HuuWJXG3L2qdyxrVyieOil30FIQQxHb26Cc0nVy54=; b=KaK4aK2o7xoSVF9Nr1DyFFJZ3F3b9kAqn89MlMJ/aIoj4c0wKG2TUbj4rRzBZpuJsD VzpslDqyXlCgEMaZHCGB3I6BxkEkZf2FrDKbrJQoDAMEiiFw49PagrFTlELjNEorru9W rSs4K6HxCALIox+FzQHU5vJ1x+i1gnmb4fmHWlt4EHECOYY+EVFMDVd2C29TgxIFD40A wONardNjKfVuslt2bAsQJV13/+Io92ri4nijUmxkpZHKXxG2EVOHNWF72AZYIovqpGD8 XDbwYR2kmdnkcOBBA888pdb3RvgRRgOGEh2jc9ZZ471fym5UXB4l3qGk/fyRETxR3+Vr 3tNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796137688; x=1796742488; h=content-type:cc:to:from:subject:message-id:mime-version:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=g6HuuWJXG3L2qdyxrVyieOil30FIQQxHb26Cc0nVy54=; b=RU3Mz+FCbZXy5LlODBrCOD7QAubMNvuGEWHiF/SqQcrW5Mwg2QBuamqtXAfA7v2hAK vS34Is70rFZ0HPSt6uK8hxHMK9k1Qeezp0rgdCV9NYBncxsm1Ec5OCr4vHcI4WTlxSa/ V8jW/lFsbCJlrzEW9vFgdBwh/qCxyEIUWqDlndR51/D+vmwdxviTZq1RzXwAVDdwvnRr T+SiSH9DtmZhwSQz1LpHo0s7auXgCteZ0TUOYiKxFXObBHnFmQ5kAr8NZSeTMeQHEnV1 zDSLnv1hEG8VSMdD1Bs6T9VNlPYRGiw3l9VNF7YjaLfbuZtWaAaJPBVDnhEgh/CoYPiy pO9w== X-Forwarded-Encrypted: i=1; AL+hErEMZihJ8fc9rPSbua+Uyb6F0VG8t8bA6TtCxlW0x0L4BTpT+8+jYQaNlXVJaUS24X5LNoFVAQ06g9IK2jF4Ic73@lists.infradead.org X-Gm-Message-State: APuU4Z+F7OUODCdIX3SSBNL0/kc6Wx3RieDYjbeDXM3JCBfp1QBdPlhT zfi2FyL4ig6TF/zNDS3NwQh+CgrU3CqWEpOwCr2/zu41DJwNuGXpQRfaxfxcMBFIOv7ufdNA88P +Rgpu4NXqIS5t/2mv7+q4gw== X-Received: from plpj15.prod.google.com ([2002:a17:903:3d8f:b0:2ef:a836:c1be]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:eccf:b0:2f0:25ea:8fa8 with SMTP id d9443c01a7336-2f03f176abamr406797445ad.19.1796137688189; Tue, 01 Dec 2026 07:08:08 -0800 (PST) Date: Tue, 1 Dec 2026 15:07:42 +0000 X-Archived-By: Mime-Version: 1.0 X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261201150801.2239894-1-jthoughton@google.com> Subject: [PATCH v4 00/18] Another attempt at HVO support on arm64 From: James Houghton 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@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261201_070810_739787_9949ADAC X-CRM114-Status: GOOD ( 33.21 ) X-Spam-Score: -9.5 (---------) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" X-Spam-Status: No, score=-26.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIMWL_WL_MED,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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