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 622ED606A31A for ; Tue, 1 Dec 2026 15:31:08 +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=AGG3VCbd; dkim=pass (2048-bit key, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=nZ5C73Rk 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:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Unsubscribe-Post:List-Owner; bh=PIKCAwzB8bRauv+TLzB1vFIBGfWwAmtPsw5bNH64b0w=; b=AGG3VCbdcIU3icl61QXY98Rh7J W+W5n1ZleAZt8QzBkXdyIeoq30jN3l/N3ZUrWnhvwzOyi9pLM+gIIglqDLPfSAvMYY5hvTjfzmHiM VdShZ9/i72ARtgthmHm6MAFnhFPaR7mc+20EQjzUMmAsykIKvrkNh11zXFBmOgDE2ycNuZzOLGZ9u +UpImU8b8ZkfWcIu05tQfJE6WOWDr2K3cFCyBWjmd1FPKRrunuQZAWUrqqx7sDbtr+ZjU/963gri0 5/zmK8c/7irSgLaSFiyCnV7zsnpCwqveBykO+4SpY2H2H6BggWK1rhxkKb8uswbBu+ggLxMTHWCE/ RTo7lr0w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYPT5-00000008UMa-3elA; Tue, 01 Dec 2026 15:08:31 +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 1xYPSq-00000008U5d-3qIs for linux-arm-kernel@lists.infradead.org; Tue, 01 Dec 2026 15:08:23 +0000 Received: by mail-pl1-x645.google.com with SMTP id d9443c01a7336-2f0717e54b0so92229485ad.0 for ; Tue, 01 Dec 2026 07:08:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796137696; x=1796742496; darn=lists.infradead.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=PIKCAwzB8bRauv+TLzB1vFIBGfWwAmtPsw5bNH64b0w=; b=nZ5C73RkxhQ01jlhUg6c3o3IzXLpe9beD/fxNTcQd/cDDcJJ+pHuCYoBKdB9RhPc1S jAccb55K05r9BxlRukjbPx5PyDyeZqg4pFZXt/zL7vQDMgdDtU2AU0VdQybxN45Tt57p aYWnvppS3vHRg15xexsrKMbKpj9c4klR37azJAYu/OfjHpVDR1qlyg/3tBrnE5CsOMbB 4e1FIXgorSFpYUgWu41IdfIwxQXmn1Iswya7fECQ8We/ekGCmyTteVi4ie47A5l8ryhl RL42nQcWbBAXKEgXxf+eFLNdF1oPg6aM/e5Fc+9l5+jwy7lSDp6Xvn10fk5U9XMDIZx5 AWlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796137696; x=1796742496; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=PIKCAwzB8bRauv+TLzB1vFIBGfWwAmtPsw5bNH64b0w=; b=LLM4+04eDVeSb8mFEQWpB25gk+2mbFeqR1rax1G+cqdPjfCXWtYQY4Mwa5Gt6RkgAV 2QVz1OntXX3upw4zicq890z6Tu9ewz/i/x92wZl0t6xhywcNzEA3ha8AO2M/9iLrl6aK YbdzDQAJySRjQbF2umQVm5sLKmPT2JhAb7bxy6U6FbTz4age/GYZZVLyG2wSK99XP8CZ oPoGsWx3MrF/I671zdniVNtPYjjqKHdIpIAiPAqEnaKIbf85UrCyflWjQn7RBikASlmN //FC5K+XZSUJAvDOUODdXZyv5QkLnAN9Np/LYSY7Vmq5n/GIzJVGo4bDQHtlrup6sgjZ 2wfA== X-Forwarded-Encrypted: i=1; AL+hErHH7pCHeS4nFWc3WW07NVQJXj3t1DViZb0+Nu86E5jgbvWPK52PlLAfPKcfiUV+zP/FdmhOHT2RYdHwRi/JkrZT@lists.infradead.org X-Gm-Message-State: APuU4Z+HWQgOm2t8MMmVqKAeXnZgGcodk6KuRZjjrb7YudohC+423VfZ pZ4z4Vox19kuKsowSGspeGd+HnDRLjrPtBHNIe3yp1G2i4SPGO+wk0by7xj6byIQChC9tuPGfc5 Myuzq+uvugLfGJw5Yyn0hyQ== X-Received: from plje1.prod.google.com ([2002:a17:902:ed81:b0:2ed:4e53:853d]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:fd8c:b0:2f0:7f99:e46b with SMTP id d9443c01a7336-2f07f99e585mr326523095ad.47.1796137695470; Tue, 01 Dec 2026 07:08:15 -0800 (PST) Date: Tue, 1 Dec 2026 15:07:51 +0000 In-Reply-To: <20261201150801.2239894-1-jthoughton@google.com> X-Archived-By: Mime-Version: 1.0 References: <20261201150801.2239894-1-jthoughton@google.com> X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261201150801.2239894-10-jthoughton@google.com> Subject: [PATCH v4 09/18] arm64: Implement vmemmap_update_pte using the AF trick 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_070816_957180_0BC531D8 X-CRM114-Status: GOOD ( 18.74 ) 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_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 vmemmap_update_pte() must modify vmemmap PTEs without introducing a time window where other CPUs on the system might fault. Normally a break-before-make sequence is required to avoid conflicts with cached translations. However, if we can guarantee that the existing translation cannot be cached, a BBM sequence is not needed. Translations with the AF unset may not be cached (see Arm ARM Rule DWZCQ); the implementation of vmemmap_update_pte() on arm64 takes advantage of this fact to replace a PTE without BBM and therefore without leaving a window open where PE might fault on this translation. Of course, if some CPUs on the system do not support HW AF management, clearing the AF will introduce potential faults. system_supports_bbm_through_af() will return false if any CPUs on the system do not support HW AF. Signed-off-by: James Houghton --- arch/arm64/include/asm/pgtable.h | 60 +++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 4285b7e7a6a7..199ca086b430 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -1249,14 +1249,24 @@ static inline void __pte_clear(struct mm_struct *mm, __set_pte(ptep, __pte(0)); } -static inline bool __ptep_test_and_clear_young(struct vm_area_struct *vma, - unsigned long address, pte_t *ptep) +/* + * Atomically clear the Accessed flag. Return the old value of the PTE. + */ +static inline pte_t __ptep_clear_young(pte_t *ptep) { atomic64_t *pteval = (atomic64_t *)&pte_val(*ptep); s64 af_mask = PTE_AF; - /* Atomically clear PTE_AF, checking that it was set before. */ - return af_mask & atomic64_fetch_andnot_relaxed(af_mask, pteval); + /* Atomically clear PTE_AF. */ + u64 oldval = atomic64_fetch_andnot_relaxed(af_mask, pteval); + + return __pte(oldval); +} + +static inline bool __ptep_test_and_clear_young(struct vm_area_struct *vma, + unsigned long address, pte_t *ptep) +{ + return pte_young(__ptep_clear_young(ptep)); } static inline bool __ptep_clear_flush_young(struct vm_area_struct *vma, @@ -1921,6 +1931,48 @@ static inline void clear_young_dirty_ptes(struct vm_area_struct *vma, #endif /* CONFIG_ARM64_CONTPTE */ +#define vmemmap_update_pte vmemmap_update_pte +static inline int vmemmap_update_pte(unsigned long addr, pte_t *ptep, + const pte_t pte) +{ + const int max_attempts = 16; + int attempts = 0; + pte_t old_pte; + + if (!system_supports_bbm_through_af()) + return -EOPNOTSUPP; + + /* This routine is only to be used for valid-to-valid transitions. */ + if (WARN_ON_ONCE(!pte_valid(pte))) + return -EINVAL; + + old_pte = __ptep_get(ptep); + + do { + if (WARN_ON_ONCE(!pte_valid(old_pte))) + return -EINVAL; + + /* We should never get a contiguous PTE here. */ + if (WARN_ON_ONCE(pte_valid_cont(old_pte))) + return -EINVAL; + + if (pte_young(old_pte)) { + /* __ptep_clear_young() returns the overwritten PTE */ + old_pte = pte_mkold(__ptep_clear_young(ptep)); + + flush_tlb_kernel_range(addr, addr + PAGE_SIZE); + } + /* + * Translations without AF cannot be cached, so we can replace + * them without BBM. + */ + } while (!try_cmpxchg_relaxed(&pte_val(*ptep), &pte_val(old_pte), + pte_val(pte)) && + ++attempts < max_attempts); + + return attempts == max_attempts ? -EAGAIN : 0; +} + #define __HAVE_ARCH_VMAP_PTE_SET_HUGE void vmap_pte_set_huge(pte_t *ptep, unsigned long addr, phys_addr_t phys, pgprot_t prot, unsigned long size); -- 2.56.1.839.g22a8290db9-goog