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 E5866606A322 for ; Tue, 1 Dec 2026 15:09:30 +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=SvClxyF4; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=iZ3relTv 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=HCFEKxJ7gPxaQZc7Cc5oKB2wSDCDwgRiskS4RWnR3Vc=; b=SvClxyF4iQm32TqrxWERwIIX5c IKE/2/4g+1jEjXvL7VPk6ndk2oyYwFWx93bAfhJ3CD2fEra0XW1FM7IGScb4HmwDG8BXkw1/lanlt BGzvT01kWMHw4RtS3muOs3dKlsHXWhu3SchGKRm6fLku/kigVg+f9/0vpLIgXP+WNbxzBDonn1kgQ g2XEixmdP3/Jgb5dXUheP5Mgd/+HD+tiL5kSflPgnTBtc4uup3ksBFUEjfXVa5W6NBKKOfa+IISrJ CGeREGbdrLQlm43+JNp5v1qQG/y8vsXehRuAr6gspqnweJyuI9cVctmAi2Iu8xK69SqpkbrvkIk5l jt49EQwQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYPTG-00000008Uc5-0PQh; Tue, 01 Dec 2026 15:08:42 +0000 Received: from mail-pl1-x646.google.com ([2607:f8b0:4864:20::646]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYPSx-00000008UAV-0XSt for linux-arm-kernel@lists.infradead.org; Tue, 01 Dec 2026 15:08:31 +0000 Received: by mail-pl1-x646.google.com with SMTP id d9443c01a7336-2f1426c0ee8so32308395ad.2 for ; Tue, 01 Dec 2026 07:08:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796137701; x=1796742501; 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=HCFEKxJ7gPxaQZc7Cc5oKB2wSDCDwgRiskS4RWnR3Vc=; b=iZ3relTvIu5yNhovkWLAe1CfmAryjApBDvgS+C2f7EvNiL2176ItDjMFgi4g3Grg0x XDUrOFMpM6ZTUeYdEdwDEyLcU/vmhG3fdPlzQEFkD9CvseLJ6C3NgRw4rJ/Bky/OsnA7 2k7G309aXmogh0Z8ld3//RVKiQEjQsxOBpCSlEH4sJVR5A3u/2Dlo/nQUBrUsr/17qQS ouEYQjmbwzqnw4rz1R2bZ7r2BJvl6OGtw2n8j65rBj9R/JgAwlAcrEFIQpXuT1E8u+PW qgZ7Icm/4USalwWPxFFypdzajdI1JL8Clh0tAT0B707R+LyoprtsiP+L7wu9hmWEuEjX FiCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796137701; x=1796742501; 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=HCFEKxJ7gPxaQZc7Cc5oKB2wSDCDwgRiskS4RWnR3Vc=; b=lBhyZ9d2/dT3rpVKH2d4Q2r8Rkt0TytnSE2Ti11CtyAb0pCSAJ6+fIc02jZm31SZ/w SLZCppRUlxzAY8lUVq2EVdiZri8F4iY2gKh83DcG2osjWItqCEgxEAGbzn8okJbZiP/+ lSv1wR6EiJAAGspuYpOLbpoFDDMouTUoTtH8KhNhkJFEW7Kp68Jfi1ZmX66Po8W4htR6 wNVrZYsWhGUi6LRgtcMISRFr0Y+7Jd+4Hyml85U+awljBUvpGV0rL1nNxIPCo5e4n+Z1 XpIgjbptXPiWZ6+5NLnu2Xza9Gdhdp6rC8Y53B2VTUTyFMDUxs9vKFD8Ipchg6uBa33T yYtA== X-Forwarded-Encrypted: i=1; AL+hErGukKWfwKV/g8xw5kl7BsfbQM7JKa/q5OXwY0CohCRm6Ibb4R39r8UyXX7064J9DBOTiDk7ciLIJMu2W6jCWLa/@lists.infradead.org X-Gm-Message-State: APuU4Z8jcsPemwhjQVYvzENxVfHE99TH9NdBk0B/gKOEhDr9h3sIxmxG eYIYl/BZSDioxKqWAnpzYsmpBq2G6SQIMTeVhVP46abBMdDss9Gktc7UANonFzbNPnygSmlJ3A6 uRhVY5z/Rq42zANSVH7Zutw== X-Received: from plbkc12.prod.google.com ([2002:a17:903:33cc:b0:2ee:2d4e:e83b]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:ec8b:b0:2e9:66f6:7894 with SMTP id d9443c01a7336-2f03f15c27amr364425315ad.40.1796137700312; Tue, 01 Dec 2026 07:08:20 -0800 (PST) Date: Tue, 1 Dec 2026 15:07:57 +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-16-jthoughton@google.com> Subject: [RFC PATCH v4 15/18] arm64: Implement vmemmap_update_pmd using 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_070823_632207_50F2D8FB X-CRM114-Status: GOOD ( 17.10 ) 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=ham 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 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 BBML3, there is no need to use the AF trick. Signed-off-by: James Houghton --- arch/arm64/include/asm/pgalloc.h | 58 ++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/arch/arm64/include/asm/pgalloc.h b/arch/arm64/include/asm/pgalloc.h index 421e07545cfd..54a68f7bdb53 100644 --- a/arch/arm64/include/asm/pgalloc.h +++ b/arch/arm64/include/asm/pgalloc.h @@ -125,9 +125,61 @@ pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep) static inline int vmemmap_update_pmd(unsigned long addr, pmd_t *pmdp, pte_t *pgtable) { - /* BBLM3 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_bbm_through_af()) + return -EOPNOTSUPP; + + if (system_supports_bbml3()) { + /* + * BBML3 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))); + + /* + * A non-contiguous block entry is cached as a single + * TLB entry, and invalidating any address it maps + * removes it (see unmap_hotplug_pmd_range()). Flushing + * the whole PMD_SIZE range is unnecessary and may + * escalate to flush_tlb_all(). + */ + 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(&pmd_val(*pmdp), &pmd_val(old_pmd), + pmd_val(new_pmd)) && + ++attempts < max_attempts); + + return attempts == max_attempts ? -EAGAIN : 0; } #endif -- 2.56.1.839.g22a8290db9-goog