From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796137696; b=tD/TEbxlLwNbAbQHJoSZzFnmgwXJtWzymdEFLy7QhwclIjkZ5tt2u+c0k1hrpEkXPVYo LshkKcMGGRHihQNOSEeh4YPvdxx4Aj/PzEZvKbiBYzC0DRAkACvoBWe6OPbTvrRhQ8iJj Z75d3jNuJY3h8+K9eBpkui0SUe2J/XmAVIQOAvG5yiCnwwjdKbKW6pRXo920zC+OR6+IL vZ3BBJQdQLexYjveqZ/2rfes8oOCzKV5i3AX1nni8Dq/fwtfm6nMTCfaidhiFS4mGXUBR OfQ7CBENNCp4JAEg1XXUz0069KTh8GmMTjiGMo2JyxvQP4LlsolhAQ20sxFJ3ke40fw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796137696; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=PIKCAwzB8bRauv+TLzB1vFIBGfWwAmtPsw5bNH64b0w=; b=xMRarKTi8lp24OJqPdZADm2EpPaqnS+6qkRJ54zZLqW2BkTpErpiKhkleUMGnJ0jv2b5 VW9HcKC+kVM86o2eg/AOpgcU4tvQ1QzinGSU6tOnyb6f03SJiIXeULaXeaw4OEmPz9MYA W4UImE7s5KEVvDVpw/e3lepVOygzWl60KcEmjrYHIc5zLKrZPwhi9Tir4DbPZ2x+oeAgq gy8Sz6G7XfdxsLM7YJ5Q0X+PvU7A4StUKxzvxCwHwiTCsLpbu3qlc+FpRVMLN0a9TnUnF 3Ze/E9L+GeWD2Dxw2A+MS0DZKE8f2f3IpbJgZNcmnHoimKrq5fj7VsVKZlFeKuYLJrg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=QIhtwAdQ; arc=none smtp.remote-ip=209.85.215.199 Received: from mail-pg1-f199.google.com (mail-pg1-f199.google.com [209.85.215.199]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 890E960A2605 for ; Tue, 1 Dec 2026 15:08:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 890E960A2605 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=QIhtwAdQ Received: by mail-pg1-f199.google.com with SMTP id 41be03b00d2f7-ce0f6cf2a01so713325a12.1 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=vger.kernel.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=QIhtwAdQZvv/nwHehBVqo6+TNPQn31B2oexdAWknEp9aEgcH0P8C6ca4A/Ij8Q/GUw fQ5Tiz0x5zHTRPCE8/ygoBKtQIP7z3/GYx8jRGkvGK9Hh7hK1h0t9eWlLhmaQTnajVAd H43FD6zzu2LrD+HCoGUyr3ubzM7hQSIs1y7kWY8F56Qet/tL2fb1MQ9O2cG8av6nCrP8 yehOIdW2VhTKH+0+Nfdl9CkopW1dpQXjojrDT/V9OhsVz2gWlxoI/qaNNS3JxHGu0E1B ucLYSqWZGLuajdNjFN5IAFhBPQdICCEF3h5q53NKxP1vCVCHF9YCcHoLe58ktWUcfy6i oYDQ== 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=CXfsv5MBCQpqmQkYVvzLye/89pn809yVj09jwdx4Qy5N/VDTNgt0wJW//ByzpzlXNe UTnO1OqHty1Q+gcXz87dOyOjfXV38tGJ12q2iZhmrs8WZHju2KNUpdObWuBKQnSmLfUE m5ZbnpTQxAXORpDS5AYTjsvR32jcVjtLO5QlvuDdgjNsQX5sP/j3aw/Y5/de+rSwWn77 lOfcfajh21/XkGT/dfLv7u/ltxEVz8m2hqFSUGP7xyAXKs5sCoOYwu39It2ktDrq+wPm aRaxLL4vSy07vhbGyf/jVgj30eS57vvn4pDB1kXVImTr630+IbAKhHz0EvSnMCuPFdy5 a81w== X-Forwarded-Encrypted: i=1; AL+hErE2Hc7Laj19f7XvcTuVmpU2uQVoNxQWCJJ03WrFPi8YcLLif4/FkjCATrqoRUj3wsxPR2D9tyvs9YSptgw=@vger.kernel.org X-Gm-Message-State: APuU4Z/eUfEQ3AvtCqCcKZBkgVePXvrhOdbv7i4Plasb1Z8ze7Y91iaT QsKggujPVQGTo5WfTVczH6t/EW1wm4dHhzXB7XMYWUnN9SxhW/wv857AOI/axwAd+03D2gyvZwZ yrEOObfOxEHNB3ymRyDUx6A== 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-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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" 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